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 
37 enum class ExpressionKind {
38  Constant,
39  Var,
40  Add,
41  Mul,
42  Div,
43  Log,
44  Abs,
45  Exp,
46  Sqrt,
47  Pow,
48  Sin,
49  Cos,
50  Tan,
51  Asin,
52  Acos,
53  Atan,
54  Atan2,
55  Sinh,
56  Cosh,
57  Tanh,
58  Min,
59  Max,
60  Ceil,
61  Floor,
62  IfThenElse,
63  NaN,
65  // TODO(soonho): add Integral
66 };
67 
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 
172 class Expression {
173  public:
175 
176 
177  Expression() { *this = Zero(); }
178 
180  // NOLINTNEXTLINE(runtime/explicit): This conversion is desirable.
181  Expression(double d);
185  // NOLINTNEXTLINE(runtime/explicit): This conversion is desirable.
186  Expression(const Variable& var);
188  ExpressionKind get_kind() const;
190  size_t get_hash() const;
192  Variables GetVariables() const;
193 
221  bool EqualTo(const Expression& e) const;
222 
226  bool Less(const Expression& e) const;
227 
229  bool is_polynomial() const;
230 
235  Polynomiald ToPolynomial() const;
236 
240  double Evaluate(const Environment& env = Environment{}) const;
241 
248  Expression EvaluatePartial(const Environment& env) const;
249 
259  Expression Expand() const;
260 
265  Expression Substitute(const Variable& var, const Expression& e) const;
266 
273  Expression Substitute(const Substitution& s) const;
274 
279  Expression Differentiate(const Variable& x) const;
280 
282  std::string to_string() const;
283 
285  static Expression Zero();
287  static Expression One();
289  static Expression Pi();
291  static Expression E();
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 
300  Expression& operator++();
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 
309  friend Expression operator-(const Expression& e);
311  Expression& operator--();
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 
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 
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 
507 bool is_constant(const Expression& e);
509 bool is_constant(const Expression& e, double v);
511 bool is_zero(const Expression& e);
513 bool is_one(const Expression& e);
515 bool is_neg_one(const Expression& e);
517 bool is_two(const Expression& e);
519 bool is_nan(const Expression& e);
521 bool is_variable(const Expression& e);
523 bool is_addition(const Expression& e);
525 bool is_multiplication(const Expression& e);
527 bool is_division(const Expression& e);
529 bool is_log(const Expression& e);
531 bool is_abs(const Expression& e);
533 bool is_exp(const Expression& e);
535 bool is_sqrt(const Expression& e);
537 bool is_pow(const Expression& e);
539 bool is_sin(const Expression& e);
541 bool is_cos(const Expression& e);
543 bool is_tan(const Expression& e);
545 bool is_asin(const Expression& e);
547 bool is_acos(const Expression& e);
549 bool is_atan(const Expression& e);
551 bool is_atan2(const Expression& e);
553 bool is_sinh(const Expression& e);
555 bool is_cosh(const Expression& e);
557 bool is_tanh(const Expression& e);
559 bool is_min(const Expression& e);
561 bool is_max(const Expression& e);
563 bool is_if_then_else(const Expression& e);
566 
570 double get_constant_value(const Expression& e);
574 const Variable& get_variable(const Expression& e);
578 const Expression& get_argument(const Expression& e);
582 const Expression& get_first_argument(const Expression& e);
586 const Expression& get_second_argument(const Expression& e);
591 double get_constant_in_addition(const Expression& e);
597 const std::map<Expression, double>& get_expr_to_coeff_map_in_addition(
598  const Expression& e);
609 const std::map<Expression, Expression>&
611 
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 
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 
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 
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 
750 template <>
751 struct dummy_value<symbolic::Expression> {
753 };
754 
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 
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 {
870 MatrixX<Expression> Jacobian(const Eigen::Ref<const VectorX<Expression>>& f,
871  const std::vector<Variable>& vars);
872 
877 MatrixX<Expression> Jacobian(const Eigen::Ref<const VectorX<Expression>>& f,
878  const Eigen::Ref<const VectorX<Variable>>& vars);
879 
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:347
shared_ptr< ExpressionTanh > to_tanh(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2180
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:378
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:628
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:276
shared_ptr< ExpressionLog > to_log(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2082
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:2200
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:449
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:684
STL namespace.
Expression operator/(Expression lhs, const Expression &rhs)
Definition: symbolic_expression.cc:468
Symbolic expression representing square-root.
Definition: symbolic_expression_cell.h:498
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:43
Represents the base class for binary expressions.
Definition: symbolic_expression_cell.h:141
Symbolic expression representing floor function.
Definition: symbolic_expression_cell.h:726
shared_ptr< ExpressionDiv > to_division(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2073
Symbolic expression representing arcsine function.
Definition: symbolic_expression_cell.h:577
size_t get_hash() const
Returns hash value.
Definition: symbolic_expression.cc:79
Symbolic expression representing sine function.
Definition: symbolic_expression_cell.h:535
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:30
shared_ptr< ExpressionAtan > to_atan(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2147
shared_ptr< UnaryExpressionCell > to_unary(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2032
shared_ptr< ExpressionMul > to_multiplication(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2064
bool EqualTo(const Expression &e) const
Checks structural equality.
Definition: symbolic_expression.cc:115
Symbolic expression representing arccosine function.
Definition: symbolic_expression_cell.h:595
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:2209
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:2094
Symbolic expression representing exponentiation using the base of natural logarithms.
Definition: symbolic_expression_cell.h:484
Computes the hash value of v using std::hash.
Definition: hash.h:36
int b
Definition: rgbd_camera.cc:91
Symbolic expression representing an uninterpreted function.
Definition: symbolic_expression_cell.h:770
Represents an abstract class which is the base of concrete symbolic-expression classes.
Definition: symbolic_expression_cell.h:34
shared_ptr< ExpressionAdd > to_addition(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2055
Symbolic expression representing hyperbolic sine function.
Definition: symbolic_expression_cell.h:642
Symbolic expression representing division.
Definition: symbolic_expression_cell.h:435
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:2119
int v
Definition: rgbd_camera_test.cc:165
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:2014
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:59
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:670
#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:47
shared_ptr< ExpressionPow > to_pow(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2107
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:177
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:2023
Symbolic expression representing ceil function.
Definition: symbolic_expression_cell.h:712
shared_ptr< ExpressionUninterpretedFunction > to_uninterpreted_function(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2227
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:174
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:2113
shared_ptr< ExpressionAsin > to_asin(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2131
Symbolic expression representing arctangent function.
Definition: symbolic_expression_cell.h:613
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:549
Symbolic expression representing tangent function.
Definition: symbolic_expression_cell.h:563
shared_ptr< BinaryExpressionCell > to_binary(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2045
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:2139
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:245
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:109
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:2088
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:2218
shared_ptr< ExpressionMin > to_min(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2188
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:698
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:2125
shared_ptr< ExpressionMax > to_max(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2194
shared_ptr< ExpressionAtan2 > to_atan2(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2155
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:2100
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:740
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:467
shared_ptr< ExpressionCosh > to_cosh(const shared_ptr< ExpressionCell > &expr_ptr)
Definition: symbolic_expression_cell.cc:2172
Symbolic expression representing a constant.
Definition: symbolic_expression_cell.h:199
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:516
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:656
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:2164
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