I am done

This commit is contained in:
2024-10-30 22:14:35 +01:00
parent 720dc28c09
commit 40e2a747cf
36901 changed files with 5011519 additions and 0 deletions

View File

@ -0,0 +1,294 @@
from sympy.core.expr import unchanged
from sympy.sets import (ConditionSet, Intersection, FiniteSet,
EmptySet, Union, Contains, ImageSet)
from sympy.sets.sets import SetKind
from sympy.core.function import (Function, Lambda)
from sympy.core.mod import Mod
from sympy.core.kind import NumberKind
from sympy.core.numbers import (oo, pi)
from sympy.core.relational import (Eq, Ne)
from sympy.core.singleton import S
from sympy.core.symbol import (Symbol, symbols)
from sympy.functions.elementary.complexes import Abs
from sympy.functions.elementary.trigonometric import (asin, sin)
from sympy.logic.boolalg import And
from sympy.matrices.dense import Matrix
from sympy.matrices.expressions.matexpr import MatrixSymbol
from sympy.sets.sets import Interval
from sympy.testing.pytest import raises, warns_deprecated_sympy
w = Symbol('w')
x = Symbol('x')
y = Symbol('y')
z = Symbol('z')
f = Function('f')
def test_CondSet():
sin_sols_principal = ConditionSet(x, Eq(sin(x), 0),
Interval(0, 2*pi, False, True))
assert pi in sin_sols_principal
assert pi/2 not in sin_sols_principal
assert 3*pi not in sin_sols_principal
assert oo not in sin_sols_principal
assert 5 in ConditionSet(x, x**2 > 4, S.Reals)
assert 1 not in ConditionSet(x, x**2 > 4, S.Reals)
# in this case, 0 is not part of the base set so
# it can't be in any subset selected by the condition
assert 0 not in ConditionSet(x, y > 5, Interval(1, 7))
# since 'in' requires a true/false, the following raises
# an error because the given value provides no information
# for the condition to evaluate (since the condition does
# not depend on the dummy symbol): the result is `y > 5`.
# In this case, ConditionSet is just acting like
# Piecewise((Interval(1, 7), y > 5), (S.EmptySet, True)).
raises(TypeError, lambda: 6 in ConditionSet(x, y > 5,
Interval(1, 7)))
X = MatrixSymbol('X', 2, 2)
matrix_set = ConditionSet(X, Eq(X*Matrix([[1, 1], [1, 1]]), X))
Y = Matrix([[0, 0], [0, 0]])
assert matrix_set.contains(Y).doit() is S.true
Z = Matrix([[1, 2], [3, 4]])
assert matrix_set.contains(Z).doit() is S.false
assert isinstance(ConditionSet(x, x < 1, {x, y}).base_set,
FiniteSet)
raises(TypeError, lambda: ConditionSet(x, x + 1, {x, y}))
raises(TypeError, lambda: ConditionSet(x, x, 1))
I = S.Integers
U = S.UniversalSet
C = ConditionSet
assert C(x, False, I) is S.EmptySet
assert C(x, True, I) is I
assert C(x, x < 1, C(x, x < 2, I)
) == C(x, (x < 1) & (x < 2), I)
assert C(y, y < 1, C(x, y < 2, I)
) == C(x, (x < 1) & (y < 2), I), C(y, y < 1, C(x, y < 2, I))
assert C(y, y < 1, C(x, x < 2, I)
) == C(y, (y < 1) & (y < 2), I)
assert C(y, y < 1, C(x, y < x, I)
) == C(x, (x < 1) & (y < x), I)
assert unchanged(C, y, x < 1, C(x, y < x, I))
assert ConditionSet(x, x < 1).base_set is U
# arg checking is not done at instantiation but this
# will raise an error when containment is tested
assert ConditionSet((x,), x < 1).base_set is U
c = ConditionSet((x, y), x < y, I**2)
assert (1, 2) in c
assert (1, pi) not in c
raises(TypeError, lambda: C(x, x > 1, C((x, y), x > 1, I**2)))
# signature mismatch since only 3 args are accepted
raises(TypeError, lambda: C((x, y), x + y < 2, U, U))
def test_CondSet_intersect():
input_conditionset = ConditionSet(x, x**2 > 4, Interval(1, 4, False,
False))
other_domain = Interval(0, 3, False, False)
output_conditionset = ConditionSet(x, x**2 > 4, Interval(
1, 3, False, False))
assert Intersection(input_conditionset, other_domain
) == output_conditionset
def test_issue_9849():
assert ConditionSet(x, Eq(x, x), S.Naturals
) is S.Naturals
assert ConditionSet(x, Eq(Abs(sin(x)), -1), S.Naturals
) == S.EmptySet
def test_simplified_FiniteSet_in_CondSet():
assert ConditionSet(x, And(x < 1, x > -3), FiniteSet(0, 1, 2)
) == FiniteSet(0)
assert ConditionSet(x, x < 0, FiniteSet(0, 1, 2)) == EmptySet
assert ConditionSet(x, And(x < -3), EmptySet) == EmptySet
y = Symbol('y')
assert (ConditionSet(x, And(x > 0), FiniteSet(-1, 0, 1, y)) ==
Union(FiniteSet(1), ConditionSet(x, And(x > 0), FiniteSet(y))))
assert (ConditionSet(x, Eq(Mod(x, 3), 1), FiniteSet(1, 4, 2, y)) ==
Union(FiniteSet(1, 4), ConditionSet(x, Eq(Mod(x, 3), 1),
FiniteSet(y))))
def test_free_symbols():
assert ConditionSet(x, Eq(y, 0), FiniteSet(z)
).free_symbols == {y, z}
assert ConditionSet(x, Eq(x, 0), FiniteSet(z)
).free_symbols == {z}
assert ConditionSet(x, Eq(x, 0), FiniteSet(x, z)
).free_symbols == {x, z}
assert ConditionSet(x, Eq(x, 0), ImageSet(Lambda(y, y**2),
S.Integers)).free_symbols == set()
def test_bound_symbols():
assert ConditionSet(x, Eq(y, 0), FiniteSet(z)
).bound_symbols == [x]
assert ConditionSet(x, Eq(x, 0), FiniteSet(x, y)
).bound_symbols == [x]
assert ConditionSet(x, x < 10, ImageSet(Lambda(y, y**2), S.Integers)
).bound_symbols == [x]
assert ConditionSet(x, x < 10, ConditionSet(y, y > 1, S.Integers)
).bound_symbols == [x]
def test_as_dummy():
_0, _1 = symbols('_0 _1')
assert ConditionSet(x, x < 1, Interval(y, oo)
).as_dummy() == ConditionSet(_0, _0 < 1, Interval(y, oo))
assert ConditionSet(x, x < 1, Interval(x, oo)
).as_dummy() == ConditionSet(_0, _0 < 1, Interval(x, oo))
assert ConditionSet(x, x < 1, ImageSet(Lambda(y, y**2), S.Integers)
).as_dummy() == ConditionSet(
_0, _0 < 1, ImageSet(Lambda(_0, _0**2), S.Integers))
e = ConditionSet((x, y), x <= y, S.Reals**2)
assert e.bound_symbols == [x, y]
assert e.as_dummy() == ConditionSet((_0, _1), _0 <= _1, S.Reals**2)
assert e.as_dummy() == ConditionSet((y, x), y <= x, S.Reals**2
).as_dummy()
def test_subs_CondSet():
s = FiniteSet(z, y)
c = ConditionSet(x, x < 2, s)
assert c.subs(x, y) == c
assert c.subs(z, y) == ConditionSet(x, x < 2, FiniteSet(y))
assert c.xreplace({x: y}) == ConditionSet(y, y < 2, s)
assert ConditionSet(x, x < y, s
).subs(y, w) == ConditionSet(x, x < w, s.subs(y, w))
# if the user uses assumptions that cause the condition
# to evaluate, that can't be helped from SymPy's end
n = Symbol('n', negative=True)
assert ConditionSet(n, 0 < n, S.Integers) is S.EmptySet
p = Symbol('p', positive=True)
assert ConditionSet(n, n < y, S.Integers
).subs(n, x) == ConditionSet(n, n < y, S.Integers)
raises(ValueError, lambda: ConditionSet(
x + 1, x < 1, S.Integers))
assert ConditionSet(
p, n < x, Interval(-5, 5)).subs(x, p) == Interval(-5, 5), ConditionSet(
p, n < x, Interval(-5, 5)).subs(x, p)
assert ConditionSet(
n, n < x, Interval(-oo, 0)).subs(x, p
) == Interval(-oo, 0)
assert ConditionSet(f(x), f(x) < 1, {w, z}
).subs(f(x), y) == ConditionSet(f(x), f(x) < 1, {w, z})
# issue 17341
k = Symbol('k')
img1 = ImageSet(Lambda(k, 2*k*pi + asin(y)), S.Integers)
img2 = ImageSet(Lambda(k, 2*k*pi + asin(S.One/3)), S.Integers)
assert ConditionSet(x, Contains(
y, Interval(-1,1)), img1).subs(y, S.One/3).dummy_eq(img2)
assert (0, 1) in ConditionSet((x, y), x + y < 3, S.Integers**2)
raises(TypeError, lambda: ConditionSet(n, n < -10, Interval(0, 10)))
def test_subs_CondSet_tebr():
with warns_deprecated_sympy():
assert ConditionSet((x, y), {x + 1, x + y}, S.Reals**2) == \
ConditionSet((x, y), Eq(x + 1, 0) & Eq(x + y, 0), S.Reals**2)
def test_dummy_eq():
C = ConditionSet
I = S.Integers
c = C(x, x < 1, I)
assert c.dummy_eq(C(y, y < 1, I))
assert c.dummy_eq(1) == False
assert c.dummy_eq(C(x, x < 1, S.Reals)) == False
c1 = ConditionSet((x, y), Eq(x + 1, 0) & Eq(x + y, 0), S.Reals**2)
c2 = ConditionSet((x, y), Eq(x + 1, 0) & Eq(x + y, 0), S.Reals**2)
c3 = ConditionSet((x, y), Eq(x + 1, 0) & Eq(x + y, 0), S.Complexes**2)
assert c1.dummy_eq(c2)
assert c1.dummy_eq(c3) is False
assert c.dummy_eq(c1) is False
assert c1.dummy_eq(c) is False
# issue 19496
m = Symbol('m')
n = Symbol('n')
a = Symbol('a')
d1 = ImageSet(Lambda(m, m*pi), S.Integers)
d2 = ImageSet(Lambda(n, n*pi), S.Integers)
c1 = ConditionSet(x, Ne(a, 0), d1)
c2 = ConditionSet(x, Ne(a, 0), d2)
assert c1.dummy_eq(c2)
def test_contains():
assert 6 in ConditionSet(x, x > 5, Interval(1, 7))
assert (8 in ConditionSet(x, y > 5, Interval(1, 7))) is False
# `in` should give True or False; in this case there is not
# enough information for that result
raises(TypeError,
lambda: 6 in ConditionSet(x, y > 5, Interval(1, 7)))
# here, there is enough information but the comparison is
# not defined
raises(TypeError, lambda: 0 in ConditionSet(x, 1/x >= 0, S.Reals))
assert ConditionSet(x, y > 5, Interval(1, 7)
).contains(6) == (y > 5)
assert ConditionSet(x, y > 5, Interval(1, 7)
).contains(8) is S.false
assert ConditionSet(x, y > 5, Interval(1, 7)
).contains(w) == And(Contains(w, Interval(1, 7)), y > 5)
# This returns an unevaluated Contains object
# because 1/0 should not be defined for 1 and 0 in the context of
# reals.
assert ConditionSet(x, 1/x >= 0, S.Reals).contains(0) == \
Contains(0, ConditionSet(x, 1/x >= 0, S.Reals), evaluate=False)
c = ConditionSet((x, y), x + y > 1, S.Integers**2)
assert not c.contains(1)
assert c.contains((2, 1))
assert not c.contains((0, 1))
c = ConditionSet((w, (x, y)), w + x + y > 1, S.Integers*S.Integers**2)
assert not c.contains(1)
assert not c.contains((1, 2))
assert not c.contains(((1, 2), 3))
assert not c.contains(((1, 2), (3, 4)))
assert c.contains((1, (3, 4)))
def test_as_relational():
assert ConditionSet((x, y), x > 1, S.Integers**2).as_relational((x, y)
) == (x > 1) & Contains(x, S.Integers) & Contains(y, S.Integers)
assert ConditionSet(x, x > 1, S.Integers).as_relational(x
) == Contains(x, S.Integers) & (x > 1)
def test_flatten():
"""Tests whether there is basic denesting functionality"""
inner = ConditionSet(x, sin(x) + x > 0)
outer = ConditionSet(x, Contains(x, inner), S.Reals)
assert outer == ConditionSet(x, sin(x) + x > 0, S.Reals)
inner = ConditionSet(y, sin(y) + y > 0)
outer = ConditionSet(x, Contains(y, inner), S.Reals)
assert outer != ConditionSet(x, sin(x) + x > 0, S.Reals)
inner = ConditionSet(x, sin(x) + x > 0).intersect(Interval(-1, 1))
outer = ConditionSet(x, Contains(x, inner), S.Reals)
assert outer == ConditionSet(x, sin(x) + x > 0, Interval(-1, 1))
def test_duplicate():
from sympy.core.function import BadSignatureError
# test coverage for line 95 in conditionset.py, check for duplicates in symbols
dup = symbols('a,a')
raises(BadSignatureError, lambda: ConditionSet(dup, x < 0))
def test_SetKind_ConditionSet():
assert ConditionSet(x, Eq(sin(x), 0), Interval(0, 2*pi)).kind is SetKind(NumberKind)
assert ConditionSet(x, x < 0).kind is SetKind(NumberKind)

View File

@ -0,0 +1,52 @@
from sympy.core.expr import unchanged
from sympy.core.numbers import oo
from sympy.core.relational import Eq
from sympy.core.singleton import S
from sympy.core.symbol import Symbol
from sympy.sets.contains import Contains
from sympy.sets.sets import (FiniteSet, Interval)
from sympy.testing.pytest import raises
def test_contains_basic():
raises(TypeError, lambda: Contains(S.Integers, 1))
assert Contains(2, S.Integers) is S.true
assert Contains(-2, S.Naturals) is S.false
i = Symbol('i', integer=True)
assert Contains(i, S.Naturals) == Contains(i, S.Naturals, evaluate=False)
def test_issue_6194():
x = Symbol('x')
assert unchanged(Contains, x, Interval(0, 1))
assert Interval(0, 1).contains(x) == (S.Zero <= x) & (x <= 1)
assert Contains(x, FiniteSet(0)) != S.false
assert Contains(x, Interval(1, 1)) != S.false
assert Contains(x, S.Integers) != S.false
def test_issue_10326():
assert Contains(oo, Interval(-oo, oo)) == False
assert Contains(-oo, Interval(-oo, oo)) == False
def test_binary_symbols():
x = Symbol('x')
y = Symbol('y')
z = Symbol('z')
assert Contains(x, FiniteSet(y, Eq(z, True))
).binary_symbols == {y, z}
def test_as_set():
x = Symbol('x')
y = Symbol('y')
assert Contains(x, FiniteSet(y)).as_set() == FiniteSet(y)
assert Contains(x, S.Integers).as_set() == S.Integers
assert Contains(x, S.Reals).as_set() == S.Reals
def test_type_error():
# Pass in a parameter not of type "set"
raises(TypeError, lambda: Contains(2, None))

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,67 @@
from sympy.sets.ordinals import Ordinal, OmegaPower, ord0, omega
from sympy.testing.pytest import raises
def test_string_ordinals():
assert str(omega) == 'w'
assert str(Ordinal(OmegaPower(5, 3), OmegaPower(3, 2))) == 'w**5*3 + w**3*2'
assert str(Ordinal(OmegaPower(5, 3), OmegaPower(0, 5))) == 'w**5*3 + 5'
assert str(Ordinal(OmegaPower(1, 3), OmegaPower(0, 5))) == 'w*3 + 5'
assert str(Ordinal(OmegaPower(omega + 1, 1), OmegaPower(3, 2))) == 'w**(w + 1) + w**3*2'
def test_addition_with_integers():
assert 3 + Ordinal(OmegaPower(5, 3)) == Ordinal(OmegaPower(5, 3))
assert Ordinal(OmegaPower(5, 3))+3 == Ordinal(OmegaPower(5, 3), OmegaPower(0, 3))
assert Ordinal(OmegaPower(5, 3), OmegaPower(0, 2))+3 == \
Ordinal(OmegaPower(5, 3), OmegaPower(0, 5))
def test_addition_with_ordinals():
assert Ordinal(OmegaPower(5, 3), OmegaPower(3, 2)) + Ordinal(OmegaPower(3, 3)) == \
Ordinal(OmegaPower(5, 3), OmegaPower(3, 5))
assert Ordinal(OmegaPower(5, 3), OmegaPower(3, 2)) + Ordinal(OmegaPower(4, 2)) == \
Ordinal(OmegaPower(5, 3), OmegaPower(4, 2))
assert Ordinal(OmegaPower(omega, 2), OmegaPower(3, 2)) + Ordinal(OmegaPower(4, 2)) == \
Ordinal(OmegaPower(omega, 2), OmegaPower(4, 2))
def test_comparison():
assert Ordinal(OmegaPower(5, 3)) > Ordinal(OmegaPower(4, 3), OmegaPower(2, 1))
assert Ordinal(OmegaPower(5, 3), OmegaPower(3, 2)) < Ordinal(OmegaPower(5, 4))
assert Ordinal(OmegaPower(5, 4)) < Ordinal(OmegaPower(5, 5), OmegaPower(4, 1))
assert Ordinal(OmegaPower(5, 3), OmegaPower(3, 2)) == \
Ordinal(OmegaPower(5, 3), OmegaPower(3, 2))
assert not Ordinal(OmegaPower(5, 3), OmegaPower(3, 2)) == Ordinal(OmegaPower(5, 3))
assert Ordinal(OmegaPower(omega, 3)) > Ordinal(OmegaPower(5, 3))
def test_multiplication_with_integers():
w = omega
assert 3*w == w
assert w*9 == Ordinal(OmegaPower(1, 9))
def test_multiplication():
w = omega
assert w*(w + 1) == w*w + w
assert (w + 1)*(w + 1) == w*w + w + 1
assert w*1 == w
assert 1*w == w
assert w*ord0 == ord0
assert ord0*w == ord0
assert w**w == w * w**w
assert (w**w)*w*w == w**(w + 2)
def test_exponentiation():
w = omega
assert w**2 == w*w
assert w**3 == w*w*w
assert w**(w + 1) == Ordinal(OmegaPower(omega + 1, 1))
assert (w**w)*(w**w) == w**(w*2)
def test_comapre_not_instance():
w = OmegaPower(omega + 1, 1)
assert(not (w == None))
assert(not (w < 5))
raises(TypeError, lambda: w < 6.66)
def test_is_successort():
w = Ordinal(OmegaPower(5, 1))
assert not w.is_successor_ordinal

View File

@ -0,0 +1,141 @@
from sympy.core.expr import unchanged
from sympy.core.singleton import S
from sympy.core.symbol import Symbol
from sympy.sets.contains import Contains
from sympy.sets.fancysets import Interval
from sympy.sets.powerset import PowerSet
from sympy.sets.sets import FiniteSet
from sympy.testing.pytest import raises, XFAIL
def test_powerset_creation():
assert unchanged(PowerSet, FiniteSet(1, 2))
assert unchanged(PowerSet, S.EmptySet)
raises(ValueError, lambda: PowerSet(123))
assert unchanged(PowerSet, S.Reals)
assert unchanged(PowerSet, S.Integers)
def test_powerset_rewrite_FiniteSet():
assert PowerSet(FiniteSet(1, 2)).rewrite(FiniteSet) == \
FiniteSet(S.EmptySet, FiniteSet(1), FiniteSet(2), FiniteSet(1, 2))
assert PowerSet(S.EmptySet).rewrite(FiniteSet) == FiniteSet(S.EmptySet)
assert PowerSet(S.Naturals).rewrite(FiniteSet) == PowerSet(S.Naturals)
def test_finiteset_rewrite_powerset():
assert FiniteSet(S.EmptySet).rewrite(PowerSet) == PowerSet(S.EmptySet)
assert FiniteSet(
S.EmptySet, FiniteSet(1),
FiniteSet(2), FiniteSet(1, 2)).rewrite(PowerSet) == \
PowerSet(FiniteSet(1, 2))
assert FiniteSet(1, 2, 3).rewrite(PowerSet) == FiniteSet(1, 2, 3)
def test_powerset__contains__():
subset_series = [
S.EmptySet,
FiniteSet(1, 2),
S.Naturals,
S.Naturals0,
S.Integers,
S.Rationals,
S.Reals,
S.Complexes]
l = len(subset_series)
for i in range(l):
for j in range(l):
if i <= j:
assert subset_series[i] in \
PowerSet(subset_series[j], evaluate=False)
else:
assert subset_series[i] not in \
PowerSet(subset_series[j], evaluate=False)
@XFAIL
def test_failing_powerset__contains__():
# XXX These are failing when evaluate=True,
# but using unevaluated PowerSet works fine.
assert FiniteSet(1, 2) not in PowerSet(S.EmptySet).rewrite(FiniteSet)
assert S.Naturals not in PowerSet(S.EmptySet).rewrite(FiniteSet)
assert S.Naturals not in PowerSet(FiniteSet(1, 2)).rewrite(FiniteSet)
assert S.Naturals0 not in PowerSet(S.EmptySet).rewrite(FiniteSet)
assert S.Naturals0 not in PowerSet(FiniteSet(1, 2)).rewrite(FiniteSet)
assert S.Integers not in PowerSet(S.EmptySet).rewrite(FiniteSet)
assert S.Integers not in PowerSet(FiniteSet(1, 2)).rewrite(FiniteSet)
assert S.Rationals not in PowerSet(S.EmptySet).rewrite(FiniteSet)
assert S.Rationals not in PowerSet(FiniteSet(1, 2)).rewrite(FiniteSet)
assert S.Reals not in PowerSet(S.EmptySet).rewrite(FiniteSet)
assert S.Reals not in PowerSet(FiniteSet(1, 2)).rewrite(FiniteSet)
assert S.Complexes not in PowerSet(S.EmptySet).rewrite(FiniteSet)
assert S.Complexes not in PowerSet(FiniteSet(1, 2)).rewrite(FiniteSet)
def test_powerset__len__():
A = PowerSet(S.EmptySet, evaluate=False)
assert len(A) == 1
A = PowerSet(A, evaluate=False)
assert len(A) == 2
A = PowerSet(A, evaluate=False)
assert len(A) == 4
A = PowerSet(A, evaluate=False)
assert len(A) == 16
def test_powerset__iter__():
a = PowerSet(FiniteSet(1, 2)).__iter__()
assert next(a) == S.EmptySet
assert next(a) == FiniteSet(1)
assert next(a) == FiniteSet(2)
assert next(a) == FiniteSet(1, 2)
a = PowerSet(S.Naturals).__iter__()
assert next(a) == S.EmptySet
assert next(a) == FiniteSet(1)
assert next(a) == FiniteSet(2)
assert next(a) == FiniteSet(1, 2)
assert next(a) == FiniteSet(3)
assert next(a) == FiniteSet(1, 3)
assert next(a) == FiniteSet(2, 3)
assert next(a) == FiniteSet(1, 2, 3)
def test_powerset_contains():
A = PowerSet(FiniteSet(1), evaluate=False)
assert A.contains(2) == Contains(2, A)
x = Symbol('x')
A = PowerSet(FiniteSet(x), evaluate=False)
assert A.contains(FiniteSet(1)) == Contains(FiniteSet(1), A)
def test_powerset_method():
# EmptySet
A = FiniteSet()
pset = A.powerset()
assert len(pset) == 1
assert pset == FiniteSet(S.EmptySet)
# FiniteSets
A = FiniteSet(1, 2)
pset = A.powerset()
assert len(pset) == 2**len(A)
assert pset == FiniteSet(FiniteSet(), FiniteSet(1),
FiniteSet(2), A)
# Not finite sets
A = Interval(0, 1)
assert A.powerset() == PowerSet(A)
def test_is_subset():
# covers line 101-102
# initialize powerset(1), which is a subset of powerset(1,2)
subset = PowerSet(FiniteSet(1))
pset = PowerSet(FiniteSet(1, 2))
bad_set = PowerSet(FiniteSet(2, 3))
# assert "subset" is subset of pset == True
assert subset.is_subset(pset)
# assert "bad_set" is subset of pset == False
assert not pset.is_subset(bad_set)

View File

@ -0,0 +1,317 @@
from sympy.sets.setexpr import SetExpr
from sympy.sets import Interval, FiniteSet, Intersection, ImageSet, Union
from sympy.core.expr import Expr
from sympy.core.function import Lambda
from sympy.core.numbers import (I, Rational, oo)
from sympy.core.singleton import S
from sympy.core.symbol import (Dummy, Symbol, symbols)
from sympy.functions.elementary.exponential import (exp, log)
from sympy.functions.elementary.miscellaneous import (Max, Min, sqrt)
from sympy.functions.elementary.trigonometric import cos
from sympy.sets.sets import Set
a, x = symbols("a, x")
_d = Dummy("d")
def test_setexpr():
se = SetExpr(Interval(0, 1))
assert isinstance(se.set, Set)
assert isinstance(se, Expr)
def test_scalar_funcs():
assert SetExpr(Interval(0, 1)).set == Interval(0, 1)
a, b = Symbol('a', real=True), Symbol('b', real=True)
a, b = 1, 2
# TODO: add support for more functions in the future:
for f in [exp, log]:
input_se = f(SetExpr(Interval(a, b)))
output = input_se.set
expected = Interval(Min(f(a), f(b)), Max(f(a), f(b)))
assert output == expected
def test_Add_Mul():
assert (SetExpr(Interval(0, 1)) + 1).set == Interval(1, 2)
assert (SetExpr(Interval(0, 1))*2).set == Interval(0, 2)
def test_Pow():
assert (SetExpr(Interval(0, 2))**2).set == Interval(0, 4)
def test_compound():
assert (exp(SetExpr(Interval(0, 1))*2 + 1)).set == \
Interval(exp(1), exp(3))
def test_Interval_Interval():
assert (SetExpr(Interval(1, 2)) + SetExpr(Interval(10, 20))).set == \
Interval(11, 22)
assert (SetExpr(Interval(1, 2))*SetExpr(Interval(10, 20))).set == \
Interval(10, 40)
def test_FiniteSet_FiniteSet():
assert (SetExpr(FiniteSet(1, 2, 3)) + SetExpr(FiniteSet(1, 2))).set == \
FiniteSet(2, 3, 4, 5)
assert (SetExpr(FiniteSet(1, 2, 3))*SetExpr(FiniteSet(1, 2))).set == \
FiniteSet(1, 2, 3, 4, 6)
def test_Interval_FiniteSet():
assert (SetExpr(FiniteSet(1, 2)) + SetExpr(Interval(0, 10))).set == \
Interval(1, 12)
def test_Many_Sets():
assert (SetExpr(Interval(0, 1)) +
SetExpr(Interval(2, 3)) +
SetExpr(FiniteSet(10, 11, 12))).set == Interval(12, 16)
def test_same_setexprs_are_not_identical():
a = SetExpr(FiniteSet(0, 1))
b = SetExpr(FiniteSet(0, 1))
assert (a + b).set == FiniteSet(0, 1, 2)
# Cannot detect the set being the same:
# assert (a + a).set == FiniteSet(0, 2)
def test_Interval_arithmetic():
i12cc = SetExpr(Interval(1, 2))
i12lo = SetExpr(Interval.Lopen(1, 2))
i12ro = SetExpr(Interval.Ropen(1, 2))
i12o = SetExpr(Interval.open(1, 2))
n23cc = SetExpr(Interval(-2, 3))
n23lo = SetExpr(Interval.Lopen(-2, 3))
n23ro = SetExpr(Interval.Ropen(-2, 3))
n23o = SetExpr(Interval.open(-2, 3))
n3n2cc = SetExpr(Interval(-3, -2))
assert i12cc + i12cc == SetExpr(Interval(2, 4))
assert i12cc - i12cc == SetExpr(Interval(-1, 1))
assert i12cc*i12cc == SetExpr(Interval(1, 4))
assert i12cc/i12cc == SetExpr(Interval(S.Half, 2))
assert i12cc**2 == SetExpr(Interval(1, 4))
assert i12cc**3 == SetExpr(Interval(1, 8))
assert i12lo + i12ro == SetExpr(Interval.open(2, 4))
assert i12lo - i12ro == SetExpr(Interval.Lopen(-1, 1))
assert i12lo*i12ro == SetExpr(Interval.open(1, 4))
assert i12lo/i12ro == SetExpr(Interval.Lopen(S.Half, 2))
assert i12lo + i12lo == SetExpr(Interval.Lopen(2, 4))
assert i12lo - i12lo == SetExpr(Interval.open(-1, 1))
assert i12lo*i12lo == SetExpr(Interval.Lopen(1, 4))
assert i12lo/i12lo == SetExpr(Interval.open(S.Half, 2))
assert i12lo + i12cc == SetExpr(Interval.Lopen(2, 4))
assert i12lo - i12cc == SetExpr(Interval.Lopen(-1, 1))
assert i12lo*i12cc == SetExpr(Interval.Lopen(1, 4))
assert i12lo/i12cc == SetExpr(Interval.Lopen(S.Half, 2))
assert i12lo + i12o == SetExpr(Interval.open(2, 4))
assert i12lo - i12o == SetExpr(Interval.open(-1, 1))
assert i12lo*i12o == SetExpr(Interval.open(1, 4))
assert i12lo/i12o == SetExpr(Interval.open(S.Half, 2))
assert i12lo**2 == SetExpr(Interval.Lopen(1, 4))
assert i12lo**3 == SetExpr(Interval.Lopen(1, 8))
assert i12ro + i12ro == SetExpr(Interval.Ropen(2, 4))
assert i12ro - i12ro == SetExpr(Interval.open(-1, 1))
assert i12ro*i12ro == SetExpr(Interval.Ropen(1, 4))
assert i12ro/i12ro == SetExpr(Interval.open(S.Half, 2))
assert i12ro + i12cc == SetExpr(Interval.Ropen(2, 4))
assert i12ro - i12cc == SetExpr(Interval.Ropen(-1, 1))
assert i12ro*i12cc == SetExpr(Interval.Ropen(1, 4))
assert i12ro/i12cc == SetExpr(Interval.Ropen(S.Half, 2))
assert i12ro + i12o == SetExpr(Interval.open(2, 4))
assert i12ro - i12o == SetExpr(Interval.open(-1, 1))
assert i12ro*i12o == SetExpr(Interval.open(1, 4))
assert i12ro/i12o == SetExpr(Interval.open(S.Half, 2))
assert i12ro**2 == SetExpr(Interval.Ropen(1, 4))
assert i12ro**3 == SetExpr(Interval.Ropen(1, 8))
assert i12o + i12lo == SetExpr(Interval.open(2, 4))
assert i12o - i12lo == SetExpr(Interval.open(-1, 1))
assert i12o*i12lo == SetExpr(Interval.open(1, 4))
assert i12o/i12lo == SetExpr(Interval.open(S.Half, 2))
assert i12o + i12ro == SetExpr(Interval.open(2, 4))
assert i12o - i12ro == SetExpr(Interval.open(-1, 1))
assert i12o*i12ro == SetExpr(Interval.open(1, 4))
assert i12o/i12ro == SetExpr(Interval.open(S.Half, 2))
assert i12o + i12cc == SetExpr(Interval.open(2, 4))
assert i12o - i12cc == SetExpr(Interval.open(-1, 1))
assert i12o*i12cc == SetExpr(Interval.open(1, 4))
assert i12o/i12cc == SetExpr(Interval.open(S.Half, 2))
assert i12o**2 == SetExpr(Interval.open(1, 4))
assert i12o**3 == SetExpr(Interval.open(1, 8))
assert n23cc + n23cc == SetExpr(Interval(-4, 6))
assert n23cc - n23cc == SetExpr(Interval(-5, 5))
assert n23cc*n23cc == SetExpr(Interval(-6, 9))
assert n23cc/n23cc == SetExpr(Interval.open(-oo, oo))
assert n23cc + n23ro == SetExpr(Interval.Ropen(-4, 6))
assert n23cc - n23ro == SetExpr(Interval.Lopen(-5, 5))
assert n23cc*n23ro == SetExpr(Interval.Ropen(-6, 9))
assert n23cc/n23ro == SetExpr(Interval.Lopen(-oo, oo))
assert n23cc + n23lo == SetExpr(Interval.Lopen(-4, 6))
assert n23cc - n23lo == SetExpr(Interval.Ropen(-5, 5))
assert n23cc*n23lo == SetExpr(Interval(-6, 9))
assert n23cc/n23lo == SetExpr(Interval.open(-oo, oo))
assert n23cc + n23o == SetExpr(Interval.open(-4, 6))
assert n23cc - n23o == SetExpr(Interval.open(-5, 5))
assert n23cc*n23o == SetExpr(Interval.open(-6, 9))
assert n23cc/n23o == SetExpr(Interval.open(-oo, oo))
assert n23cc**2 == SetExpr(Interval(0, 9))
assert n23cc**3 == SetExpr(Interval(-8, 27))
n32cc = SetExpr(Interval(-3, 2))
n32lo = SetExpr(Interval.Lopen(-3, 2))
n32ro = SetExpr(Interval.Ropen(-3, 2))
assert n32cc*n32lo == SetExpr(Interval.Ropen(-6, 9))
assert n32cc*n32cc == SetExpr(Interval(-6, 9))
assert n32lo*n32cc == SetExpr(Interval.Ropen(-6, 9))
assert n32cc*n32ro == SetExpr(Interval(-6, 9))
assert n32lo*n32ro == SetExpr(Interval.Ropen(-6, 9))
assert n32cc/n32lo == SetExpr(Interval.Ropen(-oo, oo))
assert i12cc/n32lo == SetExpr(Interval.Ropen(-oo, oo))
assert n3n2cc**2 == SetExpr(Interval(4, 9))
assert n3n2cc**3 == SetExpr(Interval(-27, -8))
assert n23cc + i12cc == SetExpr(Interval(-1, 5))
assert n23cc - i12cc == SetExpr(Interval(-4, 2))
assert n23cc*i12cc == SetExpr(Interval(-4, 6))
assert n23cc/i12cc == SetExpr(Interval(-2, 3))
def test_SetExpr_Intersection():
x, y, z, w = symbols("x y z w")
set1 = Interval(x, y)
set2 = Interval(w, z)
inter = Intersection(set1, set2)
se = SetExpr(inter)
assert exp(se).set == Intersection(
ImageSet(Lambda(x, exp(x)), set1),
ImageSet(Lambda(x, exp(x)), set2))
assert cos(se).set == ImageSet(Lambda(x, cos(x)), inter)
def test_SetExpr_Interval_div():
# TODO: some expressions cannot be calculated due to bugs (currently
# commented):
assert SetExpr(Interval(-3, -2))/SetExpr(Interval(-2, 1)) == SetExpr(Interval(-oo, oo))
assert SetExpr(Interval(2, 3))/SetExpr(Interval(-2, 2)) == SetExpr(Interval(-oo, oo))
assert SetExpr(Interval(-3, -2))/SetExpr(Interval(0, 4)) == SetExpr(Interval(-oo, Rational(-1, 2)))
assert SetExpr(Interval(2, 4))/SetExpr(Interval(-3, 0)) == SetExpr(Interval(-oo, Rational(-2, 3)))
assert SetExpr(Interval(2, 4))/SetExpr(Interval(0, 3)) == SetExpr(Interval(Rational(2, 3), oo))
# assert SetExpr(Interval(0, 1))/SetExpr(Interval(0, 1)) == SetExpr(Interval(0, oo))
# assert SetExpr(Interval(-1, 0))/SetExpr(Interval(0, 1)) == SetExpr(Interval(-oo, 0))
assert SetExpr(Interval(-1, 2))/SetExpr(Interval(-2, 2)) == SetExpr(Interval(-oo, oo))
assert 1/SetExpr(Interval(-1, 2)) == SetExpr(Union(Interval(-oo, -1), Interval(S.Half, oo)))
assert 1/SetExpr(Interval(0, 2)) == SetExpr(Interval(S.Half, oo))
assert (-1)/SetExpr(Interval(0, 2)) == SetExpr(Interval(-oo, Rational(-1, 2)))
assert 1/SetExpr(Interval(-oo, 0)) == SetExpr(Interval.open(-oo, 0))
assert 1/SetExpr(Interval(-1, 0)) == SetExpr(Interval(-oo, -1))
# assert (-2)/SetExpr(Interval(-oo, 0)) == SetExpr(Interval(0, oo))
# assert 1/SetExpr(Interval(-oo, -1)) == SetExpr(Interval(-1, 0))
# assert SetExpr(Interval(1, 2))/a == Mul(SetExpr(Interval(1, 2)), 1/a, evaluate=False)
# assert SetExpr(Interval(1, 2))/0 == SetExpr(Interval(1, 2))*zoo
# assert SetExpr(Interval(1, oo))/oo == SetExpr(Interval(0, oo))
# assert SetExpr(Interval(1, oo))/(-oo) == SetExpr(Interval(-oo, 0))
# assert SetExpr(Interval(-oo, -1))/oo == SetExpr(Interval(-oo, 0))
# assert SetExpr(Interval(-oo, -1))/(-oo) == SetExpr(Interval(0, oo))
# assert SetExpr(Interval(-oo, oo))/oo == SetExpr(Interval(-oo, oo))
# assert SetExpr(Interval(-oo, oo))/(-oo) == SetExpr(Interval(-oo, oo))
# assert SetExpr(Interval(-1, oo))/oo == SetExpr(Interval(0, oo))
# assert SetExpr(Interval(-1, oo))/(-oo) == SetExpr(Interval(-oo, 0))
# assert SetExpr(Interval(-oo, 1))/oo == SetExpr(Interval(-oo, 0))
# assert SetExpr(Interval(-oo, 1))/(-oo) == SetExpr(Interval(0, oo))
def test_SetExpr_Interval_pow():
assert SetExpr(Interval(0, 2))**2 == SetExpr(Interval(0, 4))
assert SetExpr(Interval(-1, 1))**2 == SetExpr(Interval(0, 1))
assert SetExpr(Interval(1, 2))**2 == SetExpr(Interval(1, 4))
assert SetExpr(Interval(-1, 2))**3 == SetExpr(Interval(-1, 8))
assert SetExpr(Interval(-1, 1))**0 == SetExpr(FiniteSet(1))
assert SetExpr(Interval(1, 2))**Rational(5, 2) == SetExpr(Interval(1, 4*sqrt(2)))
#assert SetExpr(Interval(-1, 2))**Rational(1, 3) == SetExpr(Interval(-1, 2**Rational(1, 3)))
#assert SetExpr(Interval(0, 2))**S.Half == SetExpr(Interval(0, sqrt(2)))
#assert SetExpr(Interval(-4, 2))**Rational(2, 3) == SetExpr(Interval(0, 2*2**Rational(1, 3)))
#assert SetExpr(Interval(-1, 5))**S.Half == SetExpr(Interval(0, sqrt(5)))
#assert SetExpr(Interval(-oo, 2))**S.Half == SetExpr(Interval(0, sqrt(2)))
#assert SetExpr(Interval(-2, 3))**(Rational(-1, 4)) == SetExpr(Interval(0, oo))
assert SetExpr(Interval(1, 5))**(-2) == SetExpr(Interval(Rational(1, 25), 1))
assert SetExpr(Interval(-1, 3))**(-2) == SetExpr(Interval(0, oo))
assert SetExpr(Interval(0, 2))**(-2) == SetExpr(Interval(Rational(1, 4), oo))
assert SetExpr(Interval(-1, 2))**(-3) == SetExpr(Union(Interval(-oo, -1), Interval(Rational(1, 8), oo)))
assert SetExpr(Interval(-3, -2))**(-3) == SetExpr(Interval(Rational(-1, 8), Rational(-1, 27)))
assert SetExpr(Interval(-3, -2))**(-2) == SetExpr(Interval(Rational(1, 9), Rational(1, 4)))
#assert SetExpr(Interval(0, oo))**S.Half == SetExpr(Interval(0, oo))
#assert SetExpr(Interval(-oo, -1))**Rational(1, 3) == SetExpr(Interval(-oo, -1))
#assert SetExpr(Interval(-2, 3))**(Rational(-1, 3)) == SetExpr(Interval(-oo, oo))
assert SetExpr(Interval(-oo, 0))**(-2) == SetExpr(Interval.open(0, oo))
assert SetExpr(Interval(-2, 0))**(-2) == SetExpr(Interval(Rational(1, 4), oo))
assert SetExpr(Interval(Rational(1, 3), S.Half))**oo == SetExpr(FiniteSet(0))
assert SetExpr(Interval(0, S.Half))**oo == SetExpr(FiniteSet(0))
assert SetExpr(Interval(S.Half, 1))**oo == SetExpr(Interval(0, oo))
assert SetExpr(Interval(0, 1))**oo == SetExpr(Interval(0, oo))
assert SetExpr(Interval(2, 3))**oo == SetExpr(FiniteSet(oo))
assert SetExpr(Interval(1, 2))**oo == SetExpr(Interval(0, oo))
assert SetExpr(Interval(S.Half, 3))**oo == SetExpr(Interval(0, oo))
assert SetExpr(Interval(Rational(-1, 3), Rational(-1, 4)))**oo == SetExpr(FiniteSet(0))
assert SetExpr(Interval(-1, Rational(-1, 2)))**oo == SetExpr(Interval(-oo, oo))
assert SetExpr(Interval(-3, -2))**oo == SetExpr(FiniteSet(-oo, oo))
assert SetExpr(Interval(-2, -1))**oo == SetExpr(Interval(-oo, oo))
assert SetExpr(Interval(-2, Rational(-1, 2)))**oo == SetExpr(Interval(-oo, oo))
assert SetExpr(Interval(Rational(-1, 2), S.Half))**oo == SetExpr(FiniteSet(0))
assert SetExpr(Interval(Rational(-1, 2), 1))**oo == SetExpr(Interval(0, oo))
assert SetExpr(Interval(Rational(-2, 3), 2))**oo == SetExpr(Interval(0, oo))
assert SetExpr(Interval(-1, 1))**oo == SetExpr(Interval(-oo, oo))
assert SetExpr(Interval(-1, S.Half))**oo == SetExpr(Interval(-oo, oo))
assert SetExpr(Interval(-1, 2))**oo == SetExpr(Interval(-oo, oo))
assert SetExpr(Interval(-2, S.Half))**oo == SetExpr(Interval(-oo, oo))
assert (SetExpr(Interval(1, 2))**x).dummy_eq(SetExpr(ImageSet(Lambda(_d, _d**x), Interval(1, 2))))
assert SetExpr(Interval(2, 3))**(-oo) == SetExpr(FiniteSet(0))
assert SetExpr(Interval(0, 2))**(-oo) == SetExpr(Interval(0, oo))
assert (SetExpr(Interval(-1, 2))**(-oo)).dummy_eq(SetExpr(ImageSet(Lambda(_d, _d**(-oo)), Interval(-1, 2))))
def test_SetExpr_Integers():
assert SetExpr(S.Integers) + 1 == SetExpr(S.Integers)
assert (SetExpr(S.Integers) + I).dummy_eq(
SetExpr(ImageSet(Lambda(_d, _d + I), S.Integers)))
assert SetExpr(S.Integers)*(-1) == SetExpr(S.Integers)
assert (SetExpr(S.Integers)*2).dummy_eq(
SetExpr(ImageSet(Lambda(_d, 2*_d), S.Integers)))
assert (SetExpr(S.Integers)*I).dummy_eq(
SetExpr(ImageSet(Lambda(_d, I*_d), S.Integers)))
# issue #18050:
assert SetExpr(S.Integers)._eval_func(Lambda(x, I*x + 1)).dummy_eq(
SetExpr(ImageSet(Lambda(_d, I*_d + 1), S.Integers)))
# needs improvement:
assert (SetExpr(S.Integers)*I + 1).dummy_eq(
SetExpr(ImageSet(Lambda(x, x + 1),
ImageSet(Lambda(_d, _d*I), S.Integers))))

File diff suppressed because it is too large Load Diff