Expression Manipulation (Docstrings)


sympy.physics.mechanics.msubs(expr, *sub_dicts, **kwargs)

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:

>>> 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)

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)
>>> msubs(sin(x)/tan(x), {x: 0}, smart=True)

It does this by first replacing all tan with sin/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.


sympy.physics.mechanics.find_dynamicsymbols(expression, exclude=None)

Find all dynamicsymbols in expression.

>>> from sympy.physics.mechanics import dynamicsymbols, find_dynamicsymbols
>>> x, y = dynamicsymbols('x, y')
>>> expr = x + x.diff()*y
>>> find_dynamicsymbols(expr)
set([x(t), y(t), Derivative(x(t), t)])

If the optional exclude kwarg is used, only dynamicsymbols not in the iterable exclude are returned.

>>> find_dynamicsymbols(expr, [x, y])
set([Derivative(x(t), t)])

Table Of Contents

Previous topic

Linearization (Docstrings)

Next topic

Printing (Docstrings)

This Page