namespace alps { class Expression; class Evaluator { public: enum Direction { left_to_right, right_to_left}; Evaluator() {} virtual bool can_evaluate(const std::string& name) const; virtual double evaluate(const std::string& name) const; virtual Expression partial_evaluate(const std::string& name) const; virtual bool can_evaluate_function(const std::string& name, const Expression& arg) const; virtual double evaluate_function(const std::string& name, const Expression& arg) const; virtual Expression partial_evaluate_function(const std::string& name, const Expression& arg) const; virtual Direction direction() const; }; class ParameterEvaluator : public Evaluator { public: ParameterEvaluator(const Parameters& p); }; class Expression : public detail::Evaluatable { public: Expression() {} Expression(const std::string&); Expression(std::istream&); Expression(double val); const Expression& operator +=(const Expression& e); operator std::string () const; operator bool() const; bool can_evaluate(const Evaluator& p=Evaluator()) const; void partial_evaluate(const Evaluator& p=Evaluator()); double value(const Evaluator& p=Evaluator()) const; void flatten(); void simplify(); }; std::ostream& operator<<(std::ostream&, const Expression&); std::istream& operator>>(std::istream&, Expression&); bool operator==(const std::string&, const alps::Expression&); bool operator==(const alps::Expression&, const std::string&); bool operator==(const alps::Expression&, const alps::Expression&); bool can_evaluate(const StringValue& v, const Evaluator& eval= Evaluator()); double evaluate(const StringValue& v, const Evaluator& p = Evaluator()); bool can_evaluate(const StringValue& v, const Parameters& p); double evaluate(const StringValue& v, const Parameters& p); Parameters evaluate(const Parameters& in); }
specifies whether an expression should be evaluated from left to right (the default) or right to left (used when symbols can be operators acting on the expression to the right of them).enum Direction { left_to_right, right_to_left}; virtual Direction direction() const;
returns true if the symbol passed as argument can be evaluated to a numerical value. The default implementation evaluates the symbols PI, Pi and pi.virtual bool can_evaluate(const std::string& name) const;
returns the numerical value of the symbol passed as argument if can_evaluate(name) is true, and throws an exception otherwise. The default implementation evaluates the symbols PI, Pi and pi.virtual double evaluate(const std::string& name) const;
returns an expression for the value of the symbol name, evaluated as far as possible. The default implementation evaluates the symbols PI, Pi and pi to numerical values and leaves all other symbols untouched.virtual Expression partial_evaluate(const std::string& name) const;
are similar to the three previous functions, but apply to functions name(arg). The default implementation evaluates the argument as far as possible. In addition, if the function is sin, cos, tan, log, exp or sqrt and can_evaluate(arg) is true, then the function can be evaluated to a number.virtual bool can_evaluate_function(const std::string& name, const Expression& arg) const; virtual double evaluate_function(const std::string& name, const Expression& arg) const; virtual Expression partial_evaluate_function(const std::string& name, const Expression& arg) const;
creates an empty expression (value 0).Expression() {}
create an expression from the argument passed.Expression(const std::string&); Expression(std::istream&); Expression(double val);
adds an expression.const Expression& operator +=(const Expression& e);
returns a text representation of the expressions.operator std::string () const;
returns true if the expression can be non-zero and false if it can be detected that the expression is always zero.operator bool() const;
returns true if the expre4ssion can be evaluated to a floating point number.bool can_evaluate(const Evaluator& p=Evaluator()) const;
evaluates the expression as far as possible, using the optional Evaluator object to evaluate functions and symbols.void partial_evaluate(const Evaluator& p=Evaluator());
returns the numerical value of the expression if can_evaluate() is true and throws a std::runtime_error otherwise.double value(const Evaluator& p=Evaluator()) const;
multiplies out all blocks of parentheses in the expression. E.g. (a+b)*(1+d) is converted to a+b+a*d+b*d.void flatten();
simplifies the expression by removing all unneeded parentheses.void simplify();
write and read an expression in textual form.std::ostream& operator<<(std::ostream&, const Expression&); std::istream& operator>>(std::istream&, Expression&);
compare two expressions.bool operator==(const std::string&, const alps::Expression&); bool operator==(const alps::Expression&, const std::string&); bool operator==(const alps::Expression&, const alps::Expression&);
test whether a parameter value given as a StringValue object can be evaluated, with optionally given an Evaluator object or a set of Parameters.bool can_evaluate(const StringValue& v, const Evaluator& eval= Evaluator()); bool can_evaluate(const StringValue& v, const Parameters& p);
evaluate a parameter value given as a StringValue, with optionally given an Evaluator object or a set of Parameters. A std::runtime_error is thrown if the corresponding can_evaluate functions return false.double evaluate(const StringValue& v, const Evaluator& p = Evaluator()); double evaluate(const StringValue& v, const Parameters& p);
evaluates all parameters given as argument as far as possible.Parameters evaluate(const Parameters& in);
copyright (c) 1994-2010 by Matthias Troyer
Distributed under the Boost Software License, Version 1.0. (See http://www.boost.org/LICENSE_1_0.txt)