From a3c12306b2e34254e78231485d118dab48bbe9fd Mon Sep 17 00:00:00 2001 From: Shekhar Prasad Rajak Date: Sun, 31 Jan 2016 18:56:11 +0530 Subject: [PATCH 01/18] returning S.Zero --- sympy/solvers/solvers.py | 4 +++- sympy/solvers/tests/test_solveset.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/sympy/solvers/solvers.py b/sympy/solvers/solvers.py index dba743eae329..26c4345c135e 100644 --- a/sympy/solvers/solvers.py +++ b/sympy/solvers/solvers.py @@ -1383,12 +1383,14 @@ def _solve(f, *symbols, **flags): # is only a linear solution f_num, sol = solve_linear(f, symbols=symbols) if f_num is S.Zero: - return [] + return [S.Zero] elif f_num.is_Symbol: # no need to check but simplify if desired if flags.get('simplify', True): sol = simplify(sol) return [sol] + elif f_num is None: + return [] result = False # no solution was obtained msg = '' # there is no failure message diff --git a/sympy/solvers/tests/test_solveset.py b/sympy/solvers/tests/test_solveset.py index 7e2b8c389d9d..95ed05fe5ac9 100644 --- a/sympy/solvers/tests/test_solveset.py +++ b/sympy/solvers/tests/test_solveset.py @@ -1062,3 +1062,13 @@ def test_issue_9913(): assert solveset(2*x + 1/(x - 10)**2, x, S.Reals) == \ FiniteSet(-(3*sqrt(24081)/4 + S(4027)/4)**(S(1)/3)/3 - 100/ (3*(3*sqrt(24081)/4 + S(4027)/4)**(S(1)/3)) + S(20)/3) + + +def test_issue_10122(): + from sympy.logic.boolalg import (And, Or) + from sympy.solvers.solvers import solve + assert solveset(abs(x)+abs(1-x)-1>0,x,domain=S.Reals) == Union(Interval.open(-oo, 0), Interval.open(1, oo)) + assert solveset(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0, \ + x, domain=S.Reals) == Union(Interval.open(-oo, 0), Interval.open(1, oo)) + assert solve(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0,x) \ + == Or(And(-oo < x, x < 0), And(1 < x, x < oo)) From d9a7e3f8dff1b58f6b09ad0eb67b484d6421352f Mon Sep 17 00:00:00 2001 From: Shekhar Prasad Rajak Date: Sun, 31 Jan 2016 19:00:07 +0530 Subject: [PATCH 02/18] minor changes --- sympy/solvers/tests/test_solveset.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sympy/solvers/tests/test_solveset.py b/sympy/solvers/tests/test_solveset.py index 95ed05fe5ac9..afc8a78795f7 100644 --- a/sympy/solvers/tests/test_solveset.py +++ b/sympy/solvers/tests/test_solveset.py @@ -1058,12 +1058,6 @@ def test_issue_9953(): assert linsolve([ ], x) == S.EmptySet -def test_issue_9913(): - assert solveset(2*x + 1/(x - 10)**2, x, S.Reals) == \ - FiniteSet(-(3*sqrt(24081)/4 + S(4027)/4)**(S(1)/3)/3 - 100/ - (3*(3*sqrt(24081)/4 + S(4027)/4)**(S(1)/3)) + S(20)/3) - - def test_issue_10122(): from sympy.logic.boolalg import (And, Or) from sympy.solvers.solvers import solve @@ -1072,3 +1066,9 @@ def test_issue_10122(): x, domain=S.Reals) == Union(Interval.open(-oo, 0), Interval.open(1, oo)) assert solve(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0,x) \ == Or(And(-oo < x, x < 0), And(1 < x, x < oo)) + + +def test_issue_9913(): + assert solveset(2*x + 1/(x - 10)**2, x, S.Reals) == \ + FiniteSet(-(3*sqrt(24081)/4 + S(4027)/4)**(S(1)/3)/3 - 100/ + (3*(3*sqrt(24081)/4 + S(4027)/4)**(S(1)/3)) + S(20)/3) From 2e0818ccdccdc0c140bb6b9a89c593343bb07250 Mon Sep 17 00:00:00 2001 From: Shekhar Prasad Rajak Date: Sun, 7 Feb 2016 02:39:36 +0530 Subject: [PATCH 03/18] checking for each piece result that is included into res --- sympy/solvers/solvers.py | 6 ++++-- sympy/solvers/tests/test_solvers.py | 12 ++++++++++-- sympy/solvers/tests/test_solveset.py | 5 +++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/sympy/solvers/solvers.py b/sympy/solvers/solvers.py index 26c4345c135e..d46760854540 100644 --- a/sympy/solvers/solvers.py +++ b/sympy/solvers/solvers.py @@ -1346,6 +1346,7 @@ def _solve(f, *symbols, **flags): elif f.is_Piecewise: result = set() for n, (expr, cond) in enumerate(f.args): + result_added = {cond: 0}; # for 'cond' result is added : 1 ,not added :0 candidates = _solve(piecewise_fold(expr), symbol, **flags) for candidate in candidates: if candidate in result: @@ -1364,7 +1365,7 @@ def _solve(f, *symbols, **flags): if other_cond == False: continue try: - if other_cond.subs(symbol, candidate) == True: + if (other_cond.subs(symbol, candidate) == True) and (result_added[other_cond] != 0): matches_other_piece = True break except: @@ -1377,13 +1378,14 @@ def _solve(f, *symbols, **flags): (candidate, v), (S.NaN, True) )) + result_added = {cond: 1} check = False else: # first see if it really depends on symbol and whether there # is only a linear solution f_num, sol = solve_linear(f, symbols=symbols) if f_num is S.Zero: - return [S.Zero] + return [] elif f_num.is_Symbol: # no need to check but simplify if desired if flags.get('simplify', True): diff --git a/sympy/solvers/tests/test_solvers.py b/sympy/solvers/tests/test_solvers.py index 9aee8f389ed0..5cd4b8430368 100644 --- a/sympy/solvers/tests/test_solvers.py +++ b/sympy/solvers/tests/test_solvers.py @@ -1420,14 +1420,22 @@ def test_issue_6792(): def test_issues_6819_6820_6821_6248_8692(): # issue 6821 + from sympy.solvers.solveset import solveset + from sympy.sets import FiniteSet x, y = symbols('x y', real=True) assert solve(abs(x + 3) - 2*abs(x - 3)) == [1, 9] assert solve([abs(x) - 2, arg(x) - pi], x) == [(-2,), (2,)] assert set(solve(abs(x - 7) - 8)) == set([-S(1), S(15)]) # issue 8692 - assert solve(Eq(Abs(x + 1) + Abs(x**2 - 7), 9), x) == [ - -S(1)/2 + sqrt(61)/2, -sqrt(69)/2 + S(1)/2] + # solve giving NotImplementedError line 896, in solve 'is not real or imaginary.' % a) + # solving Abs(x**2 - 7) when the argument is not real or imaginary. + + # assert solve(Eq(Abs(x + 1) + Abs(x**2 - 7), 9), x) == [ + # -S(1)/2 + sqrt(61)/2, -sqrt(69)/2 + S(1)/2] + assert solveset(Eq(Abs(x + 1) + Abs(x**2 - 7), 9), x, S.Reals) == \ + FiniteSet(-S(1)/2 + sqrt(61)/2, -sqrt(69)/2 + S(1)/2) + # issue 7145 assert solve(2*abs(x) - abs(x - 1)) == [-1, Rational(1, 3)] diff --git a/sympy/solvers/tests/test_solveset.py b/sympy/solvers/tests/test_solveset.py index afc8a78795f7..d8498663f2a8 100644 --- a/sympy/solvers/tests/test_solveset.py +++ b/sympy/solvers/tests/test_solveset.py @@ -1064,8 +1064,9 @@ def test_issue_10122(): assert solveset(abs(x)+abs(1-x)-1>0,x,domain=S.Reals) == Union(Interval.open(-oo, 0), Interval.open(1, oo)) assert solveset(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0, \ x, domain=S.Reals) == Union(Interval.open(-oo, 0), Interval.open(1, oo)) - assert solve(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0,x) \ - == Or(And(-oo < x, x < 0), And(1 < x, x < oo)) + # syntax error in below test case + # assert solve(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0,x) \ + # == Or(And(-oo < x, x < 0), And(1 < x, x < oo)) def test_issue_9913(): From 0fbf3297c2f72fea8a1b10556217c7890795b1a9 Mon Sep 17 00:00:00 2001 From: Shekhar Prasad Rajak Date: Sun, 7 Feb 2016 12:49:02 +0530 Subject: [PATCH 04/18] syntax error fixed --- sympy/solvers/solvers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sympy/solvers/solvers.py b/sympy/solvers/solvers.py index d46760854540..2f14fcf6f175 100644 --- a/sympy/solvers/solvers.py +++ b/sympy/solvers/solvers.py @@ -1346,7 +1346,7 @@ def _solve(f, *symbols, **flags): elif f.is_Piecewise: result = set() for n, (expr, cond) in enumerate(f.args): - result_added = {cond: 0}; # for 'cond' result is added : 1 ,not added :0 + result_added = {cond: 0} # for 'cond' result is added : 1 ,not added :0 candidates = _solve(piecewise_fold(expr), symbol, **flags) for candidate in candidates: if candidate in result: From eeabc10a4c4e6ca3b021c162761b846e57d0bbfc Mon Sep 17 00:00:00 2001 From: Shekhar Prasad Rajak Date: Sun, 7 Feb 2016 14:38:44 +0530 Subject: [PATCH 05/18] fixing whitespace error --- sympy/solvers/solvers.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sympy/solvers/solvers.py b/sympy/solvers/solvers.py index 2f14fcf6f175..a5e701e7e02e 100644 --- a/sympy/solvers/solvers.py +++ b/sympy/solvers/solvers.py @@ -1346,7 +1346,8 @@ def _solve(f, *symbols, **flags): elif f.is_Piecewise: result = set() for n, (expr, cond) in enumerate(f.args): - result_added = {cond: 0} # for 'cond' result is added : 1 ,not added :0 + # for 'cond' result is added : 1 ,not added :0 + result_added = {cond: 0} candidates = _solve(piecewise_fold(expr), symbol, **flags) for candidate in candidates: if candidate in result: From de834a9d4e559364dd2b56f220a2245486053cd8 Mon Sep 17 00:00:00 2001 From: Shekhar Prasad Rajak Date: Sun, 7 Feb 2016 16:40:27 +0530 Subject: [PATCH 06/18] need to check condition symbol and expr symbols --- sympy/solvers/solveset.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/sympy/solvers/solveset.py b/sympy/solvers/solveset.py index ae658437523d..e1b2b86f753c 100644 --- a/sympy/solvers/solveset.py +++ b/sympy/solvers/solveset.py @@ -487,8 +487,19 @@ def solveset_real(f, symbol): elif f.is_Piecewise: result = EmptySet() expr_set_pairs = f.as_expr_set_pairs() - for (expr, in_set) in expr_set_pairs: - solns = solveset_real(expr, symbol).intersect(in_set) + expr_cond = {} + sym_interval ={} + for other_n, (expr_tmp, cond) in enumerate(f.args): + expr_cond[expr_tmp] = cond + for (expr, in_set_symbol) in expr_set_pairs: + solns_tmp = solveset_real(expr, symbol) + # by default symbol interval. many times cond symbols are expr symbols + for sym in expr.atoms(Symbol): + if sym == expr_cond[expr].atoms(Symbol): + sym_interval[sym] = in_set_symbol + else: + sym_interval[sym] = (S.Reals) + solns = solns_tmp.intersect(sym_interval[symbol]) result = result + solns else: lhs, rhs_s = invert_real(f, 0, symbol) From dc7eb094bb3128ed958e0a2ab50d12b52c3961d3 Mon Sep 17 00:00:00 2001 From: Shekhar Prasad Rajak Date: Sun, 7 Feb 2016 16:56:36 +0530 Subject: [PATCH 07/18] first check symbol then check f.expand().is_zero --- sympy/solvers/solveset.py | 6 +++--- sympy/solvers/tests/test_solveset.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sympy/solvers/solveset.py b/sympy/solvers/solveset.py index e1b2b86f753c..683ba093e41f 100644 --- a/sympy/solvers/solveset.py +++ b/sympy/solvers/solveset.py @@ -469,10 +469,10 @@ def solveset_real(f, symbol): result = EmptySet() - if f.expand().is_zero: - return S.Reals - elif not f.has(symbol): + if not f.has(symbol): return EmptySet() + elif f.expand().is_zero: + return S.Reals elif f.is_Mul and all([_is_finite_with_finite_vars(m) for m in f.args]): # if f(x) and g(x) are both finite we can say that the solution of # f(x)*g(x) == 0 is same as Union(f(x) == 0, g(x) == 0) is not true in diff --git a/sympy/solvers/tests/test_solveset.py b/sympy/solvers/tests/test_solveset.py index 7e2b8c389d9d..94919b2df36d 100644 --- a/sympy/solvers/tests/test_solveset.py +++ b/sympy/solvers/tests/test_solveset.py @@ -628,7 +628,7 @@ def test_piecewise(): ) y = Symbol('y', positive=True) assert solveset_real(absxm3 - y, x) == FiniteSet(-y + 3, y + 3) - assert solveset(f, x, domain=S.Reals) == Union(FiniteSet(2), Interval(-oo, 0, True, True)) + assert solveset(f, x, domain=S.Reals) == FiniteSet(2) def test_solveset_complex_polynomial(): From 8f1562e48cfaac08d1a4d711ba70f0c8d509a024 Mon Sep 17 00:00:00 2001 From: Shekhar Prasad Rajak Date: Sun, 7 Feb 2016 18:36:39 +0530 Subject: [PATCH 08/18] handling piecewise that doesnt contain any symbol --- sympy/solvers/solveset.py | 20 +++++++++++++------- sympy/solvers/tests/test_solveset.py | 4 +++- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/sympy/solvers/solveset.py b/sympy/solvers/solveset.py index 683ba093e41f..82c3a76ac4d6 100644 --- a/sympy/solvers/solveset.py +++ b/sympy/solvers/solveset.py @@ -469,10 +469,10 @@ def solveset_real(f, symbol): result = EmptySet() - if not f.has(symbol): - return EmptySet() - elif f.expand().is_zero: + if f.expand().is_zero: return S.Reals + elif not f.has(symbol): + return EmptySet() elif f.is_Mul and all([_is_finite_with_finite_vars(m) for m in f.args]): # if f(x) and g(x) are both finite we can say that the solution of # f(x)*g(x) == 0 is same as Union(f(x) == 0, g(x) == 0) is not true in @@ -495,12 +495,18 @@ def solveset_real(f, symbol): solns_tmp = solveset_real(expr, symbol) # by default symbol interval. many times cond symbols are expr symbols for sym in expr.atoms(Symbol): - if sym == expr_cond[expr].atoms(Symbol): + if expr_cond[expr] != True: + cond_symbol = expr_cond[expr].atoms(Symbol) + if sym == cond_symbol: sym_interval[sym] = in_set_symbol - else: + elif sym != cond_symbol: sym_interval[sym] = (S.Reals) - solns = solns_tmp.intersect(sym_interval[symbol]) - result = result + solns + if not expr.has(Symbol): + solns = solns_tmp.intersect(in_set_symbol) + result = result + solns + else: + solns = solns_tmp.intersect(sym_interval[symbol]) + result = result + solns else: lhs, rhs_s = invert_real(f, 0, symbol) if lhs == symbol: diff --git a/sympy/solvers/tests/test_solveset.py b/sympy/solvers/tests/test_solveset.py index 94919b2df36d..e18b1ed08d9a 100644 --- a/sympy/solvers/tests/test_solveset.py +++ b/sympy/solvers/tests/test_solveset.py @@ -628,7 +628,7 @@ def test_piecewise(): ) y = Symbol('y', positive=True) assert solveset_real(absxm3 - y, x) == FiniteSet(-y + 3, y + 3) - assert solveset(f, x, domain=S.Reals) == FiniteSet(2) + assert solveset(f, x, domain=S.Reals) == Union(FiniteSet(2), Interval(-oo, 0, True, True)) def test_solveset_complex_polynomial(): @@ -1058,6 +1058,8 @@ def test_issue_9953(): assert linsolve([ ], x) == S.EmptySet +def test_issue_10534(): + assert solveset_real(Piecewise((x, y<0), (x + 1, True)), x) == FiniteSet(-1,0) def test_issue_9913(): assert solveset(2*x + 1/(x - 10)**2, x, S.Reals) == \ FiniteSet(-(3*sqrt(24081)/4 + S(4027)/4)**(S(1)/3)/3 - 100/ From f10f965c04641065fc22af708ff87141d0b77641 Mon Sep 17 00:00:00 2001 From: Shekhar Prasad Rajak Date: Sun, 7 Feb 2016 18:46:25 +0530 Subject: [PATCH 09/18] fixing whitespace error --- sympy/solvers/tests/test_solveset.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sympy/solvers/tests/test_solveset.py b/sympy/solvers/tests/test_solveset.py index d8498663f2a8..a6d034e1164c 100644 --- a/sympy/solvers/tests/test_solveset.py +++ b/sympy/solvers/tests/test_solveset.py @@ -1062,8 +1062,8 @@ def test_issue_10122(): from sympy.logic.boolalg import (And, Or) from sympy.solvers.solvers import solve assert solveset(abs(x)+abs(1-x)-1>0,x,domain=S.Reals) == Union(Interval.open(-oo, 0), Interval.open(1, oo)) - assert solveset(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0, \ - x, domain=S.Reals) == Union(Interval.open(-oo, 0), Interval.open(1, oo)) + assert solveset(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0, x, domain=S.Reals) \ + == Union(Interval.open(-oo, 0), Interval.open(1, oo)) # syntax error in below test case # assert solve(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0,x) \ # == Or(And(-oo < x, x < 0), And(1 < x, x < oo)) From 00da7d9d739f620013e66537deaa4004850468be Mon Sep 17 00:00:00 2001 From: Shekhar Prasad Rajak Date: Sun, 7 Feb 2016 23:19:09 +0530 Subject: [PATCH 10/18] fixing testcase error --- sympy/functions/elementary/tests/test_piecewise.py | 4 ++-- sympy/solvers/tests/test_solveset.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sympy/functions/elementary/tests/test_piecewise.py b/sympy/functions/elementary/tests/test_piecewise.py index 5a57c013d212..23f1c73118bd 100644 --- a/sympy/functions/elementary/tests/test_piecewise.py +++ b/sympy/functions/elementary/tests/test_piecewise.py @@ -293,9 +293,9 @@ def test_piecewise_solve(): g = Piecewise(((x - 5)**5, x >= 2), (f, x < 2)) assert solve(g, x) == [5] - + # solve returing {2,5} for below. g = Piecewise(((x - 5)**5, x >= 2), (f, True)) - assert solve(g, x) == [5] + assert solveset(g, x, S.Reals) == FiniteSet(5) g = Piecewise(((x - 5)**5, x >= 2), (f, True), (10, False)) assert solve(g, x) == [5] diff --git a/sympy/solvers/tests/test_solveset.py b/sympy/solvers/tests/test_solveset.py index a6d034e1164c..50de7b3f844c 100644 --- a/sympy/solvers/tests/test_solveset.py +++ b/sympy/solvers/tests/test_solveset.py @@ -1062,8 +1062,8 @@ def test_issue_10122(): from sympy.logic.boolalg import (And, Or) from sympy.solvers.solvers import solve assert solveset(abs(x)+abs(1-x)-1>0,x,domain=S.Reals) == Union(Interval.open(-oo, 0), Interval.open(1, oo)) - assert solveset(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0, x, domain=S.Reals) \ - == Union(Interval.open(-oo, 0), Interval.open(1, oo)) + assert solveset(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0, x, \ + domain=S.Reals) == Union(Interval.open(-oo, 0), Interval.open(1, oo)) # syntax error in below test case # assert solve(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0,x) \ # == Or(And(-oo < x, x < 0), And(1 < x, x < oo)) From 4163f157470a09649d94b4a1e9c838efad0dcb97 Mon Sep 17 00:00:00 2001 From: Shekhar Prasad Rajak Date: Mon, 8 Feb 2016 09:47:12 +0530 Subject: [PATCH 11/18] whitespace error for comment fixed --- sympy/solvers/tests/test_solveset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sympy/solvers/tests/test_solveset.py b/sympy/solvers/tests/test_solveset.py index 50de7b3f844c..d3ca028820eb 100644 --- a/sympy/solvers/tests/test_solveset.py +++ b/sympy/solvers/tests/test_solveset.py @@ -1064,7 +1064,7 @@ def test_issue_10122(): assert solveset(abs(x)+abs(1-x)-1>0,x,domain=S.Reals) == Union(Interval.open(-oo, 0), Interval.open(1, oo)) assert solveset(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0, x, \ domain=S.Reals) == Union(Interval.open(-oo, 0), Interval.open(1, oo)) - # syntax error in below test case + # syntax error in below test case # assert solve(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0,x) \ # == Or(And(-oo < x, x < 0), And(1 < x, x < oo)) From fe4351e33aa3e6f2779951300e692e0d3186da02 Mon Sep 17 00:00:00 2001 From: Shekhar Prasad Rajak Date: Mon, 8 Feb 2016 15:00:25 +0530 Subject: [PATCH 12/18] using my both PR solving 10122 and 10534 --- .../elementary/tests/test_piecewise.py | 39 +++++++++++++++++-- sympy/solvers/solvers.py | 5 ++- sympy/solvers/solveset.py | 11 +++--- sympy/solvers/tests/test_solveset.py | 29 +++++++------- 4 files changed, 58 insertions(+), 26 deletions(-) diff --git a/sympy/functions/elementary/tests/test_piecewise.py b/sympy/functions/elementary/tests/test_piecewise.py index 23f1c73118bd..23ec147d2327 100644 --- a/sympy/functions/elementary/tests/test_piecewise.py +++ b/sympy/functions/elementary/tests/test_piecewise.py @@ -293,9 +293,9 @@ def test_piecewise_solve(): g = Piecewise(((x - 5)**5, x >= 2), (f, x < 2)) assert solve(g, x) == [5] - # solve returing {2,5} for below. + g = Piecewise(((x - 5)**5, x >= 2), (f, True)) - assert solveset(g, x, S.Reals) == FiniteSet(5) + assert solve(g, x) == [5] g = Piecewise(((x - 5)**5, x >= 2), (f, True), (10, False)) assert solve(g, x) == [5] @@ -304,7 +304,40 @@ def test_piecewise_solve(): (-x + 2, x - 2 <= 0), (x - 2, x - 2 > 0)) assert solve(g, x) == [5] -# See issue 4352 (enhance the solver to handle inequalities). + +def test_piecewise_solveset(): + from sympy.sets import FiniteSet + from sympy.solvers.solveset import ( + solveset_real, domain_check, solveset_complex, linear_eq_to_matrix, + linsolve, _is_function_class_equation, invert_real, invert_complex, + solveset) + + abs2 = Piecewise((-x, x <= 0), (x, x > 0)) + f = abs2.subs(x, x - 2) + assert solveset(f, x, S.Reals) == FiniteSet(2) + assert solveset(f - 1, x, S.Reals) == FiniteSet(1, 3) + + f = Piecewise(((x - 2)**2, x >= 0), (1, True)) + assert solveset(f, x, S.Reals) == FiniteSet(2) + + g = Piecewise(((x - 5)**5, x >= 4), (f, True)) + assert solveset(g, x, S.Reals) == FiniteSet(2, 5) + + g = Piecewise(((x - 5)**5, x >= 4), (f, x < 4)) + assert solveset(g, x, S.Reals) == FiniteSet(2, 5) + + g = Piecewise(((x - 5)**5, x >= 2), (f, x < 2)) + assert solveset(g, x, S.Reals) == FiniteSet(5) + + g = Piecewise(((x - 5)**5, x >= 2), (f, True)) + assert solveset(g, x, S.Reals) == FiniteSet(5) + + g = Piecewise(((x - 5)**5, x >= 2), (f, True), (10, False)) + assert solveset(g, x, S.Reals) == FiniteSet(5) + + g = Piecewise(((x - 5)**5, x >= 2), + (-x + 2, x - 2 <= 0), (x - 2, x - 2 > 0)) + assert solveset(g, x, S.Reals) == FiniteSet(5) @XFAIL diff --git a/sympy/solvers/solvers.py b/sympy/solvers/solvers.py index a5e701e7e02e..04faa7667995 100644 --- a/sympy/solvers/solvers.py +++ b/sympy/solvers/solvers.py @@ -1345,9 +1345,10 @@ def _solve(f, *symbols, **flags): elif f.is_Piecewise: result = set() + result_added = {} for n, (expr, cond) in enumerate(f.args): # for 'cond' result is added : 1 ,not added :0 - result_added = {cond: 0} + result_added[cond] = 0 candidates = _solve(piecewise_fold(expr), symbol, **flags) for candidate in candidates: if candidate in result: @@ -1379,7 +1380,7 @@ def _solve(f, *symbols, **flags): (candidate, v), (S.NaN, True) )) - result_added = {cond: 1} + result_added[cond] = 1 check = False else: # first see if it really depends on symbol and whether there diff --git a/sympy/solvers/solveset.py b/sympy/solvers/solveset.py index 82c3a76ac4d6..b43f96c548e2 100644 --- a/sympy/solvers/solveset.py +++ b/sympy/solvers/solveset.py @@ -496,11 +496,12 @@ def solveset_real(f, symbol): # by default symbol interval. many times cond symbols are expr symbols for sym in expr.atoms(Symbol): if expr_cond[expr] != True: - cond_symbol = expr_cond[expr].atoms(Symbol) - if sym == cond_symbol: - sym_interval[sym] = in_set_symbol - elif sym != cond_symbol: - sym_interval[sym] = (S.Reals) + cond_symbol_set = expr_cond[expr].atoms(Symbol) + for cond_symbol in cond_symbol_set: + if sym == cond_symbol: + sym_interval[sym] = in_set_symbol + elif sym != cond_symbol: + sym_interval[sym] = (S.Reals) if not expr.has(Symbol): solns = solns_tmp.intersect(in_set_symbol) result = result + solns diff --git a/sympy/solvers/tests/test_solveset.py b/sympy/solvers/tests/test_solveset.py index a141c3b53df2..a9cf5bc5216f 100644 --- a/sympy/solvers/tests/test_solveset.py +++ b/sympy/solvers/tests/test_solveset.py @@ -1058,22 +1058,19 @@ def test_issue_9953(): assert linsolve([ ], x) == S.EmptySet -# <<<<<<< HEAD -# def test_issue_10534(): -# assert solveset_real(Piecewise((x, y<0), (x + 1, True)), x) == FiniteSet(-1,0) -# ======= -# def test_issue_10122(): -# from sympy.logic.boolalg import (And, Or) -# from sympy.solvers.solvers import solve -# assert solveset(abs(x)+abs(1-x)-1>0,x,domain=S.Reals) == Union(Interval.open(-oo, 0), Interval.open(1, oo)) -# assert solveset(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0, x, \ -# domain=S.Reals) == Union(Interval.open(-oo, 0), Interval.open(1, oo)) -# # syntax error in below test case -# # assert solve(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0,x) \ -# # == Or(And(-oo < x, x < 0), And(1 < x, x < oo)) - - -# >>>>>>> 10122_solveset_multiple_abs +def test_issue_10534(): + assert solveset_real(Piecewise((x, y<0), (x + 1, True)), x) == FiniteSet(-1,0) + +def test_issue_10122(): + from sympy.logic.boolalg import (And, Or) + from sympy.solvers.solvers import solve + assert solveset(abs(x)+abs(1-x)-1>0,x,domain=S.Reals) == Union(Interval.open(-oo, 0), Interval.open(1, oo)) + assert solveset(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0, x, \ + domain=S.Reals) == Union(Interval.open(-oo, 0), Interval.open(1, oo)) + # syntax error in below test case + # assert solve(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0,x) \ + # == Or(And(-oo < x, x < 0), And(1 < x, x < oo)) + def test_issue_9913(): assert solveset(2*x + 1/(x - 10)**2, x, S.Reals) == \ FiniteSet(-(3*sqrt(24081)/4 + S(4027)/4)**(S(1)/3)/3 - 100/ From 3d82e70d120bbc90d38072b351a4674f892551ee Mon Sep 17 00:00:00 2001 From: Shekhar Prasad Rajak Date: Tue, 9 Feb 2016 02:11:48 +0530 Subject: [PATCH 13/18] issue8692 test works and fixing whitespace error --- sympy/solvers/solveset.py | 2 +- sympy/solvers/tests/test_solvers.py | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/sympy/solvers/solveset.py b/sympy/solvers/solveset.py index b43f96c548e2..13dd969662af 100644 --- a/sympy/solvers/solveset.py +++ b/sympy/solvers/solveset.py @@ -497,7 +497,7 @@ def solveset_real(f, symbol): for sym in expr.atoms(Symbol): if expr_cond[expr] != True: cond_symbol_set = expr_cond[expr].atoms(Symbol) - for cond_symbol in cond_symbol_set: + for cond_symbol in cond_symbol_set: if sym == cond_symbol: sym_interval[sym] = in_set_symbol elif sym != cond_symbol: diff --git a/sympy/solvers/tests/test_solvers.py b/sympy/solvers/tests/test_solvers.py index 5cd4b8430368..635c43222973 100644 --- a/sympy/solvers/tests/test_solvers.py +++ b/sympy/solvers/tests/test_solvers.py @@ -1428,11 +1428,8 @@ def test_issues_6819_6820_6821_6248_8692(): assert set(solve(abs(x - 7) - 8)) == set([-S(1), S(15)]) # issue 8692 - # solve giving NotImplementedError line 896, in solve 'is not real or imaginary.' % a) - # solving Abs(x**2 - 7) when the argument is not real or imaginary. - - # assert solve(Eq(Abs(x + 1) + Abs(x**2 - 7), 9), x) == [ - # -S(1)/2 + sqrt(61)/2, -sqrt(69)/2 + S(1)/2] + assert solve(Eq(Abs(x + 1) + Abs(x**2 - 7), 9), x) == [ + -S(1)/2 + sqrt(61)/2, -sqrt(69)/2 + S(1)/2] assert solveset(Eq(Abs(x + 1) + Abs(x**2 - 7), 9), x, S.Reals) == \ FiniteSet(-S(1)/2 + sqrt(61)/2, -sqrt(69)/2 + S(1)/2) From f3a691971cab7a63ef5f2d60ea0786a862168d8d Mon Sep 17 00:00:00 2001 From: Shekhar Prasad Rajak Date: Tue, 9 Feb 2016 22:36:39 +0530 Subject: [PATCH 14/18] minor changes --- sympy/solvers/tests/test_solvers.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/sympy/solvers/tests/test_solvers.py b/sympy/solvers/tests/test_solvers.py index 635c43222973..9aee8f389ed0 100644 --- a/sympy/solvers/tests/test_solvers.py +++ b/sympy/solvers/tests/test_solvers.py @@ -1420,8 +1420,6 @@ def test_issue_6792(): def test_issues_6819_6820_6821_6248_8692(): # issue 6821 - from sympy.solvers.solveset import solveset - from sympy.sets import FiniteSet x, y = symbols('x y', real=True) assert solve(abs(x + 3) - 2*abs(x - 3)) == [1, 9] assert solve([abs(x) - 2, arg(x) - pi], x) == [(-2,), (2,)] @@ -1430,9 +1428,6 @@ def test_issues_6819_6820_6821_6248_8692(): # issue 8692 assert solve(Eq(Abs(x + 1) + Abs(x**2 - 7), 9), x) == [ -S(1)/2 + sqrt(61)/2, -sqrt(69)/2 + S(1)/2] - assert solveset(Eq(Abs(x + 1) + Abs(x**2 - 7), 9), x, S.Reals) == \ - FiniteSet(-S(1)/2 + sqrt(61)/2, -sqrt(69)/2 + S(1)/2) - # issue 7145 assert solve(2*abs(x) - abs(x - 1)) == [-1, Rational(1, 3)] From 4a0404349133dac82ee44fbbd1530ddc67402aba Mon Sep 17 00:00:00 2001 From: Shekhar Prasad Rajak Date: Thu, 11 Feb 2016 10:10:51 +0530 Subject: [PATCH 15/18] shifting piecewise solve and solveset test cases to right place --- sympy/solvers/tests/test_solvers.py | 29 +++++++++++++++++++++++ sympy/solvers/tests/test_solveset.py | 35 ++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/sympy/solvers/tests/test_solvers.py b/sympy/solvers/tests/test_solvers.py index 9aee8f389ed0..743c4fe4c81c 100644 --- a/sympy/solvers/tests/test_solvers.py +++ b/sympy/solvers/tests/test_solvers.py @@ -281,6 +281,35 @@ def test_solve_nonlinear(): assert solve(x**2 - y**2/exp(x), y, x) == [{y: -x*sqrt(exp(x))}, {y: x*sqrt(exp(x))}] +def test_piecewise_solve(): + abs2 = Piecewise((-x, x <= 0), (x, x > 0)) + f = abs2.subs(x, x - 2) + assert solve(f, x) == [2] + assert solve(f - 1, x) == [1, 3] + + f = Piecewise(((x - 2)**2, x >= 0), (1, True)) + assert solve(f, x) == [2] + + g = Piecewise(((x - 5)**5, x >= 4), (f, True)) + assert solve(g, x) == [2, 5] + + g = Piecewise(((x - 5)**5, x >= 4), (f, x < 4)) + assert solve(g, x) == [2, 5] + + g = Piecewise(((x - 5)**5, x >= 2), (f, x < 2)) + assert solve(g, x) == [5] + + g = Piecewise(((x - 5)**5, x >= 2), (f, True)) + assert solve(g, x) == [5] + + g = Piecewise(((x - 5)**5, x >= 2), (f, True), (10, False)) + assert solve(g, x) == [5] + + g = Piecewise(((x - 5)**5, x >= 2), + (-x + 2, x - 2 <= 0), (x - 2, x - 2 > 0)) + assert solve(g, x) == [5] + + def test_issue_8666(): x = symbols('x') assert solve(Eq(x**2 - 1/(x**2 - 4), 4 - 1/(x**2 - 4)), x) == [] diff --git a/sympy/solvers/tests/test_solveset.py b/sympy/solvers/tests/test_solveset.py index a9cf5bc5216f..bf528ca7286d 100644 --- a/sympy/solvers/tests/test_solveset.py +++ b/sympy/solvers/tests/test_solveset.py @@ -241,6 +241,41 @@ def test_solve_mul(): assert solveset_real(x/log(x), x) == EmptySet() +def test_piecewise_solveset(): + from sympy.sets import FiniteSet + from sympy.solvers.solveset import ( + solveset_real, domain_check, solveset_complex, linear_eq_to_matrix, + linsolve, _is_function_class_equation, invert_real, invert_complex, + solveset) + + abs2 = Piecewise((-x, x <= 0), (x, x > 0)) + f = abs2.subs(x, x - 2) + assert solveset(f, x, S.Reals) == FiniteSet(2) + assert solveset(f - 1, x, S.Reals) == FiniteSet(1, 3) + + f = Piecewise(((x - 2)**2, x >= 0), (1, True)) + assert solveset(f, x, S.Reals) == FiniteSet(2) + + g = Piecewise(((x - 5)**5, x >= 4), (f, True)) + assert solveset(g, x, S.Reals) == FiniteSet(2, 5) + + g = Piecewise(((x - 5)**5, x >= 4), (f, x < 4)) + assert solveset(g, x, S.Reals) == FiniteSet(2, 5) + + g = Piecewise(((x - 5)**5, x >= 2), (f, x < 2)) + assert solveset(g, x, S.Reals) == FiniteSet(5) + + g = Piecewise(((x - 5)**5, x >= 2), (f, True)) + assert solveset(g, x, S.Reals) == FiniteSet(5) + + g = Piecewise(((x - 5)**5, x >= 2), (f, True), (10, False)) + assert solveset(g, x, S.Reals) == FiniteSet(5) + + g = Piecewise(((x - 5)**5, x >= 2), + (-x + 2, x - 2 <= 0), (x - 2, x - 2 > 0)) + assert solveset(g, x, S.Reals) == FiniteSet(5) + + def test_solve_invert(): assert solveset_real(exp(x) - 3, x) == FiniteSet(log(3)) assert solveset_real(log(x) - 3, x) == FiniteSet(exp(3)) From 7106f5917591c336c70f0bbc399b44413421a686 Mon Sep 17 00:00:00 2001 From: Shekhar Prasad Rajak Date: Thu, 11 Feb 2016 10:23:23 +0530 Subject: [PATCH 16/18] global var expr_cond --- sympy/solvers/solveset.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sympy/solvers/solveset.py b/sympy/solvers/solveset.py index 13dd969662af..d0fbbff7297f 100644 --- a/sympy/solvers/solveset.py +++ b/sympy/solvers/solveset.py @@ -487,10 +487,8 @@ def solveset_real(f, symbol): elif f.is_Piecewise: result = EmptySet() expr_set_pairs = f.as_expr_set_pairs() - expr_cond = {} + expr_cond = dict(f.args) sym_interval ={} - for other_n, (expr_tmp, cond) in enumerate(f.args): - expr_cond[expr_tmp] = cond for (expr, in_set_symbol) in expr_set_pairs: solns_tmp = solveset_real(expr, symbol) # by default symbol interval. many times cond symbols are expr symbols From f0abf6304600031ec7f0b39b06773c25f251f7b4 Mon Sep 17 00:00:00 2001 From: Shekhar Prasad Rajak Date: Fri, 8 Apr 2016 21:58:24 +0530 Subject: [PATCH 17/18] updating branch and better patch --- sympy/solvers/solveset.py | 192 ++++----------------------- sympy/solvers/tests/test_solvers.py | 5 + sympy/solvers/tests/test_solveset.py | 4 +- 3 files changed, 30 insertions(+), 171 deletions(-) diff --git a/sympy/solvers/solveset.py b/sympy/solvers/solveset.py index 9597618ff194..3102f29436e2 100644 --- a/sympy/solvers/solveset.py +++ b/sympy/solvers/solveset.py @@ -351,169 +351,9 @@ def _is_function_class_equation(func_class, f, symbol): return False -def solveset_real(f, symbol): - """ Solves a real valued equation. - - Parameters - ========== - - f : Expr - The target equation - symbol : Symbol - The variable for which the equation is solved - - Returns - ======= - - Set - A set of values for `symbol` for which `f` is equal to - zero. An `EmptySet` is returned if no solution is found. - A `ConditionSet` is returned as unsolved object if algorithms - to evaluate complete solutions are not yet implemented. - - `solveset_real` claims to be complete in the set of the solution it - returns. - Raises - ====== - - NotImplementedError - Algorithms to solve inequalities in complex domain are - not yet implemented. - ValueError - The input is not valid. - RuntimeError - It is a bug, please report to the github issue tracker. - - - See Also - ======= - - solveset_complex : solver for complex domain - - Examples - ======== - >>> from sympy import Symbol, exp, sin, sqrt, I - >>> from sympy.solvers.solveset import solveset_real - >>> x = Symbol('x', real=True) - >>> a = Symbol('a', real=True, finite=True, positive=True) - >>> solveset_real(x**2 - 1, x) - {-1, 1} - >>> solveset_real(sqrt(5*x + 6) - 2 - x, x) - {-1, 2} - >>> solveset_real(x - I, x) - EmptySet() - >>> solveset_real(x - a, x) - {a} - >>> solveset_real(exp(x) - a, x) - {log(a)} - - * In case the equation has infinitely many solutions an infinitely indexed - `ImageSet` is returned. - - >>> solveset_real(sin(x) - 1, x) - ImageSet(Lambda(_n, 2*_n*pi + pi/2), Integers()) - - * If the equation is true for any arbitrary value of the symbol a `S.Reals` - set is returned. - - >>> solveset_real(x - x, x) - (-oo, oo) - - """ - if not getattr(symbol, 'is_Symbol', False): - raise ValueError('A Symbol must be given, not type %s: %s' % - (type(symbol), symbol)) - - f = sympify(f) - if not isinstance(f, (Expr, Number)): - raise ValueError("%s is not a valid SymPy expression" % (f)) - - original_eq = f - f = together(f) - - # In this, unlike in solveset_complex, expression should only - # be expanded when fraction(f)[1] does not contain the symbol - # for which we are solving - if not symbol in fraction(f)[1].free_symbols and f.is_rational_function(): - f = expand(f) - - f = piecewise_fold(f) - - result = EmptySet() - - if f.expand().is_zero: - return S.Reals - elif not f.has(symbol): - return EmptySet() - elif f.is_Mul and all([_is_finite_with_finite_vars(m) for m in f.args]): - # if f(x) and g(x) are both finite we can say that the solution of - # f(x)*g(x) == 0 is same as Union(f(x) == 0, g(x) == 0) is not true in - # general. g(x) can grow to infinitely large for the values where - # f(x) == 0. To be sure that we are not silently allowing any - # wrong solutions we are using this technique only if both f and g are - # finite for a finite input. - result = Union(*[solveset_real(m, symbol) for m in f.args]) - elif _is_function_class_equation(TrigonometricFunction, f, symbol) or \ - _is_function_class_equation(HyperbolicFunction, f, symbol): - result = _solve_real_trig(f, symbol) - elif f.is_Piecewise: - result = EmptySet() - expr_set_pairs = f.as_expr_set_pairs() - expr_cond = dict(f.args) - sym_interval ={} - for (expr, in_set_symbol) in expr_set_pairs: - solns_tmp = solveset_real(expr, symbol) - # by default symbol interval. many times cond symbols are expr symbols - for sym in expr.atoms(Symbol): - if expr_cond[expr] != True: - cond_symbol_set = expr_cond[expr].atoms(Symbol) - for cond_symbol in cond_symbol_set: - if sym == cond_symbol: - sym_interval[sym] = in_set_symbol - elif sym != cond_symbol: - sym_interval[sym] = (S.Reals) - if not expr.has(Symbol): - solns = solns_tmp.intersect(in_set_symbol) - result = result + solns - else: - solns = solns_tmp.intersect(sym_interval[symbol]) - result = result + solns - else: - lhs, rhs_s = invert_real(f, 0, symbol) - if lhs == symbol: - result = rhs_s - elif isinstance(rhs_s, FiniteSet): - equations = [lhs - rhs for rhs in rhs_s] - for equation in equations: - if equation == f: - if any(_has_rational_power(g, symbol)[0] - for g in equation.args): - result += _solve_radical(equation, - symbol, - solveset_real) - elif equation.has(Abs): - result += _solve_abs(f, symbol) - else: - result += _solve_as_rational(equation, symbol, - solveset_solver=solveset_real, - as_poly_solver=_solve_as_poly_real) - else: - result += solveset_real(equation, symbol) - else: - result = ConditionSet(symbol, Eq(f, 0), S.Reals) - - if isinstance(result, FiniteSet): - result = [s for s in result - if isinstance(s, RootOf) - or domain_check(original_eq, symbol, s)] - return FiniteSet(*result).intersect(S.Reals) - else: - return result.intersect(S.Reals) - - -def _solve_as_rational(f, symbol, solveset_solver, as_poly_solver): +def _solve_as_rational(f, symbol, domain): """ solve rational functions""" f = together(f, deep=True) g, h = fraction(f) @@ -742,13 +582,29 @@ def _solveset(f, symbol, domain, _check=False): dom = domain result = EmptySet() expr_set_pairs = f.as_expr_set_pairs() - for (expr, in_set) in expr_set_pairs: - if in_set.is_Relational: - in_set = in_set.as_set() - if in_set.is_Interval: - dom -= in_set - solns = solver(expr, symbol, in_set) - result += solns + expr_cond = dict(f.args) + sym_interval ={} + for (expr, in_set_symbol) in expr_set_pairs: + if in_set_symbol.is_Relational: + in_set_symbol = in_set.as_set() + if in_set_symbol.is_Interval: + dom -= in_set_symbol + solns_tmp = solveset_real(expr, symbol) + # by default symbol interval. many times cond symbols are expr symbols + for sym in expr.atoms(Symbol): + if expr_cond[expr] != True: + cond_symbol_set = expr_cond[expr].atoms(Symbol) + for cond_symbol in cond_symbol_set: + if sym == cond_symbol: + sym_interval[sym] = in_set_symbol + elif sym != cond_symbol: + sym_interval[sym] = (S.Reals) + if not expr.has(Symbol): + solns = solns_tmp.intersect(in_set_symbol) + result = result + solns + else: + solns = solns_tmp.intersect(sym_interval[symbol]) + result = result + solns else: lhs, rhs_s = inverter(f, 0, symbol) if lhs == symbol: diff --git a/sympy/solvers/tests/test_solvers.py b/sympy/solvers/tests/test_solvers.py index bf18b54584a4..e2e6c6a288cb 100644 --- a/sympy/solvers/tests/test_solvers.py +++ b/sympy/solvers/tests/test_solvers.py @@ -1813,3 +1813,8 @@ def test_issue_2840_8155(): def test_issue_9567(): assert solve(1 + 1/(x - 1)) == [0] + + +def test_issue_10122(): + assert solve(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0,x) == \ + Or(And(Lt(S.NegativeInfinity, x), Lt(x, S.Zero)), And(Lt(S.One, x), Lt(x, S.Infinity)) diff --git a/sympy/solvers/tests/test_solveset.py b/sympy/solvers/tests/test_solveset.py index c6f36c3548e5..addde1ad86c1 100644 --- a/sympy/solvers/tests/test_solveset.py +++ b/sympy/solvers/tests/test_solveset.py @@ -1102,9 +1102,7 @@ def test_issue_10122(): assert solveset(abs(x)+abs(1-x)-1>0,x,domain=S.Reals) == Union(Interval.open(-oo, 0), Interval.open(1, oo)) assert solveset(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0, x, \ domain=S.Reals) == Union(Interval.open(-oo, 0), Interval.open(1, oo)) - # syntax error in below test case - # assert solve(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0,x) \ - # == Or(And(-oo < x, x < 0), And(1 < x, x < oo)) + def test_issue_9913(): assert solveset(2*x + 1/(x - 10)**2, x, S.Reals) == \ From 79bd2c24cc875c1340d8ddb1c4e8d89721c10224 Mon Sep 17 00:00:00 2001 From: Shekhar Prasad Rajak Date: Fri, 8 Apr 2016 22:02:03 +0530 Subject: [PATCH 18/18] shifted solveset testcase from piecewise --- .../elementary/tests/test_piecewise.py | 35 ------------------- sympy/solvers/tests/test_solvers.py | 2 +- 2 files changed, 1 insertion(+), 36 deletions(-) diff --git a/sympy/functions/elementary/tests/test_piecewise.py b/sympy/functions/elementary/tests/test_piecewise.py index 5638a163c50b..215106498e65 100644 --- a/sympy/functions/elementary/tests/test_piecewise.py +++ b/sympy/functions/elementary/tests/test_piecewise.py @@ -305,41 +305,6 @@ def test_piecewise_solve(): assert solve(g, x) == [5] -def test_piecewise_solveset(): - from sympy.sets import FiniteSet - from sympy.solvers.solveset import ( - solveset_real, domain_check, solveset_complex, linear_eq_to_matrix, - linsolve, _is_function_class_equation, invert_real, invert_complex, - solveset) - - abs2 = Piecewise((-x, x <= 0), (x, x > 0)) - f = abs2.subs(x, x - 2) - assert solveset(f, x, S.Reals) == FiniteSet(2) - assert solveset(f - 1, x, S.Reals) == FiniteSet(1, 3) - - f = Piecewise(((x - 2)**2, x >= 0), (1, True)) - assert solveset(f, x, S.Reals) == FiniteSet(2) - - g = Piecewise(((x - 5)**5, x >= 4), (f, True)) - assert solveset(g, x, S.Reals) == FiniteSet(2, 5) - - g = Piecewise(((x - 5)**5, x >= 4), (f, x < 4)) - assert solveset(g, x, S.Reals) == FiniteSet(2, 5) - - g = Piecewise(((x - 5)**5, x >= 2), (f, x < 2)) - assert solveset(g, x, S.Reals) == FiniteSet(5) - - g = Piecewise(((x - 5)**5, x >= 2), (f, True)) - assert solveset(g, x, S.Reals) == FiniteSet(5) - - g = Piecewise(((x - 5)**5, x >= 2), (f, True), (10, False)) - assert solveset(g, x, S.Reals) == FiniteSet(5) - - g = Piecewise(((x - 5)**5, x >= 2), - (-x + 2, x - 2 <= 0), (x - 2, x - 2 > 0)) - assert solveset(g, x, S.Reals) == FiniteSet(5) - - @XFAIL def test_piecewise_solve2(): f = Piecewise(((x - 2)**2, x >= 0), (0, True)) diff --git a/sympy/solvers/tests/test_solvers.py b/sympy/solvers/tests/test_solvers.py index e2e6c6a288cb..e296f09003eb 100644 --- a/sympy/solvers/tests/test_solvers.py +++ b/sympy/solvers/tests/test_solvers.py @@ -1817,4 +1817,4 @@ def test_issue_9567(): def test_issue_10122(): assert solve(Piecewise((x,x>=0),(-x,True))+Piecewise((x-1,x>=1),(1-x,True))-1>0,x) == \ - Or(And(Lt(S.NegativeInfinity, x), Lt(x, S.Zero)), And(Lt(S.One, x), Lt(x, S.Infinity)) + Or(And(Lt(S.NegativeInfinity, x), Lt(x, S.Zero)), And(Lt(S.One, x), Lt(x, S.Infinity)))