# Solvers¶

The solvers module in SymPy implements methods for solving equations.

## Algebraic equations¶

Use solve() to solve algebraic equations. We suppose all equations are equaled to 0, so solving x**2 == 1 translates into the following code:

>>> from sympy.solvers import solve
>>> from sympy import Symbol
>>> x = Symbol('x')
>>> solve( x**2 - 1, x )
[1, -1]


The first argument for solve() is an equation (equaled to zero) and the second argument is the symbol that we want to solve the equation for.

static solvers.solve(f, *symbols, **flags)

Algebraically solves equations and systems of equations.

Currently supported are:
• univariate polynomial,
• transcendental
• piecewise combinations of the above
• systems of linear and polynomial equations
• sytems containing relational expressions.
Input is formed as:
f
• a single Expr or Poly that must be zero,
• an Equality
• a Relational expression or boolean
• iterable of one or more of the above
symbols (Symbol, Function or Derivative) specified as
• none given (all free symbols will be used)
• single symbol
• denested list of symbols e.g. solve(f, x, y)
• ordered iterable of symbols e.g. solve(f, [x, y])
flags
• simplified, when False, will not simplify solutions
(default=True except for polynomials of

order 3 or greater)

The output varies according to the input and can be seen by example:

>>> from sympy import solve, Poly, Eq, Function, exp
>>> from sympy.abc import x, y, z, a, b


o boolean or univariate Relational

>>> solve(x < 3)
And(im(x) == 0, re(x) < 3)


o single expression and single symbol that is in the expression

>>> solve(x - y, x)
[y]
>>> solve(x - 3, x)
[3]
>>> solve(Eq(x, 3), x)
[3]
>>> solve(Poly(x - 3), x)
[3]
>>> solve(x**2 - y**2, x)
[y, -y]
>>> solve(x**4 - 1, x)
[1, -1, -I, I]


o single expression with no symbol that is in the expression

>>> solve(3, x)
[]
>>> solve(x - 3, y)
[]

o when no symbol is given then all free symbols will be used

and sorted with default_sort_key and the result will be the same as above as if those symbols had been supplied

>>> solve(x - 3)
[3]
>>> solve(x**2 - y**2)
[y, -y]

o when a Function or Derivative is given as a symbol, it is isolated

algebraically and an implicit solution may be obtained

>>> f = Function('f')
>>> solve(f(x) - x, f(x))
[x]
>>> solve(f(x).diff(x) - f(x) - x, f(x).diff(x))
[x + f(x)]


o single expression and more than 1 symbol

when there is a linear solution
>>> solve(x - y**2, x, y)
{x: y**2}
>>> solve(x**2 - y, x, y)
{y: x**2}

when undetermined coefficients are identified
that are linear
>>> solve((a + b)*x - b + 2, a, b)
{a: -2, b: 2}

that are nonlinear
>>> solve((a + b)*x - b**2 + 2, a, b)
[(-2**(1/2), 2**(1/2)), (2**(1/2), -2**(1/2))]


if there is no linear solution then the first successful attempt for a nonlinear solution will be returned

>>> solve(x**2 - y**2, x, y)
[y, -y]
>>> solve(x**2 - y**2/exp(x), x, y)
[x*exp(x/2), -x*exp(x/2)]


o iterable of one or more of the above

involving relationals or bools
>>> solve([x < 3, x - 2])
And(im(x) == 0, re(x) == 2)
>>> solve([x > 3, x - 2])
False

when the system is linear
with a solution
>>> solve([x - 3], x)
{x: 3}
>>> solve((x + 5*y - 2, -3*x + 6*y - 15), x, y)
{x: -3, y: 1}
>>> solve((x + 5*y - 2, -3*x + 6*y - 15), x, y, z)
{x: -3, y: 1}
>>> solve((x + 5*y - 2, -3*x + 6*y - z), z, x, y)
{x: -5*y + 2, z: 21*y - 6}

without a solution
>>> solve([x + 3, x - 3])

when the system is not linear
>>> solve([x**2 + y -2, y**2 - 4], x, y)
[(-2, -2), (0, 2), (0, 2), (2, -2)]


Warning: there is a possibility of obtaining ambiguous results if no symbols are given for a nonlinear system of equations or are given as a set since the symbols are not presently reported with the solution. A warning will be issued in this situation.

>>> solve([x - 2, x**2 + y])
<BLANKLINE>
For nonlinear systems of equations, symbols should be
given as a list so as to avoid ambiguity in the results.
solve sorted the symbols as [x, y]
[(2, -4)]

>>> solve([x - 2, x**2 + f(x)], set([f(x), x]))
<BLANKLINE>
For nonlinear systems of equations, symbols should be
given as a list so as to avoid ambiguity in the results.
solve sorted the symbols as [x, f(x)]
[(2, -4)]

rsolve() for solving recurrence relationships dsolve() for solving differential equations

See ODE.

## Partial Differential Equations (PDEs)¶

static pde.pde_separate(eq, fun, sep, strategy='mul')

Separate variables in partial differential equation either by additive or multiplicative separation approach. It tries to rewrite an equation so that one of the specified variables occurs on a different side of the equation than the others.

Parameters: eq – Partial differential equation fun – Original function F(x, y, z) sep – List of separated functions [X(x), u(y, z)] strategy – Separation strategy. You can choose between additive separation (‘add’) and multiplicative separation (‘mul’) which is default.

Helper function for searching additive separable solutions.

Consider an equation of two independent variables x, y and a dependent variable w, we look for the product of two functions depending on different arguments:

$$w(x, y, z) = X(x) + y(y, z)$$

Examples:

>>> from sympy import E, Eq, Function, pde_separate_add, Derivative as D
>>> from sympy.abc import x, t
>>> u, X, T = map(Function, 'uXT')

>>> eq = Eq(D(u(x, t), x), E**(u(x, t))*D(u(x, t), t))
>>> pde_separate_add(eq, u(x, t), [X(x), T(t)])
[exp(-X(x))*Derivative(X(x), x), exp(T(t))*Derivative(T(t), t)]

static pde.pde_separate_mul(eq, fun, sep)

Helper function for searching multiplicative separable solutions.

Consider an equation of two independent variables x, y and a dependent variable w, we look for the product of two functions depending on different arguments:

$$w(x, y, z) = X(x)*u(y, z)$$

Examples:

>>> from sympy import Function, Eq, pde_separate_mul, Derivative as D
>>> from sympy.abc import x, y
>>> u, X, Y = map(Function, 'uXY')

>>> eq = Eq(D(u(x, y), x, 2), D(u(x, y), y, 2))
>>> pde_separate_mul(eq, u(x, y), [X(x), Y(y)])
[Derivative(X(x), x, x)/X(x), Derivative(Y(y), y, y)/Y(y)]


ODE

Tensor Module