This is the official documentation of the solveset module in solvers. It contains the frequently asked questions about our new module to solve equations.
SymPy already has a pretty powerful solve function. But it has a lot of major issues
SymPy has a well developed sets module, which can represent most of the set containers in Mathematics such as:
FiniteSet
Represents a finite set of discrete numbers.
Interval
Represents a real interval as a set.
ProductSet
Represents a Cartesian product of sets.
ImageSet
Represents the image of a set under a mathematical function
>>> from sympy import ImageSet, S, Lambda >>> from sympy.abc import x >>> squares = ImageSet(Lambda(x, x**2), S.Naturals) # {x**2 for x in N} >>> 4 in squares TrueComplexRegion
Represents the set of all complex numbers in a region in the Argand plane.
ConditionSet
Represents the set of elements, which satisfies a given condition.
Also, the predefined set classes such as:
Naturals \(\mathbb{N}\)
Represents the natural numbers (or counting numbers), which are all positive integers starting from 1.
Naturals0 \(\mathbb{N_0}\)
Represents the whole numbers, which are all the non-negative integers, inclusive of 0.
Integers \(\mathbb{Z}\)
Represents all integers: positive, negative and zero.
Reals \(\mathbb{R}\)
Represents the set of all real numbers.
Complexes \(\mathbb{C}\)
Represents the set of all complex numbers.
EmptySet \(\phi\)
Represents the empty set.
The above six sets are available as Singletons, like S.Integers.
It is capable of most of the set operations in mathematics:
- Union
- Intersection
- Complement
- SymmetricDifference
The main reason for using sets as output to solvers is that it can consistently represent many types of solutions. For the single variable case it can represent:
- No solution (by the empty set).
- Finitely many solutions (by FiniteSet).
- Infinitely many solutions, both countably and uncountably infinite solutions (using the ImageSet module).
- Interval
- There can also be bizarre solutions to equations like the set of rational numbers.
No other Python object (list, dictionary, generator, Python sets) provides the flexibility of mathematical sets which our sets module tries to emulate. The second reason to use sets is that they are close to the entities which mathematicians deal with and it makes it easier to reason about them. Set objects conform to Pythonic conventions when possible, i.e., x in A and for i in A both work when they can be computed. Another advantage of using objects closer to mathematical entities is that the user won’t have to “learn” our representation and she can have her expectations transferred from her mathematical experience.
For the multivariate case we represent solutions as a set of points in a n-dimensional space and a point is represented by a FiniteSet of ordered tuples, which is a point in \(\mathbb{R}^n\) or \(\mathbb{C}^n\).
Please note that, the general FiniteSet is unordered, but a FiniteSet with a tuple as its only argument becomes ordered, since a tuple is ordered. So the order in the tuple is mapped to a pre-defined order of variables while returning solutions.
For example:
>>> from sympy import FiniteSet
>>> FiniteSet(1, 2, 3) # Unordered
{1, 2, 3}
>>> FiniteSet((1, 2, 3)) # Ordered
{(1, 2, 3)}
Why not use dicts as output?
Dictionary are easy to deal with programatically but mathematically they are not very precise and use of them can quickly lead to inconsistency and a lot of confusion. For example:
There are a lot of cases where we don’t know the complete solution and we may like to output a partial solution, consider the equation \(fg = 0\). The solution of this equation is the union of the solution of the following two equations: \(f = 0\), \(g = 0\). Let’s say that we are able to solve \(f = 0\) but solving \(g = 0\) isn’t supported yet. In this case we cannot represent partial solution of the given equation \(fg = 0\) using dicts. This problem is solved with sets using a ConditionSet object:
\(sol_f \cup \{x | x ∊ \mathbb{R} ∧ g = 0\}\), where \(sol_f\) is the solution of the equation \(f = 0\).
Using a dict may lead to surprising results like:
solve(Eq(x**2, 1), x) != solve(Eq(y**2, 1), y)
Mathematically, this doesn’t make sense. Using FiniteSet here solves the problem.
It also cannot represent solutions for equations like \(|x| < 1\), which is a disk of radius 1 in the Argand Plane. This problem is solved using complex sets implemented as ComplexRegion.
solveset has a cleaner input API, unlike solve. It takes a maximum of three arguments:
solveset(equation, variable=None, domain=S.Complexes)
Equation(s)
The equation(s) to solve.
Variable(s)
The variable(s) for which the equation is to be solved.
Domain
The domain in which the equation is to be solved.
solveset removes the flags argument of solve, which had made the input API messy and output API inconsistent.
Solveset is designed to be independent of the assumptions on the variable being solved for and instead, uses the domain argument to decide the solver to dispatch the equation to, namely solveset_real or solveset_complex. It’s unlike the old solve which considers the assumption on the variable.
>>> from sympy import solveset, S >>> from sympy.abc import x >>> solveset(x**2 + 1, x) # domain=S.Complexes is default {-I, I} >>> solveset(x**2 + 1, x, domain=S.Reals) EmptySet()
Solveset uses various methods to solve an equation, here is a brief overview of the methodology:
- The domain argument is first considered to know the domain in which the user is interested to get the solution.
- If the given function is a relational (>=, <=, >, <), and the domain is real, then solve_univariate_inequality and solutions are returned. Solving for complex solutions of inequalities, like \(x^2 < 0\) is not yet supported.
- Based on the domain, the equation is dispatched to one of the two functions solveset_real or solveset_complex, which solves the given equation in the complex or real domain, respectively.
- If the given expression is a product of two or more functions, like say \(gh = 0\), then the solution to the given equation is the Union of the solution of the equations \(g = 0\) and \(h = 0\), if and only if both \(g\) and \(h\) are finite for a finite input. So, the solution is built up recursively.
- If the function is trigonometric or hyperbolic, the function _solve_real_trig is called, which solves it by converting it to complex exponential form.
- The function is now checked if there is any instance of a Piecewise expression, if it is, then it’s converted to explict expression and set pairs and then solved recursively.
- The respective solver now tries to invert the equation using the routines invert_real and invert_complex. These routines are based on the concept of mathematical inverse (though not exactly). It reduces the real/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)\). There is some work needed to be done in this to find invert of more complex expressions.
- After the invert, the equations are checked for radical or Abs (Modulus), then the method _solve_radical tries to simplify the radical, by removing it using techniques like squarring, cubing etc, and _solve_abs solves nested Modulus by considering the positive and negative variants, iteratively.
- If none of the above method is successful, then methods of polynomial is used as follows:
- The method to solve the rational function, _solve_as_rational, is called. Based on the domain, the respective poly solver _solve_as_poly_real or _solve_as_poly_complex is called to solve f as a polynomial.
- The underlying method _solve_as_poly solves the equation using polynomial techniques if it’s already a polynomial equation or, with a change of variables, can be made so.
- The final solution set returned by solveset is the intersection of the set of solutions found above and the input domain.
In the real domain, we use our ImageSet class in the sets module to return infinite solutions. ImageSet is an image of a set under a mathematical function. For example, to represent the solution of the equation \(\sin{(x)} = 0\), we can use the ImageSet as:
>>> from sympy import ImageSet, Lambda, pi, S, Dummy, pprint >>> n = Dummy('n') >>> pprint(ImageSet(Lambda(n, 2*pi*n), S.Integers), use_unicode=True) {2⋅n⋅π | n ∊ ℤ}Where n is a dummy variable. It is basically the image of the set of integers under the function \(2\pi n\).
In the complex domain, we use complex sets, which are implemented as the ComplexRegion class in the sets module, to represent infinite solution in the Argand plane. For example to represent the solution of the equation \(|z| = 1\), which is a unit circle, we can use the ComplexRegion as:
>>> from sympy import ComplexRegion, FiniteSet, Interval, pi, pprint >>> pprint(ComplexRegion(FiniteSet(1)*Interval(0, 2*pi), polar=True), use_unicode=True) {r⋅(ⅈ⋅sin(θ) + cos(θ)) | r, θ ∊ {1} × [0, 2⋅π)}Where the FiniteSet in the ProductSet is the range of the value of \(r\), which is the radius of the circle and the Interval is the range of \(\theta\), the angle from the \(x\) axis representing a unit circle in the Argand plane.
Note: We also have non-polar form notation for representing solution in rectangular form. For example, to represent first two quadrants in the Argand plane, we can write the ComplexRegion as:
>>> from sympy import ComplexRegion, Interval, pi, oo, pprint >>> pprint(ComplexRegion(Interval(-oo, oo)*Interval(0, oo)), use_unicode=True) {x + y⋅ⅈ | x, y ∊ (-∞, ∞) × [0, ∞)}where the Intervals are the range of \(x\) and \(y\) for the set of complex numbers \(x + iy\).
Solvers in a Computer Algebra System are based on heuristic algorithms, so it’s usually very hard to ensure 100% percent correctness, in every possible case. However there are still a lot of cases where we can ensure correctness. Solveset tries to verify correctness wherever it can. For example:
Consider the equation \(|x| = n\). A naive method to solve this equation would return {-n, n} as its solution, which is not correct since {-n, n} can be its solution if and only if n is positive. Solveset returns this information as well to ensure correctness.
>>> from sympy import symbols, S, pprint, solveset >>> x, n = symbols('x, n') >>> pprint(solveset(abs(x) - n, x, domain=S.Reals), use_unicode=True) ([0, ∞) ∩ {n}) ∪ ((-∞, 0] ∩ {-n})Though, there still a lot of work needs to be done in this regard.
Note: This is under Development.
After the introduction of ConditionSet, the solving of equations can be seen as set transformations. Here is an abstract view of the things we can do to solve equations.
- Apply various set transformations on the given set.
- Define a metric of the usability of solutions, or a notion of some solutions being better than others.
- Different transformations would be the nodes of a tree.
- Suitable searching techniques could be applied to get the best solution.
ConditionSet gives us the ability to represent unevaluated equations and inequalities in forms like \(\{x|f(x)=0; x \in S\}\) and \(\{x|f(x)>0; x \in S\}\) but a more powerful thing about ConditionSet is that it allows us to write the intermediate steps as set to set transformation. Some of the transformations are:
Composition: \(\{x|f(g(x))=0;x \in S\} \Rightarrow \{x|g(x)=y; x \in S, y \in \{z|f(z)=0; z \in S\}\}\)
- Polynomial Solver: \(\{x | P(x) = 0;x \in S\} \Rightarrow \{x_1,x_2, ... ,x_n\} \cap S\),
where \(x_i\) are roots of \(P(x)\).
Invert solver: \(\{x|f(x)=0;x \in S\} \Rightarrow \{g(0)| \text{ all g such that } f(g(x)) = x\}\)
- logcombine: \(\{x| \log(f(x)) + \log(g(x));x \in S\}\)
\(\Rightarrow \{x| \log(f(x).g(x)); x \in S\} \text{ if } f(x) > 0 \text{ and } g(x) > 0\) \(\Rightarrow \{x| \log(f(x)) + \log(g(x));x \in S\} \text{ otherwise}\)
- product solve: \(\{x|f(x)g(x)=0; x \in S\}\)
\(\Rightarrow \{x|f(x)=0; x \in S\} U \{x|g(x)=0; x \in S\}\) \(\text{ given } f(x) \text{ and } g(x) \text{ are bounded.}\) \(\Rightarrow \{x|f(x)g(x)=0; x \in S\}, \text{ otherwise}\)
Since the output type is same as the input type any composition of these transformations is also a valid transformation. And our aim is to find the right sequence of compositions (given the atoms) which transforms the given condition set to a set which is not a condition set i.e., FiniteSet, Interval, Set of Integers and their Union, Intersection, Complement or ImageSet. We can assign a cost function to each set, such that, the more desirable that form of set is to us, the less the value of the cost function. This way our problem is now reduced to finding the path from the initial ConditionSet to the lowest valued set on a graph where the atomic transformations forms the edges.
Creating a universal equation solver, which can solve each and every equation we encounter in mathematics is an ideal case for solvers in a Computer Algebra System. When cases which are not solved or can only be solved incompletely, a ConditionSet is used and acts as an unevaluated solveset object.
Note that, mathematically, finding a complete set of solutions for an equation is undecidable. See Richardson’s theorem.
ConditionSet is basically a Set of elements which satisfy a given condition. For example, to represent the solutions of the equation in the real domain:
\[(x^2 - 4)(\sin(x) + x)\]We can represent it as:
\(\{-2, 2\} ∪ \{x | x \in \mathbb{R} ∧ x + \sin(x) = 0\}\)
There are still a few things solveset can’t do, which the old solve can, such as solving non linear multivariate & LambertW type equations. Hence, it’s not yet a perfect replacement for old solve. The ultimate goal is to:
- Replace solve with solveset once solveset is at least as powerful as solve, i.e., solveset does everything that solve can do currently, and
- eventually rename solveset to solve.
Solveset is in its initial phase of development, so the symbolic parameters aren’t handled well for all the cases, but some work has been done in this regard to depict our ideology towards symbolic parameters. As an example, consider the solving of \(|x| = n\) for real \(x\), where \(n\) is a symbolic parameter. Solveset returns the value of \(x\) considering the domain of the symbolic parameter \(n\) as well:
\[([0, \infty) \cap \{n\}) \cup ((-\infty, 0] \cap \{-n\}).\]This simply means \(n\) is the solution only when it belongs to the Interval \([0, \infty)\) and \(-n\) is the solution only when \(-n\) belongs to the Interval \((- \infty, 0]\).
There are other cases to address too, like solving \(2^x + (a - 2)\) for \(x\) where \(a\) is a symbolic parameter. As of now, It returns the solution as an intersection with \(\mathbb{R}\), which is trivial, as it doesn’t reveal the domain of \(a\) in the solution.
Recently, we have also implemented a function to find the domain of the expression in a FiniteSet (Intersection with the interval) in which it is not-empty. It is a useful addition for dealing with symbolic parameters. For example:
>>> from sympy import Symbol, FiniteSet, Interval, not_empty_in, sqrt, oo >>> from sympy.abc import x >>> not_empty_in(FiniteSet(x/2).intersect(Interval(0, 1)), x) [0, 2] >>> not_empty_in(FiniteSet(x, x**2).intersect(Interval(1, 2)), x) [-sqrt(2), -1] U [1, 2]
[1] https://github.com/sympy/sympy/wiki/GSoC-2015-Ideas#solvers
[2] https://github.com/sympy/sympy/wiki/GSoC-2014-Application-Harsh-Gupta:-Solvers
[3] https://github.com/sympy/sympy/wiki/GSoC-2015-Application-AMiT-Kumar–Solvers-:-Extending-Solveset
[5] http://iamit.in/blog/
[6] https://github.com/sympy/sympy/pull/2948 : Action Plan for improving solvers.
[7] https://github.com/sympy/sympy/issues/6659 : solve() is a giant mess
[8] https://github.com/sympy/sympy/pull/7523 : solveset PR
[9] https://groups.google.com/forum/#!topic/sympy/-SIbX0AFL3Q
[10] https://github.com/sympy/sympy/pull/9696
[11] https://en.wikipedia.org/wiki/Richardson%27s_theorem
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 import solveset
>>> 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.
Solves a given inequality or equation with set as output
Parameters : | f : Expr or a relational.
symbol : Symbol
domain : Set
|
---|---|
Returns : | Set
\(solveset\) claims to be complete in the solution set that it returns. |
Raises : | NotImplementedError
ValueError
RuntimeError
|
See also
Notes
Python interprets 0 and 1 as False and True, respectively, but in this function they refer to solutions of an expression. So 0 and 1 return the Domain and EmptySet, respectively, while True and False return the opposite (as they are assumed to be solutions of relational expressions).
Examples
>>> from sympy import exp, sin, Symbol, pprint, S
>>> from sympy.solvers.solveset import solveset, solveset_real
>>> x = Symbol('x')
>>> pprint(solveset(exp(x) - 1, x), use_unicode=False)
{2*n*I*pi | n in Integers()}
>>> x = Symbol('x', real=True)
>>> pprint(solveset(exp(x) - 1, x), use_unicode=False)
{2*n*I*pi | n in Integers()}
>>> R = S.Reals
>>> x = Symbol('x')
>>> solveset(exp(x) - 1, x, R)
{0}
>>> solveset_real(exp(x) - 1, x)
{0}
The solution is mostly unaffected by assumptions on the symbol, but there may be some slight difference:
>>> pprint(solveset(sin(x)/x,x), use_unicode=False)
({2*n*pi | n in Integers()} \ {0}) U ({2*n*pi + pi | n in Integers()} \ {0})
>>> p = Symbol('p', positive=True)
>>> pprint(solveset(sin(p)/p, p), use_unicode=False)
{2*n*pi | n in Integers()} U {2*n*pi + pi | n in Integers()}
>>> solveset(exp(x) > 1, x, R)
(0, oo)
Reduce 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.
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 in the real domain, then, the set_h doesn’t simply return \({-n, n}\), as the nature of \(n\) is unknown; rather it will return: \(Intersection([0, oo) {n}) U Intersection((-oo, 0], {-n})\)
By default, the complex domain is used but note that inverting even seemingly simple functions like exp(x) can give very different result in the complex domain than are obtained in the real domain. (In the case of exp(x), the inversion via log is multi-valued in the complex domain, having infinitely many branches.)
If you are working with real values only (or you are not sure which function to use) you should probably use set the domain to S.Reals (or use \(invert\_real\) which does that automatically).
See also
Examples
>>> from sympy.solvers.solveset import invert_complex, invert_real
>>> from sympy.abc import x, y
>>> from sympy import exp, log
When does exp(x) == y?
>>> invert_complex(exp(x), y, x)
(x, ImageSet(Lambda(_n, I*(2*_n*pi + arg(y)) + log(Abs(y))), Integers()))
>>> invert_real(exp(x), y, x)
(x, Intersection((-oo, oo), {log(y)}))
When does exp(x) == 1?
>>> invert_complex(exp(x), 1, x)
(x, ImageSet(Lambda(_n, 2*_n*I*pi), Integers()))
>>> invert_real(exp(x), 1, x)
(x, {0})
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
>>> domain_check(x/x, x, 0) # x/x is automatically simplified to 1
True
>>> domain_check(Mul(x, 1/x, evaluate=False), x, 0)
False
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:
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 import linear_eq_to_matrix, 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]])
>>> 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]])
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:
[3 2 -1 1]
system = [2 -2 4 -2]
[2 -1 2 0]
\(system = [3x + 2y - z - 1, 2x - 2y + 4z + 2, 2x - y + 2z]\)
[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
|
Examples
>>> from sympy import Matrix, S, linsolve, 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)}
>>> 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)}
>>> 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)}
>>> 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)}
>>> 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*e)/(a*e - b*d), (a*f - c*d)/(a*e - b*d))}
>>> system = Matrix(([0,0,0], [0,0,0], [0,0,0]))
>>> linsolve(system, x, y)
{(x, y)}
>>> linsolve([ ], x)
EmptySet()
See Diophantine