Solveset

This module contains functions to solve a single equation for a single variable.

Use solveset() to solve equations or expressions (assumed to be equal to 0) for a single variable. Solving an equation like x**2 == 1 can be done as follows:

>>> from sympy.solvers.solveset import *
>>> from sympy import Symbol, Eq
>>> x = Symbol('x')
>>> solveset(Eq(x**2, 1), x)
{-1, 1}

Or one may manually rewrite the equation as an expression equal to 0:

>>> solveset(x**2 - 1, x)
{-1, 1}

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

sympy.solvers.solveset.solveset(f, symbol=None, domain=Complexes(Lambda((_x, _y), _x + _y*I), (-oo, oo) x (-oo, oo)))[source]

Solves a given inequality or equation with set as output

Parameters:

f : Expr or a relational.

The target equation or inequality

symbol : Symbol

The variable for which the equation is solved

domain : Set

The domain over which the equation is solved

Returns:

Set :

A set of values for \(symbol\) for which \(f\) is True or is equal to zero. An \(EmptySet\) is returned if no solution is found. A \(ConditionSet\) is returned as unsolved object if algorithms to evaluatee complete solution are not yet implemented.

`solveset` claims to be complete in the solution set that it returns. :

Raises:

NotImplementedError :

The algorithms to solve inequalities in complex domain are not yet implemented.

ValueError :

The input is not valid.

RuntimeError :

It is a bug, please report to the github issue tracker.

`solveset` uses two underlying functions `solveset_real` and :

`solveset_complex` to solve equations. They are the solvers for real and :

complex domain respectively. `solveset` ignores the assumptions on the :

variable being solved for and instead, uses the `domain` parameter to :

decide which solver to use. :

See also

solveset_real
solver for real domain
solveset_complex
solver for complex domain

Examples

>>> from sympy import exp, Symbol, Eq, pprint, S
>>> from sympy.solvers.solveset import solveset
>>> from sympy.abc import x
  • The default domain is complex. Not specifying a domain will lead to the solving of the equation in the complex domain.
>>> pprint(solveset(exp(x) - 1, x), use_unicode=False)
{2*n*I*pi | n in Integers()}
  • If you want to solve equation in real domain by the \(solveset\) interface, then specify that the domain is real. Alternatively use \(solveset\_real\).
>>> x = Symbol('x')
>>> solveset(exp(x) - 1, x, S.Reals)
{0}
>>> solveset(Eq(exp(x), 1), x, S.Reals)
{0}
  • Inequalities can be solved over the real domain only. Use of a complex domain leads to a NotImplementedError.
>>> solveset(exp(x) > 1, x, S.Reals)
(0, oo)
sympy.solvers.solveset.solveset_real(f, symbol)[source]

Solves a real valued equation.

Parameters:

f : Expr

The target equation

symbol : Symbol

The variable for which the equation is solved

Returns:

Set :

A set of values for \(symbol\) for which \(f\) is equal to zero. An \(EmptySet\) is returned if no solution is found. A \(ConditionSet\) is returned as unsolved object if algorithms to evaluate complete solutions are not yet implemented.

`solveset_real` claims to be complete in the set of the solution it :

returns. :

Raises:

NotImplementedError :

Algorithms to solve inequalities in complex domain are not yet implemented.

ValueError :

The input is not valid.

RuntimeError :

It is a bug, please report to the github issue tracker.

See Also :

======= :

solveset_complex : solver for complex domain

Examples

>>> from sympy import Symbol, exp, sin, sqrt, I
>>> from sympy.solvers.solveset import solveset_real
>>> x = Symbol('x', real=True)
>>> a = Symbol('a', real=True, finite=True, positive=True)
>>> solveset_real(x**2 - 1, x)
{-1, 1}
>>> solveset_real(sqrt(5*x + 6) - 2 - x, x)
{-1, 2}
>>> solveset_real(x - I, x)
EmptySet()
>>> solveset_real(x - a, x)
{a}
>>> solveset_real(exp(x) - a, x)
{log(a)}
  • In case the equation has infinitely many solutions an infinitely indexed \(ImageSet\) is returned.
>>> solveset_real(sin(x) - 1, x)
ImageSet(Lambda(_n, 2*_n*pi + pi/2), Integers())
  • If the equation is true for any arbitrary value of the symbol a \(S.Reals\) set is returned.
>>> solveset_real(x - x, x)
(-oo, oo)
sympy.solvers.solveset.solveset_complex(f, symbol)[source]

Solve a complex valued equation.

Parameters:

f : Expr

The target equation

symbol : Symbol

The variable for which the equation is solved

Returns:

Set :

A set of values for \(symbol\) for which \(f\) equal to zero. An \(EmptySet\) is returned if no solution is found. A \(ConditionSet\) is returned as an unsolved object if algorithms to evaluate complete solutions are not yet implemented.

`solveset_complex` claims to be complete in the solution set that :

it returns. :

Raises:

NotImplementedError :

The algorithms to solve inequalities in complex domain are not yet implemented.

ValueError :

The input is not valid.

RuntimeError :

It is a bug, please report to the github issue tracker.

See also

solveset_real
solver for real domain

Examples

>>> from sympy import Symbol, exp
>>> from sympy.solvers.solveset import solveset_complex
>>> from sympy.abc import x, a, b, c
>>> solveset_complex(a*x**2 + b*x +c, x)
{-b/(2*a) - sqrt(-4*a*c + b**2)/(2*a), -b/(2*a) + sqrt(-4*a*c + b**2)/(2*a)}
  • Due to the fact that complex extension of my real valued functions are multivariate even some simple equations can have infinitely many solution.
>>> solveset_complex(exp(x) - 1, x)
ImageSet(Lambda(_n, 2*_n*I*pi), Integers())
sympy.solvers.solveset.invert_real(f_x, y, x)[source]

Inverts a real valued function

Reduces the real valued equation f(x) = y to a set of equations {g(x) = h_1(y), g(x) = h_2(y), ..., g(x) = h_n(y) } where g(x) is a simpler function than f(x). The return value is a tuple (g(x), set_h), where g(x) is a function of x and set_h is the set of functions {h_1(y), h_2(y), ..., h_n(y)}. Here, y is not necessarily a symbol.

The set_h contains the functions along with the information about their domain in which they are valid, through set operations. For instance, if y = Abs(x) - n, is inverted, then, the set_h doesn’t simply return \({-n, n}\), as it doesn’t explicitly mentions about the nature of \(n\) rather it will return: \(Intersection([0, oo) {n}) U Intersection((-oo, 0], {-n})\)

See also

invert_complex

Examples

>>> from sympy.solvers.solveset import invert_real
>>> from sympy import tan, Abs, exp
>>> from sympy.abc import x, y, n
>>> invert_real(exp(x), 1, x)
(x, {0})
>>> invert_real(tan(x), y, x)
(x, ImageSet(Lambda(_n, _n*pi + atan(y)), Integers()))
  • set_h containing information about the domain
>>> invert_real(Abs(x**31 + x), y, x)
(x**31 + x, Intersection([0, oo), {y}) U Intersection((-oo, 0], {-y}))
>>> invert_real(exp(Abs(x)), y, x)
(x, Intersection([0, oo), {log(y)}) U Intersection((-oo, 0], {-log(y)}))
sympy.solvers.solveset.invert_complex(f_x, y, x)[source]

Inverts a complex valued function.

Reduces the complex valued equation f(x) = y to a set of equations {g(x) = h_1(y), g(x) = h_2(y), ..., g(x) = h_n(y) } where g(x) is a simpler function than f(x). The return value is a tuple (g(x), set_h), where g(x) is a function of x and set_h is the set of function {h_1(y), h_2(y), ..., h_n(y)}. Here, y is not necessarily a symbol.

Note that \(invert\_complex\) and \(invert\_real\) don’t always produce the same result even for a seemingly simple function like exp(x) because the complex extension of real valued log is multivariate in the complex system and has infinitely many branches. If you are working with real values only or you are not sure with function to use you should use \(invert\_real\).

See also

invert_real

Examples

>>> from sympy.solvers.solveset import invert_complex
>>> from sympy.abc import x, y
>>> from sympy import exp, log
>>> invert_complex(log(x), y, x)
(x, {exp(y)})
>>> invert_complex(log(x), 0, x)  # Second parameter is not a symbol
(x, {1})
>>> invert_complex(exp(x), y, x)
(x, ImageSet(Lambda(_n, I*(2*_n*pi + arg(y)) + log(Abs(y))), Integers()))
sympy.solvers.solveset.domain_check(f, symbol, p)[source]

Returns False if point p is infinite or any subexpression of f is infinite or becomes so after replacing symbol with p. If none of these conditions is met then True will be returned.

Examples

>>> from sympy import Mul, oo
>>> from sympy.abc import x
>>> from sympy.solvers.solveset import domain_check
>>> g = 1/(1 + (1/(x + 1))**2)
>>> domain_check(g, x, -1)
False
>>> domain_check(x**2, x, 0)
True
>>> domain_check(1/x, x, oo)
False
  • The function relies on the assumption that the original form of the equation has not been changed by automatic simplification.
>>> domain_check(x/x, x, 0) # x/x is automatically simplified to 1
True
  • To deal with automatic evaluations use evaluate=False:
>>> domain_check(Mul(x, 1/x, evaluate=False), x, 0)
False

linear_eq_to_matrix

sympy.solvers.solveset.linear_eq_to_matrix(equations, *symbols)[source]

Converts a given System of Equations into Matrix form. Here \(equations\) must be a linear system of equations in \(symbols\). The order of symbols in input \(symbols\) will determine the order of coefficients in the returned Matrix.

The Matrix form corresponds to the augmented matrix form. For example:

\[4x + 2y + 3z = 1\]
\[3x + y + z = -6\]
\[2x + 4y + 9z = 2\]

This system would return \(A\) & \(b\) as given below:

    [ 4  2  3 ]          [ 1 ]
A = [ 3  1  1 ]   b  =   [-6 ]
    [ 2  4  9 ]          [ 2 ]

Examples

>>> from sympy.solvers.solveset import linear_eq_to_matrix
>>> from sympy import symbols
>>> x, y, z = symbols('x, y, z')
>>> eqns = [x + 2*y + 3*z - 1, 3*x + y + z + 6, 2*x + 4*y + 9*z - 2]
>>> A, b = linear_eq_to_matrix(eqns, [x, y, z])
>>> A
Matrix([
[1, 2, 3],
[3, 1, 1],
[2, 4, 9]])
>>> b
Matrix([
[ 1],
[-6],
[ 2]])
>>> eqns = [x + z - 1, y + z, x - y]
>>> A, b = linear_eq_to_matrix(eqns, [x, y, z])
>>> A
Matrix([
[1,  0, 1],
[0,  1, 1],
[1, -1, 0]])
>>> b
Matrix([
[1],
[0],
[0]])
  • Symbolic coefficients are also supported
>>> a, b, c, d, e, f = symbols('a, b, c, d, e, f')
>>> eqns = [a*x + b*y - c, d*x + e*y - f]
>>> A, B = linear_eq_to_matrix(eqns, x, y)
>>> A
Matrix([
[a, b],
[d, e]])
>>> B
Matrix([
[c],
[f]])

linsolve

sympy.solvers.solveset.linsolve(system, *symbols)[source]

Solve system of N linear equations with M variables, which means both under - and overdetermined systems are supported. The possible number of solutions is zero, one or infinite. Zero solutions throws a ValueError, where as infinite solutions are represented parametrically in terms of given symbols. For unique solution a FiniteSet of ordered tuple is returned.

All Standard input formats are supported: For the given set of Equations, the respective input types are given below:

\[3x + 2y - z = 1\]
\[2x - 2y + 4z = -2\]
\[2x - y + 2z = 0\]
  • Augmented Matrix Form, \(system\) given below:
         [3   2  -1  1]
system = [2  -2   4 -2]
         [2  -1   2  0]
  • List Of Equations Form

\(system = [3x + 2y - z - 1, 2x - 2y + 4z + 2, 2x - y + 2z]\)

  • Input A & b Matrix Form (from Ax = b) are given as below:
    [3   2  -1 ]         [  1 ]
A = [2  -2   4 ]    b =  [ -2 ]
    [2  -1   2 ]         [  0 ]

\(system = (A, b)\)

Symbols to solve for should be given as input in all the cases either in an iterable or as comma separated arguments. This is done to maintain consistency in returning solutions in the form of variable input by the user.

The algorithm used here is Gauss-Jordan elimination, which results, after elimination, in an row echelon form matrix.

Returns:

A FiniteSet of ordered tuple of values of `symbols` for which :

the `system` has solution. :

Please note that general FiniteSet is unordered, the solution :

returned here is not simply a FiniteSet of solutions, rather :

it is a FiniteSet of ordered tuple, i.e. the first & only :

argument to FiniteSet is a tuple of solutions, which is ordered, :

& hence the returned solution is ordered. :

Also note that solution could also have been returned as an :

ordered tuple, FiniteSet is just a wrapper `{}` around :

the tuple. It has no other significance except for :

the fact it is just used to maintain a consistent output :

format throughout the solveset. :

Returns EmptySet(), if the linear system is inconsistent. :

Raises:

ValueError :

The input is not valid. The symbols are not given.

Examples

>>> from sympy.solvers.solveset import linsolve
>>> from sympy import Matrix, S
>>> from sympy import symbols
>>> x, y, z = symbols("x, y, z")
>>> A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 10]])
>>> b = Matrix([3, 6, 9])
>>> A
Matrix([
[1, 2,  3],
[4, 5,  6],
[7, 8, 10]])
>>> b
Matrix([
[3],
[6],
[9]])
>>> linsolve((A, b), [x, y, z])
{(-1, 2, 0)}
  • Parametric Solution: In case the system is under determined, the function will return parametric solution in terms of the given symbols. Free symbols in the system are returned as it is. For e.g. in the system below, \(z\) is returned as the solution for variable z, which means z is a free symbol, i.e. it can take arbitrary values.
>>> A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> b = Matrix([3, 6, 9])
>>> linsolve((A, b), [x, y, z])
{(z - 1, -2*z + 2, z)}
  • List of Equations as input
>>> Eqns = [3*x + 2*y - z - 1, 2*x - 2*y + 4*z + 2, - x + S(1)/2*y - z]
>>> linsolve(Eqns, x, y, z)
{(1, -2, -2)}
  • Augmented Matrix as input
>>> aug = Matrix([[2, 1, 3, 1], [2, 6, 8, 3], [6, 8, 18, 5]])
>>> aug
Matrix([
[2, 1,  3, 1],
[2, 6,  8, 3],
[6, 8, 18, 5]])
>>> linsolve(aug, x, y, z)
{(3/10, 2/5, 0)}
  • Solve for symbolic coefficients
>>> a, b, c, d, e, f = symbols('a, b, c, d, e, f')
>>> eqns = [a*x + b*y - c, d*x + e*y - f]
>>> linsolve(eqns, x, y)
{(-b*(f - c*d/a)/(a*(e - b*d/a)) + c/a, (f - c*d/a)/(e - b*d/a))}
  • A degenerate system returns solution as set of given symbols.
>>> system = Matrix(([0,0,0], [0,0,0], [0,0,0]))
>>> linsolve(system, x, y)
{(x, y)}

Diophantine Equations (DEs)

See Diophantine

Inequalities

See Inequality Solvers

Ordinary Differential equations (ODEs)

See ODE.

Partial Differential Equations (PDEs)

See PDE.