Expression Manipulation (Docstrings)¶
msubs¶
- sympy.physics.mechanics.msubs(expr, *sub_dicts, smart=False, **kwargs)[source]¶
A custom subs for use on expressions derived in physics.mechanics.
Traverses the expression tree once, performing the subs found in sub_dicts. Terms inside
Derivative
expressions are ignored:Examples
>>> from sympy.physics.mechanics import dynamicsymbols, msubs >>> x = dynamicsymbols('x') >>> msubs(x.diff() + x, {x: 1}) Derivative(x(t), t) + 1
Note that sub_dicts can be a single dictionary, or several dictionaries:
>>> x, y, z = dynamicsymbols('x, y, z') >>> sub1 = {x: 1, y: 2} >>> sub2 = {z: 3, x.diff(): 4} >>> msubs(x.diff() + x + y + z, sub1, sub2) 10
If smart=True (default False), also checks for conditions that may result in
nan
, but if simplified would yield a valid expression. For example:>>> from sympy import sin, tan >>> (sin(x)/tan(x)).subs(x, 0) nan >>> msubs(sin(x)/tan(x), {x: 0}, smart=True) 1
It does this by first replacing all
tan
withsin/cos
. Then each node is traversed. If the node is a fraction, subs is first evaluated on the denominator. If this results in 0, simplification of the entire fraction is attempted. Using this selective simplification, only subexpressions that result in 1/0 are targeted, resulting in faster performance.
find_dynamicsymbols¶
- sympy.physics.mechanics.find_dynamicsymbols(expression, exclude=None, reference_frame=None)[source]¶
Find all dynamicsymbols in expression.
- Parameters
expression : sympy expression
exclude : iterable of dynamicsymbols, optional
reference_frame : ReferenceFrame, optional
The frame with respect to which the dynamic symbols of the given vector is to be determined.
Explanation
If the optional
exclude
kwarg is used, only dynamicsymbols not in the iterableexclude
are returned. If we intend to apply this function on a vector, the optionalreference_frame
is also used to inform about the corresponding frame with respect to which the dynamic symbols of the given vector is to be determined.Examples
>>> from sympy.physics.mechanics import dynamicsymbols, find_dynamicsymbols >>> from sympy.physics.mechanics import ReferenceFrame >>> x, y = dynamicsymbols('x, y') >>> expr = x + x.diff()*y >>> find_dynamicsymbols(expr) {x(t), y(t), Derivative(x(t), t)} >>> find_dynamicsymbols(expr, exclude=[x, y]) {Derivative(x(t), t)} >>> a, b, c = dynamicsymbols('a, b, c') >>> A = ReferenceFrame('A') >>> v = a * A.x + b * A.y + c * A.z >>> find_dynamicsymbols(v, reference_frame=A) {a(t), b(t), c(t)}