dReal4
symbolic_expression.h
1 #pragma once
2 
3 #include <algorithm> // for cpplint only
4 #include <cstddef>
5 #include <functional>
6 #include <limits>
7 #include <map>
8 #include <ostream>
9 #include <string>
10 #include <type_traits>
11 #include <unordered_map>
12 #include <utility>
13 #include <vector>
14 
15 #include "dreal/symbolic/hash.h"
16 #include "dreal/symbolic/symbolic_environment.h"
17 #include "dreal/symbolic/symbolic_variable.h"
18 #include "dreal/symbolic/symbolic_variables.h"
19 
20 namespace dreal {
21 namespace drake {
22 namespace symbolic {
23 
24 /** Kinds of symbolic expressions. */
25 enum class ExpressionKind {
26  Constant, ///< floating-point constant (double)
27  RealConstant, ///< real constant (represented by an interval)
28  Var, ///< variable
29  Add, ///< addition (+)
30  Mul, ///< multiplication (*)
31  Div, ///< division (/)
32  Log, ///< logarithms
33  Abs, ///< absolute value function
34  Exp, ///< exponentiation
35  Sqrt, ///< square root
36  Pow, ///< power function
37  Sin, ///< sine
38  Cos, ///< cosine
39  Tan, ///< tangent
40  Asin, ///< arcsine
41  Acos, ///< arccosine
42  Atan, ///< arctangent
43  Atan2, ///< arctangent2 (atan2(y,x) = atan(y/x))
44  Sinh, ///< hyperbolic sine
45  Cosh, ///< hyperbolic cosine
46  Tanh, ///< hyperbolic tangent
47  Min, ///< min
48  Max, ///< max
49  IfThenElse, ///< if then else
50  NaN, ///< NaN
51  UninterpretedFunction, ///< Uninterpreted function
52  // TODO(soonho): add Integral
53 };
54 
55 /** Total ordering between ExpressionKinds. */
56 bool operator<(ExpressionKind k1, ExpressionKind k2);
57 
58 class ExpressionCell; // In symbolic_expression_cell.h
59 class ExpressionConstant; // In symbolic_expression_cell.h
60 class ExpressionRealConstant; // In symbolic_expression_cell.h
61 class ExpressionVar; // In symbolic_expression_cell.h
62 class UnaryExpressionCell; // In symbolic_expression_cell.h
63 class BinaryExpressionCell; // In symbolic_expression_cell.h
64 class ExpressionAdd; // In symbolic_expression_cell.h
65 class ExpressionMul; // In symbolic_expression_cell.h
66 class ExpressionDiv; // In symbolic_expression_cell.h
67 class ExpressionLog; // In symbolic_expression_cell.h
68 class ExpressionAbs; // In symbolic_expression_cell.h
69 class ExpressionExp; // In symbolic_expression_cell.h
70 class ExpressionSqrt; // In symbolic_expression_cell.h
71 class ExpressionPow; // In symbolic_expression_cell.h
72 class ExpressionSin; // In symbolic_expression_cell.h
73 class ExpressionCos; // In symbolic_expression_cell.h
74 class ExpressionTan; // In symbolic_expression_cell.h
75 class ExpressionAsin; // In symbolic_expression_cell.h
76 class ExpressionAcos; // In symbolic_expression_cell.h
77 class ExpressionAtan; // In symbolic_expression_cell.h
78 class ExpressionAtan2; // In symbolic_expression_cell.h
79 class ExpressionSinh; // In symbolic_expression_cell.h
80 class ExpressionCosh; // In symbolic_expression_cell.h
81 class ExpressionTanh; // In symbolic_expression_cell.h
82 class ExpressionMin; // In symbolic_expression_cell.h
83 class ExpressionMax; // In symbolic_expression_cell.h
84 class ExpressionIfThenElse; // In symbolic_expression_cell.h
85 class ExpressionUninterpretedFunction; // In symbolic_expression_cell.h
86 class Formula; // In symbolic_formula.h
87 class Expression;
88 
89 // ExpressionSubstitution is a map from a Variable to a symbolic expression. It
90 // is used in Expression::Substitute and Formula::Substitute methods as an
91 // argument.
92 using ExpressionSubstitution =
93  std::unordered_map<Variable, Expression, hash_value<Variable>>;
94 
95 // FormulaSubstitution is a map from a Variable to a symbolic formula. It
96 // is used in Expression::Substitute and Formula::Substitute methods as an
97 // argument.
98 using FormulaSubstitution =
99  std::unordered_map<Variable, Formula, hash_value<Variable>>;
100 
101 /** Represents a symbolic form of an expression.
102 
103 Its syntax tree is as follows:
104 
105 @verbatim
106  E := Var | Constant(double) | RealConstant(double, double)
107  | E + ... + E | E * ... * E | E / E | log(E)
108  | abs(E) | exp(E) | sqrt(E) | pow(E, E) | sin(E) | cos(E) | tan(E)
109  | asin(E) | acos(E) | atan(E) | atan2(E, E) | sinh(E) | cosh(E) | tanh(E)
110  | min(E, E) | max(E, E) | if_then_else(F, E, E) | NaN
111  | uninterpreted_function(name, {v_1, ..., v_n})
112 @endverbatim
113 
114 In the implementation, Expression is a simple wrapper including a
115 pointer to ExpressionCell class which is a super-class of different
116 kinds of symbolic expressions (i.e. ExpressionAdd, ExpressionMul,
117 ExpressionLog, ExpressionSin).
118 
119 @note The sharing of sub-expressions is not yet implemented.
120 
121 @note -E is represented as -1 * E internally.
122 
123 @note A subtraction E1 - E2 is represented as E1 + (-1 * E2) internally.
124 
125 The following simple simplifications are implemented:
126 @verbatim
127  E + 0 -> E
128  0 + E -> E
129  E - 0 -> E
130  E - E -> 0
131  E * 1 -> E
132  1 * E -> E
133  E * 0 -> 0
134  0 * E -> 0
135  E / 1 -> E
136  E / E -> 1
137  pow(E, 0) -> 1
138  pow(E, 1) -> E
139  E * E -> E^2 (= pow(E, 2))
140  sqrt(E * E) -> |E| (= abs(E))
141  sqrt(E) * sqrt(E) -> E
142 @endverbatim
143 
144 Constant folding is implemented:
145 @verbatim
146  E(c1) + E(c2) -> E(c1 + c2) // c1, c2 are constants
147  E(c1) - E(c2) -> E(c1 - c2)
148  E(c1) * E(c2) -> E(c1 * c2)
149  E(c1) / E(c2) -> E(c1 / c2)
150  f(E(c)) -> E(f(c)) // c is a constant, f is a math function
151 @endverbatim
152 
153 For the math functions which are only defined over a restricted domain (namely,
154 log, sqrt, pow, asin, acos), we check the domain of argument(s), and throw
155 std::domain_error exception if a function is not well-defined for a given
156 argument(s).
157 
158 Relational operators over expressions (==, !=, <, >, <=, >=) return
159 symbolic::Formula instead of bool. Those operations are declared in
160 symbolic_formula.h file. To check structural equality between two expressions a
161 separate function, Expression::EqualTo, is provided.
162 
163 */
164 class Expression {
165  public:
166  Expression(const Expression&);
167  Expression& operator=(const Expression&);
168  Expression(Expression&&) noexcept;
169  Expression& operator=(Expression&&) noexcept;
170  ~Expression();
171 
172  /** Default constructor. It constructs Zero(). */
173  Expression();
174 
175  /** Constructs a constant (floating-point). */
176  // NOLINTNEXTLINE(runtime/explicit): This conversion is desirable.
177  Expression(double d);
178  /** Constructs an expression from @p var.
179  * @pre @p var is neither a dummy nor a BOOLEAN variable.
180  */
181  // NOLINTNEXTLINE(runtime/explicit): This conversion is desirable.
182  Expression(const Variable& var);
183  /** Returns expression kind. */
184  ExpressionKind get_kind() const;
185  /** Returns hash value. */
186  size_t get_hash() const;
187  /** Collects variables in expression. */
188  const Variables& GetVariables() const;
189 
190  /** Checks structural equality.
191  *
192  * Two expressions e1 and e2 are structurally equal when they have the same
193  * internal AST(abstract-syntax tree) representation. Please note that we can
194  * have two computationally (or extensionally) equivalent expressions which
195  * are not structurally equal. For example, consider:
196  *
197  * e1 = 2 * (x + y)
198  * e2 = 2x + 2y
199  *
200  * Obviously, we know that e1 and e2 are evaluated to the same value for all
201  * assignments to x and y. However, e1 and e2 are not structurally equal by
202  * the definition. Note that e1 is a multiplication expression
203  * (is_multiplication(e1) is true) while e2 is an addition expression
204  * (is_addition(e2) is true).
205  *
206  * One main reason we use structural equality in EqualTo is due to
207  * Richardson's Theorem. It states that checking ∀x. E(x) = F(x) is
208  * undecidable when we allow sin, asin, log, exp in E and F. Read
209  * https://en.wikipedia.org/wiki/Richardson%27s_theorem for details.
210  *
211  * Note that for polynomial cases, you can use Expand method and check if two
212  * polynomial expressions p1 and p2 are computationally equal. To do so, you
213  * check the following:
214  *
215  * (p1.Expand() - p2.Expand()).EqualTo(0).
216  */
217  bool EqualTo(const Expression& e) const;
218 
219  /** Provides lexicographical ordering between expressions.
220  This function is used as a compare function in map<Expression> and
221  set<Expression> via std::less<dreal::drake::symbolic::Expression>. */
222  bool Less(const Expression& e) const;
223 
224  /** Checks if this symbolic expression is convertible to Polynomial. */
225  bool is_polynomial() const;
226 
227  /// Returns true if this symbolic expression includes an ITE (If-Then-Else)
228  /// expression.
229  bool include_ite() const;
230 
231  /** Evaluates under a given environment (by default, an empty environment).
232  * @throws std::runtime_error if NaN is detected during evaluation.
233  */
234  double Evaluate(const Environment& env = Environment{}) const;
235 
236  /** Partially evaluates this expression using an environment @p
237  * env. Internally, this method promotes @p env into a substitution
238  * (Variable → Expression) and call Evaluate::Substitute with it.
239  *
240  * @throws std::runtime_error if NaN is detected during evaluation.
241  */
242  Expression EvaluatePartial(const Environment& env) const;
243 
244  /** Expands out products and positive integer powers in expression. For
245  * example, `(x + 1) * (x - 1)` is expanded to `x^2 - 1` and `(x + y)^2` is
246  * expanded to `x^2 + 2xy + y^2`. Note that Expand applies recursively to
247  * sub-expressions. For instance, `sin(2 * (x + y))` is expanded to `sin(2x +
248  * 2y)`. It also simplifies "division by constant" cases. See
249  * "symbolic/test/symbolic_expansion_test.cc" to find the examples.
250  *
251  * @throws std::runtime_error if NaN is detected during expansion.
252  */
253  Expression Expand() const;
254 
255  /** Returns a copy of this expression replacing all occurrences of @p var
256  * with @p e.
257  * @throws std::runtime_error if NaN is detected during substitution.
258  */
259  Expression Substitute(const Variable& var, const Expression& e) const;
260 
261  /** Returns a copy of this expression replacing all occurrences of the
262  * variables in @p expr_subst with corresponding expressions in @p expr_subst
263  * and all occurrences of the variables in @p formula_subst with corresponding
264  * formulas in @p formula_subst.
265  *
266  * Note that the substitutions occur simultaneously. For example,
267  * (x / y).Substitute({{x, y}, {y, x}}, {}) gets (y / x).
268  *
269  * @throws std::runtime_error if NaN is detected during substitution.
270  */
271  Expression Substitute(const ExpressionSubstitution& expr_subst,
272  const FormulaSubstitution& formula_subst) const;
273 
274  /** Returns a copy of this expression replacing all occurrences of the
275  * variables in @p expr_subst with corresponding expressions in @p expr_subst.
276  *
277  * @note This is equivalent to `Substitute(expr_subst, {})`.
278  * @throws std::runtime_error if NaN is detected during substitution.
279  */
280  Expression Substitute(const ExpressionSubstitution& expr_subst) const;
281 
282  /** Returns a copy of this expression replacing all occurrences of the
283  * variables in @p formula_subst with corresponding formulas in @p
284  * formula_subst.
285  *
286  * @note This is equivalent to `Substitute({}, formula_subst)`.
287  * @throws std::runtime_error if NaN is detected during substitution.
288  */
289  Expression Substitute(const FormulaSubstitution& formula_subst) const;
290 
291  /** Differentiates this symbolic expression with respect to the variable @p
292  * var.
293  * @throws std::runtime_error if it is not differentiable.
294  */
295  Expression Differentiate(const Variable& x) const;
296 
297  /** Returns string representation of Expression. */
298  std::string to_string() const;
299 
300  /** Returns zero. */
301  static Expression Zero();
302  /** Returns one. */
303  static Expression One();
304  /** Returns Pi, the ratio of a circle’s circumference to its diameter. */
305  static Expression Pi();
306  /** Return e, the base of natural logarithms. */
307  static Expression E();
308  /** Returns NaN (Not-a-Number). */
309  static Expression NaN();
310 
311  friend Expression operator+(const Expression& lhs, const Expression& rhs);
312  friend Expression operator+(const Expression& lhs, Expression&& rhs);
313  friend Expression operator+(Expression&& lhs, const Expression& rhs);
314  friend Expression operator+(Expression&& lhs, Expression&& rhs);
315  // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
316  friend Expression& operator+=(Expression& lhs, const Expression& rhs);
317 
318  /** Provides prefix increment operator (i.e. ++x). */
319  Expression& operator++();
320  /** Provides postfix increment operator (i.e. x++). */
321  Expression operator++(int);
322 
323  friend Expression operator-(const Expression& lhs, const Expression& rhs);
324  friend Expression operator-(const Expression& lhs, Expression&& rhs);
325  friend Expression operator-(Expression&& lhs, const Expression& rhs);
326  friend Expression operator-(Expression&& lhs, Expression&& rhs);
327  // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
328  friend Expression& operator-=(Expression& lhs, const Expression& rhs);
329 
330  /** Provides unary plus operator. */
331  friend Expression operator+(const Expression& e);
332 
333  /** Provides unary minus operator. */
334  friend Expression operator-(const Expression& e);
335  friend Expression operator-(Expression&& e);
336 
337  /** Provides prefix decrement operator (i.e. --x). */
338  Expression& operator--();
339  /** Provides postfix decrement operator (i.e. x--). */
340  Expression operator--(int);
341 
342  friend Expression operator*(const Expression& lhs, const Expression& rhs);
343  friend Expression operator*(const Expression& lhs, Expression&& rhs);
344  friend Expression operator*(Expression&& lhs, const Expression& rhs);
345  friend Expression operator*(Expression&& lhs, Expression&& rhs);
346  // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
347  friend Expression& operator*=(Expression& lhs, const Expression& rhs);
348 
349  friend Expression operator/(Expression lhs, const Expression& rhs);
350  // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
351  friend Expression& operator/=(Expression& lhs, const Expression& rhs);
352 
353  /// Creates an real-constant expression represented by [@p lb, @p
354  /// ub]. @p use_lb_as_representative is used to select its
355  /// representative value. If it is true, @p lb is used. Otherwise,
356  /// @p ub is used.
357  friend Expression real_constant(double lb, double ub,
358  bool use_lb_as_representative);
359  friend Expression log(const Expression& e);
360  friend Expression abs(const Expression& e);
361  friend Expression exp(const Expression& e);
362  friend Expression sqrt(const Expression& e);
363  friend Expression pow(const Expression& e1, const Expression& e2);
364  friend Expression sin(const Expression& e);
365  friend Expression cos(const Expression& e);
366  friend Expression tan(const Expression& e);
367  friend Expression asin(const Expression& e);
368  friend Expression acos(const Expression& e);
369  friend Expression atan(const Expression& e);
370  friend Expression atan2(const Expression& e1, const Expression& e2);
371  friend Expression sinh(const Expression& e);
372  friend Expression cosh(const Expression& e);
373  friend Expression tanh(const Expression& e);
374  friend Expression min(const Expression& e1, const Expression& e2);
375  friend Expression max(const Expression& e1, const Expression& e2);
376 
377  /** Constructs if-then-else expression.
378 
379  @verbatim
380  if_then_else(cond, expr_then, expr_else)
381  @endverbatim
382 
383  The value returned by the above if-then-else expression is @p expr_then if
384  @p cond is evaluated to true. Otherwise, it returns @p expr_else.
385 
386  The semantics is similar to the C++'s conditional expression constructed by
387  its ternary operator, @c ?:. However, there is a key difference between the
388  C++'s conditional expression and our @c if_then_else expression in a way the
389  arguments are evaluated during the construction.
390 
391  - In case of the C++'s conditional expression, <tt> cond ? expr_then :
392  expr_else</tt>, the then expression @c expr_then (respectively, the else
393  expression @c expr_else) is \b only evaluated when the conditional
394  expression @c cond is evaluated to \b true (respectively, when @c cond is
395  evaluated to \b false).
396 
397  - In case of the symbolic expression, <tt>if_then_else(cond, expr_then,
398  expr_else)</tt>, however, \b both arguments @c expr_then and @c expr_else
399  are evaluated first and then passed to the @c if_then_else function.
400 
401  @note This function returns an \b expression and it is different from the
402  C++'s if-then-else \b statement.
403 
404  @note While it is still possible to define <tt> min, max, abs</tt> math
405  functions using @c if_then_else expression, it is highly \b recommended to
406  use the provided native definitions for them because it allows solvers to
407  detect specific math functions and to have a room for special
408  optimizations.
409 
410  @note More information about the C++'s conditional expression and ternary
411  operator is available at
412  http://en.cppreference.com/w/cpp/language/operator_other#Conditional_operator.
413  */
414  friend Expression if_then_else(const Formula& f_cond,
415  const Expression& e_then,
416  const Expression& e_else);
417  friend Expression uninterpreted_function(const std::string& name,
418  const Variables& vars);
419 
420  friend std::ostream& operator<<(std::ostream& os, const Expression& e);
421  friend void swap(Expression& a, Expression& b) { std::swap(a.ptr_, b.ptr_); }
422 
423  friend bool is_constant(const Expression& e);
424  friend bool is_real_constant(const Expression& e);
425  friend bool is_variable(const Expression& e);
426  friend bool is_addition(const Expression& e);
427  friend bool is_multiplication(const Expression& e);
428  friend bool is_division(const Expression& e);
429  friend bool is_log(const Expression& e);
430  friend bool is_abs(const Expression& e);
431  friend bool is_exp(const Expression& e);
432  friend bool is_sqrt(const Expression& e);
433  friend bool is_pow(const Expression& e);
434  friend bool is_sin(const Expression& e);
435  friend bool is_cos(const Expression& e);
436  friend bool is_tan(const Expression& e);
437  friend bool is_asin(const Expression& e);
438  friend bool is_acos(const Expression& e);
439  friend bool is_atan(const Expression& e);
440  friend bool is_atan2(const Expression& e);
441  friend bool is_sinh(const Expression& e);
442  friend bool is_cosh(const Expression& e);
443  friend bool is_tanh(const Expression& e);
444  friend bool is_min(const Expression& e);
445  friend bool is_max(const Expression& e);
446  friend bool is_if_then_else(const Expression& e);
447  friend bool is_uninterpreted_function(const Expression& e);
448 
449  // Note that the following cast functions are only for low-level operations
450  // and not exposed to the user of symbolic/symbolic_expression.h
451  // header. These functions are declared in
452  // symbolic/symbolic_expression_cell.h header.
453  friend const ExpressionConstant* to_constant(const Expression& e);
454  friend const ExpressionRealConstant* to_real_constant(const Expression& e);
455  friend const ExpressionVar* to_variable(const Expression& e);
456  friend const UnaryExpressionCell* to_unary(const Expression& e);
457  friend const BinaryExpressionCell* to_binary(const Expression& e);
458  friend const ExpressionAdd* to_addition(const Expression& e);
459  friend ExpressionAdd* to_addition(Expression& e);
460  friend const ExpressionMul* to_multiplication(const Expression& e);
461  friend ExpressionMul* to_multiplication(Expression& e);
462  friend const ExpressionDiv* to_division(const Expression& e);
463  friend const ExpressionLog* to_log(const Expression& e);
464  friend const ExpressionAbs* to_abs(const Expression& e);
465  friend const ExpressionExp* to_exp(const Expression& e);
466  friend const ExpressionSqrt* to_sqrt(const Expression& e);
467  friend const ExpressionPow* to_pow(const Expression& e);
468  friend const ExpressionSin* to_sin(const Expression& e);
469  friend const ExpressionCos* to_cos(const Expression& e);
470  friend const ExpressionTan* to_tan(const Expression& e);
471  friend const ExpressionAsin* to_asin(const Expression& e);
472  friend const ExpressionAcos* to_acos(const Expression& e);
473  friend const ExpressionAtan* to_atan(const Expression& e);
474  friend const ExpressionAtan2* to_atan2(const Expression& e);
475  friend const ExpressionSinh* to_sinh(const Expression& e);
476  friend const ExpressionCosh* to_cosh(const Expression& e);
477  friend const ExpressionTanh* to_tanh(const Expression& e);
478  friend const ExpressionMin* to_min(const Expression& e);
479  friend const ExpressionMax* to_max(const Expression& e);
480  friend const ExpressionIfThenElse* to_if_then_else(const Expression& e);
481  friend const ExpressionUninterpretedFunction* to_uninterpreted_function(
482  const Expression& e);
483 
484  friend class ExpressionAddFactory;
485  friend class ExpressionMulFactory;
486  friend class ExpressionCell;
487 
488  private:
489  static ExpressionCell* make_cell(double d);
490 
491  explicit Expression(ExpressionCell* ptr);
492 
493  ExpressionCell* ptr_{nullptr};
494 };
495 
496 Expression operator+(const Expression& lhs, const Expression& rhs);
497 Expression operator+(const Expression& lhs, Expression&& rhs);
498 Expression operator+(Expression&& lhs, const Expression& rhs);
499 Expression operator+(Expression&& lhs, Expression&& rhs);
500 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
501 Expression& operator+=(Expression& lhs, const Expression& rhs);
502 
503 Expression operator-(const Expression& lhs, const Expression& rhs);
504 Expression operator-(const Expression& lhs, Expression&& rhs);
505 Expression operator-(Expression&& lhs, const Expression& rhs);
506 Expression operator-(Expression&& lhs, Expression&& rhs);
507 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
508 Expression& operator-=(Expression& lhs, const Expression& rhs);
509 
510 Expression operator+(const Expression& e);
511 
512 Expression operator-(const Expression& e);
513 Expression operator-(Expression&& e);
514 
515 Expression operator*(const Expression& lhs, const Expression& rhs);
516 Expression operator*(const Expression& lhs, Expression&& rhs);
517 Expression operator*(Expression&& lhs, const Expression& rhs);
518 Expression operator*(Expression&& lhs, Expression&& rhs);
519 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
520 Expression& operator*=(Expression& lhs, const Expression& rhs);
521 
522 Expression operator/(Expression lhs, const Expression& rhs);
523 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
524 Expression& operator/=(Expression& lhs, const Expression& rhs);
525 
526 /// Creates an expression for (∑ᵢ expressionsᵢ).
527 /// @note When `expressions` is an empty vector, it returns Expression::Zero().
528 Expression Sum(const std::vector<Expression>& expressions);
529 
530 /// Creates an expression for (∏ᵢ expressionsᵢ).
531 /// @note When `expressions` is an empty vector, it returns Expression::One().
532 Expression Prod(const std::vector<Expression>& expressions);
533 
534 Expression real_constant(double lb, double ub, bool use_lb_as_representative);
535 Expression log(const Expression& e);
536 Expression abs(const Expression& e);
537 Expression exp(const Expression& e);
538 Expression sqrt(const Expression& e);
539 Expression pow(const Expression& e1, const Expression& e2);
540 Expression sin(const Expression& e);
541 Expression cos(const Expression& e);
542 Expression tan(const Expression& e);
543 Expression asin(const Expression& e);
544 Expression acos(const Expression& e);
545 Expression atan(const Expression& e);
546 Expression atan2(const Expression& e1, const Expression& e2);
547 Expression sinh(const Expression& e);
548 Expression cosh(const Expression& e);
549 Expression tanh(const Expression& e);
550 Expression min(const Expression& e1, const Expression& e2);
551 Expression max(const Expression& e1, const Expression& e2);
552 Expression if_then_else(const Formula& f_cond, const Expression& e_then,
553  const Expression& e_else);
554 
555 /** Constructs an uninterpreted-function expression with @p name and @p vars.
556  * An uninterpreted function is an opaque function that has no other property
557  * than its name and a set of its arguments. This is useful to applications
558  * where it is good enough to provide abstract information of a function without
559  * exposing full details. Declaring sparsity of a system is a typical example.
560  *
561  * See also `FunctionalForm::Arbitrary(Variables v)` which shares the same
562  * motivation.
563  */
564 Expression uninterpreted_function(const std::string& name,
565  const Variables& vars);
566 void swap(Expression& a, Expression& b);
567 
568 std::ostream& operator<<(std::ostream& os, const Expression& e);
569 
570 /** Checks if @p e is a floating-point constant expression. */
571 bool is_constant(const Expression& e);
572 /** Checks if @p e is a floating-point constant expression representing @p v. */
573 bool is_constant(const Expression& e, double v);
574 /** Checks if @p e is a real constant expression. */
575 bool is_real_constant(const Expression& e);
576 /** Checks if @p e is 0.0. */
577 bool is_zero(const Expression& e);
578 /** Checks if @p e is 1.0. */
579 bool is_one(const Expression& e);
580 /** Checks if @p e is -1.0. */
581 bool is_neg_one(const Expression& e);
582 /** Checks if @p e is 2.0. */
583 bool is_two(const Expression& e);
584 /** Checks if @p e is NaN. */
585 bool is_nan(const Expression& e);
586 /** Checks if @p e is a variable expression. */
587 bool is_variable(const Expression& e);
588 /** Checks if @p e is an addition expression. */
589 bool is_addition(const Expression& e);
590 /** Checks if @p e is a multiplication expression. */
591 bool is_multiplication(const Expression& e);
592 /** Checks if @p e is a division expression. */
593 bool is_division(const Expression& e);
594 /** Checks if @p e is a log expression. */
595 bool is_log(const Expression& e);
596 /** Checks if @p e is an abs expression. */
597 bool is_abs(const Expression& e);
598 /** Checks if @p e is an exp expression. */
599 bool is_exp(const Expression& e);
600 /** Checks if @p e is a square-root expression. */
601 bool is_sqrt(const Expression& e);
602 /** Checks if @p e is a power-function expression. */
603 bool is_pow(const Expression& e);
604 /** Checks if @p e is a sine expression. */
605 bool is_sin(const Expression& e);
606 /** Checks if @p e is a cosine expression. */
607 bool is_cos(const Expression& e);
608 /** Checks if @p e is a tangent expression. */
609 bool is_tan(const Expression& e);
610 /** Checks if @p e is an arcsine expression. */
611 bool is_asin(const Expression& e);
612 /** Checks if @p e is an arccosine expression. */
613 bool is_acos(const Expression& e);
614 /** Checks if @p e is an arctangent expression. */
615 bool is_atan(const Expression& e);
616 /** Checks if @p e is an arctangent2 expression. */
617 bool is_atan2(const Expression& e);
618 /** Checks if @p e is a hyperbolic-sine expression. */
619 bool is_sinh(const Expression& e);
620 /** Checks if @p e is a hyperbolic-cosine expression. */
621 bool is_cosh(const Expression& e);
622 /** Checks if @p e is a hyperbolic-tangent expression. */
623 bool is_tanh(const Expression& e);
624 /** Checks if @p e is a min expression. */
625 bool is_min(const Expression& e);
626 /** Checks if @p e is a max expression. */
627 bool is_max(const Expression& e);
628 /** Checks if @p e is an if-then-else expression. */
629 bool is_if_then_else(const Expression& e);
630 /** Checks if @p e is an uninterpreted-function expression. */
631 bool is_uninterpreted_function(const Expression& e);
632 
633 /** Returns the constant value of the floating-point constant expression @p e.
634  * @pre @p e is either a floating-point constant or real constant expression.
635  */
636 double get_constant_value(const Expression& e);
637 /** Returns the lower-bound of the floating-point constant expression @p e.
638  * @pre @p e is a real constant expression.
639  */
640 double get_lb_of_real_constant(const Expression& e);
641 /** Returns the upper-bound of the floating-point constant expression @p e.
642  * @pre @p e is a real constant expression.
643  */
644 double get_ub_of_real_constant(const Expression& e);
645 /** Returns the embedded variable in the variable expression @p e.
646  * @pre @p e is a variable expression.
647  */
648 const Variable& get_variable(const Expression& e);
649 /** Returns the argument in the unary expression @p e.
650  * @pre @p e is a unary expression.
651  */
652 const Expression& get_argument(const Expression& e);
653 /** Returns the first argument of the binary expression @p e.
654  * @pre @p e is a binary expression.
655  */
656 const Expression& get_first_argument(const Expression& e);
657 /** Returns the second argument of the binary expression @p e.
658  * @pre @p e is a binary expression.
659  */
660 const Expression& get_second_argument(const Expression& e);
661 /** Returns the constant part of the addition expression @p e. For instance,
662  * given 7 + 2 * x + 3 * y, it returns 7.
663  * @pre @p e is an addition expression.
664  */
665 double get_constant_in_addition(const Expression& e);
666 /** Returns the map from an expression to its coefficient in the addition
667  * expression @p e. For instance, given 7 + 2 * x + 3 * y, the return value
668  * maps 'x' to 2 and 'y' to 3.
669  * @pre @p e is an addition expression.
670  */
671 const std::map<Expression, double>& get_expr_to_coeff_map_in_addition(
672  const Expression& e);
673 /** Returns the constant part of the multiplication expression @p e. For
674  * instance, given 7 * x^2 * y^3, it returns 7.
675  * @pre @p e is a multiplication expression.
676  */
677 double get_constant_in_multiplication(const Expression& e);
678 /** Returns the map from a base expression to its exponent expression in the
679  * multiplication expression @p e. For instance, given 7 * x^2 * y^3 * z^x, the
680  * return value maps 'x' to 2, 'y' to 3, and 'z' to 'x'.
681  * @pre @p e is a multiplication expression.
682  */
683 const std::map<Expression, Expression>&
684 get_base_to_exponent_map_in_multiplication(const Expression& e);
685 
686 /** Returns the conditional formula in the if-then-else expression @p e.
687  * @pre @p e is an if-then-else expression.
688  */
689 const Formula& get_conditional_formula(const Expression& e);
690 
691 /** Returns the 'then' expression in the if-then-else expression @p e.
692  * @pre @p e is an if-then-else expression.
693  */
694 const Expression& get_then_expression(const Expression& e);
695 
696 /** Returns the 'else' expression in the if-then-else expression @p e.
697  * @pre @p e is an if-then-else expression.
698  */
699 const Expression& get_else_expression(const Expression& e);
700 
701 /** Returns the name of an uninterpreted-function expression @p e.
702  * @pre @p e is an uninterpreted-function expression.
703  */
704 const std::string& get_uninterpreted_function_name(const Expression& e);
705 
706 Expression operator+(const Variable& var);
707 Expression operator-(const Variable& var);
708 
709 } // namespace symbolic
710 
711 /** Computes the hash value of a symbolic expression. */
712 template <>
713 struct hash_value<symbolic::Expression> {
714  size_t operator()(const symbolic::Expression& e) const {
715  return e.get_hash();
716  }
717 };
718 
719 } // namespace drake
720 } // namespace dreal
721 
722 namespace std {
723 /* Provides std::less<dreal::drake::symbolic::Expression>. */
724 template <>
725 struct less<dreal::drake::symbolic::Expression> {
726  bool operator()(const dreal::drake::symbolic::Expression& lhs,
727  const dreal::drake::symbolic::Expression& rhs) const {
728  return lhs.Less(rhs);
729  }
730 };
731 
732 /* Provides std::equal_to<dreal::drake::symbolic::Expression>. */
733 template <>
734 struct equal_to<dreal::drake::symbolic::Expression> {
735  bool operator()(const dreal::drake::symbolic::Expression& lhs,
736  const dreal::drake::symbolic::Expression& rhs) const {
737  return lhs.EqualTo(rhs);
738  }
739 };
740 
741 template <>
742 struct hash<dreal::drake::symbolic::Expression> {
743  size_t operator()(const dreal::drake::symbolic::Expression& e) const {
744  return e.get_hash();
745  }
746 };
747 
748 /* Provides std::numeric_limits<dreal::drake::symbolic::Expression>. */
749 template <>
750 struct numeric_limits<dreal::drake::symbolic::Expression>
751  : public numeric_limits<double> {};
752 
753 } // namespace std
Symbolic expression representing tangent function.
Definition: symbolic_expression_cell.h:663
Symbolic expression representing absolute value function.
Definition: symbolic_expression_cell.h:567
Symbolic expression representing if-then-else expression.
Definition: symbolic_expression_cell.h:812
Symbolic expression representing cosine function.
Definition: symbolic_expression_cell.h:649
Symbolic expression representing an uninterpreted function.
Definition: symbolic_expression_cell.h:845
Symbolic expression representing a multiplication of powers.
Definition: symbolic_expression_cell.h:426
Symbolic expression representing arctangent function.
Definition: symbolic_expression_cell.h:713
spdlog::logger * log()
Provide a global logger.
Definition: logging.cc:33
Represents the base class for unary expressions.
Definition: symbolic_expression_cell.h:130
Symbolic expression representing a real constant represented by a double interval [lb...
Definition: symbolic_expression_cell.h:256
Sum type of symbolic::Expression and symbolic::Formula.
Definition: api.cc:9
STL namespace.
Symbolic expression representing a floating-point constant (double).
Definition: symbolic_expression_cell.h:233
Represents an abstract class which is the base of concrete symbolic-expression classes.
Definition: symbolic_expression_cell.h:26
Symbolic expression representing hyperbolic sine function.
Definition: symbolic_expression_cell.h:742
Represents a symbolic variable.
Definition: symbolic_variable.h:16
Computes the hash value of v using std::hash.
Definition: hash.h:35
Symbolic expression representing hyperbolic cosine function.
Definition: symbolic_expression_cell.h:756
Symbolic expression representing hyperbolic tangent function.
Definition: symbolic_expression_cell.h:770
Symbolic expression representing a variable.
Definition: symbolic_expression_cell.h:212
Symbolic expression representing square-root.
Definition: symbolic_expression_cell.h:598
Symbolic expression representing exponentiation using the base of natural logarithms.
Definition: symbolic_expression_cell.h:584
Factory class to help build ExpressionAdd expressions.
Definition: symbolic_expression_cell.h:348
bool Less(const Expression &e) const
Provides lexicographical ordering between expressions.
Definition: symbolic_expression.cc:196
Represents a symbolic environment (mapping from a variable to a value).
Definition: symbolic_environment.h:52
Symbolic expression representing logarithms.
Definition: symbolic_expression_cell.h:549
Symbolic expression representing division.
Definition: symbolic_expression_cell.h:535
Symbolic expression representing sine function.
Definition: symbolic_expression_cell.h:635
Factory class to help build ExpressionMul expressions.
Definition: symbolic_expression_cell.h:470
Symbolic expression representing an addition which is a sum of products.
Definition: symbolic_expression_cell.h:304
Symbolic expression representing min function.
Definition: symbolic_expression_cell.h:784
Symbolic expression representing arcsine function.
Definition: symbolic_expression_cell.h:677
Symbolic expression representing arccosine function.
Definition: symbolic_expression_cell.h:695
Symbolic expression representing max function.
Definition: symbolic_expression_cell.h:798
size_t get_hash() const
Returns hash value.
Definition: symbolic_expression.cc:144
Represents the base class for binary expressions.
Definition: symbolic_expression_cell.h:169
Symbolic expression representing atan2 function (arctangent function with two arguments).
Definition: symbolic_expression_cell.h:728
Represents a set of variables.
Definition: symbolic_variables.h:25
Represents a symbolic form of an expression.
Definition: symbolic_expression.h:164
bool EqualTo(const Expression &e) const
Checks structural equality.
Definition: symbolic_expression.cc:179
Represents a symbolic form of a first-order logic formula.
Definition: symbolic_formula.h:101
Symbolic expression representing power function.
Definition: symbolic_expression_cell.h:616