Skip to content

Commit

Permalink
Use Poly.all_roots() method in polynomial solvers
Browse files Browse the repository at this point in the history
  • Loading branch information
skirpichev committed Jan 26, 2017
1 parent 5ec40b3 commit 389d7e9
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 19 deletions.
16 changes: 10 additions & 6 deletions diofant/solvers/polysys.py
@@ -1,8 +1,10 @@
"""Solvers of systems of polynomial equations. """

import collections

from diofant.core import S
from diofant.matrices import Matrix
from diofant.polys import Poly, groebner, roots, sring
from diofant.polys import Poly, groebner, sring
from diofant.polys.polytools import parallel_poly_from_expr
from diofant.polys.polyerrors import (ComputationFailed,
PolificationFailed, CoercionFailed)
Expand All @@ -18,6 +20,13 @@ class SolveFailed(Exception):
"""Raised when solver's conditions weren't met. """


def roots(p):
r = collections.defaultdict(int)
for v in p.all_roots():
r[v] += 1
return r


def solve_linear_system(system, *symbols, **flags):
r"""Solve system of linear equations.
Expand Down Expand Up @@ -189,11 +198,6 @@ def solve_generic(polys, opt):
polynomial system is generated. Applying the above procedure
recursively, a finite number of solutions can be found.
The ability of finding all solutions by this procedure depends
on the root finding algorithms. If no solutions were found, it
means only that roots() failed, but the system is solvable. To
overcome this difficulty use numerical algorithms instead.
References
==========
Expand Down
7 changes: 6 additions & 1 deletion diofant/solvers/tests/test_polysys.py
Expand Up @@ -3,7 +3,7 @@
import pytest

from diofant import flatten, I, Integer, Poly, QQ, Rational, S, sqrt, symbols
from diofant.polys import PolynomialError, ComputationFailed
from diofant.polys import PolynomialError, ComputationFailed, RootOf
from diofant.solvers.polysys import solve_poly_system

from diofant.abc import x, y, z
Expand Down Expand Up @@ -55,6 +55,11 @@ def test_solve_poly_system():
lambda: solve_poly_system([x**3 - y**3], x, y))
pytest.raises(PolynomialError, lambda: solve_poly_system([1/x], x))

assert (solve_poly_system([x**6 + x - 1], x) ==
[{x: RootOf(x**6 + x - 1, 0)}, {x: RootOf(x**6 + x - 1, 1)},
{x: RootOf(x**6 + x - 1, 2)}, {x: RootOf(x**6 + x - 1, 3)},
{x: RootOf(x**6 + x - 1, 4)}, {x: RootOf(x**6 + x - 1, 5)}])


def test_solve_biquadratic():
x0, y0, x1, y1, r = symbols('x0 y0 x1 y1 r')
Expand Down
13 changes: 9 additions & 4 deletions diofant/solvers/tests/test_solvers.py
Expand Up @@ -378,7 +378,10 @@ def test_solve_transcendental():
assert solve(x**y - 1) == [{x: 1}, {y: 0}]
assert solve([x**y - 1]) == [{x: 1}, {y: 0}]
assert solve(x*y*(x**2 - y**2)) == [{x: 0}, {x: -y}, {x: y}, {y: 0}]
assert solve([x*y*(x**2 - y**2)]) == [{x: 0}, {x: -y}, {x: y}, {y: 0}]
assert (solve([x*y*(x**2 - y**2)], check=False) ==
[{x: RootOf(x**3 - x*y**2, x, 0)},
{x: RootOf(x**3 - x*y**2, x, 1)},
{x: RootOf(x**3 - x*y**2, x, 2)}])
# issue sympy/sympy#4739
assert solve(exp(log(5)*x) - 2**x, x) == [0]

Expand Down Expand Up @@ -1352,9 +1355,11 @@ def test_sympyissue_8587():
def test_high_order_multivariate():
assert len(solve(a*x**3 - x + 1, x)) == 3
assert len(solve(a*x**4 - x + 1, x)) == 4
assert solve(a*x**5 - x + 1, x) == [] # incomplete solution allowed
pytest.raises(NotImplementedError, lambda:
solve(a*x**5 - x + 1, x, incomplete=False))
assert solve(a*x**5 - x + 1, x) == [RootOf(a*x**5 - x + 1, x, 0),
RootOf(a*x**5 - x + 1, x, 1),
RootOf(a*x**5 - x + 1, x, 2),
RootOf(a*x**5 - x + 1, x, 3),
RootOf(a*x**5 - x + 1, x, 4)]

# result checking must always consider the denominator and RootOf
# must be checked, too
Expand Down

0 comments on commit 389d7e9

Please sign in to comment.