From 6359199bb52a4cc44e1b2c95ed3d0fbfa745c23e Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Mon, 1 Jun 2015 11:43:58 +0530 Subject: [PATCH] Improve tests and docs --- sympy/solvers/solveset.py | 66 +++++++++++++++++++++++++--- sympy/solvers/tests/test_solveset.py | 13 ++++++ 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/sympy/solvers/solveset.py b/sympy/solvers/solveset.py index 97ca2e28a919..8ef3e9da1ab2 100644 --- a/sympy/solvers/solveset.py +++ b/sympy/solvers/solveset.py @@ -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] @@ -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 @@ -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 ======= @@ -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") @@ -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] diff --git a/sympy/solvers/tests/test_solveset.py b/sympy/solvers/tests/test_solveset.py index 018e35cc72af..5934797f959e 100644 --- a/sympy/solvers/tests/test_solveset.py +++ b/sympy/solvers/tests/test_solveset.py @@ -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") @@ -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]])