Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improve tests and docs
  • Loading branch information
aktech committed Jun 1, 2015
1 parent f6fee27 commit 6359199
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
66 changes: 61 additions & 5 deletions sympy/solvers/solveset.py
Expand Up @@ -933,8 +933,26 @@ def linear_eq_to_matrix(equations, *symbols):
[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]])
"""

if not symbols:
raise ValueError('Symbols must be given, for which coefficients \
are to be found.')

if hasattr(symbols[0], '__iter__'):
symbols = symbols[0]

Expand Down Expand Up @@ -968,12 +986,13 @@ def linear_eq_to_matrix(equations, *symbols):

def linsolve(system, *symbols):
r"""
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.
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.
All Standard input formats are supported:
For the given set of Equations, the respective input types are given below:
For the given set of Equations, the respective input types
are given below:
3*x + 2*y - z = 1
2*x - 2*y + 4*z = -2
Expand All @@ -997,6 +1016,14 @@ def linsolve(system, *symbols):
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
=======
Expand All @@ -1009,12 +1036,13 @@ def linsolve(system, *symbols):
ValueError
The input is not valid.
The linear system has no solution.
The symbols are not given.
Examples
========
>>> from sympy.solvers.solveset import linsolve
>>> from sympy import Matrix
>>> from sympy import Matrix, S
>>> from sympy import symbols
>>> x, y, z = symbols("x, y, z")
Expand Down Expand Up @@ -1045,8 +1073,36 @@ def linsolve(system, *symbols):
>>> 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))}
"""

if not symbols:
raise ValueError('Symbols must be given, for which solution of the \
system are to be found.')

if hasattr(symbols[0], '__iter__'):
symbols = symbols[0]

Expand Down
13 changes: 13 additions & 0 deletions sympy/solvers/tests/test_solveset.py
Expand Up @@ -821,6 +821,16 @@ def test_linear_eq_to_matrix():
assert A == Matrix([[3, 2, -1], [2, -2, 4], [-2, 1, -2]])
assert b == Matrix([[1], [-2], [0]])

# Pure symbolic coefficients
from sympy.abc import a, b, c, d, e, f, g, h, i, j, k, l
eqns3 = [a*x + b*y + c*z - d, e*x + f*y + g*z - h, i*x + j*y + k*z - l]
A, B = linear_eq_to_matrix(eqns3, x, y, z)
assert A == Matrix([[a, b, c], [e, f, g], [i, j, k]])
assert B == Matrix([[d], [h], [l]])

# raise ValueError if no symbols are given
raises(ValueError, lambda: linear_eq_to_matrix(eqns3))


def test_linsolve():
x, y, z, u, v, w = symbols("x, y, z, u, v, w")
Expand All @@ -838,6 +848,9 @@ def test_linsolve():
assert linsolve(Eqns, (x1, x2, x3, x4)) == sol
assert linsolve(system, (x1, x2, x3, x4)) == sol

# raise ValueError if no symbols are given
raises(ValueError, lambda: linsolve(system))

# Fully symbolic test
a, b, c, d, e, f = symbols('a, b, c, d, e, f')
A = Matrix([[a, b], [c, d]])
Expand Down

0 comments on commit 6359199

Please sign in to comment.