ALPS Project

Header file expression.h

This header contains the Expression class, a class to evaluate and simplify symbolic expression and to perform limited symbolic computations

Synopsis

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);
}

The Evaluator class

The Evaluator class is used to simplify and evaluate symbolic expressions. It is used to specify how symbols and functions are evalauted
enum Direction { left_to_right, right_to_left};
virtual Direction direction() const;
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).
virtual bool can_evaluate(const std::string& name) 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 double 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 Expression partial_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 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;
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.

The ParameterEvaluator class

is derived from Evaluator and uses a Parameters argument given to the constructor to evaluate symbols.

The Expression class

can store a symbolic algebraic expression and perform manipulations on it.

Member functions

Expression() {}
creates an empty expression (value 0).
Expression(const std::string&);
Expression(std::istream&);
Expression(double val);
create an expression from the argument passed.
const Expression& operator +=(const Expression& e);
adds an expression.
operator std::string () const;
returns a text representation of the expressions.
operator bool() const;
returns true if the expression can be non-zero and false if it can be detected that the expression is always zero.
bool can_evaluate(const Evaluator& p=Evaluator()) const;
returns true if the expre4ssion can be evaluated to a floating point number.
void partial_evaluate(const Evaluator& p=Evaluator());
evaluates the expression as far as possible, using the optional Evaluator object to evaluate functions and symbols.
double value(const Evaluator& p=Evaluator()) const;
returns the numerical value of the expression if can_evaluate() is true and throws a std::runtime_error otherwise.
void flatten();
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 simplify();
simplifies the expression by removing all unneeded parentheses.

Operators

std::ostream& operator<<(std::ostream&, const Expression&);
std::istream& operator>>(std::istream&, Expression&);
write and read an expression in textual form.
bool operator==(const std::string&, const alps::Expression&);
bool operator==(const alps::Expression&, const std::string&);
bool operator==(const alps::Expression&, const alps::Expression&);
compare two expressions.

Functions to evaluate parameters

bool can_evaluate(const StringValue& v, const Evaluator& eval= Evaluator());
bool can_evaluate(const StringValue& v, const Parameters& p);
test whether a parameter value given as a StringValue object can be evaluated, with optionally given an Evaluator object or a set of Parameters.
double evaluate(const StringValue& v, const Evaluator& p = Evaluator());
double 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.
Parameters evaluate(const Parameters& in);
evaluates all parameters given as argument as far as possible.

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)