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"
27 #include "drake/common/hash.h"
30 #include "drake/common/symbolic.h"
31 
32 namespace drake {
33 
34 namespace symbolic {
35 
36 /** Kinds of symbolic expressions. */
37 enum class ExpressionKind {
38  Constant, ///< constant (double)
39  Var, ///< variable
40  Add, ///< addition (+)
41  Mul, ///< multiplication (*)
42  Div, ///< division (/)
43  Log, ///< logarithms
44  Abs, ///< absolute value function
45  Exp, ///< exponentiation
46  Sqrt, ///< square root
47  Pow, ///< power function
48  Sin, ///< sine
49  Cos, ///< cosine
50  Tan, ///< tangent
51  Asin, ///< arcsine
52  Acos, ///< arccosine
53  Atan, ///< arctangent
54  Atan2, ///< arctangent2 (atan2(y,x) = atan(y/x))
55  Sinh, ///< hyperbolic sine
56  Cosh, ///< hyperbolic cosine
57  Tanh, ///< hyperbolic tangent
58  Min, ///< min
59  Max, ///< max
60  Ceil, ///< ceil
61  Floor, ///< floor
62  IfThenElse, ///< if then else
63  NaN, ///< NaN
64  UninterpretedFunction, ///< Uninterpreted function
65  // TODO(soonho): add Integral
66 };
67 
68 /** Total ordering between ExpressionKinds. */
70 
71 class ExpressionCell; // In symbolic_expression_cell.h
72 class ExpressionConstant; // In symbolic_expression_cell.h
73 class ExpressionVar; // In symbolic_expression_cell.h
74 class UnaryExpressionCell; // In symbolic_expression_cell.h
75 class BinaryExpressionCell; // In symbolic_expression_cell.h
76 class ExpressionAdd; // In symbolic_expression_cell.h
77 class ExpressionMul; // In symbolic_expression_cell.h
78 class ExpressionDiv; // In symbolic_expression_cell.h
79 class ExpressionLog; // In symbolic_expression_cell.h
80 class ExpressionAbs; // In symbolic_expression_cell.h
81 class ExpressionExp; // In symbolic_expression_cell.h
82 class ExpressionSqrt; // In symbolic_expression_cell.h
83 class ExpressionPow; // In symbolic_expression_cell.h
84 class ExpressionSin; // In symbolic_expression_cell.h
85 class ExpressionCos; // In symbolic_expression_cell.h
86 class ExpressionTan; // In symbolic_expression_cell.h
87 class ExpressionAsin; // In symbolic_expression_cell.h
88 class ExpressionAcos; // In symbolic_expression_cell.h
89 class ExpressionAtan; // In symbolic_expression_cell.h
90 class ExpressionAtan2; // In symbolic_expression_cell.h
91 class ExpressionSinh; // In symbolic_expression_cell.h
92 class ExpressionCosh; // In symbolic_expression_cell.h
93 class ExpressionTanh; // In symbolic_expression_cell.h
94 class ExpressionMin; // In symbolic_expression_cell.h
95 class ExpressionMax; // In symbolic_expression_cell.h
96 class ExpressionCeiling; // In symbolic_expression_cell.h
97 class ExpressionFloor; // In symbolic_expression_cell.h
98 class ExpressionIfThenElse; // In symbolic_expression_cell.h
99 class ExpressionUninterpretedFunction; // In symbolic_expression_cell.h
100 class Formula; // In symbolic_formula.h
101 class Expression;
102 
103 // Substitution is a map from a Variable to a symbolic expression. It is used in
104 // Expression::Substitute and Formula::Substitute methods as an argument.
105 using Substitution =
106  std::unordered_map<Variable, Expression, hash_value<Variable>>;
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  /** Returns hash value. */
190  size_t get_hash() const;
191  /** Collects variables in expression. */
192  Variables GetVariables() const;
193 
194  /** Checks structural equality.
195  *
196  * Two expressions e1 and e2 are structurally equal when they have the same
197  * internal AST(abstract-syntax tree) representation. Please note that we can
198  * have two computationally (or extensionally) equivalent expressions which
199  * are not structurally equal. For example, consider:
200  *
201  * e1 = 2 * (x + y)
202  * e2 = 2x + 2y
203  *
204  * Obviously, we know that e1 and e2 are evaluated to the same value for all
205  * assignments to x and y. However, e1 and e2 are not structurally equal by
206  * the definition. Note that e1 is a multiplication expression
207  * (is_multiplication(e1) is true) while e2 is an addition expression
208  * (is_addition(e2) is true).
209  *
210  * One main reason we use structural equality in EqualTo is due to
211  * Richardson's Theorem. It states that checking ∀x. E(x) = F(x) is
212  * undecidable when we allow sin, asin, log, exp in E and F. Read
213  * https://en.wikipedia.org/wiki/Richardson%27s_theorem for details.
214  *
215  * Note that for polynomial cases, you can use Expand method and check if two
216  * polynomial expressions p1 and p2 are computationally equal. To do so, you
217  * check the following:
218  *
219  * (p1.Expand() - p2.Expand()).EqualTo(0).
220  */
221  bool EqualTo(const Expression& e) const;
222 
223  /** Provides lexicographical ordering between expressions.
224  This function is used as a compare function in map<Expression> and
225  set<Expression> via std::less<drake::symbolic::Expression>. */
226  bool Less(const Expression& e) const;
227 
228  /** Checks if this symbolic expression is convertible to Polynomial. */
229  bool is_polynomial() const;
230 
231  /** Returns a Polynomial representing this expression.
232  * Note that the ID of a variable is preserved in this translation.
233  * \pre{is_polynomial() is true.}
234  */
235  Polynomiald ToPolynomial() const;
236 
237  /** Evaluates under a given environment (by default, an empty environment).
238  * @throws std::runtime_error if NaN is detected during evaluation.
239  */
240  double Evaluate(const Environment& env = Environment{}) const;
241 
242  /** Partially evaluates this expression using an environment @p
243  * env. Internally, this method promotes @p env into a substitution
244  * (Variable → Expression) and call Evaluate::Substitute with it.
245  *
246  * @throws std::runtime_error if NaN is detected during evaluation.
247  */
248  Expression EvaluatePartial(const Environment& env) const;
249 
250  /** Expands out products and positive integer powers in expression. For
251  * example, `(x + 1) * (x - 1)` is expanded to `x^2 - 1` and `(x + y)^2` is
252  * expanded to `x^2 + 2xy + y^2`. Note that Expand applies recursively to
253  * sub-expressions. For instance, `sin(2 * (x + y))` is expanded to `sin(2x +
254  * 2y)`. It also simplifies "division by constant" cases. See
255  * "drake/common/test/symbolic_expansion_test.cc" to find the examples.
256  *
257  * @throws std::runtime_error if NaN is detected during expansion.
258  */
259  Expression Expand() const;
260 
261  /** Returns a copy of this expression replacing all occurrences of @p var
262  * with @p e.
263  * @throws std::runtime_error if NaN is detected during substitution.
264  */
265  Expression Substitute(const Variable& var, const Expression& e) const;
266 
267  /** Returns a copy of this expression replacing all occurrences of the
268  * variables in @p s with corresponding expressions in @p s. Note that the
269  * substitutions occur simultaneously. For example, (x / y).Substitute({{x,
270  * y}, {y, x}}) gets (y / x).
271  * @throws std::runtime_error if NaN is detected during substitution.
272  */
273  Expression Substitute(const Substitution& s) const;
274 
275  /** Differentiates this symbolic expression with respect to the variable @p
276  * var.
277  * @throws std::runtime_error if it is not differentiable.
278  */
279  Expression Differentiate(const Variable& x) const;
280 
281  /** Returns string representation of Expression. */
282  std::string to_string() const;
283 
284  /** Returns zero. */
285  static Expression Zero();
286  /** Returns one. */
287  static Expression One();
288  /** Returns Pi, the ratio of a circle’s circumference to its diameter. */
289  static Expression Pi();
290  /** Return e, the base of natural logarithms. */
291  static Expression E();
292  /** Returns NaN (Not-a-Number). */
293  static Expression NaN();
294 
295  friend Expression operator+(Expression lhs, const Expression& rhs);
296  // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
297  friend Expression& operator+=(Expression& lhs, const Expression& rhs);
298 
299  /** Provides prefix increment operator (i.e. ++x). */
300  Expression& operator++();
301  /** Provides postfix increment operator (i.e. x++). */
302  Expression operator++(int);
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 unary minus operator. */
309  friend Expression operator-(const Expression& e);
310  /** Provides prefix decrement operator (i.e. --x). */
311  Expression& operator--();
312  /** Provides postfix decrement operator (i.e. x--). */
313  Expression operator--(int);
314 
315  friend Expression operator*(Expression lhs, const Expression& rhs);
316  // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
317  friend Expression& operator*=(Expression& lhs, const Expression& rhs);
318 
319  friend Expression operator/(Expression lhs, const Expression& rhs);
320  // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
321  friend Expression& operator/=(Expression& lhs, const Expression& rhs);
322 
323  friend Expression log(const Expression& e);
324  friend Expression abs(const Expression& e);
325  friend Expression exp(const Expression& e);
326  friend Expression sqrt(const Expression& e);
327  friend Expression pow(const Expression& e1, const Expression& e2);
328  friend Expression sin(const Expression& e);
329  friend Expression cos(const Expression& e);
330  friend Expression tan(const Expression& e);
331  friend Expression asin(const Expression& e);
332  friend Expression acos(const Expression& e);
333  friend Expression atan(const Expression& e);
334  friend Expression atan2(const Expression& e1, const Expression& e2);
335  friend Expression sinh(const Expression& e);
336  friend Expression cosh(const Expression& e);
337  friend Expression tanh(const Expression& e);
338  friend Expression min(const Expression& e1, const Expression& e2);
339  friend Expression max(const Expression& e1, const Expression& e2);
340  friend Expression ceil(const Expression& e);
341  friend Expression floor(const Expression& e);
342 
343  /** Constructs if-then-else expression.
344 
345  @verbatim
346  if_then_else(cond, expr_then, expr_else)
347  @endverbatim
348 
349  The value returned by the above if-then-else expression is @p expr_then if
350  @p cond is evaluated to true. Otherwise, it returns @p expr_else.
351 
352  The semantics is similar to the C++'s conditional expression constructed by
353  its ternary operator, @c ?:. However, there is a key difference between the
354  C++'s conditional expression and our @c if_then_else expression in a way the
355  arguments are evaluated during the construction.
356 
357  - In case of the C++'s conditional expression, <tt> cond ? expr_then :
358  expr_else</tt>, the then expression @c expr_then (respectively, the else
359  expression @c expr_else) is \b only evaluated when the conditional
360  expression @c cond is evaluated to \b true (respectively, when @c cond is
361  evaluated to \b false).
362 
363  - In case of the symbolic expression, <tt>if_then_else(cond, expr_then,
364  expr_else)</tt>, however, \b both arguments @c expr_then and @c expr_else
365  are evaluated first and then passed to the @c if_then_else function.
366 
367  @note This function returns an \b expression and it is different from the
368  C++'s if-then-else \b statement.
369 
370  @note While it is still possible to define <tt> min, max, abs</tt> math
371  functions using @c if_then_else expression, it is highly \b recommended to
372  use the provided native definitions for them because it allows solvers to
373  detect specific math functions and to have a room for special
374  optimizations.
375 
376  @note More information about the C++'s conditional expression and ternary
377  operator is available at
378  http://en.cppreference.com/w/cpp/language/operator_other#Conditional_operator.
379  */
380  friend Expression if_then_else(const Formula& f_cond,
381  const Expression& e_then,
382  const Expression& e_else);
383  friend Expression uninterpreted_function(const std::string& name,
384  const Variables& vars);
385 
386  friend std::ostream& operator<<(std::ostream& os, const Expression& e);
387  friend void swap(Expression& a, Expression& b) { std::swap(a.ptr_, b.ptr_); }
388 
389  friend bool is_constant(const Expression& e);
390  friend bool is_variable(const Expression& e);
391  friend bool is_addition(const Expression& e);
392  friend bool is_multiplication(const Expression& e);
393  friend bool is_division(const Expression& e);
394  friend bool is_log(const Expression& e);
395  friend bool is_abs(const Expression& e);
396  friend bool is_exp(const Expression& e);
397  friend bool is_sqrt(const Expression& e);
398  friend bool is_pow(const Expression& e);
399  friend bool is_sin(const Expression& e);
400  friend bool is_cos(const Expression& e);
401  friend bool is_tan(const Expression& e);
402  friend bool is_asin(const Expression& e);
403  friend bool is_acos(const Expression& e);
404  friend bool is_atan(const Expression& e);
405  friend bool is_atan2(const Expression& e);
406  friend bool is_sinh(const Expression& e);
407  friend bool is_cosh(const Expression& e);
408  friend bool is_tanh(const Expression& e);
409  friend bool is_min(const Expression& e);
410  friend bool is_max(const Expression& e);
411  friend bool is_ceil(const Expression& e);
412  friend bool is_floor(const Expression& e);
413  friend bool is_if_then_else(const Expression& e);
414  friend bool is_uninterpreted_function(const Expression& e);
415 
416  // Note that the following cast functions are only for low-level operations
417  // and not exposed to the user of drake/common/symbolic_expression.h
418  // header. These functions are declared in
419  // drake/common/symbolic_expression_cell.h header.
420  friend std::shared_ptr<ExpressionConstant> to_constant(const Expression& e);
421  friend std::shared_ptr<ExpressionVar> to_variable(const Expression& e);
422  friend std::shared_ptr<UnaryExpressionCell> to_unary(const Expression& e);
423  friend std::shared_ptr<BinaryExpressionCell> to_binary(const Expression& e);
424  friend std::shared_ptr<ExpressionAdd> to_addition(const Expression& e);
425  friend std::shared_ptr<ExpressionMul> to_multiplication(const Expression& e);
426  friend std::shared_ptr<ExpressionDiv> to_division(const Expression& e);
427  friend std::shared_ptr<ExpressionLog> to_log(const Expression& e);
428  friend std::shared_ptr<ExpressionAbs> to_abs(const Expression& e);
429  friend std::shared_ptr<ExpressionExp> to_exp(const Expression& e);
430  friend std::shared_ptr<ExpressionSqrt> to_sqrt(const Expression& e);
431  friend std::shared_ptr<ExpressionPow> to_pow(const Expression& e);
432  friend std::shared_ptr<ExpressionSin> to_sin(const Expression& e);
433  friend std::shared_ptr<ExpressionCos> to_cos(const Expression& e);
434  friend std::shared_ptr<ExpressionTan> to_tan(const Expression& e);
435  friend std::shared_ptr<ExpressionAsin> to_asin(const Expression& e);
436  friend std::shared_ptr<ExpressionAcos> to_acos(const Expression& e);
437  friend std::shared_ptr<ExpressionAtan> to_atan(const Expression& e);
438  friend std::shared_ptr<ExpressionAtan2> to_atan2(const Expression& e);
439  friend std::shared_ptr<ExpressionSinh> to_sinh(const Expression& e);
440  friend std::shared_ptr<ExpressionCosh> to_cosh(const Expression& e);
441  friend std::shared_ptr<ExpressionTanh> to_tanh(const Expression& e);
442  friend std::shared_ptr<ExpressionMin> to_min(const Expression& e);
443  friend std::shared_ptr<ExpressionMax> to_max(const Expression& e);
444  friend std::shared_ptr<ExpressionCeiling> to_ceil(const Expression& e);
445  friend std::shared_ptr<ExpressionFloor> to_floor(const Expression& e);
446  friend std::shared_ptr<ExpressionIfThenElse> to_if_then_else(
447  const Expression& e);
448  friend std::shared_ptr<ExpressionUninterpretedFunction>
450 
451  friend class ExpressionAddFactory;
452  friend class ExpressionMulFactory;
453 
454  private:
455  explicit Expression(std::shared_ptr<ExpressionCell> ptr);
456 
457  std::shared_ptr<ExpressionCell> ptr_;
458 };
459 
460 Expression operator+(Expression lhs, const Expression& rhs);
461 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
462 Expression& operator+=(Expression& lhs, const Expression& rhs);
463 Expression operator-(Expression lhs, const Expression& rhs);
464 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
465 Expression& operator-=(Expression& lhs, const Expression& rhs);
467 Expression operator*(Expression lhs, const Expression& rhs);
468 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
469 Expression& operator*=(Expression& lhs, const Expression& rhs);
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 
474 Expression log(const Expression& e);
475 Expression abs(const Expression& e);
476 Expression exp(const Expression& e);
477 Expression sqrt(const Expression& e);
478 Expression pow(const Expression& e1, const Expression& e2);
479 Expression sin(const Expression& e);
480 Expression cos(const Expression& e);
481 Expression tan(const Expression& e);
482 Expression asin(const Expression& e);
483 Expression acos(const Expression& e);
484 Expression atan(const Expression& e);
485 Expression atan2(const Expression& e1, const Expression& e2);
486 Expression sinh(const Expression& e);
487 Expression cosh(const Expression& e);
488 Expression tanh(const Expression& e);
489 Expression min(const Expression& e1, const Expression& e2);
490 Expression max(const Expression& e1, const Expression& e2);
491 Expression if_then_else(const Formula& f_cond, const Expression& e_then,
492  const Expression& e_else);
493 
494 /** Constructs an uninterpreted-function expression with @p name and @p vars.
495  * An uninterpreted function is an opaque function that has no other property
496  * than its name and a set of its arguments. This is useful to applications
497  * where it is good enough to provide abstract information of a function without
498  * exposing full details. Declaring sparsity of a system is a typical example.
499  */
500 Expression uninterpreted_function(const std::string& name,
501  const Variables& vars);
502 void swap(Expression& a, Expression& b);
503 
504 std::ostream& operator<<(std::ostream& os, const Expression& e);
505 
506 /** Checks if @p e is a constant expression. */
507 bool is_constant(const Expression& e);
508 /** Checks if @p e is a constant expression representing @p v. */
509 bool is_constant(const Expression& e, double v);
510 /** Checks if @p e is 0.0. */
511 bool is_zero(const Expression& e);
512 /** Checks if @p e is 1.0. */
513 bool is_one(const Expression& e);
514 /** Checks if @p e is -1.0. */
515 bool is_neg_one(const Expression& e);
516 /** Checks if @p e is 2.0. */
517 bool is_two(const Expression& e);
518 /** Checks if @p e is NaN. */
519 bool is_nan(const Expression& e);
520 /** Checks if @p e is a variable expression. */
521 bool is_variable(const Expression& e);
522 /** Checks if @p e is an addition expression. */
523 bool is_addition(const Expression& e);
524 /** Checks if @p e is a multiplication expression. */
525 bool is_multiplication(const Expression& e);
526 /** Checks if @p e is a division expression. */
527 bool is_division(const Expression& e);
528 /** Checks if @p e is a log expression. */
529 bool is_log(const Expression& e);
530 /** Checks if @p e is an abs expression. */
531 bool is_abs(const Expression& e);
532 /** Checks if @p e is an exp expression. */
533 bool is_exp(const Expression& e);
534 /** Checks if @p e is a square-root expression. */
535 bool is_sqrt(const Expression& e);
536 /** Checks if @p e is a power-function expression. */
537 bool is_pow(const Expression& e);
538 /** Checks if @p e is a sine expression. */
539 bool is_sin(const Expression& e);
540 /** Checks if @p e is a cosine expression. */
541 bool is_cos(const Expression& e);
542 /** Checks if @p e is a tangent expression. */
543 bool is_tan(const Expression& e);
544 /** Checks if @p e is an arcsine expression. */
545 bool is_asin(const Expression& e);
546 /** Checks if @p e is an arccosine expression. */
547 bool is_acos(const Expression& e);
548 /** Checks if @p e is an arctangent expression. */
549 bool is_atan(const Expression& e);
550 /** Checks if @p e is an arctangent2 expression. */
551 bool is_atan2(const Expression& e);
552 /** Checks if @p e is a hyperbolic-sine expression. */
553 bool is_sinh(const Expression& e);
554 /** Checks if @p e is a hyperbolic-cosine expression. */
555 bool is_cosh(const Expression& e);
556 /** Checks if @p e is a hyperbolic-tangent expression. */
557 bool is_tanh(const Expression& e);
558 /** Checks if @p e is a min expression. */
559 bool is_min(const Expression& e);
560 /** Checks if @p e is a max expression. */
561 bool is_max(const Expression& e);
562 /** Checks if @p e is an if-then-else expression. */
563 bool is_if_then_else(const Expression& e);
564 /** Checks if @p e is an uninterpreted-function expression. */
566 
567 /** Returns the constant value of the constant expression @p e.
568  * \pre{@p e is a constant expression.}
569  */
570 double get_constant_value(const Expression& e);
571 /** Returns the embedded variable in the variable expression @p e.
572  * \pre{@p e is a variable expression.}
573  */
574 const Variable& get_variable(const Expression& e);
575 /** Returns the argument in the unary expression @p e.
576  * \pre{@p e is a unary expression.}
577  */
578 const Expression& get_argument(const Expression& e);
579 /** Returns the first argument of the binary expression @p e.
580  * \pre{@p e is a binary expression.}
581  */
582 const Expression& get_first_argument(const Expression& e);
583 /** Returns the second argument of the binary expression @p e.
584  * \pre{@p e is a binary expression.}
585  */
586 const Expression& get_second_argument(const Expression& e);
587 /** Returns the constant part of the addition expression @p e. For instance,
588  * given 7 + 2 * x + 3 * y, it returns 7.
589  * \pre{@p e is an addition expression.}
590  */
591 double get_constant_in_addition(const Expression& e);
592 /** Returns the map from an expression to its coefficient in the addition
593  * expression @p e. For instance, given 7 + 2 * x + 3 * y, the return value
594  * maps 'x' to 2 and 'y' to 3.
595  * \pre{@p e is an addition expression.}
596  */
597 const std::map<Expression, double>& get_expr_to_coeff_map_in_addition(
598  const Expression& e);
599 /** Returns the constant part of the multiplication expression @p e. For
600  * instance, given 7 * x^2 * y^3, it returns 7.
601  * \pre{@p e is a multiplication expression.}
602  */
604 /** Returns the map from a base expression to its exponent expression in the
605  * multiplication expression @p e. For instance, given 7 * x^2 * y^3 * z^x, the
606  * return value maps 'x' to 2, 'y' to 3, and 'z' to 'x'.
607  * \pre{@p e is a multiplication expression.}
608  */
609 const std::map<Expression, Expression>&
611 
612 /** Returns the name of an uninterpreted-function expression @p e.
613  * \pre{@p e is an uninterpreted-function expression.}
614  */
615 const std::string& get_uninterpreted_function_name(const Expression& e);
616 
617 // Matrix<Expression> * Matrix<double> => Matrix<Expression>
618 template <typename MatrixL, typename MatrixR>
619 typename std::enable_if<
620  std::is_base_of<Eigen::MatrixBase<MatrixL>, MatrixL>::value &&
621  std::is_base_of<Eigen::MatrixBase<MatrixR>, MatrixR>::value &&
624  Eigen::Matrix<Expression, MatrixL::RowsAtCompileTime,
625  MatrixR::ColsAtCompileTime>>::type
626 operator*(const MatrixL& lhs, const MatrixR& rhs) {
627  return lhs.template cast<Expression>() * rhs.template cast<Expression>();
628 }
629 
630 // Matrix<double> * Matrix<Expression> => Matrix<Expression>
631 template <typename MatrixL, typename MatrixR>
632 typename std::enable_if<
633  std::is_base_of<Eigen::MatrixBase<MatrixL>, MatrixL>::value &&
634  std::is_base_of<Eigen::MatrixBase<MatrixR>, MatrixR>::value &&
637  Eigen::Matrix<Expression, MatrixL::RowsAtCompileTime,
638  MatrixR::ColsAtCompileTime>>::type
639 operator*(const MatrixL& lhs, const MatrixR& rhs) {
640  return lhs.template cast<Expression>() * rhs.template cast<Expression>();
641 }
642 
643 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
644 Expression& operator+=(Expression& lhs, const Variable& rhs);
645 Expression operator+(const Variable& lhs, const Variable& rhs);
646 Expression operator+(Expression lhs, const Variable& rhs);
647 Expression operator+(const Variable& lhs, Expression rhs);
648 
649 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
650 Expression& operator-=(Expression& lhs, const Variable& rhs);
651 Expression operator-(const Variable& lhs, const Variable& rhs);
652 Expression operator-(Expression lhs, const Variable& rhs);
653 Expression operator-(const Variable& lhs, const Expression& rhs);
654 
655 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
656 Expression& operator*=(Expression& lhs, const Variable& rhs);
657 Expression operator*(const Variable& lhs, const Variable& rhs);
658 Expression operator*(Expression lhs, const Variable& rhs);
659 Expression operator*(const Variable& lhs, Expression rhs);
660 
661 // NOLINTNEXTLINE(runtime/references) per C++ standard signature.
662 Expression& operator/=(Expression& lhs, const Variable& rhs);
663 Expression operator/(const Variable& lhs, const Variable& rhs);
664 Expression operator/(Expression lhs, const Variable& rhs);
665 Expression operator/(const Variable& lhs, const Expression& rhs);
666 
667 Expression operator+(const Variable& var);
668 Expression operator-(const Variable& var);
669 
670 // Matrix<Expression> * Matrix<Variable> => Matrix<Expression>
671 template <typename MatrixL, typename MatrixR>
672 typename std::enable_if<
673  std::is_base_of<Eigen::MatrixBase<MatrixL>, MatrixL>::value &&
674  std::is_base_of<Eigen::MatrixBase<MatrixR>, MatrixR>::value &&
677  Eigen::Matrix<Expression, MatrixL::RowsAtCompileTime,
678  MatrixR::ColsAtCompileTime>>::type
679 operator*(const MatrixL& lhs, const MatrixR& rhs) {
680  return lhs * rhs.template cast<Expression>();
681 }
682 
683 // Matrix<Variable> * Matrix<Expression> => Matrix<Expression>
684 template <typename MatrixL, typename MatrixR>
685 typename std::enable_if<
686  std::is_base_of<Eigen::MatrixBase<MatrixL>, MatrixL>::value &&
687  std::is_base_of<Eigen::MatrixBase<MatrixR>, MatrixR>::value &&
689  std::is_same<typename MatrixR::Scalar, Expression>::value,
690  Eigen::Matrix<Expression, MatrixL::RowsAtCompileTime,
691  MatrixR::ColsAtCompileTime>>::type
692 operator*(const MatrixL& lhs, const MatrixR& rhs) {
693  return lhs.template cast<Expression>() * rhs;
694 }
695 
696 // Matrix<Variable> * Matrix<double> => Matrix<Expression>
697 template <typename MatrixL, typename MatrixR>
698 typename std::enable_if<
699  std::is_base_of<Eigen::MatrixBase<MatrixL>, MatrixL>::value &&
700  std::is_base_of<Eigen::MatrixBase<MatrixR>, MatrixR>::value &&
702  std::is_same<typename MatrixR::Scalar, double>::value,
703  Eigen::Matrix<Expression, MatrixL::RowsAtCompileTime,
704  MatrixR::ColsAtCompileTime>>::type
705 operator*(const MatrixL& lhs, const MatrixR& rhs) {
706  return lhs.template cast<Expression>() * rhs.template cast<Expression>();
707 }
708 
709 // Matrix<double> * Matrix<Variable> => Matrix<Expression>
710 template <typename MatrixL, typename MatrixR>
711 typename std::enable_if<
712  std::is_base_of<Eigen::MatrixBase<MatrixL>, MatrixL>::value &&
713  std::is_base_of<Eigen::MatrixBase<MatrixR>, MatrixR>::value &&
715  std::is_same<typename MatrixR::Scalar, Variable>::value,
716  Eigen::Matrix<Expression, MatrixL::RowsAtCompileTime,
717  MatrixR::ColsAtCompileTime>>::type
718 operator*(const MatrixL& lhs, const MatrixR& rhs) {
719  return lhs.template cast<Expression>() * rhs.template cast<Expression>();
720 }
721 
722 /// Transform<double> * Transform<Expression> => Transform<Expression>
723 template <int Dim, int LhsMode, int RhsMode, int LhsOptions, int RhsOptions>
724 auto operator*(const Eigen::Transform<Expression, Dim, LhsMode, LhsOptions>& t1,
725  const Eigen::Transform<double, Dim, RhsMode, RhsOptions>& t2) {
726  return t1 * t2.template cast<Expression>();
727 }
728 
729 /// Transform<Expression> * Transform<double> => Transform<Expression>
730 template <int Dim, int LhsMode, int RhsMode, int LhsOptions, int RhsOptions>
732  const Eigen::Transform<double, Dim, LhsMode, LhsOptions>& t1,
733  const Eigen::Transform<Expression, Dim, RhsMode, RhsOptions>& t2) {
734  return t1.template cast<Expression>() * t2;
735 }
736 } // namespace symbolic
737 
738 /** Provides specialization of @c cond function defined in drake/common/cond.h
739  * file. This specialization is required to handle @c double to @c
740  * symbolic::Expression conversion so that we can write one such as <tt>cond(x >
741  * 0.0, 1.0, -1.0)</tt>.
742  */
743 template <typename... Rest>
744 symbolic::Expression cond(const symbolic::Formula& f_cond, double v_then,
745  Rest... rest) {
746  return if_then_else(f_cond, symbolic::Expression{v_then}, cond(rest...));
747 }
748 
749 /// Specializes common/dummy_value.h.
750 template <>
751 struct dummy_value<symbolic::Expression> {
753 };
754 
755 /** Computes the hash value of a symbolic expression. */
756 template <>
757 struct hash_value<symbolic::Expression> {
758  size_t operator()(const symbolic::Expression& e) const {
759  return e.get_hash();
760  }
761 };
762 
763 /** Specializes is_numeric to be false for symbolic::Expression type. */
764 template <>
765 struct is_numeric<symbolic::Expression> {
766  static constexpr bool value = false;
767 };
768 } // namespace drake
769 
770 namespace std {
771 /* Provides std::less<drake::symbolic::Expression>. */
772 template <>
773 struct less<drake::symbolic::Expression> {
775  const drake::symbolic::Expression& rhs) const {
776  return lhs.Less(rhs);
777  }
778 };
779 
780 /* Provides std::equal_to<drake::symbolic::Expression>. */
781 template <>
782 struct equal_to<drake::symbolic::Expression> {
784  const drake::symbolic::Expression& rhs) const {
785  return lhs.EqualTo(rhs);
786  }
787 };
788 
789 } // namespace std
790 
791 #if !defined(DRAKE_DOXYGEN_CXX)
792 // Define Eigen traits needed for Matrix<drake::symbolic::Expression>.
793 namespace Eigen {
794 // Eigen scalar type traits for Matrix<drake::symbolic::Expression>.
795 template <>
796 struct NumTraits<drake::symbolic::Expression>
797  : GenericNumTraits<drake::symbolic::Expression> {
798  static inline int digits10() { return 0; }
799 };
800 
801 // Informs Eigen that Variable op Variable gets Expression.
802 template <typename BinaryOp>
803 struct ScalarBinaryOpTraits<drake::symbolic::Variable,
804  drake::symbolic::Variable, BinaryOp> {
805  enum { Defined = 1 };
806  typedef drake::symbolic::Expression ReturnType;
807 };
808 
809 // Informs Eigen that Variable op Expression gets Expression.
810 template <typename BinaryOp>
811 struct ScalarBinaryOpTraits<drake::symbolic::Variable,
812  drake::symbolic::Expression, BinaryOp> {
813  enum { Defined = 1 };
814  typedef drake::symbolic::Expression ReturnType;
815 };
816 
817 // Informs Eigen that Expression op Variable gets Expression.
818 template <typename BinaryOp>
819 struct ScalarBinaryOpTraits<drake::symbolic::Expression,
820  drake::symbolic::Variable, BinaryOp> {
821  enum { Defined = 1 };
822  typedef drake::symbolic::Expression ReturnType;
823 };
824 
825 // Informs Eigen that Variable op double gets Expression.
826 template <typename BinaryOp>
827 struct ScalarBinaryOpTraits<drake::symbolic::Variable, double, BinaryOp> {
828  enum { Defined = 1 };
829  typedef drake::symbolic::Expression ReturnType;
830 };
831 
832 // Informs Eigen that double op Variable gets Expression.
833 template <typename BinaryOp>
834 struct ScalarBinaryOpTraits<double, drake::symbolic::Variable, BinaryOp> {
835  enum { Defined = 1 };
836  typedef drake::symbolic::Expression ReturnType;
837 };
838 
839 // Informs Eigen that Expression op double gets Expression.
840 template <typename BinaryOp>
841 struct ScalarBinaryOpTraits<drake::symbolic::Expression, double, BinaryOp> {
842  enum { Defined = 1 };
843  typedef drake::symbolic::Expression ReturnType;
844 };
845 
846 // Informs Eigen that double op Expression gets Expression.
847 template <typename BinaryOp>
848 struct ScalarBinaryOpTraits<double, drake::symbolic::Expression, BinaryOp> {
849  enum { Defined = 1 };
850  typedef drake::symbolic::Expression ReturnType;
851 };
852 
853 } // namespace Eigen
854 #endif // !defined(DRAKE_DOXYGEN_CXX)
855 
856 namespace drake {
857 namespace symbolic {
858 /// Computes the Jacobian matrix J of the vector function @p f with respect to
859 /// @p vars. J(i,j) contains ∂f(i)/∂vars(j).
860 ///
861 /// For example, Jacobian([x * cos(y), x * sin(y), x^2], {x, y}) returns the
862 /// following 3x2 matrix:
863 /// <pre>
864 /// = |cos(y) -x * sin(y)|
865 /// |sin(y) x * cos(y)|
866 /// | 2 * x 0|
867 /// </pre>
868 ///
869 /// @pre {@p vars is non-empty}.
870 MatrixX<Expression> Jacobian(const Eigen::Ref<const VectorX<Expression>>& f,
871  const std::vector<Variable>& vars);
872 
873 /// Computes the Jacobian matrix J of the vector function @p f with respect to
874 /// @p vars. J(i,j) contains ∂f(i)/∂vars(j).
875 ///
876 /// @pre {@p vars is non-empty}.
877 MatrixX<Expression> Jacobian(const Eigen::Ref<const VectorX<Expression>>& f,
878  const Eigen::Ref<const VectorX<Variable>>& vars);
879 
880 /// Checks if two Eigen::Matrix<Expression> @p m1 and @p m2 are structurally
881 /// equal. That is, it returns true if and only if `m1(i, j)` is structurally
882 /// equal to `m2(i, j)` for all `i`, `j`.
883 template <typename DerivedA, typename DerivedB>
884 typename std::enable_if<
885  std::is_base_of<Eigen::MatrixBase<DerivedA>, DerivedA>::value &&
886  std::is_base_of<Eigen::MatrixBase<DerivedB>, DerivedB>::value &&
889  bool>::type
890 CheckStructuralEquality(const DerivedA& m1, const DerivedB& m2) {
891  EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(DerivedA, DerivedB);
892  DRAKE_DEMAND(m1.rows() == m2.rows() && m1.cols() == m2.cols());
893  // Note that std::equal_to<Expression> calls Expression::EqualTo which checks
894  // structural equality between two expressions.
895  return m1.binaryExpr(m2, std::equal_to<Expression>{}).all();
896 }
897 
898 } // namespace symbolic
899 
900 /*
901  * Determine if two EigenBase<> types are matrices (non-column-vectors) of
902  * Expressions and doubles, to then form an implicit formulas.
903  */
904 template <typename DerivedV, typename DerivedB>
906  : std::integral_constant<
907  bool, is_eigen_nonvector_of<DerivedV, symbolic::Expression>::value &&
908  is_eigen_nonvector_of<DerivedB, double>::value> {};
909 
910 /*
911  * Determine if two EigenBase<> types are vectors of Expressions and doubles
912  * that could make a formula.
913  */
914 template <typename DerivedV, typename DerivedB>
916  : std::integral_constant<
917  bool, is_eigen_vector_of<DerivedV, symbolic::Expression>::value &&
918  is_eigen_vector_of<DerivedB, double>::value> {};
919 
920 } // namespace drake
Symbolic expression representing a multiplication of powers.
Definition: symbolic_expression_cell.h:356
shared_ptr< ExpressionTanh > to_tanh(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2182
Expression ceil(const Expression &e)
Definition: symbolic_expression.cc:709
bool is_sinh(const Expression &e)
Checks if e is a hyperbolic-sine expression.
Definition: symbolic_expression.cc:769
bool is_two(const Expression &e)
Checks if e is 2.0.
Definition: symbolic_expression.cc:749
Factory class to help build ExpressionMul expressions.
Definition: symbolic_expression_cell.h:387
Expression operator*(Expression lhs, const Expression &rhs)
Definition: symbolic_expression.cc:321
Symbolic expression representing atan2 function (arctangent function with two arguments).
Definition: symbolic_expression_cell.h:637
bool operator()(const drake::symbolic::Expression &lhs, const drake::symbolic::Expression &rhs) const
Definition: symbolic_expression.h:783
bool is_tanh(const Expression &e)
Checks if e is a hyperbolic-tangent expression.
Definition: symbolic_expression.cc:771
Factory class to help build ExpressionAdd expressions.
Definition: symbolic_expression_cell.h:285
shared_ptr< ExpressionLog > to_log(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2084
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:2202
bool is_min(const Expression &e)
Checks if e is a min expression.
Definition: symbolic_expression.cc:772
Definition: symbolic_expression.h:915
bool is_multiplication(const Expression &e)
Checks if e is a multiplication expression.
Definition: symbolic_expression.cc:753
Expression acos(const Expression &e)
Definition: symbolic_expression.cc:634
bool is_if_then_else(const Expression &e)
Checks if e is an if-then-else expression.
Definition: symbolic_expression.cc:776
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:796
bool operator()(const drake::symbolic::Expression &lhs, const drake::symbolic::Expression &rhs) const
Definition: symbolic_expression.h:774
bool is_max(const Expression &e)
Checks if e is a max expression.
Definition: symbolic_expression.cc:773
bool is_variable(const Expression &e)
Checks if e is a variable expression.
Definition: symbolic_expression.cc:751
Definition: automotive_demo.cc:88
bool is_uninterpreted_function(const Expression &e)
Checks if e is an uninterpreted-function expression.
Definition: symbolic_expression.cc:777
This file contains traits for number (scalar) types.
static Expression NaN()
Returns NaN (Not-a-Number).
Definition: symbolic_expression.cc:104
ostream & operator<<(ostream &os, const Environment &env)
Definition: symbolic_environment.cc:83
bool operator<(ExpressionKind k1, ExpressionKind k2)
Total ordering between ExpressionKinds.
Definition: symbolic_expression.cc:37
Expression sin(const Expression &e)
Definition: symbolic_expression.cc:599
Expression abs(const Expression &e)
Definition: symbolic_expression.cc:537
Symbolic expression representing logarithms.
Definition: symbolic_expression_cell.h:458
bool is_neg_one(const Expression &e)
Checks if e is -1.0.
Definition: symbolic_expression.cc:748
Expression & operator+=(Expression &lhs, const Expression &rhs)
Definition: symbolic_expression.cc:212
Expression asin(const Expression &e)
Definition: symbolic_expression.cc:624
Expression cosh(const Expression &e)
Definition: symbolic_expression.cc:669
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:652
const Variable & get_variable(const Expression &e)
Returns the embedded variable in the variable expression e.
Definition: symbolic_expression.cc:784
const Expression & get_second_argument(const Expression &e)
Returns the second argument of the binary expression e.
Definition: symbolic_expression.cc:793
arctangent2 (atan2(y,x) = atan(y/x))
Symbolic expression representing min function.
Definition: symbolic_expression_cell.h:693
STL namespace.
Expression operator/(Expression lhs, const Expression &rhs)
Definition: symbolic_expression.cc:468
Symbolic expression representing square-root.
Definition: symbolic_expression_cell.h:507
absolute value function
Expression operator+(Expression lhs, const Expression &rhs)
Definition: symbolic_expression.cc:206
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:150
Symbolic expression representing floor function.
Definition: symbolic_expression_cell.h:735
shared_ptr< ExpressionDiv > to_division(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2075
Symbolic expression representing arcsine function.
Definition: symbolic_expression_cell.h:586
size_t get_hash() const
Returns hash value.
Definition: symbolic_expression.cc:79
Symbolic expression representing sine function.
Definition: symbolic_expression_cell.h:544
bool is_sqrt(const Expression &e)
Checks if e is a square-root expression.
Definition: symbolic_expression.cc:760
size_t operator()(const symbolic::Expression &e) const
Definition: symbolic_expression.h:758
bool is_tan(const Expression &e)
Checks if e is a tangent expression.
Definition: symbolic_expression.cc:764
Definition: autodiff_overloads.h:34
shared_ptr< ExpressionAtan > to_atan(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2149
shared_ptr< UnaryExpressionCell > to_unary(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2034
shared_ptr< ExpressionMul > to_multiplication(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2066
bool EqualTo(const Expression &e) const
Checks structural equality.
Definition: symbolic_expression.cc:115
Symbolic expression representing arccosine function.
Definition: symbolic_expression_cell.h:604
bool is_cosh(const Expression &e)
Checks if e is a hyperbolic-cosine expression.
Definition: symbolic_expression.cc:770
shared_ptr< ExpressionFloor > to_floor(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2211
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:2096
Symbolic expression representing exponentiation using the base of natural logarithms.
Definition: symbolic_expression_cell.h:493
Computes the hash value of v using std::hash.
Definition: hash.h:34
Symbolic expression representing an uninterpreted function.
Definition: symbolic_expression_cell.h:779
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:2057
Symbolic expression representing hyperbolic sine function.
Definition: symbolic_expression_cell.h:651
Symbolic expression representing division.
Definition: symbolic_expression_cell.h:444
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:806
bool is_exp(const Expression &e)
Checks if e is an exp expression.
Definition: symbolic_expression.cc:759
Expression sinh(const Expression &e)
Definition: symbolic_expression.cc:661
bool is_one(const Expression &e)
Checks if e is 1.0.
Definition: symbolic_expression.cc:747
shared_ptr< ExpressionCos > to_cos(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2121
ExpressionKind
Kinds of symbolic expressions.
Definition: symbolic_expression.h:37
shared_ptr< ExpressionConstant > to_constant(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2016
Expression floor(const Expression &e)
Definition: symbolic_expression.cc:717
#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
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:758
Expression exp(const Expression &e)
Definition: symbolic_expression.cc:545
const Expression & get_first_argument(const Expression &e)
Returns the first argument of the binary expression e.
Definition: symbolic_expression.cc:790
bool is_log(const Expression &e)
Checks if e is a log expression.
Definition: symbolic_expression.cc:757
Provides Drake&#39;s assertion implementation.
bool Less(const Expression &e) const
Provides lexicographical ordering between expressions.
Definition: symbolic_expression.cc:132
bool is_ceil(const Expression &e)
Definition: symbolic_expression.cc:774
Expression max(const Expression &e1, const Expression &e2)
Definition: symbolic_expression.cc:697
Symbolic expression representing hyperbolic tangent function.
Definition: symbolic_expression_cell.h:679
#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:2109
double get_constant_value(const Expression &e)
Returns the constant value of the constant expression e.
Definition: symbolic_expression.cc:781
Expression min(const Expression &e1, const Expression &e2)
Definition: symbolic_expression.cc:685
Symbolic expression representing a variable.
Definition: symbolic_expression_cell.h:186
Expression tanh(const Expression &e)
Definition: symbolic_expression.cc:677
bool is_cos(const Expression &e)
Checks if e is a cosine expression.
Definition: symbolic_expression.cc:763
shared_ptr< ExpressionVar > to_variable(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2025
Symbolic expression representing ceil function.
Definition: symbolic_expression_cell.h:721
shared_ptr< ExpressionUninterpretedFunction > to_uninterpreted_function(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2229
bool is_atan2(const Expression &e)
Checks if e is an arctangent2 expression.
Definition: symbolic_expression.cc:768
bool is_division(const Expression &e)
Checks if e is a division expression.
Definition: symbolic_expression.cc:756
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:387
shared_ptr< ExpressionSin > to_sin(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2115
shared_ptr< ExpressionAsin > to_asin(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2133
Symbolic expression representing arctangent function.
Definition: symbolic_expression_cell.h:622
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:799
Expression & operator-=(Expression &lhs, const Expression &rhs)
Definition: symbolic_expression.cc:270
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:811
Symbolic expression representing cosine function.
Definition: symbolic_expression_cell.h:558
Symbolic expression representing tangent function.
Definition: symbolic_expression_cell.h:572
shared_ptr< BinaryExpressionCell > to_binary(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2047
bool is_constant(const Expression &e)
Checks if e is a constant expression.
Definition: symbolic_expression.cc:742
shared_ptr< ExpressionAcos > to_acos(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2141
Expression pow(const Expression &e1, const Expression &e2)
Definition: symbolic_expression.cc:569
Symbolic expression representing an addition which is a sum of products.
Definition: symbolic_expression_cell.h:254
Expression if_then_else(const Formula &f_cond, const Expression &e_then, const Expression &e_else)
Definition: symbolic_expression.cc:725
Represents the base class for unary expressions.
Definition: symbolic_expression_cell.h:118
Expression tan(const Expression &e)
Definition: symbolic_expression.cc:616
shared_ptr< ExpressionAbs > to_abs(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2090
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:766
shared_ptr< ExpressionIfThenElse > to_if_then_else(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2220
shared_ptr< ExpressionMin > to_min(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2190
Expression uninterpreted_function(const string &name, const Variables &vars)
Constructs an uninterpreted-function expression with name and vars.
Definition: symbolic_expression.cc:738
Symbolic expression representing max function.
Definition: symbolic_expression_cell.h:707
Represents a set of variables.
Definition: symbolic_variables.h:30
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:862
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:890
bool is_addition(const Expression &e)
Checks if e is an addition expression.
Definition: symbolic_expression.cc:752
Expression cos(const Expression &e)
Definition: symbolic_expression.cc:607
shared_ptr< ExpressionTan > to_tan(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2127
shared_ptr< ExpressionMax > to_max(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2196
shared_ptr< ExpressionAtan2 > to_atan2(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2157
std::unordered_map< Variable, Expression, hash_value< Variable >> Substitution
Definition: symbolic_expression.h:106
Expression sqrt(const Expression &e)
Definition: symbolic_expression.cc:553
shared_ptr< ExpressionSqrt > to_sqrt(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2102
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:749
bool is_atan(const Expression &e)
Checks if e is an arctangent expression.
Definition: symbolic_expression.cc:767
is_numeric is true for types that are on the real line.
Definition: number_traits.h:31
Definition: symbolic_expression.h:905
Expression operator-(Expression lhs, const Expression &rhs)
Definition: symbolic_expression.cc:264
Symbolic expression representing absolute value function.
Definition: symbolic_expression_cell.h:476
shared_ptr< ExpressionCosh > to_cosh(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2174
Symbolic expression representing a constant.
Definition: symbolic_expression_cell.h:208
Provides public header files of Drake&#39;s symbolic library.
Expression atan(const Expression &e)
Definition: symbolic_expression.cc:644
double get_constant_in_multiplication(const Expression &e)
Returns the constant part of the multiplication expression e.
Definition: symbolic_expression.cc:803
const Expression & get_argument(const Expression &e)
Returns the argument in the unary expression e.
Definition: symbolic_expression.cc:787
bool is_floor(const Expression &e)
Definition: symbolic_expression.cc:775
Symbolic expression representing power function.
Definition: symbolic_expression_cell.h:525
bool is_sin(const Expression &e)
Checks if e is a sine expression.
Definition: symbolic_expression.cc:762
bool is_nan(const Expression &e)
Checks if e is NaN.
Definition: symbolic_expression.cc:750
bool is_zero(const Expression &e)
Checks if e is 0.0.
Definition: symbolic_expression.cc:746
Symbolic expression representing hyperbolic cosine function.
Definition: symbolic_expression_cell.h:665
Expression & operator*=(Expression &lhs, const Expression &rhs)
Definition: symbolic_expression.cc:327
Expression log(const Expression &e)
Definition: symbolic_expression.cc:527
shared_ptr< ExpressionSinh > to_sinh(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2166
Expression & operator/=(Expression &lhs, const Expression &rhs)
Definition: symbolic_expression.cc:474
bool is_pow(const Expression &e)
Checks if e is a power-function expression.
Definition: symbolic_expression.cc:761
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:765