[SciPy-user] Solve

Gael Varoquaux gael.varoquaux@normalesup....
Tue Nov 6 08:01:03 CST 2007


On Tue, Nov 06, 2007 at 10:41:17AM -0300, Eduardo Rodrigues wrote:
> Hi, I am starting in Python. I usually use Maple and I am with a problem to 
> do the same project in Python.
> I would like to solve an equation like x+y+1=0 for x. In Maple I use 
> "solve(x+y+1=0,x);".
> Do exist a similar command in Python/NumPy/SciPy?

Do you want a numericaly result, given the value of y, or do you want a
formal result ? The numerical value can be calculated using scipy, for
linear equation with the method exposed by Michael Nandris, for non
linear equations, you should look at scipy.optimize.zeros.

If you want a formal result, or a functional result, you can use sympy:

from sympy import solve, Symbol
x = Symbol('x')
y = Symbol('y')
f = solve(x+y+1, x) # f gives you the symbolic result
F = lambda v: f.subs(y, v)

F is a function that returns the exact expression of the root of you
equation. If you have several roots, than you need to do something like:

F = lambda v: [r.subs(y, v) for r in f]

Currently sympy is still yound, and won't be able to solve everythin you
throw at it.

HTH,

Gaël


More information about the SciPy-user mailing list