Drake
symbolic_expression.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifndef DRAKE_COMMON_SYMBOLIC_HEADER
4 // TODO(soonho-tri): Change to #error, when #6613 merged.
5 #warning Do not directly include this file. Include "drake/common/symbolic.h".
6 #endif
7 
8 #include <algorithm> // for cpplint only
9 #include <cstddef>
10 #include <functional>
11 #include <limits>
12 #include <map>
13 #include <memory>
14 #include <ostream>
15 #include <string>
16 #include <type_traits>
17 #include <unordered_map>
18 #include <utility>
19 #include <vector>
20 
21 #include <Eigen/Core>
22 
23 #include "drake/common/cond.h"
29 #include "drake/common/hash.h"
32 #include "drake/common/symbolic.h"
33 
34 namespace drake {
35 
36 namespace symbolic {
37 
38 /** Kinds of symbolic expressions. */
39 enum class ExpressionKind {
40  Constant, ///< constant (double)
41  Var, ///< variable
42  Add, ///< addition (+)
43  Mul, ///< multiplication (*)
44  Div, ///< division (/)
45  Log, ///< logarithms
46  Abs, ///< absolute value function
47  Exp, ///< exponentiation
48  Sqrt, ///< square root
49  Pow, ///< power function
50  Sin, ///< sine
51  Cos, ///< cosine
52  Tan, ///< tangent
53  Asin, ///< arcsine
54  Acos, ///< arccosine
55  Atan, ///< arctangent
56  Atan2, ///< arctangent2 (atan2(y,x) = atan(y/x))
57  Sinh, ///< hyperbolic sine
58  Cosh, ///< hyperbolic cosine
59  Tanh, ///< hyperbolic tangent
60  Min, ///< min
61  Max, ///< max
62  Ceil, ///< ceil
63  Floor, ///< floor
64  IfThenElse, ///< if then else
65  NaN, ///< NaN
66  UninterpretedFunction, ///< Uninterpreted function
67  // TODO(soonho): add Integral
68 };
69 
70 /** Total ordering between ExpressionKinds. */
72 
73 class ExpressionCell; // In symbolic_expression_cell.h
74 class ExpressionConstant; // In symbolic_expression_cell.h
75 class ExpressionVar; // In symbolic_expression_cell.h
76 class UnaryExpressionCell; // In symbolic_expression_cell.h
77 class BinaryExpressionCell; // In symbolic_expression_cell.h
78 class ExpressionAdd; // In symbolic_expression_cell.h
79 class ExpressionMul; // In symbolic_expression_cell.h
80 class ExpressionDiv; // In symbolic_expression_cell.h
81 class ExpressionLog; // In symbolic_expression_cell.h
82 class ExpressionAbs; // In symbolic_expression_cell.h
83 class ExpressionExp; // In symbolic_expression_cell.h
84 class ExpressionSqrt; // In symbolic_expression_cell.h
85 class ExpressionPow; // In symbolic_expression_cell.h
86 class ExpressionSin; // In symbolic_expression_cell.h
87 class ExpressionCos; // In symbolic_expression_cell.h
88 class ExpressionTan; // In symbolic_expression_cell.h
89 class ExpressionAsin; // In symbolic_expression_cell.h
90 class ExpressionAcos; // In symbolic_expression_cell.h
91 class ExpressionAtan; // In symbolic_expression_cell.h
92 class ExpressionAtan2; // In symbolic_expression_cell.h
93 class ExpressionSinh; // In symbolic_expression_cell.h
94 class ExpressionCosh; // In symbolic_expression_cell.h
95 class ExpressionTanh; // In symbolic_expression_cell.h
96 class ExpressionMin; // In symbolic_expression_cell.h
97 class ExpressionMax; // In symbolic_expression_cell.h
98 class ExpressionCeiling; // In symbolic_expression_cell.h
99 class ExpressionFloor; // In symbolic_expression_cell.h
100 class ExpressionIfThenElse; // In symbolic_expression_cell.h
101 class ExpressionUninterpretedFunction; // In symbolic_expression_cell.h
102 class Formula; // In symbolic_formula.h
103 class Expression;
104 
105 // Substitution is a map from a Variable to a symbolic expression. It is used in
106 // Expression::Substitute and Formula::Substitute methods as an argument.
107 using Substitution = std::unordered_map<Variable, Expression>;
108 
109 /** Represents a symbolic form of an expression.
110 
111 Its syntax tree is as follows:
112 
113 @verbatim
114  E := Var | Constant | E + ... + E | E * ... * E | E / E | log(E)
115  | abs(E) | exp(E) | sqrt(E) | pow(E, E) | sin(E) | cos(E) | tan(E)
116  | asin(E) | acos(E) | atan(E) | atan2(E, E) | sinh(E) | cosh(E) | tanh(E)
117  | min(E, E) | max(E, E) | ceil(E) | floor(E) | if_then_else(F, E, E)
118  | NaN | uninterpreted_function(name, {v_1, ..., v_n})
119 @endverbatim
120 
121 In the implementation, Expression is a simple wrapper including a shared pointer
122 to ExpressionCell class which is a super-class of different kinds of symbolic
123 expressions (i.e. ExpressionAdd, ExpressionMul, ExpressionLog,
124 ExpressionSin). Note that it includes a shared pointer, not a unique pointer, to
125 allow sharing sub-expressions.
126 
127 @note The sharing of sub-expressions is not yet implemented.
128 
129 @note -E is represented as -1 * E internally.
130 
131 @note A subtraction E1 - E2 is represented as E1 + (-1 * E2) internally.
132 
133 The following simple simplifications are implemented:
134 @verbatim
135  E + 0 -> E
136  0 + E -> E
137  E - 0 -> E
138  E - E -> 0
139  E * 1 -> E
140  1 * E -> E
141  E * 0 -> 0
142  0 * E -> 0
143  E / 1 -> E
144  E / E -> 1
145  pow(E, 0) -> 1
146  pow(E, 1) -> E
147  E * E -> E^2 (= pow(E, 2))
148  sqrt(E * E) -> |E| (= abs(E))
149  sqrt(E) * sqrt(E) -> E
150 @endverbatim
151 
152 Constant folding is implemented:
153 @verbatim
154  E(c1) + E(c2) -> E(c1 + c2) // c1, c2 are constants
155  E(c1) - E(c2) -> E(c1 - c2)
156  E(c1) * E(c2) -> E(c1 * c2)
157  E(c1) / E(c2) -> E(c1 / c2)
158  f(E(c)) -> E(f(c)) // c is a constant, f is a math function
159 @endverbatim
160 
161 For the math functions which are only defined over a restricted domain (namely,
162 log, sqrt, pow, asin, acos), we check the domain of argument(s), and throw
163 std::domain_error exception if a function is not well-defined for a given
164 argument(s).
165 
166 Relational operators over expressions (==, !=, <, >, <=, >=) return
167 symbolic::Formula instead of bool. Those operations are declared in
168 symbolic_formula.h file. To check structural equality between two expressions a
169 separate function, Expression::EqualTo, is provided.
170 
171 symbolic::Expression can be used as a scalar type of Eigen types.
172 */
173 class Expression {
174  public:
176 
177  /** Default constructor. It constructs Zero(). */
178  Expression() { *this = Zero(); }
179 
180  /** Constructs a constant. */
181  // NOLINTNEXTLINE(runtime/explicit): This conversion is desirable.
182  Expression(double d);
183  /** Constructs an expression from @p var.
184  * @pre @p var is neither a dummy nor a BOOLEAN variable.
185  */
186  // NOLINTNEXTLINE(runtime/explicit): This conversion is desirable.
187  Expression(const Variable& var);
188  /** Returns expression kind. */
189  ExpressionKind get_kind() const;
190  /** Collects variables in expression. */
191  Variables GetVariables() const;
192 
193  /** Checks structural equality.
194  *
195  * Two expressions e1 and e2 are structurally equal when they have the same
196  * internal AST(abstract-syntax tree) representation. Please note that we can
197  * have two computationally (or extensionally) equivalent expressions which
198  * are not structurally equal. For example, consider:
199  *
200  * e1 = 2 * (x + y)
201  * e2 = 2x + 2y
202  *
203  * Obviously, we know that e1 and e2 are evaluated to the same value for all
204  * assignments to x and y. However, e1 and e2 are not structurally equal by
205  * the definition. Note that e1 is a multiplication expression
206  * (is_multiplication(e1) is true) while e2 is an addition expression
207  * (is_addition(e2) is true).
208  *
209  * One main reason we use structural equality in EqualTo is due to
210  * Richardson's Theorem. It states that checking ∀x. E(x) = F(x) is
211  * undecidable when we allow sin, asin, log, exp in E and F. Read
212  * https://en.wikipedia.org/wiki/Richardson%27s_theorem for details.
213  *
214  * Note that for polynomial cases, you can use Expand method and check if two
215  * polynomial expressions p1 and p2 are computationally equal. To do so, you
216  * check the following:
217  *
218  * (p1.Expand() - p2.Expand()).EqualTo(0).
219  */
220  bool EqualTo(const Expression& e) const;
221 
222  /** Provides lexicographical ordering between expressions.
223  This function is used as a compare function in map<Expression> and
224  set<Expression> via std::less<drake::symbolic::Expression>. */
225  bool Less(const Expression& e) const;
226 
227  /** Checks if this symbolic expression is convertible to Polynomial. */
228  bool is_polynomial() const;
229 
230  /** Returns a Polynomial representing this expression.
231  * Note that the ID of a variable is preserved in this translation.
232  * \pre{is_polynomial() is true.}
233  */
234  Polynomiald ToPolynomial() const;
235 
236  /** Evaluates under a given environment (by default, an empty environment).
237  * @throws std::runtime_error if NaN is detected during evaluation.
238  */
239  double Evaluate(const Environment& env = Environment{}) const;
240 
241  /** Partially evaluates this expression using an environment @p
242  * env. Internally, this method promotes @p env into a substitution
243  * (Variable → Expression) and call Evaluate::Substitute with it.
244  *
245  * @throws std::runtime_error if NaN is detected during evaluation.
246  */
247  Expression EvaluatePartial(const Environment& env) const;
248 
249  /** Expands out products and positive integer powers in expression. For
250  * example, `(x + 1) * (x - 1)` is expanded to `x^2 - 1` and `(x + y)^2` is
251  * expanded to `x^2 + 2xy + y^2`. Note that Expand applies recursively to
252  * sub-expressions. For instance, `sin(2 * (x + y))` is expanded to `sin(2x +
253  * 2y)`. It also simplifies "division by constant" cases. See
254  * "drake/common/test/symbolic_expansion_test.cc" to find the examples.
255  *
256  * @throws std::runtime_error if NaN is detected during expansion.
257  */
258  Expression Expand() const;
259 
260  /** Returns a copy of this expression replacing all occurrences of @p var
261  * with @p e.
262  * @throws std::runtime_error if NaN is detected during substitution.
263  */
264  Expression Substitute(const Variable& var, const Expression& e) const;
265 
266  /** Returns a copy of this expression replacing all occurrences of the
267  * variables in @p s with corresponding expressions in @p s. Note that the
268  * substitutions occur simultaneously. For example, (x / y).Substitute({{x,
269  * y}, {y, x}}) gets (y / x).
270  * @throws std::runtime_error if NaN is detected during substitution.
271  */
272  Expression Substitute(const Substitution& s) const;
273 
274  /** Differentiates this symbolic expression with respect to the variable @p
275  * var.
276  * @throws std::runtime_error if it is not differentiable.
277  */
278  Expression Differentiate(const Variable& x) const;
279 
280  /** Let `f` be this Expression, computes a row vector of derivatives,
281  * `[∂f/∂vars(0), ... , ∂f/∂vars(n-1)]` with respect to the variables
282  * @p vars.
283  */
285  const Eigen::Ref<const VectorX<Variable>>& vars) const;
286 
287  /** Returns string representation of Expression. */
288  std::string to_string() const;
289 
290  /** Returns zero. */
291  static Expression Zero();
292  /** Returns one. */
293  static Expression One();
294  /** Returns Pi, the ratio of a circle’s circumference to its diameter. */
295  static Expression Pi();
296  /** Return e, the base of natural logarithms. */
297  static Expression E();
298  /** Returns NaN (Not-a-Number). */
299  static Expression NaN();
300 
301  /** Implements the @ref hash_append concept. */
302  template <class HashAlgorithm>
303  friend void hash_append(HashAlgorithm& hasher,
304  const Expression& item) noexcept {
305  DelegatingHasher delegating_hasher(
306  [&hasher](const void* data, const size_t length) {
307  return hasher(data, length);
308  });
309  item.HashAppend(&delegating_hasher);
310  }
311 
312  friend Expression operator+(Expression lhs, const Expression& rhs);
313  // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
314  friend Expression& operator+=(Expression& lhs, const Expression& rhs);
315 
316  /** Provides prefix increment operator (i.e. ++x). */
317  Expression& operator++();
318  /** Provides postfix increment operator (i.e. x++). */
319  Expression operator++(int);
320  /** Provides unary plus operator. */
321  friend Expression operator+(const Expression& e);
322 
323  friend Expression operator-(Expression lhs, const Expression& rhs);
324  // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
325  friend Expression& operator-=(Expression& lhs, const Expression& rhs);
326 
327  /** Provides unary minus operator. */
328  friend Expression operator-(const Expression& e);
329  /** Provides prefix decrement operator (i.e. --x). */
330  Expression& operator--();
331  /** Provides postfix decrement operator (i.e. x--). */
332  Expression operator--(int);
333 
334  friend Expression operator*(Expression lhs, const Expression& rhs);
335  // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
336  friend Expression& operator*=(Expression& lhs, const Expression& rhs);
337 
338  friend Expression operator/(Expression lhs, const Expression& rhs);
339  // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
340  friend Expression& operator/=(Expression& lhs, const Expression& rhs);
341 
342  friend Expression log(const Expression& e);
343  friend Expression abs(const Expression& e);
344  friend Expression exp(const Expression& e);
345  friend Expression sqrt(const Expression& e);
346  friend Expression pow(const Expression& e1, const Expression& e2);
347  friend Expression sin(const Expression& e);
348  friend Expression cos(const Expression& e);
349  friend Expression tan(const Expression& e);
350  friend Expression asin(const Expression& e);
351  friend Expression acos(const Expression& e);
352  friend Expression atan(const Expression& e);
353  friend Expression atan2(const Expression& e1, const Expression& e2);
354  friend Expression sinh(const Expression& e);
355  friend Expression cosh(const Expression& e);
356  friend Expression tanh(const Expression& e);
357  friend Expression min(const Expression& e1, const Expression& e2);
358  friend Expression max(const Expression& e1, const Expression& e2);
359  friend Expression ceil(const Expression& e);
360  friend Expression floor(const Expression& e);
361 
362  /** Constructs if-then-else expression.
363 
364  @verbatim
365  if_then_else(cond, expr_then, expr_else)
366  @endverbatim
367 
368  The value returned by the above if-then-else expression is @p expr_then if
369  @p cond is evaluated to true. Otherwise, it returns @p expr_else.
370 
371  The semantics is similar to the C++'s conditional expression constructed by
372  its ternary operator, @c ?:. However, there is a key difference between the
373  C++'s conditional expression and our @c if_then_else expression in a way the
374  arguments are evaluated during the construction.
375 
376  - In case of the C++'s conditional expression, <tt> cond ? expr_then :
377  expr_else</tt>, the then expression @c expr_then (respectively, the else
378  expression @c expr_else) is \b only evaluated when the conditional
379  expression @c cond is evaluated to \b true (respectively, when @c cond is
380  evaluated to \b false).
381 
382  - In case of the symbolic expression, <tt>if_then_else(cond, expr_then,
383  expr_else)</tt>, however, \b both arguments @c expr_then and @c expr_else
384  are evaluated first and then passed to the @c if_then_else function.
385 
386  @note This function returns an \b expression and it is different from the
387  C++'s if-then-else \b statement.
388 
389  @note While it is still possible to define <tt> min, max, abs</tt> math
390  functions using @c if_then_else expression, it is highly \b recommended to
391  use the provided native definitions for them because it allows solvers to
392  detect specific math functions and to have a room for special
393  optimizations.
394 
395  @note More information about the C++'s conditional expression and ternary
396  operator is available at
397  http://en.cppreference.com/w/cpp/language/operator_other#Conditional_operator.
398  */
399  friend Expression if_then_else(const Formula& f_cond,
400  const Expression& e_then,
401  const Expression& e_else);
402  friend Expression uninterpreted_function(std::string name,
403  std::vector<Expression> arguments);
404 
405  friend std::ostream& operator<<(std::ostream& os, const Expression& e);
406  friend void swap(Expression& a, Expression& b) { std::swap(a.ptr_, b.ptr_); }
407 
408  friend bool is_constant(const Expression& e);
409  friend bool is_variable(const Expression& e);
410  friend bool is_addition(const Expression& e);
411  friend bool is_multiplication(const Expression& e);
412  friend bool is_division(const Expression& e);
413  friend bool is_log(const Expression& e);
414  friend bool is_abs(const Expression& e);
415  friend bool is_exp(const Expression& e);
416  friend bool is_sqrt(const Expression& e);
417  friend bool is_pow(const Expression& e);
418  friend bool is_sin(const Expression& e);
419  friend bool is_cos(const Expression& e);
420  friend bool is_tan(const Expression& e);
421  friend bool is_asin(const Expression& e);
422  friend bool is_acos(const Expression& e);
423  friend bool is_atan(const Expression& e);
424  friend bool is_atan2(const Expression& e);
425  friend bool is_sinh(const Expression& e);
426  friend bool is_cosh(const Expression& e);
427  friend bool is_tanh(const Expression& e);
428  friend bool is_min(const Expression& e);
429  friend bool is_max(const Expression& e);
430  friend bool is_ceil(const Expression& e);
431  friend bool is_floor(const Expression& e);
432  friend bool is_if_then_else(const Expression& e);
433  friend bool is_uninterpreted_function(const Expression& e);
434 
435  // Note that the following cast functions are only for low-level operations
436  // and not exposed to the user of drake/common/symbolic_expression.h
437  // header. These functions are declared in
438  // drake/common/symbolic_expression_cell.h header.
439  friend std::shared_ptr<ExpressionConstant> to_constant(const Expression& e);
440  friend std::shared_ptr<ExpressionVar> to_variable(const Expression& e);
441  friend std::shared_ptr<UnaryExpressionCell> to_unary(const Expression& e);
442  friend std::shared_ptr<BinaryExpressionCell> to_binary(const Expression& e);
443  friend std::shared_ptr<ExpressionAdd> to_addition(const Expression& e);
444  friend std::shared_ptr<ExpressionMul> to_multiplication(const Expression& e);
445  friend std::shared_ptr<ExpressionDiv> to_division(const Expression& e);
446  friend std::shared_ptr<ExpressionLog> to_log(const Expression& e);
447  friend std::shared_ptr<ExpressionAbs> to_abs(const Expression& e);
448  friend std::shared_ptr<ExpressionExp> to_exp(const Expression& e);
449  friend std::shared_ptr<ExpressionSqrt> to_sqrt(const Expression& e);
450  friend std::shared_ptr<ExpressionPow> to_pow(const Expression& e);
451  friend std::shared_ptr<ExpressionSin> to_sin(const Expression& e);
452  friend std::shared_ptr<ExpressionCos> to_cos(const Expression& e);
453  friend std::shared_ptr<ExpressionTan> to_tan(const Expression& e);
454  friend std::shared_ptr<ExpressionAsin> to_asin(const Expression& e);
455  friend std::shared_ptr<ExpressionAcos> to_acos(const Expression& e);
456  friend std::shared_ptr<ExpressionAtan> to_atan(const Expression& e);
457  friend std::shared_ptr<ExpressionAtan2> to_atan2(const Expression& e);
458  friend std::shared_ptr<ExpressionSinh> to_sinh(const Expression& e);
459  friend std::shared_ptr<ExpressionCosh> to_cosh(const Expression& e);
460  friend std::shared_ptr<ExpressionTanh> to_tanh(const Expression& e);
461  friend std::shared_ptr<ExpressionMin> to_min(const Expression& e);
462  friend std::shared_ptr<ExpressionMax> to_max(const Expression& e);
463  friend std::shared_ptr<ExpressionCeiling> to_ceil(const Expression& e);
464  friend std::shared_ptr<ExpressionFloor> to_floor(const Expression& e);
465  friend std::shared_ptr<ExpressionIfThenElse> to_if_then_else(
466  const Expression& e);
467  friend std::shared_ptr<ExpressionUninterpretedFunction>
469 
470  friend class ExpressionAddFactory;
471  friend class ExpressionMulFactory;
472 
473  private:
474  explicit Expression(std::shared_ptr<ExpressionCell> ptr);
475  void HashAppend(DelegatingHasher* hasher) const;
476 
477  std::shared_ptr<ExpressionCell> ptr_;
478 };
479 
480 Expression operator+(Expression lhs, const Expression& rhs);
481 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
482 Expression& operator+=(Expression& lhs, const Expression& rhs);
484 Expression operator-(Expression lhs, const Expression& rhs);
485 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
486 Expression& operator-=(Expression& lhs, const Expression& rhs);
488 Expression operator*(Expression lhs, const Expression& rhs);
489 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
490 Expression& operator*=(Expression& lhs, const Expression& rhs);
491 Expression operator/(Expression lhs, const Expression& rhs);
492 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
493 Expression& operator/=(Expression& lhs, const Expression& rhs);
494 
495 Expression log(const Expression& e);
496 Expression abs(const Expression& e);
497 Expression exp(const Expression& e);
498 Expression sqrt(const Expression& e);
499 Expression pow(const Expression& e1, const Expression& e2);
500 Expression sin(const Expression& e);
501 Expression cos(const Expression& e);
502 Expression tan(const Expression& e);
503 Expression asin(const Expression& e);
504 Expression acos(const Expression& e);
505 Expression atan(const Expression& e);
506 Expression atan2(const Expression& e1, const Expression& e2);
507 Expression sinh(const Expression& e);
508 Expression cosh(const Expression& e);
509 Expression tanh(const Expression& e);
510 Expression min(const Expression& e1, const Expression& e2);
511 Expression max(const Expression& e1, const Expression& e2);
512 Expression ceil(const Expression& e);
513 Expression floor(const Expression& e);
514 Expression if_then_else(const Formula& f_cond, const Expression& e_then,
515  const Expression& e_else);
516 
517 /** Constructs an uninterpreted-function expression with @p name and @p
518  * arguments. An uninterpreted function is an opaque function that has no other
519  * property than its name and a list of its arguments. This is useful to
520  * applications where it is good enough to provide abstract information of a
521  * function without exposing full details. Declaring sparsity of a system is a
522  * typical example.
523  */
525  std::vector<Expression> arguments);
526 void swap(Expression& a, Expression& b);
527 
528 std::ostream& operator<<(std::ostream& os, const Expression& e);
529 
530 /** Checks if @p e is a constant expression. */
531 bool is_constant(const Expression& e);
532 /** Checks if @p e is a constant expression representing @p v. */
533 bool is_constant(const Expression& e, double v);
534 /** Checks if @p e is 0.0. */
535 bool is_zero(const Expression& e);
536 /** Checks if @p e is 1.0. */
537 bool is_one(const Expression& e);
538 /** Checks if @p e is -1.0. */
539 bool is_neg_one(const Expression& e);
540 /** Checks if @p e is 2.0. */
541 bool is_two(const Expression& e);
542 /** Checks if @p e is NaN. */
543 bool is_nan(const Expression& e);
544 /** Checks if @p e is a variable expression. */
545 bool is_variable(const Expression& e);
546 /** Checks if @p e is an addition expression. */
547 bool is_addition(const Expression& e);
548 /** Checks if @p e is a multiplication expression. */
549 bool is_multiplication(const Expression& e);
550 /** Checks if @p e is a division expression. */
551 bool is_division(const Expression& e);
552 /** Checks if @p e is a log expression. */
553 bool is_log(const Expression& e);
554 /** Checks if @p e is an abs expression. */
555 bool is_abs(const Expression& e);
556 /** Checks if @p e is an exp expression. */
557 bool is_exp(const Expression& e);
558 /** Checks if @p e is a square-root expression. */
559 bool is_sqrt(const Expression& e);
560 /** Checks if @p e is a power-function expression. */
561 bool is_pow(const Expression& e);
562 /** Checks if @p e is a sine expression. */
563 bool is_sin(const Expression& e);
564 /** Checks if @p e is a cosine expression. */
565 bool is_cos(const Expression& e);
566 /** Checks if @p e is a tangent expression. */
567 bool is_tan(const Expression& e);
568 /** Checks if @p e is an arcsine expression. */
569 bool is_asin(const Expression& e);
570 /** Checks if @p e is an arccosine expression. */
571 bool is_acos(const Expression& e);
572 /** Checks if @p e is an arctangent expression. */
573 bool is_atan(const Expression& e);
574 /** Checks if @p e is an arctangent2 expression. */
575 bool is_atan2(const Expression& e);
576 /** Checks if @p e is a hyperbolic-sine expression. */
577 bool is_sinh(const Expression& e);
578 /** Checks if @p e is a hyperbolic-cosine expression. */
579 bool is_cosh(const Expression& e);
580 /** Checks if @p e is a hyperbolic-tangent expression. */
581 bool is_tanh(const Expression& e);
582 /** Checks if @p e is a min expression. */
583 bool is_min(const Expression& e);
584 /** Checks if @p e is a max expression. */
585 bool is_max(const Expression& e);
586 /** Checks if @p e is a ceil expression. */
587 bool is_ceil(const Expression& e);
588 /** Checks if @p e is a floor expression. */
589 bool is_floor(const Expression& e);
590 /** Checks if @p e is an if-then-else expression. */
591 bool is_if_then_else(const Expression& e);
592 /** Checks if @p e is an uninterpreted-function expression. */
594 
595 /** Returns the constant value of the constant expression @p e.
596  * \pre{@p e is a constant expression.}
597  */
598 double get_constant_value(const Expression& e);
599 /** Returns the embedded variable in the variable expression @p e.
600  * \pre{@p e is a variable expression.}
601  */
602 const Variable& get_variable(const Expression& e);
603 /** Returns the argument in the unary expression @p e.
604  * \pre{@p e is a unary expression.}
605  */
606 const Expression& get_argument(const Expression& e);
607 /** Returns the first argument of the binary expression @p e.
608  * \pre{@p e is a binary expression.}
609  */
610 const Expression& get_first_argument(const Expression& e);
611 /** Returns the second argument of the binary expression @p e.
612  * \pre{@p e is a binary expression.}
613  */
614 const Expression& get_second_argument(const Expression& e);
615 /** Returns the constant part of the addition expression @p e. For instance,
616  * given 7 + 2 * x + 3 * y, it returns 7.
617  * \pre{@p e is an addition expression.}
618  */
619 double get_constant_in_addition(const Expression& e);
620 /** Returns the map from an expression to its coefficient in the addition
621  * expression @p e. For instance, given 7 + 2 * x + 3 * y, the return value
622  * maps 'x' to 2 and 'y' to 3.
623  * \pre{@p e is an addition expression.}
624  */
625 const std::map<Expression, double>& get_expr_to_coeff_map_in_addition(
626  const Expression& e);
627 /** Returns the constant part of the multiplication expression @p e. For
628  * instance, given 7 * x^2 * y^3, it returns 7.
629  * \pre{@p e is a multiplication expression.}
630  */
632 /** Returns the map from a base expression to its exponent expression in the
633  * multiplication expression @p e. For instance, given 7 * x^2 * y^3 * z^x, the
634  * return value maps 'x' to 2, 'y' to 3, and 'z' to 'x'.
635  * \pre{@p e is a multiplication expression.}
636  */
637 const std::map<Expression, Expression>&
639 
640 /** Returns the name of an uninterpreted-function expression @p e.
641  * \pre @p e is an uninterpreted-function expression.
642  */
643 const std::string& get_uninterpreted_function_name(const Expression& e);
644 
645 /** Returns the arguments of an uninterpreted-function expression @p e.
646  * \pre @p e is an uninterpreted-function expression.
647  */
648 const std::vector<Expression>& get_uninterpreted_function_arguments(
649  const Expression& e);
650 
651 /** Returns the conditional formula in the if-then-else expression @p e.
652  * @pre @p e is an if-then-else expression.
653  */
655 
656 /** Returns the 'then' expression in the if-then-else expression @p e.
657  * @pre @p e is an if-then-else expression.
658  */
659 const Expression& get_then_expression(const Expression& e);
660 
661 /** Returns the 'else' expression in the if-then-else expression @p e.
662  * @pre @p e is an if-then-else expression.
663  */
664 const Expression& get_else_expression(const Expression& e);
665 
666 // Matrix<Expression> * Matrix<double> => Matrix<Expression>
667 template <typename MatrixL, typename MatrixR>
668 typename std::enable_if<
669  std::is_base_of<Eigen::MatrixBase<MatrixL>, MatrixL>::value &&
670  std::is_base_of<Eigen::MatrixBase<MatrixR>, MatrixR>::value &&
673  Eigen::Matrix<Expression, MatrixL::RowsAtCompileTime,
674  MatrixR::ColsAtCompileTime>>::type
675 operator*(const MatrixL& lhs, const MatrixR& rhs) {
676  return lhs.template cast<Expression>() * rhs.template cast<Expression>();
677 }
678 
679 // Matrix<double> * Matrix<Expression> => Matrix<Expression>
680 template <typename MatrixL, typename MatrixR>
681 typename std::enable_if<
682  std::is_base_of<Eigen::MatrixBase<MatrixL>, MatrixL>::value &&
683  std::is_base_of<Eigen::MatrixBase<MatrixR>, MatrixR>::value &&
686  Eigen::Matrix<Expression, MatrixL::RowsAtCompileTime,
687  MatrixR::ColsAtCompileTime>>::type
688 operator*(const MatrixL& lhs, const MatrixR& rhs) {
689  return lhs.template cast<Expression>() * rhs.template cast<Expression>();
690 }
691 
692 Expression operator+(const Variable& var);
693 Expression operator-(const Variable& var);
694 
695 // Matrix<Expression> * Matrix<Variable> => Matrix<Expression>
696 template <typename MatrixL, typename MatrixR>
697 typename std::enable_if<
698  std::is_base_of<Eigen::MatrixBase<MatrixL>, MatrixL>::value &&
699  std::is_base_of<Eigen::MatrixBase<MatrixR>, MatrixR>::value &&
702  Eigen::Matrix<Expression, MatrixL::RowsAtCompileTime,
703  MatrixR::ColsAtCompileTime>>::type
704 operator*(const MatrixL& lhs, const MatrixR& rhs) {
705  return lhs * rhs.template cast<Expression>();
706 }
707 
708 // Matrix<Variable> * Matrix<Expression> => Matrix<Expression>
709 template <typename MatrixL, typename MatrixR>
710 typename std::enable_if<
711  std::is_base_of<Eigen::MatrixBase<MatrixL>, MatrixL>::value &&
712  std::is_base_of<Eigen::MatrixBase<MatrixR>, MatrixR>::value &&
714  std::is_same<typename MatrixR::Scalar, Expression>::value,
715  Eigen::Matrix<Expression, MatrixL::RowsAtCompileTime,
716  MatrixR::ColsAtCompileTime>>::type
717 operator*(const MatrixL& lhs, const MatrixR& rhs) {
718  return lhs.template cast<Expression>() * rhs;
719 }
720 
721 // Matrix<Variable> * Matrix<double> => Matrix<Expression>
722 template <typename MatrixL, typename MatrixR>
723 typename std::enable_if<
724  std::is_base_of<Eigen::MatrixBase<MatrixL>, MatrixL>::value &&
725  std::is_base_of<Eigen::MatrixBase<MatrixR>, MatrixR>::value &&
727  std::is_same<typename MatrixR::Scalar, double>::value,
728  Eigen::Matrix<Expression, MatrixL::RowsAtCompileTime,
729  MatrixR::ColsAtCompileTime>>::type
730 operator*(const MatrixL& lhs, const MatrixR& rhs) {
731  return lhs.template cast<Expression>() * rhs.template cast<Expression>();
732 }
733 
734 // Matrix<double> * Matrix<Variable> => Matrix<Expression>
735 template <typename MatrixL, typename MatrixR>
736 typename std::enable_if<
737  std::is_base_of<Eigen::MatrixBase<MatrixL>, MatrixL>::value &&
738  std::is_base_of<Eigen::MatrixBase<MatrixR>, MatrixR>::value &&
740  std::is_same<typename MatrixR::Scalar, Variable>::value,
741  Eigen::Matrix<Expression, MatrixL::RowsAtCompileTime,
742  MatrixR::ColsAtCompileTime>>::type
743 operator*(const MatrixL& lhs, const MatrixR& rhs) {
744  return lhs.template cast<Expression>() * rhs.template cast<Expression>();
745 }
746 
747 /// Transform<double> * Transform<Expression> => Transform<Expression>
748 template <int Dim, int LhsMode, int RhsMode, int LhsOptions, int RhsOptions>
749 auto operator*(const Eigen::Transform<Expression, Dim, LhsMode, LhsOptions>& t1,
750  const Eigen::Transform<double, Dim, RhsMode, RhsOptions>& t2) {
751  return t1 * t2.template cast<Expression>();
752 }
753 
754 /// Transform<Expression> * Transform<double> => Transform<Expression>
755 template <int Dim, int LhsMode, int RhsMode, int LhsOptions, int RhsOptions>
757  const Eigen::Transform<double, Dim, LhsMode, LhsOptions>& t1,
758  const Eigen::Transform<Expression, Dim, RhsMode, RhsOptions>& t2) {
759  return t1.template cast<Expression>() * t2;
760 }
761 
762 /// Evaluates a symbolic matrix `m` using the `env` by evaluating each element.
763 /// @returns a matrix of double whose size is the size of `m`.
764 /// @throws std::runtime_error if NaN is detected during evaluation.
765 template <typename Derived>
766 auto Evaluate(const Eigen::MatrixBase<Derived>& m, const Environment& env) {
768  "Evaluate only accepts a symbolic matrix.");
769  // Without the trailing `.eval()`, it returns an Eigen Expression (of type
770  // CwiseUnaryOp) and `symbolic::Expression::Evaluate` is only called when a
771  // value is needed (i.e. lazy-evaluation). We add the trailing `.eval()` call
772  // to enforce eager-evaluation and provide a fully evaluated matrix (of
773  // double) to a caller.
774  //
775  // Please refer to https://eigen.tuxfamily.org/dox/TopicPitfalls.html for more
776  // information.
777  return m.unaryExpr([&env](const Expression& e) { return e.Evaluate(env); })
778  .eval();
779 }
780 
781 } // namespace symbolic
782 
783 /** Provides specialization of @c cond function defined in drake/common/cond.h
784  * file. This specialization is required to handle @c double to @c
785  * symbolic::Expression conversion so that we can write one such as <tt>cond(x >
786  * 0.0, 1.0, -1.0)</tt>.
787  */
788 template <typename... Rest>
789 symbolic::Expression cond(const symbolic::Formula& f_cond, double v_then,
790  Rest... rest) {
791  return if_then_else(f_cond, symbolic::Expression{v_then}, cond(rest...));
792 }
793 
794 /// Specializes common/dummy_value.h.
795 template <>
796 struct dummy_value<symbolic::Expression> {
798 };
799 
800 #pragma GCC diagnostic push
801 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
802 /** Specializes is_numeric to be false for symbolic::Expression type. */
803 template <>
804 struct is_numeric<symbolic::Expression> {
805  // TODO(jwnimmer-tri) Remove number_traits.h on or about 2018-12-01.
806  DRAKE_DEPRECATED("This trait will be removed")
807  static constexpr bool value = false;
808 };
809 #pragma GCC diagnostic pop
810 
811 /// Returns the symbolic expression's value() as a double.
812 ///
813 /// @throws If it is not possible to evaluate the symbolic expression with an
814 /// empty environment.
816 
817 } // namespace drake
818 
819 namespace std {
820 /* Provides std::hash<drake::symbolic::Expression>. */
821 template <>
822 struct hash<drake::symbolic::Expression> : public drake::DefaultHash {};
823 #if defined(__GLIBCXX__)
824 // https://gcc.gnu.org/onlinedocs/libstdc++/manual/unordered_associative.html
825 template <>
826 struct __is_fast_hash<hash<drake::symbolic::Expression>> : std::false_type {};
827 #endif
828 
829 /* Provides std::less<drake::symbolic::Expression>. */
830 template <>
831 struct less<drake::symbolic::Expression> {
833  const drake::symbolic::Expression& rhs) const {
834  return lhs.Less(rhs);
835  }
836 };
837 
838 /* Provides std::equal_to<drake::symbolic::Expression>. */
839 template <>
840 struct equal_to<drake::symbolic::Expression> {
842  const drake::symbolic::Expression& rhs) const {
843  return lhs.EqualTo(rhs);
844  }
845 };
846 
847 /* Provides std::numeric_limits<drake::symbolic::Expression>. */
848 template <>
849 struct numeric_limits<drake::symbolic::Expression>
850  : public numeric_limits<double> {};
851 
852 } // namespace std
853 
854 #if !defined(DRAKE_DOXYGEN_CXX)
855 // Define Eigen traits needed for Matrix<drake::symbolic::Expression>.
856 namespace Eigen {
857 // Eigen scalar type traits for Matrix<drake::symbolic::Expression>.
858 template <>
859 struct NumTraits<drake::symbolic::Expression>
860  : GenericNumTraits<drake::symbolic::Expression> {
861  static inline int digits10() { return 0; }
862 };
863 
864 // Informs Eigen that Variable op Variable gets Expression.
865 template <typename BinaryOp>
866 struct ScalarBinaryOpTraits<drake::symbolic::Variable,
867  drake::symbolic::Variable, BinaryOp> {
868  enum { Defined = 1 };
869  typedef drake::symbolic::Expression ReturnType;
870 };
871 
872 // Informs Eigen that Variable op Expression gets Expression.
873 template <typename BinaryOp>
874 struct ScalarBinaryOpTraits<drake::symbolic::Variable,
875  drake::symbolic::Expression, BinaryOp> {
876  enum { Defined = 1 };
877  typedef drake::symbolic::Expression ReturnType;
878 };
879 
880 // Informs Eigen that Expression op Variable gets Expression.
881 template <typename BinaryOp>
882 struct ScalarBinaryOpTraits<drake::symbolic::Expression,
883  drake::symbolic::Variable, BinaryOp> {
884  enum { Defined = 1 };
885  typedef drake::symbolic::Expression ReturnType;
886 };
887 
888 // Informs Eigen that Variable op double gets Expression.
889 template <typename BinaryOp>
890 struct ScalarBinaryOpTraits<drake::symbolic::Variable, double, BinaryOp> {
891  enum { Defined = 1 };
892  typedef drake::symbolic::Expression ReturnType;
893 };
894 
895 // Informs Eigen that double op Variable gets Expression.
896 template <typename BinaryOp>
897 struct ScalarBinaryOpTraits<double, drake::symbolic::Variable, BinaryOp> {
898  enum { Defined = 1 };
899  typedef drake::symbolic::Expression ReturnType;
900 };
901 
902 // Informs Eigen that Expression op double gets Expression.
903 template <typename BinaryOp>
904 struct ScalarBinaryOpTraits<drake::symbolic::Expression, double, BinaryOp> {
905  enum { Defined = 1 };
906  typedef drake::symbolic::Expression ReturnType;
907 };
908 
909 // Informs Eigen that double op Expression gets Expression.
910 template <typename BinaryOp>
911 struct ScalarBinaryOpTraits<double, drake::symbolic::Expression, BinaryOp> {
912  enum { Defined = 1 };
913  typedef drake::symbolic::Expression ReturnType;
914 };
915 
916 } // namespace Eigen
917 #endif // !defined(DRAKE_DOXYGEN_CXX)
918 
919 namespace drake {
920 namespace symbolic {
921 
922 /// Constructs a vector of variables from the vector of variable expressions.
923 /// @throw std::logic_error if there is an expression in @p vec which is not a
924 /// variable.
925 VectorX<Variable> GetVariableVector(
926  const Eigen::Ref<const VectorX<Expression>>& evec);
927 
928 /// Computes the Jacobian matrix J of the vector function @p f with respect to
929 /// @p vars. J(i,j) contains ∂f(i)/∂vars(j).
930 ///
931 /// For example, Jacobian([x * cos(y), x * sin(y), x^2], {x, y}) returns the
932 /// following 3x2 matrix:
933 /// <pre>
934 /// = |cos(y) -x * sin(y)|
935 /// |sin(y) x * cos(y)|
936 /// | 2 * x 0|
937 /// </pre>
938 ///
939 /// @pre {@p vars is non-empty}.
940 MatrixX<Expression> Jacobian(const Eigen::Ref<const VectorX<Expression>>& f,
941  const std::vector<Variable>& vars);
942 
943 /// Computes the Jacobian matrix J of the vector function @p f with respect to
944 /// @p vars. J(i,j) contains ∂f(i)/∂vars(j).
945 ///
946 /// @pre {@p vars is non-empty}.
947 MatrixX<Expression> Jacobian(const Eigen::Ref<const VectorX<Expression>>& f,
948  const Eigen::Ref<const VectorX<Variable>>& vars);
949 
950 /// Returns the Taylor series expansion of `f` around `a` of order `order`.
951 ///
952 /// @param[in] f Symbolic expression to approximate using Taylor series
953 /// expansion.
954 /// @param[in] a Symbolic environment which specifies the point of
955 /// approximation. If a partial environment is provided,
956 /// the unspecified variables are treated as symbolic
957 /// variables (e.g. decision variable).
958 /// @param[in] order Positive integer which specifies the maximum order of the
959 /// resulting polynomial approximating `f` around `a`.
960 Expression TaylorExpand(const Expression& f, const Environment& a, int order);
961 
962 /// Returns the distinct variables in the matrix of expressions.
963 Variables GetDistinctVariables(const Eigen::Ref<const MatrixX<Expression>>& v);
964 
965 /// Checks if two Eigen::Matrix<Expression> @p m1 and @p m2 are structurally
966 /// equal. That is, it returns true if and only if `m1(i, j)` is structurally
967 /// equal to `m2(i, j)` for all `i`, `j`.
968 template <typename DerivedA, typename DerivedB>
969 typename std::enable_if<
970  std::is_base_of<Eigen::MatrixBase<DerivedA>, DerivedA>::value &&
971  std::is_base_of<Eigen::MatrixBase<DerivedB>, DerivedB>::value &&
974  bool>::type
975 CheckStructuralEquality(const DerivedA& m1, const DerivedB& m2) {
976  EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(DerivedA, DerivedB);
977  DRAKE_DEMAND(m1.rows() == m2.rows() && m1.cols() == m2.cols());
978  // Note that std::equal_to<Expression> calls Expression::EqualTo which checks
979  // structural equality between two expressions.
980  return m1.binaryExpr(m2, std::equal_to<Expression>{}).all();
981 }
982 
983 } // namespace symbolic
984 
985 /*
986  * Determine if two EigenBase<> types are matrices (non-column-vectors) of
987  * Expressions and doubles, to then form an implicit formulas.
988  */
989 template <typename DerivedV, typename DerivedB>
991  : std::integral_constant<
992  bool, is_eigen_nonvector_of<DerivedV, symbolic::Expression>::value &&
993  is_eigen_nonvector_of<DerivedB, double>::value> {};
994 
995 /*
996  * Determine if two EigenBase<> types are vectors of Expressions and doubles
997  * that could make a formula.
998  */
999 template <typename DerivedV, typename DerivedB>
1001  : std::integral_constant<
1002  bool, is_eigen_vector_of<DerivedV, symbolic::Expression>::value &&
1003  is_eigen_vector_of<DerivedB, double>::value> {};
1004 
1005 } // namespace drake
Symbolic expression representing a multiplication of powers.
Definition: symbolic_expression_cell.h:366
shared_ptr< ExpressionTanh > to_tanh(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2232
Expression ceil(const Expression &e)
Definition: symbolic_expression.cc:722
An adapter that forwards the HashAlgorithm::operator(data, length) function concept into a runtime-pr...
Definition: hash.h:250
bool is_sinh(const Expression &e)
Checks if e is a hyperbolic-sine expression.
Definition: symbolic_expression.cc:783
bool is_two(const Expression &e)
Checks if e is 2.0.
Definition: symbolic_expression.cc:763
Factory class to help build ExpressionMul expressions.
Definition: symbolic_expression_cell.h:398
Expression operator*(Expression lhs, const Expression &rhs)
Definition: symbolic_expression.cc:334
Symbolic expression representing atan2 function (arctangent function with two arguments).
Definition: symbolic_expression_cell.h:648
bool operator()(const drake::symbolic::Expression &lhs, const drake::symbolic::Expression &rhs) const
Definition: symbolic_expression.h:841
bool is_tanh(const Expression &e)
Checks if e is a hyperbolic-tangent expression.
Definition: symbolic_expression.cc:785
const vector< Expression > & get_uninterpreted_function_arguments(const Expression &e)
Returns the arguments of an uninterpreted-function expression e.
Definition: symbolic_expression.cc:829
Factory class to help build ExpressionAdd expressions.
Definition: symbolic_expression_cell.h:295
shared_ptr< ExpressionLog > to_log(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2134
Represents a symbolic variable.
Definition: symbolic_variable.h:24
This file contains abbreviated definitions for certain specializations of Eigen::Matrix that are comm...
shared_ptr< ExpressionCeiling > to_ceil(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2252
bool is_min(const Expression &e)
Checks if e is a min expression.
Definition: symbolic_expression.cc:786
Definition: symbolic_expression.h:1000
bool is_multiplication(const Expression &e)
Checks if e is a multiplication expression.
Definition: symbolic_expression.cc:767
double value
Definition: wrap_test_util_py.cc:12
Expression acos(const Expression &e)
Definition: symbolic_expression.cc:647
bool is_if_then_else(const Expression &e)
Checks if e is an if-then-else expression.
Definition: symbolic_expression.cc:790
std::vector< Number > x
Definition: ipopt_solver.cc:150
double get_constant_in_addition(const Expression &e)
Returns the constant part of the addition expression e.
Definition: symbolic_expression.cc:810
bool operator()(const drake::symbolic::Expression &lhs, const drake::symbolic::Expression &rhs) const
Definition: symbolic_expression.h:832
bool is_max(const Expression &e)
Checks if e is a max expression.
Definition: symbolic_expression.cc:787
bool is_variable(const Expression &e)
Checks if e is a variable expression.
Definition: symbolic_expression.cc:765
Definition: bullet_model.cc:22
bool is_uninterpreted_function(const Expression &e)
Checks if e is an uninterpreted-function expression.
Definition: symbolic_expression.cc:791
static Expression NaN()
Returns NaN (Not-a-Number).
Definition: symbolic_expression.cc:110
ostream & operator<<(ostream &os, const Environment &env)
Definition: symbolic_environment.cc:111
bool operator<(ExpressionKind k1, ExpressionKind k2)
Total ordering between ExpressionKinds.
Definition: symbolic_expression.cc:40
Expression sin(const Expression &e)
Definition: symbolic_expression.cc:612
Expression abs(const Expression &e)
Definition: symbolic_expression.cc:550
Symbolic expression representing logarithms.
Definition: symbolic_expression_cell.h:469
bool is_neg_one(const Expression &e)
Checks if e is -1.0.
Definition: symbolic_expression.cc:762
Expression & operator+=(Expression &lhs, const Expression &rhs)
Definition: symbolic_expression.cc:223
Expression asin(const Expression &e)
Definition: symbolic_expression.cc:637
Expression cosh(const Expression &e)
Definition: symbolic_expression.cc:682
Represents a symbolic form of a first-order logic formula.
Definition: symbolic_formula.h:114
Expression uninterpreted_function(string name, vector< Expression > arguments)
Constructs an uninterpreted-function expression with name and arguments.
Definition: symbolic_expression.cc:751
Expression atan2(const Expression &e1, const Expression &e2)
Definition: symbolic_expression.cc:665
const Variable & get_variable(const Expression &e)
Returns the embedded variable in the variable expression e.
Definition: symbolic_expression.cc:798
const Expression & get_second_argument(const Expression &e)
Returns the second argument of the binary expression e.
Definition: symbolic_expression.cc:807
arctangent2 (atan2(y,x) = atan(y/x))
Symbolic expression representing min function.
Definition: symbolic_expression_cell.h:704
STL namespace.
Expression operator/(Expression lhs, const Expression &rhs)
Definition: symbolic_expression.cc:481
Symbolic expression representing square-root.
Definition: symbolic_expression_cell.h:518
absolute value function
Expression operator+(Expression lhs, const Expression &rhs)
Definition: symbolic_expression.cc:217
A scalar multi-variate polynomial, modeled after the msspoly in spotless.
Definition: polynomial.h:42
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > VectorX
A column vector of any size, templated on scalar type.
Definition: eigen_types.h:46
Represents the base class for binary expressions.
Definition: symbolic_expression_cell.h:155
Symbolic expression representing floor function.
Definition: symbolic_expression_cell.h:746
shared_ptr< ExpressionDiv > to_division(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2125
Symbolic expression representing arcsine function.
Definition: symbolic_expression_cell.h:597
Symbolic expression representing sine function.
Definition: symbolic_expression_cell.h:555
Eigen::Matrix< Scalar, 1, Eigen::Dynamic > RowVectorX
A row vector of any size, templated on scalar type.
Definition: eigen_types.h:71
bool is_sqrt(const Expression &e)
Checks if e is a square-root expression.
Definition: symbolic_expression.cc:774
bool is_tan(const Expression &e)
Checks if e is a tangent expression.
Definition: symbolic_expression.cc:778
std::unordered_map< Variable, Expression > Substitution
Definition: symbolic_expression.h:107
Definition: autodiff_overloads.h:34
shared_ptr< ExpressionAtan > to_atan(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2199
shared_ptr< UnaryExpressionCell > to_unary(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2084
shared_ptr< ExpressionMul > to_multiplication(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2116
bool EqualTo(const Expression &e) const
Checks structural equality.
Definition: symbolic_expression.cc:121
auto Evaluate(const Eigen::MatrixBase< Derived > &m, const Environment &env)
Evaluates a symbolic matrix m using the env by evaluating each element.
Definition: symbolic_expression.h:766
Symbolic expression representing arccosine function.
Definition: symbolic_expression_cell.h:615
bool is_cosh(const Expression &e)
Checks if e is a hyperbolic-cosine expression.
Definition: symbolic_expression.cc:784
shared_ptr< ExpressionFloor > to_floor(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2261
std::string to_string(const drake::geometry::Identifier< Tag > &id)
Enables use of identifiers with to_string.
Definition: identifier.h:215
shared_ptr< ExpressionExp > to_exp(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2146
bool is_ceil(const Expression &e)
Checks if e is a ceil expression.
Definition: symbolic_expression.cc:788
Symbolic expression representing exponentiation using the base of natural logarithms.
Definition: symbolic_expression_cell.h:504
friend void hash_append(HashAlgorithm &hasher, const Expression &item) noexcept
Implements the hash_append generic hashing concept.
Definition: symbolic_expression.h:303
Symbolic expression representing an uninterpreted function.
Definition: symbolic_expression_cell.h:791
Represents an abstract class which is the base of concrete symbolic-expression classes.
Definition: symbolic_expression_cell.h:46
shared_ptr< ExpressionAdd > to_addition(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2107
Symbolic expression representing hyperbolic sine function.
Definition: symbolic_expression_cell.h:662
Symbolic expression representing division.
Definition: symbolic_expression_cell.h:455
const map< Expression, Expression > & get_base_to_exponent_map_in_multiplication(const Expression &e)
Returns the map from a base expression to its exponent expression in the multiplication expression e...
Definition: symbolic_expression.cc:820
bool is_exp(const Expression &e)
Checks if e is an exp expression.
Definition: symbolic_expression.cc:773
bool is_floor(const Expression &e)
Checks if e is a floor expression.
Definition: symbolic_expression.cc:789
Expression sinh(const Expression &e)
Definition: symbolic_expression.cc:674
bool is_one(const Expression &e)
Checks if e is 1.0.
Definition: symbolic_expression.cc:761
shared_ptr< ExpressionCos > to_cos(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2171
ExpressionKind
Kinds of symbolic expressions.
Definition: symbolic_expression.h:39
shared_ptr< ExpressionConstant > to_constant(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2066
const Expression & get_then_expression(const Expression &e)
Returns the &#39;then&#39; expression in the if-then-else expression e.
Definition: symbolic_expression.cc:838
#define DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(Classname)
DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN defaults the special member functions for copy-construction, copy-assignment, move-construction, and move-assignment.
Definition: drake_copyable.h:57
Expression floor(const Expression &e)
Definition: symbolic_expression.cc:730
bool is_abs(const Expression &e)
Checks if e is an abs expression.
Definition: symbolic_expression.cc:772
Expression exp(const Expression &e)
Definition: symbolic_expression.cc:558
const Expression & get_first_argument(const Expression &e)
Returns the first argument of the binary expression e.
Definition: symbolic_expression.cc:804
bool is_log(const Expression &e)
Checks if e is a log expression.
Definition: symbolic_expression.cc:771
Provides Drake&#39;s assertion implementation.
bool Less(const Expression &e) const
Provides lexicographical ordering between expressions.
Definition: symbolic_expression.cc:134
Expression max(const Expression &e1, const Expression &e2)
Definition: symbolic_expression.cc:710
Symbolic expression representing hyperbolic tangent function.
Definition: symbolic_expression_cell.h:690
double ExtractDoubleOrThrow(const Eigen::AutoDiffScalar< DerType > &scalar)
Returns the autodiff scalar&#39;s value() as a double.
Definition: autodiff_overloads.h:145
#define DRAKE_DEMAND(condition)
Evaluates condition and iff the value is false will trigger an assertion failure with a message showi...
Definition: drake_assert.h:45
shared_ptr< ExpressionPow > to_pow(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2159
double get_constant_value(const Expression &e)
Returns the constant value of the constant expression e.
Definition: symbolic_expression.cc:795
Expression min(const Expression &e1, const Expression &e2)
Definition: symbolic_expression.cc:698
Symbolic expression representing a variable.
Definition: symbolic_expression_cell.h:192
Expression tanh(const Expression &e)
Definition: symbolic_expression.cc:690
bool is_cos(const Expression &e)
Checks if e is a cosine expression.
Definition: symbolic_expression.cc:777
shared_ptr< ExpressionVar > to_variable(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2075
Symbolic expression representing ceil function.
Definition: symbolic_expression_cell.h:732
Variables GetDistinctVariables(const Eigen::Ref< const MatrixX< Expression >> &v)
Returns the distinct variables in the matrix of expressions.
Definition: symbolic_expression.cc:1011
shared_ptr< ExpressionUninterpretedFunction > to_uninterpreted_function(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2279
bool is_atan2(const Expression &e)
Checks if e is an arctangent2 expression.
Definition: symbolic_expression.cc:782
bool is_division(const Expression &e)
Checks if e is a division expression.
Definition: symbolic_expression.cc:770
Eigen::AutoDiffScalar< typename Eigen::internal::remove_all< DerType >::type::PlainObject > cond(bool f_cond, const Eigen::AutoDiffScalar< DerType > &e_then, Rest...rest)
Provides special case of cond expression for Eigen::AutoDiffScalar type.
Definition: autodiff_overloads.h:185
Represents a symbolic form of an expression.
Definition: symbolic_expression.h:173
void swap(Expression &a, Expression &b)
Definition: symbolic_expression.h:406
shared_ptr< ExpressionSin > to_sin(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2165
ReferenceType type
Definition: loader.cc:34
shared_ptr< ExpressionAsin > to_asin(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2183
double Evaluate(const Environment &env=Environment{}) const
Evaluates under a given environment (by default, an empty environment).
Definition: symbolic_expression.cc:162
const Expression & get_else_expression(const Expression &e)
Returns the &#39;else&#39; expression in the if-then-else expression e.
Definition: symbolic_expression.cc:842
Symbolic expression representing arctangent function.
Definition: symbolic_expression_cell.h:633
const map< Expression, double > & get_expr_to_coeff_map_in_addition(const Expression &e)
Returns the map from an expression to its coefficient in the addition expression e.
Definition: symbolic_expression.cc:813
Expression & operator-=(Expression &lhs, const Expression &rhs)
Definition: symbolic_expression.cc:283
const VectorXDecisionVariable * vars
Definition: nlopt_solver.cc:105
const string & get_uninterpreted_function_name(const Expression &e)
Returns the name of an uninterpreted-function expression e.
Definition: symbolic_expression.cc:825
Symbolic expression representing cosine function.
Definition: symbolic_expression_cell.h:569
Symbolic expression representing tangent function.
Definition: symbolic_expression_cell.h:583
shared_ptr< BinaryExpressionCell > to_binary(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2097
#define DRAKE_DEPRECATED(message)
Use DRAKE_DEPRECATED("message") to discourage use of particular classes, typedefs, variables, non-static data members, functions, arguments, enumerations, and template specializations.
Definition: drake_deprecated.h:33
VectorX< Variable > GetVariableVector(const Eigen::Ref< const VectorX< Expression >> &evec)
Constructs a vector of variables from the vector of variable expressions.
Definition: symbolic_expression.cc:849
bool is_constant(const Expression &e)
Checks if e is a constant expression.
Definition: symbolic_expression.cc:756
shared_ptr< ExpressionAcos > to_acos(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2191
Expression pow(const Expression &e1, const Expression &e2)
Definition: symbolic_expression.cc:582
Symbolic expression representing an addition which is a sum of products.
Definition: symbolic_expression_cell.h:263
Expression if_then_else(const Formula &f_cond, const Expression &e_then, const Expression &e_else)
Definition: symbolic_expression.cc:738
Derived::Scalar all(const Eigen::DenseBase< Derived > &m)
Checks truth for all elements in matrix m.
Definition: drake_bool.h:36
Represents the base class for unary expressions.
Definition: symbolic_expression_cell.h:123
A hashing functor, somewhat like std::hash.
Definition: hash.h:202
Expression tan(const Expression &e)
Definition: symbolic_expression.cc:629
shared_ptr< ExpressionAbs > to_abs(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2140
Provides a "dummy" value for a ScalarType – a value that is unlikely to be mistaken for a purposeful...
Definition: dummy_value.h:17
bool is_acos(const Expression &e)
Checks if e is an arccosine expression.
Definition: symbolic_expression.cc:780
shared_ptr< ExpressionIfThenElse > to_if_then_else(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2270
shared_ptr< ExpressionMin > to_min(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2240
Symbolic expression representing max function.
Definition: symbolic_expression_cell.h:718
Represents a set of variables.
Definition: symbolic_variables.h:31
MatrixX< Expression > Jacobian(const Eigen::Ref< const VectorX< Expression >> &f, const vector< Variable > &vars)
Computes the Jacobian matrix J of the vector function f with respect to vars.
Definition: symbolic_expression.cc:863
std::enable_if< std::is_base_of< Eigen::MatrixBase< DerivedA >, DerivedA >::value &&std::is_base_of< Eigen::MatrixBase< DerivedB >, DerivedB >::value &&std::is_same< typename DerivedA::Scalar, Expression >::value &&std::is_same< typename DerivedB::Scalar, Expression >::value, bool >::type CheckStructuralEquality(const DerivedA &m1, const DerivedB &m2)
Checks if two Eigen::Matrix<Expression> m1 and m2 are structurally equal.
Definition: symbolic_expression.h:975
bool is_addition(const Expression &e)
Checks if e is an addition expression.
Definition: symbolic_expression.cc:766
Expression cos(const Expression &e)
Definition: symbolic_expression.cc:620
shared_ptr< ExpressionTan > to_tan(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2177
shared_ptr< ExpressionMax > to_max(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2246
shared_ptr< ExpressionAtan2 > to_atan2(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2207
Expression sqrt(const Expression &e)
Definition: symbolic_expression.cc:566
shared_ptr< ExpressionSqrt > to_sqrt(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2152
Represents a symbolic environment (mapping from a variable to a value).
Definition: symbolic_environment.h:56
Symbolic expression representing if-then-else expression.
Definition: symbolic_expression_cell.h:760
bool is_atan(const Expression &e)
Checks if e is an arctangent expression.
Definition: symbolic_expression.cc:781
This traits type is deprecated, and will be removed.
Definition: number_traits.h:9
Definition: symbolic_expression.h:990
Expression operator-(Expression lhs, const Expression &rhs)
Definition: symbolic_expression.cc:277
Symbolic expression representing absolute value function.
Definition: symbolic_expression_cell.h:487
shared_ptr< ExpressionCosh > to_cosh(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2224
Symbolic expression representing a constant.
Definition: symbolic_expression_cell.h:215
Provides public header files of Drake&#39;s symbolic library.
Expression atan(const Expression &e)
Definition: symbolic_expression.cc:657
double get_constant_in_multiplication(const Expression &e)
Returns the constant part of the multiplication expression e.
Definition: symbolic_expression.cc:817
const Expression & get_argument(const Expression &e)
Returns the argument in the unary expression e.
Definition: symbolic_expression.cc:801
Symbolic expression representing power function.
Definition: symbolic_expression_cell.h:536
bool is_sin(const Expression &e)
Checks if e is a sine expression.
Definition: symbolic_expression.cc:776
bool is_nan(const Expression &e)
Checks if e is NaN.
Definition: symbolic_expression.cc:764
bool is_zero(const Expression &e)
Checks if e is 0.0.
Definition: symbolic_expression.cc:760
Expression TaylorExpand(const Expression &f, const Environment &a, const int order)
Returns the Taylor series expansion of f around a of order order.
Definition: symbolic_expression.cc:988
Symbolic expression representing hyperbolic cosine function.
Definition: symbolic_expression_cell.h:676
Expression & operator*=(Expression &lhs, const Expression &rhs)
Definition: symbolic_expression.cc:340
Expression log(const Expression &e)
Definition: symbolic_expression.cc:540
const Formula & get_conditional_formula(const Expression &e)
Returns the conditional formula in the if-then-else expression e.
Definition: symbolic_expression.cc:834
std::string name
Definition: geometry_base_test.cc:343
shared_ptr< ExpressionSinh > to_sinh(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2216
Expression & operator/=(Expression &lhs, const Expression &rhs)
Definition: symbolic_expression.cc:487
bool is_pow(const Expression &e)
Checks if e is a power-function expression.
Definition: symbolic_expression.cc:775
Provides careful macros to selectively enable or disable the special member functions for copy-constr...
bool is_asin(const Expression &e)
Checks if e is an arcsine expression.
Definition: symbolic_expression.cc:779
int data
Definition: value_test.cc:20