Repository: sympy/sympy

Issue:
solve() leads to ZeroDivisionError: polynomial division
In sympy 1.0 with Python 3.5 I tried to solve the following system of polynomial equations:

```python
from sympy import symbols, solve

a, b, c, d, e, f, g = symbols('a b c d e f g')
terms = [
    1 + a*b + d*e,
    1 + a*c + d*f,
    1 + b*c + e*f,
    g - a**2 - d**2,
    g - b**2 - e**2,
    g - c**2 - f**2,
]
s = solve(terms, [a, b, c, d, e, f, g])
print('Solutions: {!r}'.format(s))
```

However, after ~15 seconds of computation, `solve()` fails with the following exception:

```
Traceback (most recent call last):
  File "<...>", line 14, in <module>
    s = solve(terms, [a, b, c, d, e, f, g])
  File "/usr/lib/python3/dist-packages/sympy/solvers/solvers.py", line 1055, in solve
    solution = _solve_system(f, symbols, **flags)
  File "/usr/lib/python3/dist-packages/sympy/solvers/solvers.py", line 1708, in _solve_system
    res = solve_poly_system(polys, *syms)
  File "/usr/lib/python3/dist-packages/sympy/solvers/polysys.py", line 49, in solve_poly_system
    return solve_generic(polys, opt)
  File "/usr/lib/python3/dist-packages/sympy/solvers/polysys.py", line 228, in solve_generic
    result = _solve_reduced_system(polys, opt.gens, entry=True)
  File "/usr/lib/python3/dist-packages/sympy/solvers/polysys.py", line 222, in _solve_reduced_system
    for solution in _solve_reduced_system(new_system, new_gens):
  File "/usr/lib/python3/dist-packages/sympy/solvers/polysys.py", line 184, in _solve_reduced_system
    basis = groebner(system, gens, polys=True)
  File "/usr/lib/python3/dist-packages/sympy/polys/polytools.py", line 6483, in groebner
    return GroebnerBasis(F, *gens, **args)
  File "/usr/lib/python3/dist-packages/sympy/polys/polytools.py", line 6523, in __new__
    G = _groebner(polys, ring, method=opt.method)
  File "/usr/lib/python3/dist-packages/sympy/polys/groebnertools.py", line 45, in groebner
    G = _groebner(seq, ring)
  File "/usr/lib/python3/dist-packages/sympy/polys/groebnertools.py", line 201, in _buchberger
    r = p.rem(f[:i])
  File "/usr/lib/python3/dist-packages/sympy/polys/rings.py", line 1446, in rem
    raise ZeroDivisionError("polynomial division")
ZeroDivisionError: polynomial division
```


Discussion:

Patch:
diff --git a/sympy/polys/polytools.py b/sympy/polys/polytools.py
index dbab1ecb72..77772f6775 100644
--- a/sympy/polys/polytools.py
+++ b/sympy/polys/polytools.py
@@ -6577,8 +6577,7 @@ def __new__(cls, F, *gens, **args):
         from sympy.polys.rings import PolyRing
         ring = PolyRing(opt.gens, opt.domain, opt.order)
 
-        for i, poly in enumerate(polys):
-            polys[i] = ring.from_dict(poly.rep.to_dict())
+        polys = [ring.from_dict(poly.rep.to_dict()) for poly in polys if poly]
 
         G = _groebner(polys, ring, method=opt.method)
         G = [Poly._from_dict(g, opt) for g in G]


Test patch:
diff --git a/sympy/polys/tests/test_polytools.py b/sympy/polys/tests/test_polytools.py
index 727ad8ddb8..f50ad2e9e8 100644
--- a/sympy/polys/tests/test_polytools.py
+++ b/sympy/polys/tests/test_polytools.py
@@ -3067,6 +3067,7 @@ def test_GroebnerBasis():
     H = [y**3 - 2*y, x**2 - 2*y**2, x*y - 2*y]
     P = [ Poly(h, x, y) for h in H ]
 
+    assert groebner(F + [0], x, y, order='grevlex') == G
     assert isinstance(G, GroebnerBasis) is True
 
     assert len(G) == 3
diff --git a/sympy/solvers/tests/test_solvers.py b/sympy/solvers/tests/test_solvers.py
index 2083b6e109..63fb5d92c6 100644
--- a/sympy/solvers/tests/test_solvers.py
+++ b/sympy/solvers/tests/test_solvers.py
@@ -1794,3 +1794,29 @@ def test_issue_2840_8155():
 
 def test_issue_9567():
     assert solve(1 + 1/(x - 1)) == [0]
+
+def test_issue_12114():
+    a, b, c, d, e, f, g = symbols('a,b,c,d,e,f,g')
+    terms = [1 + a*b + d*e, 1 + a*c + d*f, 1 + b*c + e*f,
+             g - a**2 - d**2, g - b**2 - e**2, g - c**2 - f**2]
+    s = solve(terms, [a, b, c, d, e, f, g])
+    assert s == [{a: -sqrt(-f**2 - 1), b: -sqrt(-f**2 - 1),
+                  c: -sqrt(-f**2 - 1), d: f, e: f, g: -1},
+                 {a: sqrt(-f**2 - 1), b: sqrt(-f**2 - 1),
+                  c: sqrt(-f**2 - 1), d: f, e: f, g: -1},
+                 {a: -sqrt(3)*f/2 - sqrt(-f**2 + 2)/2,
+                  b: sqrt(3)*f/2 - sqrt(-f**2 + 2)/2, c: sqrt(-f**2 + 2),
+                  d: -f/2 + sqrt(-3*f**2 + 6)/2,
+                  e: -f/2 - sqrt(3)*sqrt(-f**2 + 2)/2, g: 2},
+                 {a: -sqrt(3)*f/2 + sqrt(-f**2 + 2)/2,
+                  b: sqrt(3)*f/2 + sqrt(-f**2 + 2)/2, c: -sqrt(-f**2 + 2),
+                  d: -f/2 - sqrt(-3*f**2 + 6)/2,
+                  e: -f/2 + sqrt(3)*sqrt(-f**2 + 2)/2, g: 2},
+                 {a: sqrt(3)*f/2 - sqrt(-f**2 + 2)/2,
+                  b: -sqrt(3)*f/2 - sqrt(-f**2 + 2)/2, c: sqrt(-f**2 + 2),
+                  d: -f/2 - sqrt(-3*f**2 + 6)/2,
+                  e: -f/2 + sqrt(3)*sqrt(-f**2 + 2)/2, g: 2},
+                 {a: sqrt(3)*f/2 + sqrt(-f**2 + 2)/2,
+                  b: -sqrt(3)*f/2 + sqrt(-f**2 + 2)/2, c: -sqrt(-f**2 + 2),
+                  d: -f/2 + sqrt(-3*f**2 + 6)/2,
+                  e: -f/2 - sqrt(3)*sqrt(-f**2 + 2)/2, g: 2}]