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"
23 #include "drake/common/drake_assert.h"
24 #include "drake/common/drake_copyable.h"
25 #include "drake/common/dummy_value.h"
26 #include "drake/common/eigen_types.h"
27 #include "drake/common/extract_double.h"
28 #include "drake/common/hash.h"
29 #include "drake/common/number_traits.h"
30 #include "drake/common/polynomial.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  /** Returns string representation of Expression. */
280  std::string to_string() const;
281 
282  /** Returns zero. */
283  static Expression Zero();
284  /** Returns one. */
285  static Expression One();
286  /** Returns Pi, the ratio of a circle’s circumference to its diameter. */
287  static Expression Pi();
288  /** Return e, the base of natural logarithms. */
289  static Expression E();
290  /** Returns NaN (Not-a-Number). */
291  static Expression NaN();
292 
293  /** Implements the @ref hash_append concept. */
294  template <class HashAlgorithm>
295  friend void hash_append(
296  HashAlgorithm& hasher, const Expression& item) noexcept {
297  DelegatingHasher delegating_hasher(
298  [&hasher](const void* data, const size_t length) {
299  return hasher(data, length);
300  });
301  item.HashAppend(&delegating_hasher);
302  }
303 
304  friend Expression operator+(Expression lhs, const Expression& rhs);
305  // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
306  friend Expression& operator+=(Expression& lhs, const Expression& rhs);
307 
308  /** Provides prefix increment operator (i.e. ++x). */
309  Expression& operator++();
310  /** Provides postfix increment operator (i.e. x++). */
311  Expression operator++(int);
312 
313  friend Expression operator-(Expression lhs, const Expression& rhs);
314  // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
315  friend Expression& operator-=(Expression& lhs, const Expression& rhs);
316 
317  /** Provides unary minus operator. */
318  friend Expression operator-(const Expression& e);
319  /** Provides prefix decrement operator (i.e. --x). */
320  Expression& operator--();
321  /** Provides postfix decrement operator (i.e. x--). */
322  Expression operator--(int);
323 
324  friend Expression operator*(Expression lhs, const Expression& rhs);
325  // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
326  friend Expression& operator*=(Expression& lhs, const Expression& rhs);
327 
328  friend Expression operator/(Expression lhs, const Expression& rhs);
329  // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
330  friend Expression& operator/=(Expression& lhs, const Expression& rhs);
331 
332  friend Expression log(const Expression& e);
333  friend Expression abs(const Expression& e);
334  friend Expression exp(const Expression& e);
335  friend Expression sqrt(const Expression& e);
336  friend Expression pow(const Expression& e1, const Expression& e2);
337  friend Expression sin(const Expression& e);
338  friend Expression cos(const Expression& e);
339  friend Expression tan(const Expression& e);
340  friend Expression asin(const Expression& e);
341  friend Expression acos(const Expression& e);
342  friend Expression atan(const Expression& e);
343  friend Expression atan2(const Expression& e1, const Expression& e2);
344  friend Expression sinh(const Expression& e);
345  friend Expression cosh(const Expression& e);
346  friend Expression tanh(const Expression& e);
347  friend Expression min(const Expression& e1, const Expression& e2);
348  friend Expression max(const Expression& e1, const Expression& e2);
349  friend Expression ceil(const Expression& e);
350  friend Expression floor(const Expression& e);
351 
352  /** Constructs if-then-else expression.
353 
354  @verbatim
355  if_then_else(cond, expr_then, expr_else)
356  @endverbatim
357 
358  The value returned by the above if-then-else expression is @p expr_then if
359  @p cond is evaluated to true. Otherwise, it returns @p expr_else.
360 
361  The semantics is similar to the C++'s conditional expression constructed by
362  its ternary operator, @c ?:. However, there is a key difference between the
363  C++'s conditional expression and our @c if_then_else expression in a way the
364  arguments are evaluated during the construction.
365 
366  - In case of the C++'s conditional expression, <tt> cond ? expr_then :
367  expr_else</tt>, the then expression @c expr_then (respectively, the else
368  expression @c expr_else) is \b only evaluated when the conditional
369  expression @c cond is evaluated to \b true (respectively, when @c cond is
370  evaluated to \b false).
371 
372  - In case of the symbolic expression, <tt>if_then_else(cond, expr_then,
373  expr_else)</tt>, however, \b both arguments @c expr_then and @c expr_else
374  are evaluated first and then passed to the @c if_then_else function.
375 
376  @note This function returns an \b expression and it is different from the
377  C++'s if-then-else \b statement.
378 
379  @note While it is still possible to define <tt> min, max, abs</tt> math
380  functions using @c if_then_else expression, it is highly \b recommended to
381  use the provided native definitions for them because it allows solvers to
382  detect specific math functions and to have a room for special
383  optimizations.
384 
385  @note More information about the C++'s conditional expression and ternary
386  operator is available at
387  http://en.cppreference.com/w/cpp/language/operator_other#Conditional_operator.
388  */
389  friend Expression if_then_else(const Formula& f_cond,
390  const Expression& e_then,
391  const Expression& e_else);
392  friend Expression uninterpreted_function(const std::string& name,
393  const Variables& vars);
394 
395  friend std::ostream& operator<<(std::ostream& os, const Expression& e);
396  friend void swap(Expression& a, Expression& b) { std::swap(a.ptr_, b.ptr_); }
397 
398  friend bool is_constant(const Expression& e);
399  friend bool is_variable(const Expression& e);
400  friend bool is_addition(const Expression& e);
401  friend bool is_multiplication(const Expression& e);
402  friend bool is_division(const Expression& e);
403  friend bool is_log(const Expression& e);
404  friend bool is_abs(const Expression& e);
405  friend bool is_exp(const Expression& e);
406  friend bool is_sqrt(const Expression& e);
407  friend bool is_pow(const Expression& e);
408  friend bool is_sin(const Expression& e);
409  friend bool is_cos(const Expression& e);
410  friend bool is_tan(const Expression& e);
411  friend bool is_asin(const Expression& e);
412  friend bool is_acos(const Expression& e);
413  friend bool is_atan(const Expression& e);
414  friend bool is_atan2(const Expression& e);
415  friend bool is_sinh(const Expression& e);
416  friend bool is_cosh(const Expression& e);
417  friend bool is_tanh(const Expression& e);
418  friend bool is_min(const Expression& e);
419  friend bool is_max(const Expression& e);
420  friend bool is_ceil(const Expression& e);
421  friend bool is_floor(const Expression& e);
422  friend bool is_if_then_else(const Expression& e);
423  friend bool is_uninterpreted_function(const Expression& e);
424 
425  // Note that the following cast functions are only for low-level operations
426  // and not exposed to the user of drake/common/symbolic_expression.h
427  // header. These functions are declared in
428  // drake/common/symbolic_expression_cell.h header.
429  friend std::shared_ptr<ExpressionConstant> to_constant(const Expression& e);
430  friend std::shared_ptr<ExpressionVar> to_variable(const Expression& e);
431  friend std::shared_ptr<UnaryExpressionCell> to_unary(const Expression& e);
432  friend std::shared_ptr<BinaryExpressionCell> to_binary(const Expression& e);
433  friend std::shared_ptr<ExpressionAdd> to_addition(const Expression& e);
434  friend std::shared_ptr<ExpressionMul> to_multiplication(const Expression& e);
435  friend std::shared_ptr<ExpressionDiv> to_division(const Expression& e);
436  friend std::shared_ptr<ExpressionLog> to_log(const Expression& e);
437  friend std::shared_ptr<ExpressionAbs> to_abs(const Expression& e);
438  friend std::shared_ptr<ExpressionExp> to_exp(const Expression& e);
439  friend std::shared_ptr<ExpressionSqrt> to_sqrt(const Expression& e);
440  friend std::shared_ptr<ExpressionPow> to_pow(const Expression& e);
441  friend std::shared_ptr<ExpressionSin> to_sin(const Expression& e);
442  friend std::shared_ptr<ExpressionCos> to_cos(const Expression& e);
443  friend std::shared_ptr<ExpressionTan> to_tan(const Expression& e);
444  friend std::shared_ptr<ExpressionAsin> to_asin(const Expression& e);
445  friend std::shared_ptr<ExpressionAcos> to_acos(const Expression& e);
446  friend std::shared_ptr<ExpressionAtan> to_atan(const Expression& e);
447  friend std::shared_ptr<ExpressionAtan2> to_atan2(const Expression& e);
448  friend std::shared_ptr<ExpressionSinh> to_sinh(const Expression& e);
449  friend std::shared_ptr<ExpressionCosh> to_cosh(const Expression& e);
450  friend std::shared_ptr<ExpressionTanh> to_tanh(const Expression& e);
451  friend std::shared_ptr<ExpressionMin> to_min(const Expression& e);
452  friend std::shared_ptr<ExpressionMax> to_max(const Expression& e);
453  friend std::shared_ptr<ExpressionCeiling> to_ceil(const Expression& e);
454  friend std::shared_ptr<ExpressionFloor> to_floor(const Expression& e);
455  friend std::shared_ptr<ExpressionIfThenElse> to_if_then_else(
456  const Expression& e);
457  friend std::shared_ptr<ExpressionUninterpretedFunction>
459 
460  friend class ExpressionAddFactory;
461  friend class ExpressionMulFactory;
462 
463  private:
464  explicit Expression(std::shared_ptr<ExpressionCell> ptr);
465  void HashAppend(DelegatingHasher* hasher) const;
466 
467  std::shared_ptr<ExpressionCell> ptr_;
468 };
469 
470 Expression operator+(Expression lhs, const Expression& rhs);
471 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
472 Expression& operator+=(Expression& lhs, const Expression& rhs);
473 Expression operator-(Expression lhs, const Expression& rhs);
474 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
475 Expression& operator-=(Expression& lhs, const Expression& rhs);
477 Expression operator*(Expression lhs, const Expression& rhs);
478 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
479 Expression& operator*=(Expression& lhs, const Expression& rhs);
480 Expression operator/(Expression lhs, const Expression& rhs);
481 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
482 Expression& operator/=(Expression& lhs, const Expression& rhs);
483 
484 Expression log(const Expression& e);
485 Expression abs(const Expression& e);
486 Expression exp(const Expression& e);
487 Expression sqrt(const Expression& e);
488 Expression pow(const Expression& e1, const Expression& e2);
489 Expression sin(const Expression& e);
490 Expression cos(const Expression& e);
491 Expression tan(const Expression& e);
492 Expression asin(const Expression& e);
493 Expression acos(const Expression& e);
494 Expression atan(const Expression& e);
495 Expression atan2(const Expression& e1, const Expression& e2);
496 Expression sinh(const Expression& e);
497 Expression cosh(const Expression& e);
498 Expression tanh(const Expression& e);
499 Expression min(const Expression& e1, const Expression& e2);
500 Expression max(const Expression& e1, const Expression& e2);
501 Expression ceil(const Expression& e);
502 Expression floor(const Expression& e);
503 Expression if_then_else(const Formula& f_cond, const Expression& e_then,
504  const Expression& e_else);
505 
506 /** Constructs an uninterpreted-function expression with @p name and @p vars.
507  * An uninterpreted function is an opaque function that has no other property
508  * than its name and a set of its arguments. This is useful to applications
509  * where it is good enough to provide abstract information of a function without
510  * exposing full details. Declaring sparsity of a system is a typical example.
511  */
512 Expression uninterpreted_function(const std::string& name,
513  const Variables& vars);
514 void swap(Expression& a, Expression& b);
515 
516 std::ostream& operator<<(std::ostream& os, const Expression& e);
517 
518 /** Checks if @p e is a constant expression. */
519 bool is_constant(const Expression& e);
520 /** Checks if @p e is a constant expression representing @p v. */
521 bool is_constant(const Expression& e, double v);
522 /** Checks if @p e is 0.0. */
523 bool is_zero(const Expression& e);
524 /** Checks if @p e is 1.0. */
525 bool is_one(const Expression& e);
526 /** Checks if @p e is -1.0. */
527 bool is_neg_one(const Expression& e);
528 /** Checks if @p e is 2.0. */
529 bool is_two(const Expression& e);
530 /** Checks if @p e is NaN. */
531 bool is_nan(const Expression& e);
532 /** Checks if @p e is a variable expression. */
533 bool is_variable(const Expression& e);
534 /** Checks if @p e is an addition expression. */
535 bool is_addition(const Expression& e);
536 /** Checks if @p e is a multiplication expression. */
537 bool is_multiplication(const Expression& e);
538 /** Checks if @p e is a division expression. */
539 bool is_division(const Expression& e);
540 /** Checks if @p e is a log expression. */
541 bool is_log(const Expression& e);
542 /** Checks if @p e is an abs expression. */
543 bool is_abs(const Expression& e);
544 /** Checks if @p e is an exp expression. */
545 bool is_exp(const Expression& e);
546 /** Checks if @p e is a square-root expression. */
547 bool is_sqrt(const Expression& e);
548 /** Checks if @p e is a power-function expression. */
549 bool is_pow(const Expression& e);
550 /** Checks if @p e is a sine expression. */
551 bool is_sin(const Expression& e);
552 /** Checks if @p e is a cosine expression. */
553 bool is_cos(const Expression& e);
554 /** Checks if @p e is a tangent expression. */
555 bool is_tan(const Expression& e);
556 /** Checks if @p e is an arcsine expression. */
557 bool is_asin(const Expression& e);
558 /** Checks if @p e is an arccosine expression. */
559 bool is_acos(const Expression& e);
560 /** Checks if @p e is an arctangent expression. */
561 bool is_atan(const Expression& e);
562 /** Checks if @p e is an arctangent2 expression. */
563 bool is_atan2(const Expression& e);
564 /** Checks if @p e is a hyperbolic-sine expression. */
565 bool is_sinh(const Expression& e);
566 /** Checks if @p e is a hyperbolic-cosine expression. */
567 bool is_cosh(const Expression& e);
568 /** Checks if @p e is a hyperbolic-tangent expression. */
569 bool is_tanh(const Expression& e);
570 /** Checks if @p e is a min expression. */
571 bool is_min(const Expression& e);
572 /** Checks if @p e is a max expression. */
573 bool is_max(const Expression& e);
574 /** Checks if @p e is an if-then-else expression. */
575 bool is_if_then_else(const Expression& e);
576 /** Checks if @p e is an uninterpreted-function expression. */
578 
579 /** Returns the constant value of the constant expression @p e.
580  * \pre{@p e is a constant expression.}
581  */
582 double get_constant_value(const Expression& e);
583 /** Returns the embedded variable in the variable expression @p e.
584  * \pre{@p e is a variable expression.}
585  */
586 const Variable& get_variable(const Expression& e);
587 /** Returns the argument in the unary expression @p e.
588  * \pre{@p e is a unary expression.}
589  */
590 const Expression& get_argument(const Expression& e);
591 /** Returns the first argument of the binary expression @p e.
592  * \pre{@p e is a binary expression.}
593  */
594 const Expression& get_first_argument(const Expression& e);
595 /** Returns the second argument of the binary expression @p e.
596  * \pre{@p e is a binary expression.}
597  */
598 const Expression& get_second_argument(const Expression& e);
599 /** Returns the constant part of the addition expression @p e. For instance,
600  * given 7 + 2 * x + 3 * y, it returns 7.
601  * \pre{@p e is an addition expression.}
602  */
603 double get_constant_in_addition(const Expression& e);
604 /** Returns the map from an expression to its coefficient in the addition
605  * expression @p e. For instance, given 7 + 2 * x + 3 * y, the return value
606  * maps 'x' to 2 and 'y' to 3.
607  * \pre{@p e is an addition expression.}
608  */
609 const std::map<Expression, double>& get_expr_to_coeff_map_in_addition(
610  const Expression& e);
611 /** Returns the constant part of the multiplication expression @p e. For
612  * instance, given 7 * x^2 * y^3, it returns 7.
613  * \pre{@p e is a multiplication expression.}
614  */
616 /** Returns the map from a base expression to its exponent expression in the
617  * multiplication expression @p e. For instance, given 7 * x^2 * y^3 * z^x, the
618  * return value maps 'x' to 2, 'y' to 3, and 'z' to 'x'.
619  * \pre{@p e is a multiplication expression.}
620  */
621 const std::map<Expression, Expression>&
623 
624 /** Returns the name of an uninterpreted-function expression @p e.
625  * \pre{@p e is an uninterpreted-function expression.}
626  */
627 const std::string& get_uninterpreted_function_name(const Expression& e);
628 
629 /** Returns the conditional formula in the if-then-else expression @p e.
630  * @pre @p e is an if-then-else expression.
631  */
633 
634 /** Returns the 'then' expression in the if-then-else expression @p e.
635  * @pre @p e is an if-then-else expression.
636  */
637 const Expression& get_then_expression(const Expression& e);
638 
639 /** Returns the 'else' expression in the if-then-else expression @p e.
640  * @pre @p e is an if-then-else expression.
641  */
642 const Expression& get_else_expression(const Expression& e);
643 
644 // Matrix<Expression> * Matrix<double> => Matrix<Expression>
645 template <typename MatrixL, typename MatrixR>
646 typename std::enable_if<
647  std::is_base_of<Eigen::MatrixBase<MatrixL>, MatrixL>::value &&
648  std::is_base_of<Eigen::MatrixBase<MatrixR>, MatrixR>::value &&
651  Eigen::Matrix<Expression, MatrixL::RowsAtCompileTime,
652  MatrixR::ColsAtCompileTime>>::type
653 operator*(const MatrixL& lhs, const MatrixR& rhs) {
654  return lhs.template cast<Expression>() * rhs.template cast<Expression>();
655 }
656 
657 // Matrix<double> * Matrix<Expression> => Matrix<Expression>
658 template <typename MatrixL, typename MatrixR>
659 typename std::enable_if<
660  std::is_base_of<Eigen::MatrixBase<MatrixL>, MatrixL>::value &&
661  std::is_base_of<Eigen::MatrixBase<MatrixR>, MatrixR>::value &&
664  Eigen::Matrix<Expression, MatrixL::RowsAtCompileTime,
665  MatrixR::ColsAtCompileTime>>::type
666 operator*(const MatrixL& lhs, const MatrixR& rhs) {
667  return lhs.template cast<Expression>() * rhs.template cast<Expression>();
668 }
669 
670 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
671 Expression& operator+=(Expression& lhs, const Variable& rhs);
672 Expression operator+(const Variable& lhs, const Variable& rhs);
673 Expression operator+(Expression lhs, const Variable& rhs);
674 Expression operator+(const Variable& lhs, Expression rhs);
675 
676 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
677 Expression& operator-=(Expression& lhs, const Variable& rhs);
678 Expression operator-(const Variable& lhs, const Variable& rhs);
679 Expression operator-(Expression lhs, const Variable& rhs);
680 Expression operator-(const Variable& lhs, const Expression& rhs);
681 
682 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
683 Expression& operator*=(Expression& lhs, const Variable& rhs);
684 Expression operator*(const Variable& lhs, const Variable& rhs);
685 Expression operator*(Expression lhs, const Variable& rhs);
686 Expression operator*(const Variable& lhs, Expression rhs);
687 
688 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
689 Expression& operator/=(Expression& lhs, const Variable& rhs);
690 Expression operator/(const Variable& lhs, const Variable& rhs);
691 Expression operator/(Expression lhs, const Variable& rhs);
692 Expression operator/(const Variable& lhs, const Expression& rhs);
693 
694 Expression operator+(const Variable& var);
695 Expression operator-(const Variable& var);
696 
697 // Matrix<Expression> * Matrix<Variable> => Matrix<Expression>
698 template <typename MatrixL, typename MatrixR>
699 typename std::enable_if<
700  std::is_base_of<Eigen::MatrixBase<MatrixL>, MatrixL>::value &&
701  std::is_base_of<Eigen::MatrixBase<MatrixR>, MatrixR>::value &&
704  Eigen::Matrix<Expression, MatrixL::RowsAtCompileTime,
705  MatrixR::ColsAtCompileTime>>::type
706 operator*(const MatrixL& lhs, const MatrixR& rhs) {
707  return lhs * rhs.template cast<Expression>();
708 }
709 
710 // Matrix<Variable> * Matrix<Expression> => Matrix<Expression>
711 template <typename MatrixL, typename MatrixR>
712 typename std::enable_if<
713  std::is_base_of<Eigen::MatrixBase<MatrixL>, MatrixL>::value &&
714  std::is_base_of<Eigen::MatrixBase<MatrixR>, MatrixR>::value &&
716  std::is_same<typename MatrixR::Scalar, Expression>::value,
717  Eigen::Matrix<Expression, MatrixL::RowsAtCompileTime,
718  MatrixR::ColsAtCompileTime>>::type
719 operator*(const MatrixL& lhs, const MatrixR& rhs) {
720  return lhs.template cast<Expression>() * rhs;
721 }
722 
723 // Matrix<Variable> * Matrix<double> => Matrix<Expression>
724 template <typename MatrixL, typename MatrixR>
725 typename std::enable_if<
726  std::is_base_of<Eigen::MatrixBase<MatrixL>, MatrixL>::value &&
727  std::is_base_of<Eigen::MatrixBase<MatrixR>, MatrixR>::value &&
729  std::is_same<typename MatrixR::Scalar, double>::value,
730  Eigen::Matrix<Expression, MatrixL::RowsAtCompileTime,
731  MatrixR::ColsAtCompileTime>>::type
732 operator*(const MatrixL& lhs, const MatrixR& rhs) {
733  return lhs.template cast<Expression>() * rhs.template cast<Expression>();
734 }
735 
736 // Matrix<double> * Matrix<Variable> => Matrix<Expression>
737 template <typename MatrixL, typename MatrixR>
738 typename std::enable_if<
739  std::is_base_of<Eigen::MatrixBase<MatrixL>, MatrixL>::value &&
740  std::is_base_of<Eigen::MatrixBase<MatrixR>, MatrixR>::value &&
742  std::is_same<typename MatrixR::Scalar, Variable>::value,
743  Eigen::Matrix<Expression, MatrixL::RowsAtCompileTime,
744  MatrixR::ColsAtCompileTime>>::type
745 operator*(const MatrixL& lhs, const MatrixR& rhs) {
746  return lhs.template cast<Expression>() * rhs.template cast<Expression>();
747 }
748 
749 /// Transform<double> * Transform<Expression> => Transform<Expression>
750 template <int Dim, int LhsMode, int RhsMode, int LhsOptions, int RhsOptions>
751 auto operator*(const Eigen::Transform<Expression, Dim, LhsMode, LhsOptions>& t1,
752  const Eigen::Transform<double, Dim, RhsMode, RhsOptions>& t2) {
753  return t1 * t2.template cast<Expression>();
754 }
755 
756 /// Transform<Expression> * Transform<double> => Transform<Expression>
757 template <int Dim, int LhsMode, int RhsMode, int LhsOptions, int RhsOptions>
759  const Eigen::Transform<double, Dim, LhsMode, LhsOptions>& t1,
760  const Eigen::Transform<Expression, Dim, RhsMode, RhsOptions>& t2) {
761  return t1.template cast<Expression>() * t2;
762 }
763 } // namespace symbolic
764 
765 /** Provides specialization of @c cond function defined in drake/common/cond.h
766  * file. This specialization is required to handle @c double to @c
767  * symbolic::Expression conversion so that we can write one such as <tt>cond(x >
768  * 0.0, 1.0, -1.0)</tt>.
769  */
770 template <typename... Rest>
771 symbolic::Expression cond(const symbolic::Formula& f_cond, double v_then,
772  Rest... rest) {
773  return if_then_else(f_cond, symbolic::Expression{v_then}, cond(rest...));
774 }
775 
776 /// Specializes common/dummy_value.h.
777 template <>
778 struct dummy_value<symbolic::Expression> {
780 };
781 
782 /** Specializes is_numeric to be false for symbolic::Expression type. */
783 template <>
784 struct is_numeric<symbolic::Expression> {
785  static constexpr bool value = false;
786 };
787 
788 /// Returns the symbolic expression's value() as a double.
789 ///
790 /// @throws If it is not possible to evaluate the symbolic expression with an
791 /// empty environment.
793 
794 } // namespace drake
795 
796 namespace std {
797 /* Provides std::hash<drake::symbolic::Expression>. */
798 template <>
799 struct hash<drake::symbolic::Expression>
800  : public drake::DefaultHash {};
801 #if defined(__GLIBCXX__)
802 // https://gcc.gnu.org/onlinedocs/libstdc++/manual/unordered_associative.html
803 template<>
804 struct __is_fast_hash<hash<drake::symbolic::Expression>> : std::false_type {};
805 #endif
806 
807 /* Provides std::less<drake::symbolic::Expression>. */
808 template <>
809 struct less<drake::symbolic::Expression> {
811  const drake::symbolic::Expression& rhs) const {
812  return lhs.Less(rhs);
813  }
814 };
815 
816 /* Provides std::equal_to<drake::symbolic::Expression>. */
817 template <>
818 struct equal_to<drake::symbolic::Expression> {
820  const drake::symbolic::Expression& rhs) const {
821  return lhs.EqualTo(rhs);
822  }
823 };
824 
825 } // namespace std
826 
827 #if !defined(DRAKE_DOXYGEN_CXX)
828 // Define Eigen traits needed for Matrix<drake::symbolic::Expression>.
829 namespace Eigen {
830 // Eigen scalar type traits for Matrix<drake::symbolic::Expression>.
831 template <>
832 struct NumTraits<drake::symbolic::Expression>
833  : GenericNumTraits<drake::symbolic::Expression> {
834  static inline int digits10() { return 0; }
835 };
836 
837 // Informs Eigen that Variable op Variable gets Expression.
838 template <typename BinaryOp>
839 struct ScalarBinaryOpTraits<drake::symbolic::Variable,
840  drake::symbolic::Variable, BinaryOp> {
841  enum { Defined = 1 };
842  typedef drake::symbolic::Expression ReturnType;
843 };
844 
845 // Informs Eigen that Variable op Expression gets Expression.
846 template <typename BinaryOp>
847 struct ScalarBinaryOpTraits<drake::symbolic::Variable,
848  drake::symbolic::Expression, BinaryOp> {
849  enum { Defined = 1 };
850  typedef drake::symbolic::Expression ReturnType;
851 };
852 
853 // Informs Eigen that Expression op Variable gets Expression.
854 template <typename BinaryOp>
855 struct ScalarBinaryOpTraits<drake::symbolic::Expression,
856  drake::symbolic::Variable, BinaryOp> {
857  enum { Defined = 1 };
858  typedef drake::symbolic::Expression ReturnType;
859 };
860 
861 // Informs Eigen that Variable op double gets Expression.
862 template <typename BinaryOp>
863 struct ScalarBinaryOpTraits<drake::symbolic::Variable, double, BinaryOp> {
864  enum { Defined = 1 };
865  typedef drake::symbolic::Expression ReturnType;
866 };
867 
868 // Informs Eigen that double op Variable gets Expression.
869 template <typename BinaryOp>
870 struct ScalarBinaryOpTraits<double, drake::symbolic::Variable, BinaryOp> {
871  enum { Defined = 1 };
872  typedef drake::symbolic::Expression ReturnType;
873 };
874 
875 // Informs Eigen that Expression op double gets Expression.
876 template <typename BinaryOp>
877 struct ScalarBinaryOpTraits<drake::symbolic::Expression, double, BinaryOp> {
878  enum { Defined = 1 };
879  typedef drake::symbolic::Expression ReturnType;
880 };
881 
882 // Informs Eigen that double op Expression gets Expression.
883 template <typename BinaryOp>
884 struct ScalarBinaryOpTraits<double, drake::symbolic::Expression, BinaryOp> {
885  enum { Defined = 1 };
886  typedef drake::symbolic::Expression ReturnType;
887 };
888 
889 } // namespace Eigen
890 #endif // !defined(DRAKE_DOXYGEN_CXX)
891 
892 namespace drake {
893 namespace symbolic {
894 /// Computes the Jacobian matrix J of the vector function @p f with respect to
895 /// @p vars. J(i,j) contains ∂f(i)/∂vars(j).
896 ///
897 /// For example, Jacobian([x * cos(y), x * sin(y), x^2], {x, y}) returns the
898 /// following 3x2 matrix:
899 /// <pre>
900 /// = |cos(y) -x * sin(y)|
901 /// |sin(y) x * cos(y)|
902 /// | 2 * x 0|
903 /// </pre>
904 ///
905 /// @pre {@p vars is non-empty}.
906 MatrixX<Expression> Jacobian(const Eigen::Ref<const VectorX<Expression>>& f,
907  const std::vector<Variable>& vars);
908 
909 /// Computes the Jacobian matrix J of the vector function @p f with respect to
910 /// @p vars. J(i,j) contains ∂f(i)/∂vars(j).
911 ///
912 /// @pre {@p vars is non-empty}.
913 MatrixX<Expression> Jacobian(const Eigen::Ref<const VectorX<Expression>>& f,
914  const Eigen::Ref<const VectorX<Variable>>& vars);
915 
916 /// Checks if two Eigen::Matrix<Expression> @p m1 and @p m2 are structurally
917 /// equal. That is, it returns true if and only if `m1(i, j)` is structurally
918 /// equal to `m2(i, j)` for all `i`, `j`.
919 template <typename DerivedA, typename DerivedB>
920 typename std::enable_if<
921  std::is_base_of<Eigen::MatrixBase<DerivedA>, DerivedA>::value &&
922  std::is_base_of<Eigen::MatrixBase<DerivedB>, DerivedB>::value &&
925  bool>::type
926 CheckStructuralEquality(const DerivedA& m1, const DerivedB& m2) {
927  EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(DerivedA, DerivedB);
928  DRAKE_DEMAND(m1.rows() == m2.rows() && m1.cols() == m2.cols());
929  // Note that std::equal_to<Expression> calls Expression::EqualTo which checks
930  // structural equality between two expressions.
931  return m1.binaryExpr(m2, std::equal_to<Expression>{}).all();
932 }
933 
934 } // namespace symbolic
935 
936 /*
937  * Determine if two EigenBase<> types are matrices (non-column-vectors) of
938  * Expressions and doubles, to then form an implicit formulas.
939  */
940 template <typename DerivedV, typename DerivedB>
942  : std::integral_constant<
943  bool, is_eigen_nonvector_of<DerivedV, symbolic::Expression>::value &&
944  is_eigen_nonvector_of<DerivedB, double>::value> {};
945 
946 /*
947  * Determine if two EigenBase<> types are vectors of Expressions and doubles
948  * that could make a formula.
949  */
950 template <typename DerivedV, typename DerivedB>
952  : std::integral_constant<
953  bool, is_eigen_vector_of<DerivedV, symbolic::Expression>::value &&
954  is_eigen_vector_of<DerivedB, double>::value> {};
955 
956 } // namespace drake
Symbolic expression representing a multiplication of powers.
Definition: symbolic_expression_cell.h:363
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:708
An adapter that forwards the HashAlgorithm::operator(data, length) function concept into a runtime-pr...
Definition: hash.h:226
bool is_sinh(const Expression &e)
Checks if e is a hyperbolic-sine expression.
Definition: symbolic_expression.cc:768
bool is_two(const Expression &e)
Checks if e is 2.0.
Definition: symbolic_expression.cc:748
Factory class to help build ExpressionMul expressions.
Definition: symbolic_expression_cell.h:395
Expression operator*(Expression lhs, const Expression &rhs)
Definition: symbolic_expression.cc:320
Symbolic expression representing atan2 function (arctangent function with two arguments).
Definition: symbolic_expression_cell.h:645
bool operator()(const drake::symbolic::Expression &lhs, const drake::symbolic::Expression &rhs) const
Definition: symbolic_expression.h:819
bool is_tanh(const Expression &e)
Checks if e is a hyperbolic-tangent expression.
Definition: symbolic_expression.cc:770
Factory class to help build ExpressionAdd expressions.
Definition: symbolic_expression_cell.h:292
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
std::string name
Definition: multilane_loader_test.cc:59
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:771
Definition: symbolic_expression.h:951
bool is_multiplication(const Expression &e)
Checks if e is a multiplication expression.
Definition: symbolic_expression.cc:752
Expression acos(const Expression &e)
Definition: symbolic_expression.cc:633
bool is_if_then_else(const Expression &e)
Checks if e is an if-then-else expression.
Definition: symbolic_expression.cc:775
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:795
bool operator()(const drake::symbolic::Expression &lhs, const drake::symbolic::Expression &rhs) const
Definition: symbolic_expression.h:810
bool is_max(const Expression &e)
Checks if e is a max expression.
Definition: symbolic_expression.cc:772
bool is_variable(const Expression &e)
Checks if e is a variable expression.
Definition: symbolic_expression.cc:750
Definition: automotive_demo.cc:89
bool is_uninterpreted_function(const Expression &e)
Checks if e is an uninterpreted-function expression.
Definition: symbolic_expression.cc:776
static Expression NaN()
Returns NaN (Not-a-Number).
Definition: symbolic_expression.cc:107
ostream & operator<<(ostream &os, const Environment &env)
Definition: symbolic_environment.cc:99
bool operator<(ExpressionKind k1, ExpressionKind k2)
Total ordering between ExpressionKinds.
Definition: symbolic_expression.cc:38
Expression sin(const Expression &e)
Definition: symbolic_expression.cc:598
Expression abs(const Expression &e)
Definition: symbolic_expression.cc:536
Symbolic expression representing logarithms.
Definition: symbolic_expression_cell.h:466
bool is_neg_one(const Expression &e)
Checks if e is -1.0.
Definition: symbolic_expression.cc:747
Expression & operator+=(Expression &lhs, const Expression &rhs)
Definition: symbolic_expression.cc:211
Expression asin(const Expression &e)
Definition: symbolic_expression.cc:623
Expression cosh(const Expression &e)
Definition: symbolic_expression.cc:668
Represents a symbolic form of a first-order logic formula.
Definition: symbolic_formula.h:113
Expression atan2(const Expression &e1, const Expression &e2)
Definition: symbolic_expression.cc:651
const Variable & get_variable(const Expression &e)
Returns the embedded variable in the variable expression e.
Definition: symbolic_expression.cc:783
const Expression & get_second_argument(const Expression &e)
Returns the second argument of the binary expression e.
Definition: symbolic_expression.cc:792
arctangent2 (atan2(y,x) = atan(y/x))
Symbolic expression representing min function.
Definition: symbolic_expression_cell.h:701
STL namespace.
Expression operator/(Expression lhs, const Expression &rhs)
Definition: symbolic_expression.cc:467
Symbolic expression representing square-root.
Definition: symbolic_expression_cell.h:515
absolute value function
Expression operator+(Expression lhs, const Expression &rhs)
Definition: symbolic_expression.cc:205
A scalar multi-variate polynomial, modeled after the msspoly in spotless.
Definition: polynomial.h:42
Represents the base class for binary expressions.
Definition: symbolic_expression_cell.h:152
Symbolic expression representing floor function.
Definition: symbolic_expression_cell.h:743
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:594
Symbolic expression representing sine function.
Definition: symbolic_expression_cell.h:552
bool is_sqrt(const Expression &e)
Checks if e is a square-root expression.
Definition: symbolic_expression.cc:759
bool is_tan(const Expression &e)
Checks if e is a tangent expression.
Definition: symbolic_expression.cc:763
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:118
Symbolic expression representing arccosine function.
Definition: symbolic_expression_cell.h:612
bool is_cosh(const Expression &e)
Checks if e is a hyperbolic-cosine expression.
Definition: symbolic_expression.cc:769
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:203
shared_ptr< ExpressionExp > to_exp(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2135
Symbolic expression representing exponentiation using the base of natural logarithms.
Definition: symbolic_expression_cell.h:501
friend void hash_append(HashAlgorithm &hasher, const Expression &item) noexcept
Implements the hash_append generic hashing concept.
Definition: symbolic_expression.h:295
Symbolic expression representing an uninterpreted function.
Definition: symbolic_expression_cell.h:788
Represents an abstract class which is the base of concrete symbolic-expression classes.
Definition: symbolic_expression_cell.h:43
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:659
Symbolic expression representing division.
Definition: symbolic_expression_cell.h:452
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:805
bool is_exp(const Expression &e)
Checks if e is an exp expression.
Definition: symbolic_expression.cc:758
Expression sinh(const Expression &e)
Definition: symbolic_expression.cc:660
bool is_one(const Expression &e)
Checks if e is 1.0.
Definition: symbolic_expression.cc:746
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:818
#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:716
int value
Definition: copyable_unique_ptr_test.cc:61
bool is_abs(const Expression &e)
Checks if e is an abs expression.
Definition: symbolic_expression.cc:757
Expression exp(const Expression &e)
Definition: symbolic_expression.cc:544
const Expression & get_first_argument(const Expression &e)
Returns the first argument of the binary expression e.
Definition: symbolic_expression.cc:789
bool is_log(const Expression &e)
Checks if e is a log expression.
Definition: symbolic_expression.cc:756
bool Less(const Expression &e) const
Provides lexicographical ordering between expressions.
Definition: symbolic_expression.cc:131
bool is_ceil(const Expression &e)
Definition: symbolic_expression.cc:773
Expression max(const Expression &e1, const Expression &e2)
Definition: symbolic_expression.cc:696
Symbolic expression representing hyperbolic tangent function.
Definition: symbolic_expression_cell.h:687
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:780
Expression min(const Expression &e1, const Expression &e2)
Definition: symbolic_expression.cc:684
Symbolic expression representing a variable.
Definition: symbolic_expression_cell.h:189
Expression tanh(const Expression &e)
Definition: symbolic_expression.cc:676
bool is_cos(const Expression &e)
Checks if e is a cosine expression.
Definition: symbolic_expression.cc:762
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:729
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:767
bool is_division(const Expression &e)
Checks if e is a division expression.
Definition: symbolic_expression.cc:755
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:396
double length
Definition: multilane_loader_test.cc:71
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
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:822
Symbolic expression representing arctangent function.
Definition: symbolic_expression_cell.h:630
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:798
Expression & operator-=(Expression &lhs, const Expression &rhs)
Definition: symbolic_expression.cc:269
const VectorXDecisionVariable * vars
Definition: nlopt_solver.cc:103
const string & get_uninterpreted_function_name(const Expression &e)
Returns the name of an uninterpreted-function expression e.
Definition: symbolic_expression.cc:810
Symbolic expression representing cosine function.
Definition: symbolic_expression_cell.h:566
Symbolic expression representing tangent function.
Definition: symbolic_expression_cell.h:580
shared_ptr< BinaryExpressionCell > to_binary(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2086
bool is_constant(const Expression &e)
Checks if e is a constant expression.
Definition: symbolic_expression.cc:741
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:568
Symbolic expression representing an addition which is a sum of products.
Definition: symbolic_expression_cell.h:260
Expression if_then_else(const Formula &f_cond, const Expression &e_then, const Expression &e_else)
Definition: symbolic_expression.cc:724
Represents the base class for unary expressions.
Definition: symbolic_expression_cell.h:120
A hashing functor, somewhat like std::hash.
Definition: hash.h:178
Expression tan(const Expression &e)
Definition: symbolic_expression.cc:615
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:765
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:737
Symbolic expression representing max function.
Definition: symbolic_expression_cell.h:715
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:873
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:926
bool is_addition(const Expression &e)
Checks if e is an addition expression.
Definition: symbolic_expression.cc:751
Expression cos(const Expression &e)
Definition: symbolic_expression.cc:606
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:552
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:757
bool is_atan(const Expression &e)
Checks if e is an arctangent expression.
Definition: symbolic_expression.cc:766
is_numeric is true for types that are on the real line.
Definition: number_traits.h:31
Definition: symbolic_expression.h:941
Expression operator-(Expression lhs, const Expression &rhs)
Definition: symbolic_expression.cc:263
Symbolic expression representing absolute value function.
Definition: symbolic_expression_cell.h:484
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:212
Expression atan(const Expression &e)
Definition: symbolic_expression.cc:643
double get_constant_in_multiplication(const Expression &e)
Returns the constant part of the multiplication expression e.
Definition: symbolic_expression.cc:802
const Expression & get_argument(const Expression &e)
Returns the argument in the unary expression e.
Definition: symbolic_expression.cc:786
bool is_floor(const Expression &e)
Definition: symbolic_expression.cc:774
Symbolic expression representing power function.
Definition: symbolic_expression_cell.h:533
bool is_sin(const Expression &e)
Checks if e is a sine expression.
Definition: symbolic_expression.cc:761
bool is_nan(const Expression &e)
Checks if e is NaN.
Definition: symbolic_expression.cc:749
bool is_zero(const Expression &e)
Checks if e is 0.0.
Definition: symbolic_expression.cc:745
Symbolic expression representing hyperbolic cosine function.
Definition: symbolic_expression_cell.h:673
Expression & operator*=(Expression &lhs, const Expression &rhs)
Definition: symbolic_expression.cc:326
Expression log(const Expression &e)
Definition: symbolic_expression.cc:526
const Formula & get_conditional_formula(const Expression &e)
Returns the conditional formula in the if-then-else expression e.
Definition: symbolic_expression.cc:814
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:473
bool is_pow(const Expression &e)
Checks if e is a power-function expression.
Definition: symbolic_expression.cc:760
bool is_asin(const Expression &e)
Checks if e is an arcsine expression.
Definition: symbolic_expression.cc:764
int data
Definition: value_test.cc:19