Represents a symbolic form of an expression.
Its syntax tree is as follows:
E := Var | Constant | E + ... + E | E * ... * E | E / E | log(E)
| abs(E) | exp(E) | sqrt(E) | pow(E, E) | sin(E) | cos(E) | tan(E)
| asin(E) | acos(E) | atan(E) | atan2(E, E) | sinh(E) | cosh(E) | tanh(E)
| min(E, E) | max(E, E) | ceil(E) | floor(E) | if_then_else(F, E, E)
| NaN | uninterpreted_function(name, {v_1, ..., v_n})
In the implementation, Expression directly stores Constant values inline, but in all other cases stores a shared pointer to a const ExpressionCell class that is a super-class of different kinds of symbolic expressions (i.e., ExpressionAdd, ExpressionMul, ExpressionLog, ExpressionSin), which makes it efficient to copy, move, and assign to an Expression.
The following simple simplifications are implemented:
E + 0 -> E
0 + E -> E
E - 0 -> E
E - E -> 0
E * 1 -> E
1 * E -> E
E * 0 -> 0
0 * E -> 0
E / 1 -> E
E / E -> 1
pow(E, 0) -> 1
pow(E, 1) -> E
E * E -> E^2 (= pow(E, 2))
sqrt(E * E) -> |E| (= abs(E))
sqrt(E) * sqrt(E) -> E
Constant folding is implemented:
E(c1) + E(c2) -> E(c1 + c2) // c1, c2 are constants
E(c1) - E(c2) -> E(c1 - c2)
E(c1) * E(c2) -> E(c1 * c2)
E(c1) / E(c2) -> E(c1 / c2)
f(E(c)) -> E(f(c)) // c is a constant, f is a math function
For the math functions which are only defined over a restricted domain (namely, log, sqrt, pow, asin, acos), we check the domain of argument(s), and throw std::domain_error exception if a function is not well-defined for a given argument(s).
Relational operators over expressions (==, !=, <, >, <=, >=) return symbolic::Formula instead of bool. Those operations are declared in formula.h file. To check structural equality between two expressions a separate function, Expression::EqualTo, is provided.
Regarding the arithmetic of an Expression when operating on NaNs, we have the following rules:
#include <drake/common/symbolic/expression/expression.h>
Public Member Functions | |
| ~Expression ()=default | |
| Expression ()=default | |
| Default constructor. | |
| Expression (double constant) | |
| Constructs a constant. | |
| Expression (const Variable &var) | |
Constructs an expression from var. | |
| ExpressionKind | get_kind () const |
| Returns expression kind. | |
| Variables | GetVariables () const |
| Collects variables in expression. | |
| Variables | GetFreeVariables () const |
| Same as GetVariables(); we provide this overload for compatibility with Formula. | |
| bool | EqualTo (const Expression &e) const |
| Checks structural equality. | |
| bool | Less (const Expression &e) const |
| Provides lexicographical ordering between expressions. | |
| bool | is_polynomial () const |
| Checks if this symbolic expression is convertible to Polynomial. | |
| double | Evaluate (const Environment &env=Environment{}, RandomGenerator *random_generator=nullptr) const |
| Evaluates using a given environment (by default, an empty environment) and a random number generator. | |
| double | Evaluate (RandomGenerator *random_generator) const |
| Evaluates using an empty environment and a random number generator. | |
| Expression | EvaluatePartial (const Environment &env) const |
Partially evaluates this expression using an environment env. | |
| bool | is_expanded () const |
| Returns true if this symbolic expression is already expanded. | |
| Expression | Expand () const |
| Expands out products and positive integer powers in expression. | |
| Expression | Substitute (const Variable &var, const Expression &e) const |
Returns a copy of this expression replacing all occurrences of var with e. | |
| Expression | Substitute (const Substitution &s) const |
Returns a copy of this expression replacing all occurrences of the variables in s with corresponding expressions in s. | |
| Expression | Differentiate (const Variable &x) const |
Differentiates this symbolic expression with respect to the variable var. | |
| RowVectorX< Expression > | Jacobian (const Eigen::Ref< const VectorX< Variable > > &vars) const |
Let f be this Expression, computes a row vector of derivatives, [∂f/∂vars(0), ... , ∂f/∂vars(n-1)] with respect to the variables vars. | |
| std::string | to_string () const |
| Returns string representation of Expression. | |
| Expression & | operator++ () |
| Provides prefix increment operator (i.e. | |
| Expression | operator++ (int) |
| Provides postfix increment operator (i.e. | |
| Expression & | operator-- () |
| Provides prefix decrement operator (i.e. | |
| Expression | operator-- (int) |
| Provides postfix decrement operator (i.e. | |
Implements CopyConstructible, CopyAssignable, MoveConstructible, MoveAssignable | |
| Expression (const Expression &)=default | |
| Expression & | operator= (const Expression &)=default |
| Expression (Expression &&)=default | |
| Expression & | operator= (Expression &&)=default |
Static Public Member Functions | |
| static Expression | Zero () |
| Returns zero. | |
| static Expression | One () |
| Returns one. | |
| static Expression | Pi () |
| Returns Pi, the ratio of a circle’s circumference to its diameter. | |
| static Expression | E () |
| Return e, the base of natural logarithms. | |
| static Expression | NaN () |
| Returns NaN (Not-a-Number). | |
Friends | |
| class | ExpressionAddFactory |
| class | ExpressionMulFactory |
| template<bool> | |
| struct | internal::Gemm |
| template<class HashAlgorithm> | |
| void | hash_append (HashAlgorithm &hasher, const Expression &item) noexcept |
| Implements the hash_append generic hashing concept. | |
| Expression | operator+ (Expression lhs, const Expression &rhs) |
| Expression & | operator+= (Expression &lhs, const Expression &rhs) |
| Expression | operator+ (const Expression &e) |
| Provides unary plus operator. | |
| Expression | operator- (Expression lhs, const Expression &rhs) |
| Expression & | operator-= (Expression &lhs, const Expression &rhs) |
| Expression | operator- (const Expression &e) |
| Provides unary minus operator. | |
| Expression | operator* (Expression lhs, const Expression &rhs) |
| Expression & | operator*= (Expression &lhs, const Expression &rhs) |
| Expression | operator/ (Expression lhs, const Expression &rhs) |
| Expression & | operator/= (Expression &lhs, const Expression &rhs) |
| Expression | log (const Expression &e) |
| Expression | abs (const Expression &e) |
| Expression | exp (const Expression &e) |
| Expression | sqrt (const Expression &e) |
| Expression | pow (const Expression &e1, const Expression &e2) |
| Expression | sin (const Expression &e) |
| Expression | cos (const Expression &e) |
| Expression | tan (const Expression &e) |
| Expression | asin (const Expression &e) |
| Expression | acos (const Expression &e) |
| Expression | atan (const Expression &e) |
| Expression | atan2 (const Expression &e1, const Expression &e2) |
| Expression | sinh (const Expression &e) |
| Expression | cosh (const Expression &e) |
| Expression | tanh (const Expression &e) |
| Expression | min (const Expression &e1, const Expression &e2) |
| Expression | max (const Expression &e1, const Expression &e2) |
| Expression | clamp (const Expression &v, const Expression &lo, const Expression &hi) |
| Expression | ceil (const Expression &e) |
| Expression | floor (const Expression &e) |
| Expression | if_then_else (const Formula &f_cond, const Expression &e_then, const Expression &e_else) |
| Constructs if-then-else expression. | |
| Expression | uninterpreted_function (std::string name, std::vector< Expression > arguments) |
Constructs an uninterpreted-function expression with name and arguments. | |
| std::ostream & | operator<< (std::ostream &os, const Expression &e) |
| void | swap (Expression &a, Expression &b) |
| bool | is_constant (const Expression &e) |
Checks if e is a constant expression. | |
| bool | is_constant (const Expression &e, double value) |
Checks if e is a constant expression representing v. | |
| bool | is_nan (const Expression &e) |
Checks if e is NaN. | |
| bool | is_variable (const Expression &e) |
Checks if e is a variable expression. | |
| bool | is_addition (const Expression &e) |
Checks if e is an addition expression. | |
| bool | is_multiplication (const Expression &e) |
Checks if e is a multiplication expression. | |
| bool | is_division (const Expression &e) |
Checks if e is a division expression. | |
| bool | is_log (const Expression &e) |
Checks if e is a log expression. | |
| bool | is_abs (const Expression &e) |
Checks if e is an abs expression. | |
| bool | is_exp (const Expression &e) |
Checks if e is an exp expression. | |
| bool | is_sqrt (const Expression &e) |
Checks if e is a square-root expression. | |
| bool | is_pow (const Expression &e) |
Checks if e is a power-function expression. | |
| bool | is_sin (const Expression &e) |
Checks if e is a sine expression. | |
| bool | is_cos (const Expression &e) |
Checks if e is a cosine expression. | |
| bool | is_tan (const Expression &e) |
Checks if e is a tangent expression. | |
| bool | is_asin (const Expression &e) |
Checks if e is an arcsine expression. | |
| bool | is_acos (const Expression &e) |
Checks if e is an arccosine expression. | |
| bool | is_atan (const Expression &e) |
Checks if e is an arctangent expression. | |
| bool | is_atan2 (const Expression &e) |
Checks if e is an arctangent2 expression. | |
| bool | is_sinh (const Expression &e) |
Checks if e is a hyperbolic-sine expression. | |
| bool | is_cosh (const Expression &e) |
Checks if e is a hyperbolic-cosine expression. | |
| bool | is_tanh (const Expression &e) |
Checks if e is a hyperbolic-tangent expression. | |
| bool | is_min (const Expression &e) |
Checks if e is a min expression. | |
| bool | is_max (const Expression &e) |
Checks if e is a max expression. | |
| bool | is_ceil (const Expression &e) |
Checks if e is a ceil expression. | |
| bool | is_floor (const Expression &e) |
Checks if e is a floor expression. | |
| bool | is_if_then_else (const Expression &e) |
Checks if e is an if-then-else expression. | |
| bool | is_uninterpreted_function (const Expression &e) |
Checks if e is an uninterpreted-function expression. | |
| double | get_constant_value (const Expression &e) |
Returns the constant value of the constant expression e. | |
| const ExpressionVar & | to_variable (const Expression &e) |
| const UnaryExpressionCell & | to_unary (const Expression &e) |
| const BinaryExpressionCell & | to_binary (const Expression &e) |
| const ExpressionAdd & | to_addition (const Expression &e) |
| const ExpressionMul & | to_multiplication (const Expression &e) |
| const ExpressionDiv & | to_division (const Expression &e) |
| const ExpressionLog & | to_log (const Expression &e) |
| const ExpressionAbs & | to_abs (const Expression &e) |
| const ExpressionExp & | to_exp (const Expression &e) |
| const ExpressionSqrt & | to_sqrt (const Expression &e) |
| const ExpressionPow & | to_pow (const Expression &e) |
| const ExpressionSin & | to_sin (const Expression &e) |
| const ExpressionCos & | to_cos (const Expression &e) |
| const ExpressionTan & | to_tan (const Expression &e) |
| const ExpressionAsin & | to_asin (const Expression &e) |
| const ExpressionAcos & | to_acos (const Expression &e) |
| const ExpressionAtan & | to_atan (const Expression &e) |
| const ExpressionAtan2 & | to_atan2 (const Expression &e) |
| const ExpressionSinh & | to_sinh (const Expression &e) |
| const ExpressionCosh & | to_cosh (const Expression &e) |
| const ExpressionTanh & | to_tanh (const Expression &e) |
| const ExpressionMin & | to_min (const Expression &e) |
| const ExpressionMax & | to_max (const Expression &e) |
| const ExpressionCeiling & | to_ceil (const Expression &e) |
| const ExpressionFloor & | to_floor (const Expression &e) |
| const ExpressionIfThenElse & | to_if_then_else (const Expression &e) |
| const ExpressionUninterpretedFunction & | to_uninterpreted_function (const Expression &e) |
| ExpressionVar & | to_variable (Expression *e) |
| UnaryExpressionCell & | to_unary (Expression *e) |
| BinaryExpressionCell & | to_binary (Expression *e) |
| ExpressionAdd & | to_addition (Expression *e) |
| ExpressionMul & | to_multiplication (Expression *e) |
| ExpressionDiv & | to_division (Expression *e) |
| ExpressionLog & | to_log (Expression *e) |
| ExpressionAbs & | to_abs (Expression *e) |
| ExpressionExp & | to_exp (Expression *e) |
| ExpressionSqrt & | to_sqrt (Expression *e) |
| ExpressionPow & | to_pow (Expression *e) |
| ExpressionSin & | to_sin (Expression *e) |
| ExpressionCos & | to_cos (Expression *e) |
| ExpressionTan & | to_tan (Expression *e) |
| ExpressionAsin & | to_asin (Expression *e) |
| ExpressionAcos & | to_acos (Expression *e) |
| ExpressionAtan & | to_atan (Expression *e) |
| ExpressionAtan2 & | to_atan2 (Expression *e) |
| ExpressionSinh & | to_sinh (Expression *e) |
| ExpressionCosh & | to_cosh (Expression *e) |
| ExpressionTanh & | to_tanh (Expression *e) |
| ExpressionMin & | to_min (Expression *e) |
| ExpressionMax & | to_max (Expression *e) |
| ExpressionCeiling & | to_ceil (Expression *e) |
| ExpressionFloor & | to_floor (Expression *e) |
| ExpressionIfThenElse & | to_if_then_else (Expression *e) |
| ExpressionUninterpretedFunction & | to_uninterpreted_function (Expression *e) |
|
default |
|
default |
|
default |
|
default |
Default constructor.
It constructs Zero().
| Expression | ( | double | constant | ) |
Constructs a constant.
| Expression | ( | const Variable & | var | ) |
Constructs an expression from var.
var is not a BOOLEAN variable.
|
nodiscard |
Differentiates this symbolic expression with respect to the variable var.
| std::exception | if it is not differentiable. |
|
static |
Return e, the base of natural logarithms.
|
nodiscard |
Checks structural equality.
Two expressions e1 and e2 are structurally equal when they have the same internal AST(abstract-syntax tree) representation. Please note that we can have two computationally (or extensionally) equivalent expressions which are not structurally equal. For example, consider:
e1 = 2 * (x + y) e2 = 2x + 2y
Obviously, we know that e1 and e2 are evaluated to the same value for all assignments to x and y. However, e1 and e2 are not structurally equal by the definition. Note that e1 is a multiplication expression (is_multiplication(e1) is true) while e2 is an addition expression (is_addition(e2) is true).
One main reason we use structural equality in EqualTo is due to Richardson's Theorem. It states that checking ∀x. E(x) = F(x) is undecidable when we allow sin, asin, log, exp in E and F. Read https://en.wikipedia.org/wiki/Richardson%27s_theorem for details.
Note that for polynomial cases, you can use Expand method and check if two polynomial expressions p1 and p2 are computationally equal. To do so, you check the following:
p1.Expand().EqualTo(p2.Expand())
| double Evaluate | ( | const Environment & | env = Environment{}, |
| RandomGenerator * | random_generator = nullptr ) const |
Evaluates using a given environment (by default, an empty environment) and a random number generator.
If there is a random variable in this expression which is unassigned in env, this method uses random_generator to sample a value and use the value to substitute all occurrences of the variable in this expression.
| std::exception | if there exists a non-random variable in this expression whose assignment is not provided by env. |
| std::exception | if an unassigned random variable is detected while random_generator is nullptr. |
| std::exception | if NaN is detected during evaluation. |
| double Evaluate | ( | RandomGenerator * | random_generator | ) | const |
Evaluates using an empty environment and a random number generator.
It uses random_generator to sample values for the random variables in this expression.
See the above overload for the exceptions that it might throw.
|
nodiscard |
Partially evaluates this expression using an environment env.
Internally, this method promotes env into a substitution (Variable → Expression) and call Evaluate::Substitute with it.
| std::exception | if NaN is detected during evaluation. |
|
nodiscard |
Expands out products and positive integer powers in expression.
For example, (x + 1) * (x - 1) is expanded to x^2 - 1 and (x + y)^2 is expanded to x^2 + 2xy + y^2. Note that Expand applies recursively to sub-expressions. For instance, sin(2 * (x + y)) is expanded to sin(2x + 2y). It also simplifies "division by constant" cases. See "drake/common/test/symbolic_expansion_test.cc" to find the examples.
| std::exception | if NaN is detected during expansion. |
|
nodiscard |
Returns expression kind.
|
nodiscard |
Same as GetVariables(); we provide this overload for compatibility with Formula.
|
nodiscard |
Collects variables in expression.
|
nodiscard |
Returns true if this symbolic expression is already expanded.
Expression::Expand() uses this flag to avoid calling ExpressionCell::Expand() on an pre-expanded expressions. Expression::Expand() also sets this flag before returning the result.
|
nodiscard |
Checks if this symbolic expression is convertible to Polynomial.
|
nodiscard |
Let f be this Expression, computes a row vector of derivatives, [∂f/∂vars(0), ... , ∂f/∂vars(n-1)] with respect to the variables vars.
|
nodiscard |
Provides lexicographical ordering between expressions.
This function is used as a compare function in map<Expression> and set<Expression> via std::less<drake::symbolic::Expression>.
|
static |
Returns NaN (Not-a-Number).
|
static |
Returns one.
| Expression & operator++ | ( | ) |
Provides prefix increment operator (i.e.
++x).
| Expression operator++ | ( | int | ) |
Provides postfix increment operator (i.e.
x++).
| Expression & operator-- | ( | ) |
Provides prefix decrement operator (i.e.
–x).
| Expression operator-- | ( | int | ) |
Provides postfix decrement operator (i.e.
x–).
|
default |
|
default |
|
static |
Returns Pi, the ratio of a circle’s circumference to its diameter.
|
nodiscard |
Returns a copy of this expression replacing all occurrences of the variables in s with corresponding expressions in s.
Note that the substitutions occur simultaneously. For example, (x / y).Substitute({{x, y}, {y, x}}) gets (y / x).
| std::exception | if NaN is detected during substitution. |
|
nodiscard |
Returns a copy of this expression replacing all occurrences of var with e.
| std::exception | if NaN is detected during substitution. |
|
nodiscard |
Returns string representation of Expression.
|
static |
Returns zero.
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
Returns the constant value of the constant expression e.
e is a constant expression.}
|
friend |
Implements the hash_append generic hashing concept.
|
friend |
Constructs if-then-else expression.
if_then_else(cond, expr_then, expr_else)
The value returned by the above if-then-else expression is expr_then if cond is evaluated to true. Otherwise, it returns expr_else.
The semantics is similar to the C++'s conditional expression constructed by its ternary operator, ?:. However, there is a key difference between the C++'s conditional expression and our if_then_else expression in a way the arguments are evaluated during the construction.
expr_then (respectively, the else expression expr_else) is only evaluated when the conditional expression cond is evaluated to true (respectively, when cond is evaluated to false).expr_then and expr_else are evaluated first and then passed to the if_then_else function.if_then_else expression, it is highly recommended to use the provided native definitions for them because it allows solvers to detect specific math functions and to have a room for special optimizations.
|
friend |
|
friend |
Checks if e is an abs expression.
|
friend |
Checks if e is an arccosine expression.
|
friend |
Checks if e is an addition expression.
|
friend |
Checks if e is an arcsine expression.
|
friend |
Checks if e is an arctangent expression.
|
friend |
Checks if e is an arctangent2 expression.
|
friend |
Checks if e is a ceil expression.
|
friend |
Checks if e is a constant expression.
|
friend |
Checks if e is a constant expression representing v.
|
friend |
Checks if e is a cosine expression.
|
friend |
Checks if e is a hyperbolic-cosine expression.
|
friend |
Checks if e is a division expression.
|
friend |
Checks if e is an exp expression.
|
friend |
Checks if e is a floor expression.
|
friend |
Checks if e is an if-then-else expression.
|
friend |
Checks if e is a log expression.
|
friend |
Checks if e is a max expression.
|
friend |
Checks if e is a min expression.
|
friend |
Checks if e is a multiplication expression.
|
friend |
Checks if e is NaN.
|
friend |
Checks if e is a power-function expression.
|
friend |
Checks if e is a sine expression.
|
friend |
Checks if e is a hyperbolic-sine expression.
|
friend |
Checks if e is a square-root expression.
|
friend |
Checks if e is a tangent expression.
|
friend |
Checks if e is a hyperbolic-tangent expression.
|
friend |
Checks if e is an uninterpreted-function expression.
|
friend |
Checks if e is a variable expression.
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
Provides unary plus operator.
|
friend |
|
friend |
|
friend |
Provides unary minus operator.
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
Constructs an uninterpreted-function expression with name and arguments.
An uninterpreted function is an opaque function that has no other property than its name and a list of its arguments. This is useful to applications where it is good enough to provide abstract information of a function without exposing full details. Declaring sparsity of a system is a typical example.