Solveset¶
This is the official documentation of the solveset
module in solvers.
It contains the frequently asked questions about our new module to solve
equations.
What’s wrong with solve():¶
SymPy already has a pretty powerful solve
function. But it has a lot of major
issues
 It doesn’t have a consistent output for various types of solutions
It needs to return a lot of types of solutions consistently:
 Single solution : \(x = 1\)
 Multiple solutions: \(x^2 = 1\)
 No Solution: \(x^2 + 1 = 0 ; x \in \mathbb{R}\)
 Interval of solution: \(\lfloor x \rfloor = 0\)
 Infinitely many solutions: \(sin(x) = 0\)
 Multivariate functions with point solutions: \(x^2 + y^2 = 0\)
 Multivariate functions with nonpoint solution: \(x^2 + y^2 = 1\)
 System of equations: \(x + y = 1\) and \(x  y = 0\)
 Relational: \(x > 0\)
 And the most important case: “We don’t Know”
 The input API is also a mess, there are a lot of parameters. Many of them are not needed and they make it hard for the user and the developers to work on solvers.
 There are cases like finding the maxima and minima of function using
critical points where it is important to know if it has returned all the
solutions.
solve
does not guarantee this.
Why Solveset?¶
solveset
has a cleaner input and output interface:solveset
returns a set object and a set object takes care of all types of output. For cases where it doesn’t “know” all the solutions aConditionSet
with a partial solution is returned. For input it only takes the equation, the variables to solve for and the optional argumentdomain
over which the equation is to be solved.solveset
can return infinitely many solutions. For example solving for \(\sin{(x)} = 0\) returns \(\{2 n \pi  n \in \mathbb{Z}\} \cup \{2 n \pi + \pi  n \in \mathbb{Z}\}\), whereassolve
only returns \([0, \pi]\). There is a clear code level and interface level separation between solvers for equations in the complex domain and the real domain. For example solving \(e^x = 1\) when \(x\) is to be solved in the complex domain, returns the set of all solutions, that is \(\{2 n i \pi  n \in \mathbb{Z}\}\), whereas if \(x\) is to be solved in the real domain then only \(\{0\}\) is returned.
Why do we use Sets as an output type?¶
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 True
ComplexRegion
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 nonnegative 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
ndimensional 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 predefined 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
.
Input API of solveset
¶
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 theflags
argument ofsolve
, which had made the input API messy and output API inconsistent.
What is this domain argument about?¶
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, namelysolveset_real
orsolveset_complex
. It’s unlike the oldsolve
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()
What are the general methods employed by solveset to solve an equation?¶
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, thensolve_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 functionssolveset_real
orsolveset_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
andinvert_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 solvef
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.
How do we manipulate and return an infinite solution?¶
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 theImageSet
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 theComplexRegion
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 theProductSet
is the range of the value of \(r\), which is the radius of the circle and theInterval
is the range of \(\theta\), the angle from the \(x\) axis representing a unit circle in the Argand plane.Note: We also have nonpolar 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\).
How does solveset
ensure that it is not returning any wrong solution?¶
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 ifn
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}Though, there still a lot of work needs to be done in this regard.
Search based solver and stepbystep solution¶
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 \(\{xf(x)=0; x \in S\}\) and \(\{xf(x)>0; x \in S\}\) but a more powerful thing aboutConditionSet
is that it allows us to write the intermediate steps as set to set transformation. Some of the transformations are:
Composition: \(\{xf(g(x))=0;x \in S\} \Rightarrow \{xg(x)=y; x \in S, y \in \{zf(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: \(\{xf(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: \(\{xf(x)g(x)=0; x \in S\}\)
\(\Rightarrow \{xf(x)=0; x \in S\} U \{xg(x)=0; x \in S\}\) \(\text{ given } f(x) \text{ and } g(x) \text{ are bounded.}\) \(\Rightarrow \{xf(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.
How do we deal with cases where only some of the solutions are known?¶
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\}\)
What will you do with the old solve?¶
There are still a few things
solveset
can’t do, which the oldsolve
can, such as solving non linear multivariate & LambertW type equations. Hence, it’s not yet a perfect replacement for oldsolve
. The ultimate goal is to:
 Replace
solve
withsolveset
once solveset is at least as powerful assolve
, i.e.,solveset
does everything thatsolve
can do currently, and eventually rename
solveset
tosolve
.
How are symbolic parameters handled in solveset?¶
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 theInterval
\(( \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 notempty. 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]
References¶
[1] https://github.com/sympy/sympy/wiki/GSoC2015Ideas#solvers
[2] https://github.com/sympy/sympy/wiki/GSoC2014ApplicationHarshGupta:Solvers
[3] https://github.com/sympy/sympy/wiki/GSoC2015ApplicationAMiTKumar–Solvers:ExtendingSolveset
[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
Solveset Module Reference¶
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.

sympy.solvers.solveset.
solveset
(f, symbol=None, domain=S.Complexes)[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 \(f\) is False or nonzero. 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.
See also
solveset_real
 solver for real domain
solveset_complex
 solver for complex domain
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
 The default domain is complex. Not specifying a domain will lead to the solving of the equation in the complex domain (and this is not affected by the assumptions on the symbol):
>>> 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()}
 If you want to use \(solveset\) to solve the equation in the real domain, provide a real domain. (Using \(solveset\_real\) does this automatically.)
>>> 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()}
 Inequalities can be solved over the real domain only. Use of a complex domain leads to a NotImplementedError.
>>> solveset(exp(x) > 1, x, R) (0, oo)

sympy.solvers.solveset.
invert_complex
(f_x, y, x, domain=S.Complexes)¶ 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) }
whereg(x)
is a simpler function thanf(x)
. The return value is a tuple(g(x), set_h)
, whereg(x)
is a function ofx
andset_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, ify = Abs(x)  n
, is inverted in the real domain, then, theset_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 ofexp(x)
, the inversion vialog
is multivalued 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})

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 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]])
 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 GaussJordan 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 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)}
 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*e)/(a*e  b*d), (a*f  c*d)/(a*e  b*d))}
 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)}
 For an empty system linsolve returns empty set
>>> linsolve([ ], x) EmptySet()
Diophantine Equations (DEs)¶
See Diophantine