Repository: sympy/sympy

Issue:
linsolve returns incorrect solutions for nonlinear equations
For example,
`linsolve([x + y - 1, x ** 2 + y - 3], [x, y])`
return the incorrect solution `{(-2, 3)}`. I expect it to raise an error because not all equations are linear.

Discussion:

Patch:
diff --git a/sympy/solvers/solveset.py b/sympy/solvers/solveset.py
index 0ab5d5539d..7c3c13a6c4 100644
--- a/sympy/solvers/solveset.py
+++ b/sympy/solvers/solveset.py
@@ -1288,6 +1288,17 @@ def linsolve(system, *symbols):
 
         # 3). List of equations Form
         if not system[0].is_Matrix:
+            system = list(system)
+            for i, eq in enumerate(system):
+                try:
+                    # since we are checking it, we might as well take the
+                    # expanded expr that it will give
+                    system[i] = Poly(eq, symbols).as_expr()
+                    if any (degree(eq, sym) > 1 for sym in symbols):
+                        raise PolynomialError
+                except PolynomialError:
+                        raise ValueError(filldedent(
+'%s contains non-linear terms of variables to be evaluated') %(eq))
             A, b = linear_eq_to_matrix(system, symbols)
 
     else:


Test patch:
diff --git a/sympy/solvers/tests/test_solveset.py b/sympy/solvers/tests/test_solveset.py
index 87f0a23ac6..e6f41a01b5 100644
--- a/sympy/solvers/tests/test_solveset.py
+++ b/sympy/solvers/tests/test_solveset.py
@@ -1011,6 +1011,11 @@ def test_linsolve():
     # raise ValueError for garbage value
     raises(ValueError, lambda: linsolve(Eqns[0], x1, x2, x3, x4))
 
+    #raise ValueError if equations are non-linear in given variables
+    raises(ValueError, lambda: linsolve([x + y - 1, x ** 2 + y - 3], [x, y]))
+    raises(ValueError, lambda: linsolve([cos(x) + y, x + y], [x, y]))
+    assert linsolve([x + z - 1, x ** 2 + y - 3], [z, y]) == {(-x + 1, -x**2 + 3)}
+
     # Fully symbolic test
     a, b, c, d, e, f = symbols('a, b, c, d, e, f')
     A = Matrix([[a, b], [c, d]])