def test_issue_10285(): assert FiniteSet(-x - 1).intersect(Interval.Ropen(1, 2)) == FiniteSet(x).intersect(Interval.Lopen(-3, -2)) eq = -x - 2 * (-x - y) s = signsimp(eq) ivl = Interval.open(0, 1) assert FiniteSet(eq).intersect(ivl) == FiniteSet(s).intersect(ivl) assert FiniteSet(-eq).intersect(ivl) == FiniteSet(s).intersect(Interval.open(-1, 0)) eq -= 1 ivl = Interval.Lopen(1, oo) assert FiniteSet(eq).intersect(ivl) == FiniteSet(s).intersect(Interval.Lopen(2, oo))
def test_reduce_rational_inequalities_real_relational(): assert reduce_rational_inequalities([], x) == False assert reduce_rational_inequalities( [[(x**2 + 3*x + 2)/(x**2 - 16) >= 0]], x, relational=False) == \ Union(Interval.open(-oo, -4), Interval(-2, -1), Interval.open(4, oo)) assert reduce_rational_inequalities( [[((-2*x - 10)*(3 - x))/((x**2 + 5)*(x - 2)**2) < 0]], x, relational=False) == \ Union(Interval.open(-5, 2), Interval.open(2, 3)) assert reduce_rational_inequalities([[(x + 1)/(x - 5) <= 0]], x, relational=False) == \ Interval.Ropen(-1, 5) assert reduce_rational_inequalities([[(x**2 + 4*x + 3)/(x - 1) > 0]], x, relational=False) == \ Union(Interval.open(-3, -1), Interval.open(1, oo)) assert reduce_rational_inequalities([[(x**2 - 16)/(x - 1)**2 < 0]], x, relational=False) == \ Union(Interval.open(-4, 1), Interval.open(1, 4)) assert reduce_rational_inequalities([[(3*x + 1)/(x + 4) >= 1]], x, relational=False) == \ Union(Interval.open(-oo, -4), Interval.Ropen(S(3)/2, oo)) assert reduce_rational_inequalities([[(x - 8)/x <= 3 - x]], x, relational=False) == \ Union(Interval.Lopen(-oo, -2), Interval.Lopen(0, 4)) # issue sympy/sympy#10237 assert reduce_rational_inequalities([[x < oo, x >= 0, -oo < x]], x, relational=False) == Interval(0, oo)
def test_reduce_rational_inequalities_real_relational(): assert reduce_rational_inequalities([], x) == False assert reduce_rational_inequalities( [[(x**2 + 3*x + 2)/(x**2 - 16) >= 0]], x, relational=False) == \ Union(Interval.open(-oo, -4), Interval(-2, -1), Interval.open(4, oo)) assert reduce_rational_inequalities( [[((-2*x - 10)*(3 - x))/((x**2 + 5)*(x - 2)**2) < 0]], x, relational=False) == \ Union(Interval.open(-5, 2), Interval.open(2, 3)) assert reduce_rational_inequalities([[(x + 1)/(x - 5) <= 0]], x, relational=False) == \ Interval.Ropen(-1, 5) assert reduce_rational_inequalities([[(x**2 + 4*x + 3)/(x - 1) > 0]], x, relational=False) == \ Union(Interval.open(-3, -1), Interval.open(1, oo)) assert reduce_rational_inequalities([[(x**2 - 16)/(x - 1)**2 < 0]], x, relational=False) == \ Union(Interval.open(-4, 1), Interval.open(1, 4)) assert reduce_rational_inequalities([[(3*x + 1)/(x + 4) >= 1]], x, relational=False) == \ Union(Interval.open(-oo, -4), Interval.Ropen(S(3)/2, oo)) assert reduce_rational_inequalities([[(x - 8)/x <= 3 - x]], x, relational=False) == \ Union(Interval.Lopen(-oo, -2), Interval.Lopen(0, 4)) # issue sympy/sympy#10237 assert reduce_rational_inequalities( [[x < oo, x >= 0, -oo < x]], x, relational=False) == Interval(0, oo)
def test_issue_10285(): assert FiniteSet(-x - 1).intersect(Interval.Ropen(1, 2)) == \ FiniteSet(x).intersect(Interval.Lopen(-3, -2)) eq = -x - 2 * (-x - y) s = signsimp(eq) ivl = Interval.open(0, 1) assert FiniteSet(eq).intersect(ivl) == FiniteSet(s).intersect(ivl) assert FiniteSet(-eq).intersect(ivl) == \ FiniteSet(s).intersect(Interval.open(-1, 0)) eq -= 1 ivl = Interval.Lopen(1, oo) assert FiniteSet(eq).intersect(ivl) == \ FiniteSet(s).intersect(Interval.Lopen(2, oo))
def test_solve_abs(): assert solveset_real(Abs(x) - 2, x) == FiniteSet(-2, 2) assert solveset_real(Abs(x + 3) - 2 * Abs(x - 3), x) == FiniteSet(1, 9) assert solveset_real(2 * Abs(x) - Abs(x - 1), x) == FiniteSet(-1, Rational(1, 3)) assert solveset_real(Abs(x - 7) - 8, x) == FiniteSet(-S(1), S(15)) # issue 9565. Note: solveset_real does not solve this as it is # solveset's job to handle Relationals assert solveset(Abs((x - 1) / (x - 5)) <= S(1) / 3, domain=S.Reals) == Interval(-1, 2) # issue #10069 eq = abs(1 / (x - 1)) - 1 > 0 u = Union(Interval.open(0, 1), Interval.open(1, 2)) assert solveset_real(eq, x) == u assert solveset(eq, x, domain=S.Reals) == u raises(ValueError, lambda: solveset(abs(x) - 1, x))
def test_solve_abs(): assert solveset_real(Abs(x) - 2, x) == FiniteSet(-2, 2) assert solveset_real(Abs(x + 3) - 2*Abs(x - 3), x) == \ FiniteSet(1, 9) assert solveset_real(2*Abs(x) - Abs(x - 1), x) == \ FiniteSet(-1, Rational(1, 3)) assert solveset_real(Abs(x - 7) - 8, x) == FiniteSet(-S(1), S(15)) # issue 9565 assert solveset_real(Abs((x - 1)/(x - 5)) <= S(1)/3, x) == Interval(-1, 2) # issue #10069 eq = abs(1/(x - 1)) - 1 > 0 u = Union(Interval.open(0, 1), Interval.open(1, 2)) assert solveset_real(eq, x) == u assert solveset(eq, x, domain=S.Reals) == u raises(ValueError, lambda: solveset(abs(x) - 1, x))
def test_solve_abs(): assert solveset_real(Abs(x) - 2, x) == FiniteSet(-2, 2) assert solveset_real(Abs(x + 3) - 2*Abs(x - 3), x) == \ FiniteSet(1, 9) assert solveset_real(2*Abs(x) - Abs(x - 1), x) == \ FiniteSet(-1, Rational(1, 3)) assert solveset_real(Abs(x - 7) - 8, x) == FiniteSet(-S(1), S(15)) # issue 9565. Note: solveset_real does not solve this as it is # solveset's job to handle Relationals assert solveset(Abs((x - 1)/(x - 5)) <= S(1)/3, domain=S.Reals ) == Interval(-1, 2) # issue #10069 assert solveset_real(abs(1/(x - 1)) - 1 > 0, x) == \ ConditionSet(x, Eq((1 - Abs(x - 1))/Abs(x - 1) > 0, 0), S.Reals) assert solveset(abs(1/(x - 1)) - 1 > 0, x, domain=S.Reals ) == Union(Interval.open(0, 1), Interval.open(1, 2))
def test_trig_inequalities(): # all the inequalities are solved in a periodic interval. assert isolve(sin(x) < S.Half, x, relational=False) == \ Union(Interval(0, pi/6, False, True), Interval.open(pi*Rational(5, 6), 2*pi)) assert isolve(sin(x) > S.Half, x, relational=False) == \ Interval(pi/6, pi*Rational(5, 6), True, True) assert isolve(cos(x) < S.Zero, x, relational=False) == \ Interval(pi/2, pi*Rational(3, 2), True, True) assert isolve(cos(x) >= S.Zero, x, relational=False) == \ Union(Interval(0, pi/2), Interval.Ropen(pi*Rational(3, 2), 2*pi)) assert isolve(tan(x) < S.One, x, relational=False) == \ Union(Interval.Ropen(0, pi/4), Interval.open(pi/2, pi)) assert isolve(sin(x) <= S.Zero, x, relational=False) == \ Union(FiniteSet(S.Zero), Interval.Ropen(pi, 2*pi)) assert isolve(sin(x) <= S.One, x, relational=False) == S.Reals assert isolve(cos(x) < S(-2), x, relational=False) == S.EmptySet assert isolve(sin(x) >= S.NegativeOne, x, relational=False) == S.Reals assert isolve(cos(x) > S.One, x, relational=False) == S.EmptySet
def test_issue_8715(): eq = x + 1 / x > -2 + 1 / x assert solveset(eq, x, S.Reals) == \ (Interval.open(-2, oo) - FiniteSet(0)) assert solveset(eq.subs(x,log(x)), x, S.Reals) == \ Interval.open(exp(-2), oo) - FiniteSet(1)
def test_issue_8715(): eq = x + 1 / x > -2 + 1 / x assert solveset(eq, x, S.Reals) == (Interval.open(-2, oo) - FiniteSet(0)) assert solveset(eq.subs(x, log(x)), x, S.Reals) == Interval.open(exp(-2), oo) - FiniteSet(1)
def test_find_real_domain(): from sympy.abc import t as my_t from sympy import Union, Interval, oo, Intersection, Abs assert_equals(find_real_domain(1 / my_t, my_t), Union(Interval.open(-oo, 0), Interval.open(0, oo))) assert_equals(find_real_domain(Abs(my_t), my_t), S.Reals)
def test_Union_contains(): assert zoo not in Union( Interval.open(-oo, 0), Interval.open(0, oo))
def inequation_to_interval(inequation): """ Devuelve un intervalo a partir de una inequación Parte del supuesto de que de un lado de la inecuación se encuentra "x", sin operaciones. """ args = inequation.args less = [LessThan, StrictLessThan] greater = [GreaterThan, StrictGreaterThan] if inequation.__class__ in greater + less: # [StrictGreaterThan, GreaterThan, StrictLessThan, LessThan] inequation = _adapt(inequation) args = inequation.args a = args[0] b = args[1] if inequation.__class__ in greater: if inequation.__class__ == StrictGreaterThan: # Ejemplo: x > b # Intervalo: (b, +oo) return Interval.open(b, oo) elif inequation.__class__ == GreaterThan: # Ejemplo: x >= b # Intervalo: [b, +oo) return Interval(b, oo) # Cerrado por izquierda elif inequation.__class__ in less: if inequation.__class__ == StrictLessThan: # Ejemplo: x < b # Intervalo: (-oo, b) return Interval.open(-oo, b) elif inequation.__class__ == LessThan: # Ejemplo: x <= b # Intervalo: (-oo, b] return Interval(-oo, b) # Cerrado por derecha elif inequation.__class__ == And: # El primer valor en args siempre es una inequación _args = [] for arg in args: _args.append(_adapt(arg)) args = _args d = {1: [], 2: []} for arg in args: # FIXME: Y si hay otro And o un Or ? if arg.__class__ in less: d[1].append(inequation_to_interval(arg)) elif arg.__class__ in greater: d[2].append(inequation_to_interval(arg)) if d[1]: __less = True _less = d[1][0] for _i in d[1][1:]: _less = Interval.union(_less, _i) else: __less = False _less = EmptySet() if d[2]: __greater = True _greater = d[2][0] for _i in d[2][1:]: _greater = Interval.union(_greater, _i) else: __greater = False _greater = EmptySet() if __greater and __less: return _less.intersect(_greater) elif __less and not __greater: return _less elif __greater and not __less: return _greater elif inequation.__class__ == Or: interval = inequation_to_interval(args[0]) for arg in args[1:]: interval = Union(interval, inequation_to_interval(arg)) return interval elif inequation.__class__ == BooleanTrue: # Por ejemplo: 4 > 2, todos los x pertenecientes # a los reales cumplen esta inecuación return REALS elif inequation.__class__ == BooleanFalse: # Por ejemplo: 2 > 4, ningún x perteneciente a los # reales cumple con esta inecuación return EmptySet() else: print inequation, inequation.__class__