Extracting the numbers out of a SymPy expression - python

How can I extract all the numerical values from a SymPy expression?
For example, for this expression: sin(a/pi + a) + 0.629116159212, I want pi, -1, and 0.629116159212.
I found the srepr function in SymPy, which returns for the example:
Add(sin(Add(Mul(Pow(pi, Integer(-1)), Symbol('a')), Symbol('a'))), Float('0.62911615921200004', precision=53))
How can I extract all the numbers from this, i.e., everything that is not a Symbol?

The method atoms returns a set of all atomic (i.e., unsplittable) components of an expression.
The attribute is_number tells you whether some expression (or atom) is a number.
Combined:
from sympy import sin, pi
from sympy.abc import a
expr = sin(a/pi + a) + 0.629116159212
numbers = {atom for atom in expr.atoms() if atom.is_number}
Now, if you need to preserve the count of appearances, things get a bit more complicated, since atoms returns a set.
Here, we additionally can use:
Alternative 1:
sympy.preorder_traversal (or postorder_traversal) which iterates through all subexpressions of an expression.
(Thanks to Oscar Benjamin and A.S. Meurer.)
Alternative2:
The method find of expressions, which returns all expressions matching some criterion.
The attribute is_Atom.
from sympy import sin, pi, preorder_traversal
from sympy.abc import a
expr = sin(a/pi + 1/a) + 0.629116159212
is_atomic_number = lambda expr: expr.is_Atom and expr.is_number
# Alternative 1:
[
subexpression
for subexpression in preorder_traversal(expr)
if is_atomic_number(subexpression)
]
# Alternative 2:
expr.find(is_atomic_number,group=True)

Related

How to apart Exponential function in python

I am trying to apart Exponential function in python.
import sympy as sym
from sympy.abc import t
from sympy import exp
u = (3*(exp(4*t) - 1)*exp(-4*t))/4
apart = sym.apart(u, t)
print(apart)
But i get the error:
exp(4*t) contains an element of the set of generators
it looks like exp() is confusing it. For a workaround
import sympy as sym
from sympy.abc import t,z
from sympy import exp
u = (3*(exp(4*t) - 1)*exp(-4*t))/4
expr = sym.apart(u.subs(exp(t),z), z)
expr = expr.subs(z,exp(t))
Which gives
Out[3]: 3/4 - 3*exp(-4*t)/4
Using 3.7 on conda
Your expression is a univariate, rational function in terms of exp(t):
>>> u.subs(exp(t),y)
3*(y**4 - 1)/(4*y**4)
>>> apart(_)
3/4 - 3/(4*y**4)
>>> _.subs(y, exp(t))
3/4 - 3*exp(-4*t)/4
But SymPy can handle such non-symbol generators so for such an expression sym.apart(u) would have given the same result as shown above. When you said the generator was t it detected the exp(t) and raised the error since an expression like t + exp(t) has two generators that depend on t.

SymPy prints function names instead of its value

I recently started learning IPython. I have a problem with Sympy library. It only prints the function instead of exact value.
import matplotlib.pyplot as plt
import numpy as nm
from sympy import *
x,y = symbols("x y")
expr = cos(x)
a = expr.subs(x,1)
print(a)
The output of above program is cos(1). But I want to print the exact value of cos(1).
First of all some nitpicking: “cos(1)” is the most concise way to represent the exact value of cos(1). Numerical representations like “0.54”, “0.540302”, or “0.54030230587” are not exact but are only accurate within the respective number of digits. The fact that cos(1) is not simply translated to one of those numerical approximations is one of the features of SymPy, as it ensures that something like simplify(acos(cos(1))) actually yields 1 and not 0.99999999.
That being said, there are of course purposes where you really need a numerical representation. In this case, you can use the class function n of SymPy expressions or the function N from SymPy, for example:
from sympy import cos, N
expr = cos(1)
print(expr.n()) # 0.540302305868140
print(N(expr)) # 0.540302305868140

Python Sympy Arbitrary Approximation to Arbitrary Sympy Expression?

I find myself wanting to use approxmations provided as part of the mpmath package, but getting confused on exactly what they are supposed to be doing:
http://docs.sympy.org/dev/modules/mpmath/calculus/approximation.html
What exactly is the difference between a sympy expression and a sympy.mpmath expression ?
If I want a taylor approximation to a symbolic expression without understanding what mpmath package is doing I can do the following:
#Imports
import sympy
import sympy.parsing
import sympy.parsing.sympy_parser
import Library_TaylorApproximation
#Create a sympy expression to approximate
ExampleStringExpression = 'sin(x)'
ExampleSympyExpression = sympy.parsing.sympy_parser.parse_expr(ExampleStringExpression)
#Create a taylor expantion sympy expression around the point x=0
SympyTaylorApproximation = sympy.series(
ExampleSympyExpression,
sympy.Symbol('x'),
1,
4,
).removeO()
#Cast the sympy expressions to python functions which can be evaluated:
VariableNames = [str(var) for var in SympyTaylorApproximation.free_symbols]
PythonFunctionOriginal = sympy.lambdify(VariableNames, ExampleSympyExpression)
PythonFunctionApproximation = sympy.lambdify(VariableNames, SympyTaylorApproximation)
#Evaluate the approximation and the original at a point:
print PythonFunctionOriginal(2)
print PythonFunctionApproximation(2)
#>>> 0.909297426826
#>>> 0.870987413961
However, if I try to do the same thing with mpmath based on the documentation:
TaylorCoefficients = sympy.mpmath.taylor(ExampleSympyExpression, 1, 4 )
print 'TaylorCoefficients', TaylorCoefficients
#>>> TypeError: 'sin' object is not callable
I can try to cram the python function in there (which is callable):
TaylorCoefficients = sympy.mpmath.taylor(PythonFunctionOriginal, 1, 4 )
print 'TaylorCoefficients', TaylorCoefficients
#>>> TaylorCoefficients [mpf('0.8414709848078965'), mpf('0.0'), mpf('0.0'), mpf('0.0'), mpf('-8.3694689805155739e+57')]
But the above does not make any sense, because I know that derivatives cannot be taken of a python function.
I can call the mpmath function sin:
TaylorCoefficients = sympy.mpmath.taylor(sympy.mpmath.sin, 1, 4 )
print 'TaylorCoefficients', TaylorCoefficients
#>>> TaylorCoefficients [mpf('0.8414709848078965'), mpf('0.54030230586813977'), mpf('-0.42073549240394825'), mpf('-0.090050384311356632'), mpf('0.035061291033662352')]
But then I cannot do manipulations on it the way I would want too -> like If I want
SinTimesCos = sympy.mpmath.sin*sympy.mpmath.cos
TaylorCoefficients = sympy.mpmath.taylor(SinTimesCos, 1, 4 )
print 'TaylorCoefficients', TaylorCoefficients
#>>> TypeError: unsupported operand type(s) for *: 'function' and 'function'
Exactly WHAT is an mpmath function ?
It is not a sympy expression, and it is also not a python function. How do I do manipulations on arbitrary expressions?
It would appear that I cannot take approximations of arbitrary sympy expressions in the documentation.
http://docs.sympy.org/dev/modules/mpmath/calculus/approximation.html
How do I take arbitrary approximations ( Pade / Cheby Chev / Fourier )
to arbitrary sympy expressions?
EDIT:
So an example of what I am looking for is the following approximation:
#Start with a sympy expression of (a, b, x)
expressionString = 'cos(a*x)*sin(b*x)*(x**2)'
expressionSympy = sympy.parsing.sympy_parser.parse_expr(expressionString)
#Do not want to decide on value of `a or b` in advance.
#Do want approximation with respect to x:
wantedSympyExpression = SympyChebyChev( expressionSympy, sympy.Symbol('x') )
Result could either be a list of coefficient expressions that are functions of a, and b:
wantedSympyExpressionCoefficients = [ Coef0Expression(a,b), Coef1Expression(a,b), ... , CoefNExpression(a,b)]
OR the result could be the entire sympy expression itself (which is itself a function of a, b):
wantedSympyExpression = Coef0Expression(a,b) + Coef1Expression(a,b) *(x**2) + ... + CoefNExpression(a,b) (x**N)
Note that a and b are not chosen in advance of performing the approximation.
mpmath functions are ordinary Python functions. They simply do their math in arbitrary-precision arithmetic.
But the above does not make any sense, because I know that derivatives cannot be taken of a python function.
You can't take the derivative symbolically, but you can compute an approximation of the derivative by evaluating the function several times and using numerical differentiation techniques. This is what sympy.mpmath.taylor does. Quoting the docs:
The coefficients are computed using high-order numerical differentiation. The function must be possible to evaluate to arbitrary precision.
If you have a SymPy expression and want to evaluate it to arbitrary precision, use evalf, like
sympy.sin(1).evalf(100)
You can use sin(x).evalf(100, subs={x:1}) to replace the x with 1 before evaluating. evalf uses mpmath under the hood, so this will give you the same result that mpmath would, but without having to use mpmath directly.

Collecting a fraction expression within a larger fraction (sympy)

I am using IPython (Anaconda distribution) with the sympy symbolic maths library.
I have the following expression:
t⋅(h + l)
───────────────────────
l⋅(h + l⋅sin(θ))⋅cos(θ)
I would like to rearrange this to get it in terms of (h/l) and (t/l):
(t/l)⋅((h/l)+1)
─────────────────────
((h/l)+sin(θ))⋅cos(θ)
This is quite easy to do by hand; just divide both sides of the fraction by l and rearrange.
So far I have had no luck with sympy's built in functions.
I have tried using expand followed by collect(expr,h/l), but it doesn't change the expression. I suspect this doesn't work because there are no h/l terms for it to collect in the first place.
How do I get sympy to do this?
Python code for the first expression to save you time:
t*(h + l)/(l*(h + l*sin(theta))*cos(theta))
Building on strubbly's idea:
In [2]: expr = t *(h +l )/(l *(h +l *sin (theta ))*cos (theta ))
In [3]: expr
Out[3]:
t*(h + l)
-------------------------------
l*(h + l*sin(theta))*cos(theta)
In [4]: repl1 = [x-h/l, y-t/l]
In [7]: repl2 = solve(repl1, t, l)
In [8]: repl2
Out[8]:
h h*y
{l: -, t: ---}
x x
In [9]: simplify(expr.subs(repl2)).subs({x: h/l, y: t/l})
Out[9]:
/h \
t*|- + 1|
\l /
-----------------------------
/h \
l*|- + sin(theta)|*cos(theta)
\l /
That is, introduce two variables x and y to replace h/l and t/l (In[4]), invert the equalities in order to get the replacement dictionary (In[7]). Replace, simplify (to get rid of the l), then substitute back the original values for x and y. One variable gets still simplified away.
One should tell .subs( ... ) not to evaluate the expression after substitution. I don't know whether that's currently supported.
So I used x = h/l and y = t/l and substituted. Then simplified. This gave me
x*(y + 1)/((y + sin(theta))*cos(theta))
Which I think is what you want. I can't see how to simplify "with respect to" h/l but this works...
Unless you glom the ratios together, the leading fraction will split across the division line. There are two ways to glom: with an UnevaluatedExpr and with "symbol trickery". First the UnevaluatedExpr:
>>> from sympy import UnevaluatedExpr as UE
>>> eq
t*(h + l)*cos(th)/(l*(h + l*sin(th)))
>>> factor_terms(_.subs(t, UE(t/l)*l).subs(h, UE(h/l)*l))
cos(th)*(sin(th) + h/l)**(-1)*(1 + h/l)*(t/l)
Notice how the order is not as you hoped. So now replace that UE with a symbol that looks like a UE:
>>> _.replace(lambda x: isinstance(x, UE), lambda x: Symbol(str(x)))
t/l*(h/l + 1)*cos(th)/(h/l + sin(th))
So that look like you wanted. The t/l and h/l are actually symbols with a complex name. (You can even use latex for the symbol names.)
I dont really know if you can uses regex, but if you can, you can use re.sub to replace all instances of h with (h/1). or if the expression is a string, you can use str.replace to do the same thing.

Sympy won't evaluate 2x but will evaluate x*2

I'm using Sympy's sympify function to simplify 2 expressions so I can compare them for equality.
For example:
expr1 = sympify("(2 * x) + (x + 10)")
expr2 = sympify("(x + 10) + (x * 2)")
if expr1 == expr2:
print "Congrats those are essentially the same!"
However when using the form 2x as apposed to x*2 i get a parse exception eg:
expr1 = sympify("2x + (x + 10)")
Is there any way I can get sympy to understand the 2x form ?
If not, is there any other library that will allow this form ?
Well, you could modify the sympy lexer (or parser / grammar / whatever).
You could also wrap it with a function that transformed your input strings for you, using something like this:
>>> import re
>>> expr = '2x + 1'
>>> re.sub(r"(\d+)(\w+)", r"(\1 * \2)", expr)
'(2 * x) + 1'
But ask yourself why this notation isn't there to begin with.
For example, all of these are valid python, and though it's been a long while since I messed with sympy, I bet they mean something besides multiplication in sympy too:
0x32 # hex for 50
5e-3 # 0.005
2j # 2 * sqrt(-1) (so that one *is* multiplication, but by 1j, not j!)
15L # 15 (L used to represent long integers in python)
And what does x2 mean? Is it a variable named x2 or does it mean (x * 2). I purposely left this case out of the regular expression above because it's so ambiguous.
The development version of SymPy has the ability to parse such expressions. See http://docs.sympy.org/dev/modules/parsing#sympy.parsing.sympy_parser.implicit_multiplication_application. It is still not enabled by default in sympify, because sympify only does very basic extensions to the Python syntax (i.e., wrapping of numeric literals and undefined names, and converting ^ to **). But there is an example there that shows how to use it.
Note that this currently also applies implicit function application as well. Probably the two functionalities should be split up.
EDIT: Those functions are being split up in a pull request.

Categories

Resources