For example, if I run
import sympy x, y, z = sympy.symbols('x:z') f = sympy.exp(x + y) - sympy.sqrt(z)
is there any method of f
that I can use to get a list or tuple of sympy.Symbol
objects that the expression contains? I'd rather not have to parse srepr(f)
or parse downward through f.args
.
In this case, g.args[0].args[1].args[0]
gives me Symbol("z")
, while g.args[1].args[0].args
gives me the tuple (Symbol("x"), Symbol("y"))
, but obviously these are expression-specific.
To evaluate a numerical expression into a floating point number, use evalf . SymPy can evaluate floating point expressions to arbitrary precision. By default, 15 digits of precision are used, but you can pass any number as the argument to evalf .
What is SymPy? SymPy is a Python library that allows you to compute mathematical objects symbolically.
Basics. Exact SymPy expressions can be converted to floating-point approximations (decimal numbers) using either the . evalf() method or the N() function.
The subs() function in SymPy replaces all occurrences of first parameter with second. This function is useful if we want to evaluate a certain expression. For example, we want to calculate values of following expression by substituting a with 5.
You can use:
f.free_symbols
which will return a set of all free symbols.
Example:
>>> import sympy >>> x, y, z = sympy.symbols('x:z') >>> f = sympy.exp(x + y) - sympy.sqrt(z) >>> f.free_symbols set([x, z, y])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With