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