Skip to content

Commit

Permalink
Merge pull request #17723 from czgdp1807/pr-13540
Browse files Browse the repository at this point in the history
`solve` now acknowledges `dict=True` for Relational args
  • Loading branch information
czgdp1807 committed Oct 21, 2019
2 parents 78d5a35 + 6c377a1 commit 4c87ea3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
25 changes: 25 additions & 0 deletions sympy/solvers/solvers.py
Expand Up @@ -58,6 +58,7 @@

from types import GeneratorType
from collections import defaultdict
import itertools
import warnings


Expand Down Expand Up @@ -983,6 +984,30 @@ def _sympified_list(w):
f[i] = fi

if fi.is_Relational:
if flags.get('dict', False):
solution = reduce_inequalities(f, symbols=symbols)
if isinstance(solution, Equality):
return [{solution.lhs: solution.rhs}]
solution = list(solution.args)
for sol in solution:
# Behavior for types like StrictLessThan is not defined
if not isinstance(sol, (Or, Equality)):
warnings.warn(filldedent('''
Warning: Ignoring dict=True due to
incompatible type of %s.''' % sol))
return reduce_inequalities(f, symbols=symbols)
intermediate = [sol for sol in solution
if isinstance(sol, Equality)]
solution[:] = [sol.args for sol in solution
if sol not in intermediate]
solutions = [intermediate + list(sol)
for sol in itertools.product(*solution)]
for i in range(len(solutions)):
ele = {}
for sol in solutions[i]:
ele[sol.lhs] = sol.rhs
solutions[i] = ele
return [] if solutions == [{}] else solutions
return reduce_inequalities(f, symbols=symbols)

if isinstance(fi, Poly):
Expand Down
10 changes: 10 additions & 0 deletions sympy/solvers/tests/test_solvers.py
Expand Up @@ -175,6 +175,16 @@ def test_solve_args():
assert solve([x-1, False], [x], set=True) == ([], set())


def test_solve_dict():
assert solve(Eq(2*x,1), dict=True) == [{x: S(1)/2}]
assert solve([Eq(x**2-1, 0), Gt(x, 0)], (x,), dict=True) == [{x: 1}]
assert solve([Eq(x**3-6*x**2+11*x-6, 0), Eq(y**2, 1), Eq(z, 1)],
(x, y, z,), dict=True) == [{x: 1, y: -1, z: 1}, {x: 1, y: 1, z: 1},
{x: 2, y: -1, z: 1}, {x: 2, y: 1, z: 1},
{x: 3, y: -1, z: 1}, {x: 3, y: 1, z: 1}]
assert solve(Gt(x, 0), (x,), dict=True) == And(Lt(0, x), Lt(x, oo))


def test_solve_polynomial1():
assert solve(3*x - 2, x) == [Rational(2, 3)]
assert solve(Eq(3*x, 2), x) == [Rational(2, 3)]
Expand Down

0 comments on commit 4c87ea3

Please sign in to comment.