pydrake.symbolic

Symbolic variable, variables, monomial, expression, polynomial, and formula

class pydrake.symbolic.Expression
Differentiate(self: pydrake.symbolic.Expression, arg0: pydrake.symbolic.Variable) → pydrake.symbolic.Expression

Differentiates this symbolic expression with respect to the variable var.

Raises:RuntimeError if it is not differentiable.
EqualTo(self: pydrake.symbolic.Expression, arg0: pydrake.symbolic.Expression) → bool

Checks structural equality.

Two expressions e1 and e2 are structurally equal when they have the same internal AST(abstract-syntax tree) representation. Please note that we can have two computationally (or extensionally) equivalent expressions which are not structurally equal. For example, consider:

e1 = 2 * (x + y) e2 = 2x + 2y

Obviously, we know that e1 and e2 are evaluated to the same value for all assignments to x and y. However, e1 and e2 are not structurally equal by the definition. Note that e1 is a multiplication expression (is_multiplication(e1) is true) while e2 is an addition expression (is_addition(e2) is true).

One main reason we use structural equality in EqualTo is due to Richardson’s Theorem. It states that checking ∀x. E(x) = F(x) is undecidable when we allow sin, asin, log, exp in E and F. Read https://en.wikipedia.org/wiki/Richardson%27s_theorem for details.

Note that for polynomial cases, you can use Expand method and check if two polynomial expressions p1 and p2 are computationally equal. To do so, you check the following:

(p1.Expand() - p2.Expand()).EqualTo(0).

Evaluate(*args, **kwargs)

Overloaded function.

  1. Evaluate(self: pydrake.symbolic.Expression, env: Dict[pydrake.symbolic.Variable, float] = {}) -> float

Evaluates under a given environment (by default, an empty environment).

Raises:RuntimeError if NaN is detected during evaluation.
  1. Evaluate(self: pydrake.symbolic.Expression, arg0: Dict[pydrake.symbolic.Variable, float]) -> float

Evaluates under a given environment (by default, an empty environment).

Raises:RuntimeError if NaN is detected during evaluation.
EvaluatePartial(self: pydrake.symbolic.Expression, arg0: Dict[pydrake.symbolic.Variable, float]) → pydrake.symbolic.Expression

Partially evaluates this expression using an environment env. Internally, this method promotes env into a substitution (Variable → Expression) and call Evaluate::Substitute with it.

Raises:RuntimeError if NaN is detected during evaluation.
Expand(self: pydrake.symbolic.Expression) → pydrake.symbolic.Expression

Expands out products and positive integer powers in expression. For example, (x + 1) * (x - 1) is expanded to x^2 - 1 and (x + y)^2 is expanded to x^2 + 2xy + y^2. Note that Expand applies recursively to sub-expressions. For instance, sin(2 * (x + y)) is expanded to sin(2x + 2y). It also simplifies “division by constant” cases. See “drake/common/test/symbolic_expansion_test.cc” to find the examples.

Raises:RuntimeError if NaN is detected during expansion.
Jacobian(self: pydrake.symbolic.Expression, arg0: numpy.ndarray[object[m, 1]]) → numpy.ndarray[object[1, n]]

Let f be this Expression, computes a row vector of derivatives, [∂f/∂vars(0), ... , ∂f/∂vars(n-1)] with respect to the variables vars.

Substitute(*args, **kwargs)

Overloaded function.

  1. Substitute(self: pydrake.symbolic.Expression, arg0: pydrake.symbolic.Variable, arg1: pydrake.symbolic.Expression) -> pydrake.symbolic.Expression

Returns a copy of this expression replacing all occurrences of var with e.

Raises:RuntimeError if NaN is detected during substitution.
  1. Substitute(self: pydrake.symbolic.Expression, arg0: Dict[pydrake.symbolic.Variable, pydrake.symbolic.Expression]) -> pydrake.symbolic.Expression

Returns a copy of this expression replacing all occurrences of the variables in s with corresponding expressions in s. Note that the substitutions occur simultaneously. For example, (x / y).Substitute({{x, y}, {y, x}}) gets (y / x).

Raises:RuntimeError if NaN is detected during substitution.
__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: pydrake.symbolic.Expression) -> None

Default constructor. It constructs Zero().

  1. __init__(self: pydrake.symbolic.Expression, arg0: float) -> None

Constructs a constant.

  1. __init__(self: pydrake.symbolic.Expression, arg0: pydrake.symbolic.Variable) -> None

Constructs an expression from var.

Precondition:
var is neither a dummy nor a BOOLEAN variable.
arccos(self: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
arcsin(self: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
arctan2(self: pydrake.symbolic.Expression, arg0: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
ceil(self: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
cos(self: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
cosh(self: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
exp(self: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
floor(self: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
log(self: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
max(self: pydrake.symbolic.Expression, arg0: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
min(self: pydrake.symbolic.Expression, arg0: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
sin(self: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
sinh(self: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
sqrt(self: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
tan(self: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
tanh(self: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
to_string(self: pydrake.symbolic.Expression) → unicode

Returns string representation of Expression.

class pydrake.symbolic.Formula

Represents a symbolic form of a first-order logic formula.

It has the following grammar:

F := ⊥ | ⊤ | Var | E = E | E ≠ E | E > E | E ≥ E | E < E | E ≤ E
| E ∧ ... ∧ E | E ∨ ... ∨ E | ¬F | ∀ x₁, ..., xn. F

In the implementation, Formula is a simple wrapper including a shared pointer to FormulaCell class which is a super-class of different kinds of symbolic formulas (i.e. FormulaAnd, FormulaOr, FormulaEq). Note that it includes a shared pointer, not a unique pointer, to allow sharing sub-expressions.

Note

The sharing of sub-expressions is not yet implemented.

The following simple simplifications are implemented:

E1 = E2        ->  True    (if E1 and E2 are structurally equal)
E1 ≠ E2        ->  False   (if E1 and E2 are structurally equal)
E1 > E2        ->  False   (if E1 and E2 are structurally equal)
E1 ≥ E2        ->  True    (if E1 and E2 are structurally equal)
E1 < E2        ->  False   (if E1 and E2 are structurally equal)
E1 ≤ E2        ->  True    (if E1 and E2 are structurally equal)
F1 ∧ F2        ->  False   (if either F1 or F2 is False)
F1 ∨ F2        ->  True    (if either F1 or F2 is True)
¬(¬(F))        ->  F

We flatten nested conjunctions (or disjunctions) at the construction. A conjunction (resp. disjunction) takes a set of conjuncts (resp. disjuncts). Note that any duplicated conjunct/disjunct is removed. For example, both of f1 && (f2 && f1) and (f1 && f2) && f1 are flattened to f1 && f2 && f1 and simplified into f1 && f2. As a result, the two are identified as the same formula.

Note

Formula class has an explicit conversion operator to bool. It evaluates a symbolic formula under an empty environment. If a symbolic formula includes variables, the conversion operator throws an exception. This operator is only intended for third-party code doing things like (imag(SymbolicExpression(0)) == SymbolicExpression(0)) { ... }; that we found in Eigen3 codebase. In general, a user of this class should explicitly call Evaluate from within Drake for readability.

EqualTo(self: pydrake.symbolic.Formula, arg0: pydrake.symbolic.Formula) → bool

Checks structural equality.

Evaluate(self: pydrake.symbolic.Formula, arg0: Dict[pydrake.symbolic.Variable, float]) → bool

Evaluates under a given environment (by default, an empty environment).

Raises:
  • RuntimeError if a variable v is needed for an evaluation but
  • not provided by env.
False() → pydrake.symbolic.Formula

Symbolic formula representing false.

False_() → pydrake.symbolic.Formula

Symbolic formula representing false.

GetFreeVariables(self: pydrake.symbolic.Formula) → pydrake.symbolic.Variables

Gets free variables (unquantified variables).

Substitute(*args, **kwargs)

Overloaded function.

  1. Substitute(self: pydrake.symbolic.Formula, arg0: pydrake.symbolic.Variable, arg1: pydrake.symbolic.Expression) -> pydrake.symbolic.Formula

Returns a copy of this formula replacing all occurrences of var with e.

Raises:RuntimeError if NaN is detected during substitution.
  1. Substitute(self: pydrake.symbolic.Formula, arg0: pydrake.symbolic.Variable, arg1: pydrake.symbolic.Variable) -> pydrake.symbolic.Formula

Returns a copy of this formula replacing all occurrences of var with e.

Raises:RuntimeError if NaN is detected during substitution.
  1. Substitute(self: pydrake.symbolic.Formula, arg0: pydrake.symbolic.Variable, arg1: float) -> pydrake.symbolic.Formula

Returns a copy of this formula replacing all occurrences of var with e.

Raises:RuntimeError if NaN is detected during substitution.
  1. Substitute(self: pydrake.symbolic.Formula, arg0: Dict[pydrake.symbolic.Variable, pydrake.symbolic.Expression]) -> pydrake.symbolic.Formula

Returns a copy of this formula replacing all occurrences of the variables in s with corresponding expressions in s. Note that the substitutions occur simultaneously. For example, (x / y > 0).Substitute({{x, y}, {y, x}}) gets (y / x > 0).

Raises:RuntimeError if NaN is detected during substitution.
True() → pydrake.symbolic.Formula

Symbolic formula representing true.

True_() → pydrake.symbolic.Formula

Symbolic formula representing true.

__init__

x.__init__(…) initializes x; see help(type(x)) for signature

to_string(self: pydrake.symbolic.Formula) → unicode

Returns string representation of Formula.

pydrake.symbolic.Jacobian(arg0: numpy.ndarray[object[m, 1]], arg1: numpy.ndarray[object[m, 1]]) → numpy.ndarray[object[m, n]]

Let f be this Expression, computes a row vector of derivatives, [∂f/∂vars(0), ... , ∂f/∂vars(n-1)] with respect to the variables vars.

class pydrake.symbolic.Monomial
EqualTo(self: pydrake.symbolic.Monomial, arg0: pydrake.symbolic.Monomial) → bool
Evaluate(self: pydrake.symbolic.Monomial, arg0: Dict[pydrake.symbolic.Variable, float]) → float

Evaluates under a given environment env.

Raises:
  • ValueError exception if there is a variable in this monomial whose
  • assignment is not provided by env.
GetVariables(self: pydrake.symbolic.Monomial) → pydrake.symbolic.Variables

Returns the set of variables in this monomial.

ToExpression(self: pydrake.symbolic.Monomial) → pydrake.symbolic.Expression

Returns a symbolic expression representing this monomial.

__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: pydrake.symbolic.Monomial, arg0: pydrake.symbolic.Variable) -> None

Constructs a Monomial from var.

  1. __init__(self: pydrake.symbolic.Monomial, arg0: pydrake.symbolic.Variable, arg1: int) -> None

Constructs a Monomial from var and exponent.

  1. __init__(self: pydrake.symbolic.Monomial, arg0: Dict[pydrake.symbolic.Variable, int]) -> None

Constructs a Monomial from powers.

Raises:RuntimeError if powers includes a negative exponent.
degree(self: pydrake.symbolic.Monomial, arg0: pydrake.symbolic.Variable) → int

Returns the degree of this Monomial in a variable v.

get_powers(self: pydrake.symbolic.Monomial) → Dict[pydrake.symbolic.Variable, int]

Returns the internal representation of Monomial, the map from a base (Variable) to its exponent (int).

pow_in_place(self: pydrake.symbolic.Monomial, arg0: int) → pydrake.symbolic.Monomial

Returns this monomial raised to p.

Raises:RuntimeError if p is negative.
total_degree(self: pydrake.symbolic.Monomial) → int

Returns the total degree of this Monomial.

pydrake.symbolic.MonomialBasis(*args, **kwargs)

Overloaded function.

  1. MonomialBasis(arg0: numpy.ndarray[object[m, 1]], arg1: int) -> numpy.ndarray[object[m, 1]]

Returns all monomials up to a given degree under the graded reverse lexicographic order. Note that graded reverse lexicographic order uses the total order among Variable which is based on a variable’s unique ID. For example, for a given variable ordering x > y > z, MonomialBasis({x, y, z}, 2) returns a column vector [x^2, xy, y^2, xz, yz, z^2, x, y, z, 1].

Precondition:
vars is a non-empty set.
Precondition:
degree is a non-negative integer.
  1. MonomialBasis(arg0: pydrake.symbolic.Variables, arg1: int) -> numpy.ndarray[object[m, 1]]

Returns all monomials up to a given degree under the graded reverse lexicographic order. Note that graded reverse lexicographic order uses the total order among Variable which is based on a variable’s unique ID. For example, for a given variable ordering x > y > z, MonomialBasis({x, y, z}, 2) returns a column vector [x^2, xy, y^2, xz, yz, z^2, x, y, z, 1].

Precondition:
vars is a non-empty set.
Precondition:
degree is a non-negative integer.
class pydrake.symbolic.Polynomial

Represents symbolic polynomials. A symbolic polynomial keeps a mapping from a monomial of indeterminates to its coefficient in a symbolic expression.

A polynomial p has to satisfy an invariant such that p.decision_variables() p.indeterminates() = . We have CheckInvariant() method to check the invariant.

Note that arithmetic operations (+,-,*) between a Polynomial and a Variable are not provided. The problem is that Variable class has no intrinsic information if a variable is a decision variable or an indeterminate while we need this information to perform arithmetic operations over Polynomials.

AddProduct(self: pydrake.symbolic.Polynomial, arg0: pydrake.symbolic.Expression, arg1: pydrake.symbolic.Monomial) → pydrake.symbolic.Polynomial

Adds coeff * m to this polynomial.

Degree(self: pydrake.symbolic.Polynomial, arg0: pydrake.symbolic.Variable) → int

Returns the highest degree of this polynomial in a variable v.

Differentiate(self: pydrake.symbolic.Polynomial, arg0: pydrake.symbolic.Variable) → pydrake.symbolic.Polynomial

Differentiates this polynomial with respect to the variable x. Note that a variable x can be either a decision variable or an indeterminate.

EqualTo(self: pydrake.symbolic.Polynomial, arg0: pydrake.symbolic.Polynomial) → bool

Returns true if this polynomial and p are structurally equal.

Evaluate(self: pydrake.symbolic.Polynomial, arg0: Dict[pydrake.symbolic.Variable, float]) → float

Evaluates this polynomial under a given environment env.

Raises:
  • ValueError if there is a variable in this polynomial whose
  • assignment is not provided by env.
Jacobian(self: pydrake.symbolic.Polynomial, arg0: numpy.ndarray[object[m, 1]]) → numpy.ndarray[object[1, n]]

Computes the Jacobian matrix J of the polynomial with respect to vars. J(0,i) contains ∂f/∂vars(i).

ToExpression(self: pydrake.symbolic.Polynomial) → pydrake.symbolic.Expression

Returns an equivalent symbolic expression of this polynomial.

TotalDegree(self: pydrake.symbolic.Polynomial) → int

Returns the total degree of this polynomial.

__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: pydrake.symbolic.Polynomial) -> None

Constructs a zero polynomial.

  1. __init__(self: pydrake.symbolic.Polynomial, arg0: Dict[pydrake.symbolic.Monomial, pydrake.symbolic.Expression]) -> None

Constructs a polynomial from a map, Monomial → Expression.

  1. __init__(self: pydrake.symbolic.Polynomial, arg0: pydrake.symbolic.Monomial) -> None

Constructs a polynomial from a monomial m. Note that all variables in m are considered as indeterminates.

  1. __init__(self: pydrake.symbolic.Polynomial, arg0: pydrake.symbolic.Expression) -> None

Constructs a polynomial from an expression e. Note that all variables in e are considered as indeterminates.

Raises:RuntimeError if e is not a polynomial.
  1. __init__(self: pydrake.symbolic.Polynomial, arg0: pydrake.symbolic.Expression, arg1: pydrake.symbolic.Variables) -> None

Constructs a polynomial from an expression e by decomposing it with respect to indeterminates.

Note

It collects the intersection of the variables appeared in e and the provided indeterminates.

Raises:RuntimeError if e is not a polynomial in indeterminates.
  1. __init__(self: pydrake.symbolic.Polynomial, arg0: pydrake.symbolic.Expression, arg1: numpy.ndarray[object[m, 1]]) -> None

Constructs a polynomial from an expression e by decomposing it with respect to indeterminates.

Note

It collects the intersection of the variables appeared in e and the provided indeterminates.

Raises:RuntimeError if e is not a polynomial in indeterminates.
decision_variables(self: pydrake.symbolic.Polynomial) → pydrake.symbolic.Variables

Returns the decision variables of this polynomial.

indeterminates(self: pydrake.symbolic.Polynomial) → pydrake.symbolic.Variables

Returns the indeterminates of this polynomial.

monomial_to_coefficient_map(self: pydrake.symbolic.Polynomial) → Dict[pydrake.symbolic.Monomial, pydrake.symbolic.Expression]

Returns the mapping from a Monomial to its corresponding coefficient of this polynomial.

class pydrake.symbolic.Variable

Represents a symbolic variable.

EqualTo(self: pydrake.symbolic.Variable, arg0: pydrake.symbolic.Variable) → bool

Checks the equality of two variables based on their ID values.

__init__(self: pydrake.symbolic.Variable, arg0: unicode) → None

Constructs a default value. This overload is used by Eigen when EIGEN_INITIALIZE_MATRICES_BY_ZERO is enabled.

get_id(self: pydrake.symbolic.Variable) → int
class pydrake.symbolic.Variables

Represents a set of variables.

This class is based on std::set<Variable>. The intent is to add things that we need including set-union (Variables::insert, operator+, operator+=), set-minus (Variables::erase, operator-, operator-=), and subset/superset checking functions (Variables::IsSubsetOf, Variables::IsSupersetOf, Variables::IsStrictSubsetOf, Variables::IsStrictSupersetOf).

EqualTo(self: pydrake.symbolic.Variables, arg0: pydrake.symbolic.Variables) → bool
IsStrictSubsetOf(self: pydrake.symbolic.Variables, arg0: pydrake.symbolic.Variables) → bool

Return true if vars is a strict subset of the Variables.

IsStrictSupersetOf(self: pydrake.symbolic.Variables, arg0: pydrake.symbolic.Variables) → bool

Return true if vars is a strict superset of the Variables.

IsSubsetOf(self: pydrake.symbolic.Variables, arg0: pydrake.symbolic.Variables) → bool

Return true if vars is a subset of the Variables.

IsSupersetOf(self: pydrake.symbolic.Variables, arg0: pydrake.symbolic.Variables) → bool

Return true if vars is a superset of the Variables.

__init__(*args, **kwargs)

Overloaded function.

  1. __init__(self: pydrake.symbolic.Variables) -> None

Default constructor.

  1. __init__(self: pydrake.symbolic.Variables, arg0: numpy.ndarray[object[m, 1]]) -> None

Constructs from an Eigen vector of variables.

empty(self: pydrake.symbolic.Variables) → bool
erase(*args, **kwargs)

Overloaded function.

  1. erase(self: pydrake.symbolic.Variables, arg0: pydrake.symbolic.Variable) -> int

Erases key from a set. Return number of erased elements (0 or 1).

  1. erase(self: pydrake.symbolic.Variables, arg0: pydrake.symbolic.Variables) -> int

Erases variables in vars from a set. Return number of erased elements ([0, vars.size()]).

include(self: pydrake.symbolic.Variables, arg0: pydrake.symbolic.Variable) → bool

Return true if key is included in the Variables.

insert(*args, **kwargs)

Overloaded function.

  1. insert(self: pydrake.symbolic.Variables, arg0: pydrake.symbolic.Variable) -> None

Inserts a variable var into a set.

  1. insert(self: pydrake.symbolic.Variables, arg0: pydrake.symbolic.Variables) -> None

Inserts variables in [first, last) into a set.

size(self: pydrake.symbolic.Variables) → int

Returns the number of elements.

to_string(self: pydrake.symbolic.Variables) → unicode

Returns string representation of Variables.

pydrake.symbolic.abs(arg0: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
pydrake.symbolic.acos(arg0: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
pydrake.symbolic.asin(arg0: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
pydrake.symbolic.atan(arg0: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
pydrake.symbolic.atan2(arg0: pydrake.symbolic.Expression, arg1: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
pydrake.symbolic.ceil(arg0: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
pydrake.symbolic.cos(arg0: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
pydrake.symbolic.cosh(arg0: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
pydrake.symbolic.exp(arg0: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
pydrake.symbolic.floor(arg0: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
pydrake.symbolic.if_then_else(arg0: drake::symbolic::Formula, arg1: pydrake.symbolic.Expression, arg2: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
pydrake.symbolic.intersect(arg0: pydrake.symbolic.Variables, arg1: pydrake.symbolic.Variables) → pydrake.symbolic.Variables
pydrake.symbolic.log(arg0: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
pydrake.symbolic.logical_and(*formulas)
pydrake.symbolic.logical_not(arg0: pydrake.symbolic.Formula) → pydrake.symbolic.Formula
pydrake.symbolic.logical_or(*formulas)
pydrake.symbolic.max(arg0: pydrake.symbolic.Expression, arg1: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
pydrake.symbolic.min(arg0: pydrake.symbolic.Expression, arg1: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
pydrake.symbolic.pow(arg0: pydrake.symbolic.Expression, arg1: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
pydrake.symbolic.sin(arg0: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
pydrake.symbolic.sinh(arg0: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
pydrake.symbolic.sqrt(arg0: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
pydrake.symbolic.tan(arg0: pydrake.symbolic.Expression) → pydrake.symbolic.Expression
pydrake.symbolic.tanh(arg0: pydrake.symbolic.Expression) → pydrake.symbolic.Expression