>>>from sympy.parsing.sympy_parser import (parse_expr, ... standard_transformations, function_exponentiation)
>>> transformations = standard_transformations + (function_exponentiation,)
>>>parse= parse_expr('2x', transformations=transformations)
parse = parse_expr("2x", transformations=transformations)
>>> parse.coeff("x",0)
2
>>> parse.coeff("x")
2
>>> parse = parse_expr("2x+5", transformations=transformations)
>>> parse.coeff("x")
2
>>> parse.coeff("x",0)
5
I am quite new to python and sympy.
The problem here is that any time I want to get the constant 0 it returns the coefficient of x. But this doesn't happen when the constant is not zero(shown in the second equation). I am trying to use this to solve linear equations in which I don't know the user input. But it keeps giving me a wrong answer when there is no constant attached after x.
There is some discussion on Github: https://github.com/sympy/sympy/issues/5657
One way to do it is to convert to a polynomial:
>>> (2*x + 3).as_poly()
Poly(2*x + 3, x, domain='ZZ')
>>> (2*x + 3).as_poly().nth(0)
3
>>> (2*x + 3).as_poly().nth(1)
2
>>> (2*x).as_poly().nth(0)
0
>>> (2*x).as_poly().nth(1)
2
Unfortunately converting to a Poly first is slower.
Related
Hello wonderful people,
I am building a physics model for some project. I have found a nice equation for my interest variable, but I would like to be able to solve the problem repeatedly with different parameters. What I would like to do is to save my equation as an object in a file (using pickle for example), then loading it at runtime and feed it the parameters it needs.
How would you achieve this?
With a simple example, the whole process would look like this:
(in a jupyter notebook)
import sympy as sp
import pickle
a, b, c = symbols("a b c")
eqn = sp.Eq(b + c, a) #for a real equation I would simplify it before using sympy
with open("eqn.txt") as f:
pickle.dump(eqn, f)
and then later in the app's code:
...
with open("eqn.txt") as f:
eqn = pickle.load(f)
b = 1
c = 2
#magic line to put b and c into the equation
a = sp.solve(eqn, a)
print(a) # 3
Implementing the whole equation directly in a function is probably not an option although I am considering how to implement it manually. It just looks really, really hard to do and if I could do it in two lines using simpy, that'd be great.
Thanks for your time!
with open("eqn.txt") as f:
eqn = pickle.load(f)
b, c = 1, 2 # b,c are python symbols here
reps = dict(zip(symbols('b c'), (b, c))) # keys are SymPy Symbols, values are 1,2
eqn = eqn.xreplace(reps) #magic line to put b and c into the equation
a = sp.solve(eqn, a)
print(a) # 3
It is important to keep in mind the distinction between b = 1 and b = Symbol('b'). The left hand side of the expressions are Python variables and on the right, an int or a SymPy Symbol, respectively. In a SymPy expression you might reference a Python variable and its value will be included in the equation:
>>> from sympy import *
>>> b = 1
>>> b + 1
2
>>> b = Symbol('b')
>>> b + 1
b + 1
>>> eq = _
>>> eq.subs(b,1)
2
>>> b=2 # assigning a new value to b does not change the object in eq
>>> eq
b + 1
>>> eq.subs(b, 2) # i.e., eq.subs(2,2) doesn't work -- no 2 in eq
b + 1
>>> eq.subs(Symbol('b'), 2) # replace the Symbol with value of 2 works
3
So in reps above, zipping the symbols to their corresponding values creates a mapping that can be used to do the replacement.
There is more discussion of such issues in the documentation gotchas file, but this should help with your current issue.
This Sympy code works as I expect:
>>> x = sp.Symbol("x")
>>> y = sp.Symbol("y")
>>> z = sp.Symbol("z")
>>> (x+y+z).evalf(subs={"x":1, "y":2, "z":3})
6.0
However, if I use real-valued Symbols instead, the expression isn't simplified:
>>> x = sp.Symbol("x", real=True)
>>> y = sp.Symbol("y", real=True)
>>> z = sp.Symbol("z", real=True)
>>> (x+y+z).evalf(subs={"x":1, "y":2, "z":3})
x + y + z
I was unable to find an explanation for this by searching with keywords like sympy symbol real evalf - I only get unrelated results.
Why isn't the expression simplified in the second case? How can I substitute in values for real-valued Symbols and evaluate the expression?
Use the symbols as keys for the dictionary of substitutions, rather than string names:
>>> (x + y + z).evalf(subs={x: 1, y: 2, z: 3})
6.00000000000000
There appears to be an inconsistency between how complex and real symbols are treated:
>>> x = sp.Symbol('x')
>>> x.subs(x, 1)
1
>>> x.subs('x', 1)
1
>>> x = sp.Symbol('x', real=True)
>>> x.subs(x, 1)
1
>>> x.subs('x', 1)
x
I can't find anything relevant about this in the documentation, and the built-in help text isn't useful either. My best guess is that a string 'x' is naively converted using sp.Symbol, and the resulting symbol is always a complex-valued symbol that doesn't match the real-valued one with the same name.
I would consider this behaviour a bug and file a bug report (or look for an existing report). IMO, if a string is usable at all, it should match any symbol with that name; and an expression shouldn't be able to contain two different variables with the same name and different types; and trying to substitute in a variable with a matching name and incompatible type should probably raise an exception:
>>> x = sp.Symbol('x')
>>> # why allow this?
>>> broken = sp.Symbol('x', real=True) + x
>>> broken # if the types matched, it would simplify to 2*x
x + x
>>> # surely 2 is the only value that makes sense?
>>> broken.subs('x', 1)
x + 1
>>> x.subs('x', 1)
1
>>> # If this is allowed at all, surely the result should be 1?
>>> x.subs(sp.Symbol('x', real=True), 1)
x
I tried to solve this equation but still running.
I gave the symbol and the equation is "Eq((1-(1+ x )(-60))/ x+32*(1+x)(-60) , 41.81)".
The way solve and solveset usual work is to split an expression into numerator and denominator, and return solutions for the one that are not in the other.
Let's define a helper function to put the solutions from nsolve into a FiniteSet and one to give the final solution:
>>> from sympy import FiniteSet, nsolve, Add, Eq
>>> from sympy.abc import x
>>> rr = lambda x: FiniteSet(*[i[0] for i in real_roots(x, multiple=False)])
>>> sol = lambda n, d: list(rr(n) - rr(d))
>>> go = lambda eq: sol(*eq.rewrite(Add).as_numer_denom())
Now we try this out on your original expression:
>>> eq = Eq(32/(x + 1)**60 + (1 - 1/(x + 1)**60)/x, 41.81)
>>> fsol = go(eq) # very slow
>>> [i.n(3) for i in fsol]
[-3.33, -2.56, -1.44, -0.568, -0.228, 0.0220]
If you check those out by substituting into the original expression (written as an expression) you will find that only the last one is valid
>>> expr = eq.rewrite(Add)
>>> [expr.subs(x, i).n(3) for i in fsol]
[-42.1, -42.2, 4.72e+22, 2.64e+23, 1.97e+8, 1.31e-15]
Now let's replace that Float with a Rational and get solutions:
>>> req = nsimplify(eq, rational=True); req
Eq(32/(x + 1)**60 + (1 - 1/(x + 1)**60)/x, 4181/100)
>>> rsol = go(_) # pretty fast
>>> [i.n(3) for i in rsol]
[-2.00, 0.0220]
We know the 2nd solution is right; let's check the first:
>>> req.subs(x, rsol[0]).rewrite(Add).n(3)
-0.e-114
So both solutions appear to be valid and you don't get any spurious solutions which (by the way) I wasn't expecting from nsolve.
An exact analytic solution to this is unlikely but you can get numeric solutions e.g.:
In [18]: nsolve(eq, x, -2)
Out[18]: -1.99561339048822
Since this can be transformed into a polynomial you can find all real solutions like:
In [20]: p = Poly(nsimplify(eq).rewrite(Add).as_numer_denom()[0])
In [21]: [r[0].n() for r in p.real_roots(multiple=False)]
Out[21]: [-1.99561339048822, -1.0, 0, 0.0219988833527669]
Using as_numer_denom like this can potentially introduce spurious solutions though so you should check them (e.g. by plotting the function around each root). For example 0 is not actually a root.
I want to integrate exp(-(x^2 + y^2)) in python using sympy library.
I could find the integral of exp(-(x^2))
>>> B1 = sympy.exp(-alpha1 * (r1_x**2))
>>> p = integrate(B1,r1_x)
>>> p
pi**(1/2)*erf(alpha1**(1/2)*r1_x)/(2*alpha1**(1/2))
But when I want to try integrate exp(-(x^2 + y^2))
>>> B1 = sympy.exp(-alpha1 * (r1_x**2 + r1_y**2))
>>> p = integrate(B1,r1_x)
>>> p
Integral(exp(-alpha1*(r1_x**2 + r1_y**2)), r1_x)
There is no output and python can't take the integral!
(I am the lead developer of SymPy)
DSM is correct that you can get this to work by calling expand, and that there is no general way to do this (because in general, integrals don't have closed forms).
I just wanted to point out that if SymPy cannot do an integral that does have a closed form, we consider this a bug, and you should feel free to report it at http://code.google.com/p/sympy/issues.
sympy doesn't always recognize every form, and so sometimes you have to give it a little help:
>>> import sympy
>>> alpha1, r1_x, r1_y = sympy.var("alpha1 r1_x r1_y")
>>> B1 = sympy.exp(-alpha1 * (r1_x**2 + r1_y**2))
>>> B1.integrate(r1_x)
Integral(exp(-alpha1*(r1_x**2 + r1_y**2)), r1_x)
>>> B1.expand(alpha1)
exp(-alpha1*r1_x**2)*exp(-alpha1*r1_y**2)
>>> B1.expand(alpha1).integrate(r1_x)
sqrt(pi)*exp(-alpha1*r1_y**2)*erf(sqrt(alpha1)*r1_x)/(2*sqrt(alpha1))
I have a 1x1 matrix that contains a value. I want just the value.
matrix([[-0.16666667+0.66666667j]])
I want the complex number inside that matrix. How do I get it?
>>> m = matrix([[-1.0/6 + (2.0j/3)]])
>>> m
matrix([[-0.16666667+0.66666667j]])
>>> m.shape
(1, 1)
>>> m[0,0]
(-0.16666666666666666+0.66666666666666663j)
>>> m[(0,0)]
(-0.16666666666666666+0.66666666666666663j)
or, while we're at it:
>>> m.tolist()[0][0] # seldom useful, though
(-0.16666666666666666+0.6666666666666666j)
>>> m.flat[0] # more frequently useful
(-0.16666666666666666+0.66666666666666663j)
To convince the OP that the above is actually a complex number :^) --
>>> m[(0,0)]
(-0.16666666666666666+0.66666666666666663j)
>>> type(m[(0,0)])
<type 'numpy.complex128'>
>>> x = m[(0,0)]
>>> x + 3
(2.8333333333333335+0.66666666666666663j)
>>> abs(x)
0.68718427093627676
>>> x.real
-0.16666666666666666
>>> x.imag
0.66666666666666663
[Edited to correct a sign difference between my number and the OP's. Doesn't change anything but couldn't stand looking at it once I noticed..]
The following Python snippet also seems to work, using the data in your example above.
import numpy
m = numpy.matrix([[-0.16666667+0.66666667j]])
print m.item(0)
# the result of running the above is
(-0.16666667+0.66666667j)