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

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.

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: replacing "-1" in expressions?

I am currently working with Sympy 1.0 and Python 2.7, and my aim is to replace every numerical parameter in an expression with a symbol.
For example:
2 * x + 3 * y - 10 * z -> a * x + b * y - c * z
So far I used expression.atoms(Number) to obtain the list of numerical parameters, to later replace them either using .subs or by writing everything to string and using .replace. However, I am facing an issue with expressions such as:
expression = sympy.simpify("x - z")
Where -z is actually -1 * z if I analyze the expression as a tree...but -1 does not appear among the result of expression.atoms(Number), so I cannot replace it.
Even converting the expression to a string to then use .replace does not really help, as -1 * z is always written out as -z.
There is probably some solution that I overlooked, but so far I am stuck. Can you help me? Thank you in advance for your time.
In fact, it turns out I probably did a moronic error when I first tried to use .subs; as Patrick noted in the comments, .subs works perfectly in this case.

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.

Getting a better answer from sympy inverse laplace transform

Trying to compute the following lines I'm getting a realy complex result.
from sympy import *
s = symbols("s")
t = symbols("t")
h = 1/(s**3 + s**2/5 + s)
inverse_laplace_transform(h,s,t)
The result is the following:
(-(I*exp(-t/10)*sin(3*sqrt(11)*t/10) - exp(-t/10)*cos(3*sqrt(11)*t/10))*gamma(-3*sqrt(11)*I/5)*gamma(-1/10 - 3*sqrt(11)*I/10)/(gamma(9/10 - 3*sqrt(11)*I/10)*gamma(1 - 3*sqrt(11)*I/5)) + (I*exp(-t/10)*sin(3*sqrt(11)*t/10) + exp(-t/10)*cos(3*sqrt(11)*t/10))*gamma(3*sqrt(11)*I/5)*gamma(-1/10 + 3*sqrt(11)*I/10)/(gamma(9/10 + 3*sqrt(11)*I/10)*gamma(1 + 3*sqrt(11)*I/5)) + gamma(1/10 - 3*sqrt(11)*I/10)*gamma(1/10 + 3*sqrt(11)*I/10)/(gamma(11/10 - 3*sqrt(11)*I/10)*gamma(11/10 + 3*sqrt(11)*I/10)))*Heaviside(t)
However the answer should be simpler, Wolframalpha proves it.
Is there any way to simplify this result?
I tried a bit with this one and the way I could find a simpler solution is using something like:
from sympy import *
s = symbols("s")
t = symbols("t", positive=True)
h = 1/(s**3 + s**2/5 + s)
inverse_laplace_transform(h,s,t).evalf().simplify()
Notice that I define t as a positive variable, otherwise the sympy function returns a large term followed by the Heaviaside function. The result still contains many gamma functions that I could not reduce to the expression returned by Wolfram. Using evalf() some of those are converted to their numeric value and then after simplification you get a expression similar like the one in Wolfram but with floating numbers.
Unfortunately this part of Sympy is not quite mature. I also tried with Maxima and the result is quite close to the one in Wolfram. So it seems that Wolfram is not doing anything really special there.

class sympy.core.add.Add convert to float

This part of program on Python uses SymPy library.
As a result it's produces a value in sympy.core.add.Add class which is not capable with other parts of the program.
from sympy import *
x = symbols('x')
y = 1 - (0.1 * coeff1) / (x + 2) - sin(x) * (2 * x + coeff1)
yprime = y.diff(x)
How to convert sympy.core.add.Add class to ordinary float or int in order to use variable yprime in further calculations?
How about just ending your code with
yprime = float(yprime)
I think you can take a look at the documentation here http://docs.sympy.org/dev/modules/core.html#module-sympy.core.add
There are some functions starting with "as_" for the type sympy.core.add.Add, with which you can convert add.add into other formats and then change into float or int.
I just found the solution.
using yprime=float(yprime) won't work because yprime is an element of a class of numpy only.
But you can transform that element in order to make the python standard language be capable to read it.
try:
yprime=str(yprime)
#this will transform yprime to a string
yprime=eval(yprime)
#this will return a float or a int object so you can do calculations without any problem but make sure that the constants are well defined.

Categories

Resources