Poly solvers
Contents
Poly solvers¶
This module provides functions for solving systems of linear equations that are used internally in sympy.
Low-level linear systems solver.
solve_lin_sys¶
- sympy.polys.solvers.solve_lin_sys(eqs, ring, _raw=True)[source]¶
Solve a system of linear equations from a PolynomialRing
- Parameters
eqs: list[PolyElement]
The linear equations to be solved as elements of a PolynomialRing (assumed equal to zero).
ring: PolynomialRing
The polynomial ring from which eqs are drawn. The generators of this ring are the unkowns to be solved for and the domain of the ring is the domain of the coefficients of the system of equations.
_raw: bool
If _raw is False, the keys and values in the returned dictionary will be of type Expr (and the unit of the field will be removed from the keys) otherwise the low-level polys types will be returned, e.g. PolyElement: PythonRational.
- Returns
None
if the system has no solution.dict[Symbol, Expr] if _raw=False
dict[Symbol, DomainElement] if _raw=True.
Explanation
Solves a system of linear equations given as PolyElement instances of a PolynomialRing. The basic arithmetic is carried out using instance of DomainElement which is more efficient than
Expr
for the most common inputs.While this is a public function it is intended primarily for internal use so its interface is not necessarily convenient. Users are suggested to use the
sympy.solvers.solveset.linsolve()
function (which uses this function internally) instead.Examples
>>> from sympy import symbols >>> from sympy.polys.solvers import solve_lin_sys, sympy_eqs_to_ring >>> x, y = symbols('x, y') >>> eqs = [x - y, x + y - 2] >>> eqs_ring, ring = sympy_eqs_to_ring(eqs, [x, y]) >>> solve_lin_sys(eqs_ring, ring) {y: 1, x: 1}
Passing
_raw=False
returns the same result except that the keys areExpr
rather than low-level poly types.>>> solve_lin_sys(eqs_ring, ring, _raw=False) {x: 1, y: 1}
See also
sympy_eqs_to_ring
prepares the inputs to
solve_lin_sys
.linsolve
linsolve
usessolve_lin_sys
internally.sympy.solvers.solvers.solve
solve
usessolve_lin_sys
internally.
eqs_to_matrix¶
- sympy.polys.solvers.eqs_to_matrix(eqs_coeffs, eqs_rhs, gens, domain)[source]¶
Get matrix from linear equations in dict format.
- Parameters
eqs_coeffs: list[dict[Symbol, DomainElement]]
The left hand sides of the equations as dicts mapping from symbols to coefficients where the coefficients are instances of DomainElement.
eqs_rhs: list[DomainElements]
The right hand sides of the equations as instances of DomainElement.
gens: list[Symbol]
The unknowns in the system of equations.
domain: Domain
The domain for coefficients of both lhs and rhs.
- Returns
The augmented matrix representation of the system as a DomainMatrix.
Explanation
Get the matrix representation of a system of linear equations represented as dicts with low-level DomainElement coefficients. This is an internal function that is used by solve_lin_sys.
Examples
>>> from sympy import symbols, ZZ >>> from sympy.polys.solvers import eqs_to_matrix >>> x, y = symbols('x, y') >>> eqs_coeff = [{x:ZZ(1), y:ZZ(1)}, {x:ZZ(1), y:ZZ(-1)}] >>> eqs_rhs = [ZZ(0), ZZ(-1)] >>> eqs_to_matrix(eqs_coeff, eqs_rhs, [x, y], ZZ) DomainMatrix([[1, 1, 0], [1, -1, 1]], (2, 3), ZZ)
See also
solve_lin_sys
Uses
eqs_to_matrix()
internally
sympy_eqs_to_ring¶
- sympy.polys.solvers.sympy_eqs_to_ring(eqs, symbols)[source]¶
Convert a system of equations from Expr to a PolyRing
- Parameters
eqs: List of Expr
A list of equations as Expr instances
symbols: List of Symbol
A list of the symbols that are the unknowns in the system of equations.
- Returns
Tuple[List[PolyElement], Ring]: The equations as PolyElement instances
and the ring of polynomials within which each equation is represented.
Explanation
High-level functions like
solve
expect Expr as inputs but can usesolve_lin_sys
internally. This function converts equations fromExpr
to the low-level poly types used by thesolve_lin_sys
function.Examples
>>> from sympy import symbols >>> from sympy.polys.solvers import sympy_eqs_to_ring >>> a, x, y = symbols('a, x, y') >>> eqs = [x-y, x+a*y] >>> eqs_ring, ring = sympy_eqs_to_ring(eqs, [x, y]) >>> eqs_ring [x - y, x + a*y] >>> type(eqs_ring[0]) <class 'sympy.polys.rings.PolyElement'> >>> ring ZZ(a)[x,y]
With the equations in this form they can be passed to
solve_lin_sys
:>>> from sympy.polys.solvers import solve_lin_sys >>> solve_lin_sys(eqs_ring, ring) {y: 0, x: 0}
_solve_lin_sys¶
- sympy.polys.solvers._solve_lin_sys(eqs_coeffs, eqs_rhs, ring)[source]¶
Solve a linear system from dict of PolynomialRing coefficients
Explanation
This is an internal function used by
solve_lin_sys()
after the equations have been preprocessed. The role of this function is to split the system into connected components and pass those to_solve_lin_sys_component()
.Examples
Setup a system for \(x-y=0\) and \(x+y=2\) and solve:
>>> from sympy import symbols, sring >>> from sympy.polys.solvers import _solve_lin_sys >>> x, y = symbols('x, y') >>> R, (xr, yr) = sring([x, y], [x, y]) >>> eqs = [{xr:R.one, yr:-R.one}, {xr:R.one, yr:R.one}] >>> eqs_rhs = [R.zero, -2*R.one] >>> _solve_lin_sys(eqs, eqs_rhs, R) {y: 1, x: 1}
See also
solve_lin_sys
This function is used internally by
solve_lin_sys()
.
_solve_lin_sys_component¶
- sympy.polys.solvers._solve_lin_sys_component(eqs_coeffs, eqs_rhs, ring)[source]¶
Solve a linear system from dict of PolynomialRing coefficients
Explanation
This is an internal function used by
solve_lin_sys()
after the equations have been preprocessed. After_solve_lin_sys()
splits the system into connected components this function is called for each component. The system of equations is solved using Gauss-Jordan elimination with division followed by back-substitution.Examples
Setup a system for \(x-y=0\) and \(x+y=2\) and solve:
>>> from sympy import symbols, sring >>> from sympy.polys.solvers import _solve_lin_sys_component >>> x, y = symbols('x, y') >>> R, (xr, yr) = sring([x, y], [x, y]) >>> eqs = [{xr:R.one, yr:-R.one}, {xr:R.one, yr:R.one}] >>> eqs_rhs = [R.zero, -2*R.one] >>> _solve_lin_sys_component(eqs, eqs_rhs, R) {y: 1, x: 1}
See also
solve_lin_sys
This function is used internally by
solve_lin_sys()
.