def test_finite_basic(): x = Symbol('x') A = FiniteSet(1, 2, 3) B = FiniteSet(3, 4, 5) AorB = Union(A, B) AandB = A.intersect(B) assert A.is_subset(AorB) and B.is_subset(AorB) assert AandB.is_subset(A) assert AandB == FiniteSet(3) assert A.inf == 1 and A.sup == 3 assert AorB.inf == 1 and AorB.sup == 5 assert FiniteSet(x, 1, 5).sup == Max(x, 5) assert FiniteSet(x, 1, 5).inf == Min(x, 1) # issue 7335 assert FiniteSet(S.EmptySet) != S.EmptySet assert FiniteSet(FiniteSet(1, 2, 3)) != FiniteSet(1, 2, 3) assert FiniteSet((1, 2, 3)) != FiniteSet(1, 2, 3) # Ensure a variety of types can exist in a FiniteSet s = FiniteSet((1, 2), Float, A, -5, x, 'eggs', x**2, Interval) assert (A > B) is False assert (A >= B) is False assert (A < B) is False assert (A <= B) is False assert AorB > A and AorB > B assert AorB >= A and AorB >= B assert A >= A and A <= A assert A >= AandB and B >= AandB assert A > AandB and B > AandB
def test_contains(): assert Interval(0, 2).contains(1) is S.true assert Interval(0, 2).contains(3) is S.false assert Interval(0, 2, True, False).contains(0) is S.false assert Interval(0, 2, True, False).contains(2) is S.true assert Interval(0, 2, False, True).contains(0) is S.true assert Interval(0, 2, False, True).contains(2) is S.false assert Interval(0, 2, True, True).contains(0) is S.false assert Interval(0, 2, True, True).contains(2) is S.false assert (Interval(0, 2) in Interval(0, 2)) is False assert FiniteSet(1, 2, 3).contains(2) is S.true assert FiniteSet(1, 2, Symbol('x')).contains(Symbol('x')) is S.true # issue 8197 from sympy.abc import a, b assert isinstance(FiniteSet(b).contains(-a), Contains) assert isinstance(FiniteSet(b).contains(a), Contains) assert isinstance(FiniteSet(a).contains(1), Contains) raises(TypeError, lambda: 1 in FiniteSet(a)) # issue 8209 rad1 = Pow(Pow(2, S(1)/3) - 1, S(1)/3) rad2 = Pow(S(1)/9, S(1)/3) - Pow(S(2)/9, S(1)/3) + Pow(S(4)/9, S(1)/3) s1 = FiniteSet(rad1) s2 = FiniteSet(rad2) assert s1 - s2 == S.EmptySet items = [1, 2, S.Infinity, S('ham'), -1.1] fset = FiniteSet(*items) assert all(item in fset for item in items) assert all(fset.contains(item) is S.true for item in items) assert Union(Interval(0, 1), Interval(2, 5)).contains(3) is S.true assert Union(Interval(0, 1), Interval(2, 5)).contains(6) is S.false assert Union(Interval(0, 1), FiniteSet(2, 5)).contains(3) is S.false assert S.EmptySet.contains(1) is S.false assert FiniteSet(rootof(x**3 + x - 1, 0)).contains(S.Infinity) is S.false assert rootof(x**5 + x**3 + 1, 0) in S.Reals assert not rootof(x**5 + x**3 + 1, 1) in S.Reals # non-bool results assert Union(Interval(1, 2), Interval(3, 4)).contains(x) == \ Or(And(x <= 2, x >= 1), And(x <= 4, x >= 3)) assert Intersection(Interval(1, x), Interval(2, 3)).contains(y) == \ And(y <= 3, y <= x, y >= 1, y >= 2) assert (S.Complexes).contains(S.ComplexInfinity) == S.false
def test_powerset(): # 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 I = Interval(0, 1) raises(NotImplementedError, I.powerset)
def test_real(): x = Symbol('x', real=True, finite=True) I = Interval(0, 5) J = Interval(10, 20) A = FiniteSet(1, 2, 30, x, S.Pi) B = FiniteSet(-4, 0) C = FiniteSet(100) D = FiniteSet('Ham', 'Eggs') assert all(s.is_subset(S.Reals) for s in [I, J, A, B, C]) assert not D.is_subset(S.Reals) assert all((a + b).is_subset(S.Reals) for a in [I, J, A, B, C] for b in [I, J, A, B, C]) assert not any((a + D).is_subset(S.Reals) for a in [I, J, A, B, C, D]) assert not (I + A + D).is_subset(S.Reals)
def test_finite_basic(): x = Symbol('x') A = FiniteSet(1,2,3) B = FiniteSet(3,4,5) AorB = Union(A,B) AandB = A.intersect(B) assert AorB.subset(A) and AorB.subset(B) assert A.subset(AandB) assert AandB == FiniteSet(3) assert A.inf == 1 and A.sup == 3 assert AorB.inf == 1 and AorB.sup ==5 assert FiniteSet(x, 1, 5).sup == Max(x,5) assert FiniteSet(x, 1, 5).inf == Min(x,1) # Ensure a variety of types can exist in a FiniteSet S = FiniteSet((1,2), Float, A, -5, x, 'eggs', x**2, Interval)
def test_contains(): assert Interval(0, 2).contains(1) is S.true assert Interval(0, 2).contains(3) is S.false assert Interval(0, 2, True, False).contains(0) is S.false assert Interval(0, 2, True, False).contains(2) is S.true assert Interval(0, 2, False, True).contains(0) is S.true assert Interval(0, 2, False, True).contains(2) is S.false assert Interval(0, 2, True, True).contains(0) is S.false assert Interval(0, 2, True, True).contains(2) is S.false assert FiniteSet(1, 2, 3).contains(2) is S.true assert FiniteSet(1, 2, Symbol("x")).contains(Symbol("x")) is S.true # issue 8197 from sympy.abc import a, b assert isinstance(FiniteSet(b).contains(-a), Contains) assert isinstance(FiniteSet(b).contains(a), Contains) assert isinstance(FiniteSet(a).contains(1), Contains) raises(TypeError, lambda: 1 in FiniteSet(a)) # issue 8209 rad1 = Pow(Pow(2, S(1) / 3) - 1, S(1) / 3) rad2 = Pow(S(1) / 9, S(1) / 3) - Pow(S(2) / 9, S(1) / 3) + Pow(S(4) / 9, S(1) / 3) s1 = FiniteSet(rad1) s2 = FiniteSet(rad2) assert s1 - s2 == S.EmptySet items = [1, 2, S.Infinity, S("ham"), -1.1] fset = FiniteSet(*items) assert all(item in fset for item in items) assert all(fset.contains(item) is S.true for item in items) assert Union(Interval(0, 1), Interval(2, 5)).contains(3) is S.true assert Union(Interval(0, 1), Interval(2, 5)).contains(6) is S.false assert Union(Interval(0, 1), FiniteSet(2, 5)).contains(3) is S.false assert S.EmptySet.contains(1) is S.false assert FiniteSet(RootOf(x ** 3 + x - 1, 0)).contains(S.Infinity) is S.false assert RootOf(x ** 5 + x ** 3 + 1, 0) in S.Reals assert not RootOf(x ** 5 + x ** 3 + 1, 1) in S.Reals
def test_contains(): assert Interval(0, 2).contains(1) == True assert Interval(0, 2).contains(3) == False assert Interval(0, 2, True, False).contains(0) == False assert Interval(0, 2, True, False).contains(2) == True assert Interval(0, 2, False, True).contains(0) == True assert Interval(0, 2, False, True).contains(2) == False assert Interval(0, 2, True, True).contains(0) == False assert Interval(0, 2, True, True).contains(2) == False assert FiniteSet(1,2,3).contains(2) assert FiniteSet(1,2,Symbol('x')).contains(Symbol('x')) items = [1, 2, S.Infinity, S('ham'), -1.1] fset = FiniteSet(*items) assert all(item in fset for item in items) assert all(fset.contains(item) is True for item in items) assert Union(Interval(0, 1), Interval(2, 5)).contains(3) == True assert Union(Interval(0, 1), Interval(2, 5)).contains(6) == False assert Union(Interval(0, 1), FiniteSet(2, 5)).contains(3) == False assert S.EmptySet.contains(1) == False
class Type_A_Interval_III(object): #generate interval of type: {x, y, ...} (Finite set) or a Singleton - must not result in an EmptySet() def __init__(self): self.set_str = "" self.val_lst = [] self.interval = FiniteSet() def generate_interval(self): if randint(0, 1) == 0: #generate FiniteSet amount = randint(1, 5) for i in range(amount): if self.val_lst == []: self.val_lst.append(randint(-15,15)) else: self.val_lst.append(randint(self.val_lst[i-1]-5, self.val_lst[i-1]+5)) else: #generate Singleton self.val_lst.append(randint(-20, 20)) for i in range(len(self.val_lst)): self.interval = self.interval.union(FiniteSet(self.val_lst[i])) self.set_str += '\{'+str(self.interval)+'\}'
from sympy import FiniteSet s = FiniteSet(1, 2, 3) ps = s.powerset() print(ps) # from sympy import FiniteSet # s = FiniteSet(1, 2, 3) # t = FiniteSet(2, 4, 6) # unioned = s.union(t) # print(unioned) # from sympy import FiniteSet # s = FiniteSet(1, 2) # t = FiniteSet(2, 3) # intersected = s.intersect(t) # print(intersected) # from sympy import FiniteSet # s = FiniteSet(1, 2) # t = FiniteSet(3, 4) # p = s*t # # u = FiniteSet(5, 6) # # p = s*t*u # for elem in p: # print(elem)
def test_sin(): x, y = symbols('x y') assert sin.nargs == FiniteSet(1) assert sin(nan) == nan assert sin(oo*I) == oo*I assert sin(-oo*I) == -oo*I assert sin(oo).args[0] == oo assert sin(0) == 0 assert sin(asin(x)) == x assert sin(atan(x)) == x / sqrt(1 + x**2) assert sin(acos(x)) == sqrt(1 - x**2) assert sin(acot(x)) == 1 / (sqrt(1 + 1 / x**2) * x) assert sin(atan2(y, x)) == y / sqrt(x**2 + y**2) assert sin(pi*I) == sinh(pi)*I assert sin(-pi*I) == -sinh(pi)*I assert sin(-2*I) == -sinh(2)*I assert sin(pi) == 0 assert sin(-pi) == 0 assert sin(2*pi) == 0 assert sin(-2*pi) == 0 assert sin(-3*10**73*pi) == 0 assert sin(7*10**103*pi) == 0 assert sin(pi/2) == 1 assert sin(-pi/2) == -1 assert sin(5*pi/2) == 1 assert sin(7*pi/2) == -1 n = symbols('n', integer=True) assert sin(pi*n/2) == (-1)**(n/2 - S.Half) assert sin(pi/3) == S.Half*sqrt(3) assert sin(-2*pi/3) == -S.Half*sqrt(3) assert sin(pi/4) == S.Half*sqrt(2) assert sin(-pi/4) == -S.Half*sqrt(2) assert sin(17*pi/4) == S.Half*sqrt(2) assert sin(-3*pi/4) == -S.Half*sqrt(2) assert sin(pi/6) == S.Half assert sin(-pi/6) == -S.Half assert sin(7*pi/6) == -S.Half assert sin(-5*pi/6) == -S.Half assert sin(1*pi/5) == sqrt((5 - sqrt(5)) / 8) assert sin(2*pi/5) == sqrt((5 + sqrt(5)) / 8) assert sin(3*pi/5) == sin(2*pi/5) assert sin(4*pi/5) == sin(1*pi/5) assert sin(6*pi/5) == -sin(1*pi/5) assert sin(8*pi/5) == -sin(2*pi/5) assert sin(-1273*pi/5) == -sin(2*pi/5) assert sin(pi/8) == sqrt((2 - sqrt(2))/4) assert sin(104*pi/105) == sin(pi/105) assert sin(106*pi/105) == -sin(pi/105) assert sin(-104*pi/105) == -sin(pi/105) assert sin(-106*pi/105) == sin(pi/105) assert sin(x*I) == sinh(x)*I assert sin(k*pi) == 0 assert sin(17*k*pi) == 0 assert sin(k*pi*I) == sinh(k*pi)*I assert sin(r).is_real is True assert isinstance(sin( re(x) - im(y)), sin) is True assert isinstance(sin(-re(x) + im(y)), sin) is False for d in list(range(1, 22)) + [60, 85]: for n in xrange(0, d*2 + 1): x = n*pi/d e = abs( float(sin(x)) - sin(float(x)) ) assert e < 1e-12
print A.intersection(B) # Diferencia entre conjuntos print A - B print B - A """ Conjunto y operaciones con conjuntos usando la libreria SYMPY """ # Utilizando FiniteSet de sympy from sympy import FiniteSet C = FiniteSet(1, 2, 3) # Subconjunto y subconjunto propio A = FiniteSet(1,2,3) B = FiniteSet(1,2,3,4,5) A.subset(B) # Union de dos conjuntos A = FiniteSet(1, 2, 3) B = FiniteSet(2, 4, 6) A.union(B) # Interseccion de dos conjuntos A = FiniteSet(1, 2) B = FiniteSet(2, 3) A.intersect(B)
def test_issue_9808(): assert Complement(FiniteSet(y), FiniteSet(1)) == Complement(FiniteSet(y), FiniteSet(1), evaluate=False) assert Complement(FiniteSet(1, 2, x), FiniteSet(x, y, 2, 3)) == \ Complement(FiniteSet(1), FiniteSet(y), evaluate=False)
def test_issue_Symbol_inter(): i = Interval(0, oo) r = S.Reals mat = Matrix([0, 0, 0]) assert Intersection(r, i, FiniteSet(m), FiniteSet(m, n)) == \ Intersection(i, FiniteSet(m)) assert Intersection(FiniteSet(1, m, n), FiniteSet(m, n, 2), i) == \ Intersection(i, FiniteSet(m, n)) assert Intersection(FiniteSet(m, n, x), FiniteSet(m, z), r) == \ Intersection(r, FiniteSet(m, z), FiniteSet(n, x)) assert Intersection(FiniteSet(m, n, 3), FiniteSet(m, n, x), r) == \ Intersection(r, FiniteSet(3, m, n), evaluate=False) assert Intersection(FiniteSet(m, n, 3), FiniteSet(m, n, 2, 3), r) == \ Union(FiniteSet(3), Intersection(r, FiniteSet(m, n))) assert Intersection(r, FiniteSet(mat, 2, n), FiniteSet(0, mat, n)) == \ Intersection(r, FiniteSet(n)) assert Intersection(FiniteSet(sin(x), cos(x)), FiniteSet(sin(x), cos(x), 1), r) == \ Intersection(r, FiniteSet(sin(x), cos(x))) assert Intersection(FiniteSet(x**2, 1, sin(x)), FiniteSet(x**2, 2, sin(x)), r) == \ Intersection(r, FiniteSet(x**2, sin(x)))
def test_powerset(): # 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 I = Interval(0, 1) raises(NotImplementedError, I.powerset)
def test_supinf(): x = Symbol('x', real=True) y = Symbol('y', real=True) assert (Interval(0, 1) + FiniteSet(2)).sup == 2 assert (Interval(0, 1) + FiniteSet(2)).inf == 0 assert (Interval(0, 1) + FiniteSet(x)).sup == Max(1, x) assert (Interval(0, 1) + FiniteSet(x)).inf == Min(0, x) assert FiniteSet(5, 1, x).sup == Max(5, x) assert FiniteSet(5, 1, x).inf == Min(1, x) assert FiniteSet(5, 1, x, y).sup == Max(5, x, y) assert FiniteSet(5, 1, x, y).inf == Min(1, x, y) assert FiniteSet(5, 1, x, y, S.Infinity, S.NegativeInfinity).sup == \ S.Infinity assert FiniteSet(5, 1, x, y, S.Infinity, S.NegativeInfinity).inf == \ S.NegativeInfinity assert FiniteSet('Ham', 'Eggs').sup == Max('Ham', 'Eggs')
def test_Intersection_as_relational(): x = Symbol('x') assert (Intersection(Interval(0, 1), FiniteSet(2), evaluate=False).as_relational(x) == And( And(Le(0, x), Le(x, 1)), Eq(x, 2)))
def test_Union_as_relational(): x = Symbol('x') assert (Interval(0, 1) + FiniteSet(2)).as_relational(x) == \ Or(And(Le(0, x), Le(x, 1)), Eq(x, 2)) assert (Interval(0, 1, True, True) + FiniteSet(1)).as_relational(x) == \ And(Lt(0, x), Le(x, 1))
def test_Finite_as_relational(): x = Symbol('x') y = Symbol('y') assert FiniteSet(1, 2).as_relational(x) == Or(Eq(x, 1), Eq(x, 2)) assert FiniteSet(y, -5).as_relational(x) == Or(Eq(x, y), Eq(x, -5))
def test_union(): assert Union(Interval(1, 2), Interval(2, 3)) == Interval(1, 3) assert Union(Interval(1, 2), Interval(2, 3, True)) == Interval(1, 3) assert Union(Interval(1, 3), Interval(2, 4)) == Interval(1, 4) assert Union(Interval(1, 2), Interval(1, 3)) == Interval(1, 3) assert Union(Interval(1, 3), Interval(1, 2)) == Interval(1, 3) assert Union(Interval(1, 3, False, True), Interval(1, 2)) == \ Interval(1, 3, False, True) assert Union(Interval(1, 3), Interval(1, 2, False, True)) == Interval(1, 3) assert Union(Interval(1, 2, True), Interval(1, 3)) == Interval(1, 3) assert Union(Interval(1, 2, True), Interval(1, 3, True)) == \ Interval(1, 3, True) assert Union(Interval(1, 2, True), Interval(1, 3, True, True)) == \ Interval(1, 3, True, True) assert Union(Interval(1, 2, True, True), Interval(1, 3, True)) == \ Interval(1, 3, True) assert Union(Interval(1, 3), Interval(2, 3)) == Interval(1, 3) assert Union(Interval(1, 3, False, True), Interval(2, 3)) == \ Interval(1, 3) assert Union(Interval(1, 2, False, True), Interval(2, 3, True)) != \ Interval(1, 3) assert Union(Interval(1, 2), S.EmptySet) == Interval(1, 2) assert Union(S.EmptySet) == S.EmptySet assert Union(Interval(0, 1), [FiniteSet(1.0/n) for n in range(1, 10)]) == \ Interval(0, 1) assert Interval(1, 2).union(Interval(2, 3)) == \ Interval(1, 2) + Interval(2, 3) assert Interval(1, 2).union(Interval(2, 3)) == Interval(1, 3) assert Union(Set()) == Set() assert FiniteSet(1) + FiniteSet(2) + FiniteSet(3) == FiniteSet(1, 2, 3) assert FiniteSet('ham') + FiniteSet('eggs') == FiniteSet('ham', 'eggs') assert FiniteSet(1, 2, 3) + S.EmptySet == FiniteSet(1, 2, 3) assert FiniteSet(1, 2, 3) & FiniteSet(2, 3, 4) == FiniteSet(2, 3) assert FiniteSet(1, 2, 3) | FiniteSet(2, 3, 4) == FiniteSet(1, 2, 3, 4) x = Symbol("x") y = Symbol("y") z = Symbol("z") assert S.EmptySet | FiniteSet(x, FiniteSet(y, z)) == \ FiniteSet(x, FiniteSet(y, z)) # Test that Intervals and FiniteSets play nicely assert Interval(1, 3) + FiniteSet(2) == Interval(1, 3) assert Interval(1, 3, True, True) + FiniteSet(3) == \ Interval(1, 3, True, False) X = Interval(1, 3) + FiniteSet(5) Y = Interval(1, 2) + FiniteSet(3) XandY = X.intersect(Y) assert 2 in X and 3 in X and 3 in XandY assert XandY.is_subset(X) and XandY.is_subset(Y) raises(TypeError, lambda: Union(1, 2, 3)) assert X.is_iterable is False # issue 7843 assert Union(S.EmptySet, FiniteSet(-sqrt(-I), sqrt(-I))) == \ FiniteSet(-sqrt(-I), sqrt(-I))
def test_contains(): assert Interval(0, 2).contains(1) is S.true assert Interval(0, 2).contains(3) is S.false assert Interval(0, 2, True, False).contains(0) is S.false assert Interval(0, 2, True, False).contains(2) is S.true assert Interval(0, 2, False, True).contains(0) is S.true assert Interval(0, 2, False, True).contains(2) is S.false assert Interval(0, 2, True, True).contains(0) is S.false assert Interval(0, 2, True, True).contains(2) is S.false assert FiniteSet(1, 2, 3).contains(2) is S.true assert FiniteSet(1, 2, Symbol('x')).contains(Symbol('x')) is S.true # issue 8197 from sympy.abc import a, b assert isinstance(FiniteSet(b).contains(-a), Contains) assert isinstance(FiniteSet(b).contains(a), Contains) assert isinstance(FiniteSet(a).contains(1), Contains) raises(TypeError, lambda: 1 in FiniteSet(a)) # issue 8209 rad1 = Pow(Pow(2, S(1) / 3) - 1, S(1) / 3) rad2 = Pow(S(1) / 9, S(1) / 3) - Pow(S(2) / 9, S(1) / 3) + Pow(S(4) / 9, S(1) / 3) s1 = FiniteSet(rad1) s2 = FiniteSet(rad2) assert s1 - s2 == S.EmptySet items = [1, 2, S.Infinity, S('ham'), -1.1] fset = FiniteSet(*items) assert all(item in fset for item in items) assert all(fset.contains(item) is S.true for item in items) assert Union(Interval(0, 1), Interval(2, 5)).contains(3) is S.true assert Union(Interval(0, 1), Interval(2, 5)).contains(6) is S.false assert Union(Interval(0, 1), FiniteSet(2, 5)).contains(3) is S.false assert S.EmptySet.contains(1) is S.false assert FiniteSet(RootOf(x**3 + x - 1, 0)).contains(S.Infinity) is S.false assert RootOf(x**5 + x**3 + 1, 0) in S.Reals assert not RootOf(x**5 + x**3 + 1, 1) in S.Reals # non-bool results assert Union(Interval(1, 2), Interval(3, 4)).contains(x) == \ Or(And(x <= 2, x >= 1), And(x <= 4, x >= 3)) assert Intersection(Interval(1, x), Interval(2, 3)).contains(y) == \ And(y <= 3, y <= x, y >= 1, y >= 2)
def test_Union_of_ProductSets_shares(): line = Interval(0, 2) points = FiniteSet(0, 1, 2) assert Union(line * line, line * points) == line * line
def test_is_subset(): assert Interval(0, 1).is_subset(Interval(0, 2)) is True assert Interval(0, 3).is_subset(Interval(0, 2)) is False assert FiniteSet(1, 2).is_subset(FiniteSet(1, 2, 3, 4)) assert FiniteSet(4, 5).is_subset(FiniteSet(1, 2, 3, 4)) is False assert FiniteSet(1).is_subset(Interval(0, 2)) assert FiniteSet(1, 2).is_subset(Interval(0, 2, True, True)) is False assert (Interval(1, 2) + FiniteSet(3)).is_subset( (Interval(0, 2, False, True) + FiniteSet(2, 3))) assert Interval(3, 4).is_subset(Union(Interval(0, 1), Interval(2, 5))) is True assert Interval(3, 6).is_subset(Union(Interval(0, 1), Interval( 2, 5))) is False assert FiniteSet(1, 2, 3, 4).is_subset(Interval(0, 5)) is True assert S.EmptySet.is_subset(FiniteSet(1, 2, 3)) is True assert Interval(0, 1).is_subset(S.EmptySet) is False assert S.EmptySet.is_subset(S.EmptySet) is True raises(ValueError, lambda: S.EmptySet.is_subset(1)) # tests for the issubset alias assert FiniteSet(1, 2, 3, 4).issubset(Interval(0, 5)) is True assert S.EmptySet.issubset(FiniteSet(1, 2, 3)) is True
def test_image_piecewise(): f = Piecewise((x, x <= -1), (1 / x**2, x <= 5), (x**3, True)) f1 = Piecewise((0, x <= 1), (1, x <= 2), (2, True)) assert imageset(x, f, Interval(-5, 5)) == Union(Interval(-5, -1), Interval(S(1) / 25, oo)) assert imageset(x, f1, Interval(1, 2)) == FiniteSet(0, 1)
def test_issue_9956(): assert Union(Interval(-oo, oo), FiniteSet(1)) == Interval(-oo, oo) assert Interval(-oo, oo).contains(1) is S.true
def test_image_FiniteSet(): x = Symbol('x', real=True) assert imageset(x, 2 * x, FiniteSet(1, 2, 3)) == FiniteSet(2, 4, 6)
def test_issue_10248(): assert list(Intersection(S.Reals, FiniteSet(x))) == [And(x < oo, x > -oo)]
def test_image_Union(): x = Symbol('x', real=True) assert imageset(x, x**2, Interval(-2, 0) + FiniteSet(1, 2, 3)) == \ (Interval(0, 4) + FiniteSet(9))
def test_cos(): x, y = symbols('x y') assert cos.nargs == FiniteSet(1) assert cos(nan) == nan assert cos(oo*I) == oo assert cos(-oo*I) == oo assert cos(0) == 1 assert cos(acos(x)) == x assert cos(atan(x)) == 1 / sqrt(1 + x**2) assert cos(asin(x)) == sqrt(1 - x**2) assert cos(acot(x)) == 1 / sqrt(1 + 1 / x**2) assert cos(atan2(y, x)) == x / sqrt(x**2 + y**2) assert cos(pi*I) == cosh(pi) assert cos(-pi*I) == cosh(pi) assert cos(-2*I) == cosh(2) assert cos(pi/2) == 0 assert cos(-pi/2) == 0 assert cos(pi/2) == 0 assert cos(-pi/2) == 0 assert cos((-3*10**73 + 1)*pi/2) == 0 assert cos((7*10**103 + 1)*pi/2) == 0 n = symbols('n', integer=True) assert cos(pi*n/2) == 0 assert cos(pi) == -1 assert cos(-pi) == -1 assert cos(2*pi) == 1 assert cos(5*pi) == -1 assert cos(8*pi) == 1 assert cos(pi/3) == S.Half assert cos(-2*pi/3) == -S.Half assert cos(pi/4) == S.Half*sqrt(2) assert cos(-pi/4) == S.Half*sqrt(2) assert cos(11*pi/4) == -S.Half*sqrt(2) assert cos(-3*pi/4) == -S.Half*sqrt(2) assert cos(pi/6) == S.Half*sqrt(3) assert cos(-pi/6) == S.Half*sqrt(3) assert cos(7*pi/6) == -S.Half*sqrt(3) assert cos(-5*pi/6) == -S.Half*sqrt(3) assert cos(1*pi/5) == (sqrt(5) + 1)/4 assert cos(2*pi/5) == (sqrt(5) - 1)/4 assert cos(3*pi/5) == -cos(2*pi/5) assert cos(4*pi/5) == -cos(1*pi/5) assert cos(6*pi/5) == -cos(1*pi/5) assert cos(8*pi/5) == cos(2*pi/5) assert cos(-1273*pi/5) == -cos(2*pi/5) assert cos(pi/8) == sqrt((2 + sqrt(2))/4) assert cos(104*pi/105) == -cos(pi/105) assert cos(106*pi/105) == -cos(pi/105) assert cos(-104*pi/105) == -cos(pi/105) assert cos(-106*pi/105) == -cos(pi/105) assert cos(x*I) == cosh(x) assert cos(k*pi*I) == cosh(k*pi) assert cos(r).is_real is True assert cos(k*pi) == (-1)**k assert cos(2*k*pi) == 1 for d in list(range(1, 22)) + [60, 85]: for n in xrange(0, 2*d + 1): x = n*pi/d e = abs( float(cos(x)) - cos(float(x)) ) assert e < 1e-12
def test_union_boundary_of_joining_sets(): """ Testing the boundary of unions is a hard problem """ assert Union(Interval(0, 10), Interval(10, 15), evaluate=False).boundary \ == FiniteSet(0, 15)
from sympy import FiniteSet, pi # Unions & Intersections s = FiniteSet(1, 2, 3) t = FiniteSet(2, 4, 6) union = s.union(t) print(union) intersection = s.intersect(t) print(intersection) ### Cartesian Products cartesianProduct = s * t print(cartesianProduct) for elem in cartesianProduct: print(elem) # Raise set to the power (calculate triplets) cartesianProductCubed = s ** 3 for elem in cartesianProductCubed: print(elem) def time_period(length, g): T = 2*pi*(length/g)**0.5 return T L = FiniteSet(15, 18, 21, 22.5, 25)
def test_boundary_ProductSet_line(): line_in_r2 = Interval(0, 1) * FiniteSet(0) assert line_in_r2.boundary == line_in_r2
from sympy import FiniteSet s = FiniteSet(1, 2, 3, 4, 5, 6) a = FiniteSet(2, 3, 5) b = FiniteSet(1, 3, 5) e = a.union(b) print(len(e)/len(s)) # from sympy import FiniteSet # s = FiniteSet(1, 2, 3, 4, 5, 6) # a = FiniteSet(2, 3, 5) # b = FiniteSet(1, 3, 5) # e = a.intersect(b) # print(len(e)/len(s))
def set_second_part(self, num, element): set_str = "" #set_type = choice(['finite_set', 'infinite_set', 'empty_set', 'powerset']) #self.set_type = choice(['empty_set', 'finite_set', 'infinite_set']) self.set_type = choice(['infinite_set', 'empty_set', 'finite_set']) uberset = "" x = 0 y = 0 temp_str = '' temp_set = None #generate set in formal notation: {x | x in R and ...} if num == 0: uberset = self.f_set_dict[element] if element != 'x' else self.f_set_dict[choice(list(self.f_set_dict.keys()))] set_str += ' \ | \ '+element+'\in'+uberset set_str += '\wedge \ ' if self.set_type == 'finite_set': self.set_description[0] = 'finite_set' c = choice([0, 1, 2, 3]) if c == 0: temp_set = FiniteSet(1) if uberset == '\mathbb N_{> 0}' else FiniteSet(0, 1) set_str += element+'^{2} = \ '+element self.set_description[1] = '\{'+str(temp_set)+'\}' self.set_description[2] = temp_set elif c == 1: x = randint(0, 5) if element == 'n' else randint(-5, 0) y = randint(5, 10) if element == 'n' else randint(0, 5) fin_set = FiniteSet() for i in range(x+1, y, 1): fin_set = fin_set.union(FiniteSet(i)) if element == 'n' or element == 'z': set_str += element+'\in \ ('+str(x)+', '+str(y)+')' self.set_description[1] = '\{'+str(fin_set)+'\}' self.set_description[2] = fin_set else: x = randint(1, 99) set_str += element+'\subseteq \ \{'+str(x)+'\}' self.set_description[1] = '\{\emptyset, '+str(x)+'\}' self.set_description[2] = FiniteSet(EmptySet(), x) elif c == 2: if choice([0, 1]) == 0: x = randint(-5, -5) y = randint(-5, 5) set_str = '\{'+str(x)+', '+str(y)+'\}' self.set_description[1] = '\{'+str(FiniteSet(x, y))+'\}' self.set_description[2] = FiniteSet(x, y) else: x = choice([x for x in np.arange(0, 99) if sqrt(x).is_integer()]) set_str += '\sqrt{'+element+'} = '+str(int(sqrt(x))) self.set_description[1] = '\{'+str(x)+'\}' self.set_description[2] = FiniteSet(x) elif c == 3: element+'\\textless 2 \wedge'+element+'\in \mathbb{N}' if uberset == '\mathbb N_{> 0}': temp_set = FiniteSet(1) elif uberset == '\mathbb{N}': temp_set = FiniteSet(0, 1) else: temp_set = FiniteSet(0, 1) set_str += element+'^{2} = \ '+element self.set_description[1] = '\{'+str(temp_set)+'\}' self.set_description[2] = temp_set elif self.set_type == 'infinite_set': self.set_description[0] = 'infinite_set' if element == 'n': if choice([0, 1]) == 0: set_str += element+'> 0' self.set_description[1] = '\mathbb N_{> 0}' self.set_description[2] = 'NPOS' else: set_str += element+'\in \mathbb{Z}' self.set_description[1] = '\mathbb{N}' self.set_description[2] = 'N' elif element == 'z': if choice([0, 1]) == 0: set_str += element+'\geq 0' self.set_description[1] = '\mathbb{N}' self.set_description[2] = 'N' else: set_str += '0 > '+element self.set_description[1] = 'ZNEG' self.set_description[2] = '-Z' elif element == 'q': if choice([0, 1]) == 0: set_str += element+'\in \mathbb{Z}' self.set_description[1] = '\mathbb{Z}' self.set_description[2] = 'Z' else: set_str += element+' \in \mathbb{Q}' self.set_description[1] = 'Q' self.set_description[2] = QQ elif element == 'r': if choice([0, 1]) == 0: set_str += element+'^{2} \in \mathbb{N}' self.set_description[0] = 'finite_set' self.set_description[1] = '\{'+str(FiniteSet(0, 1))+'\}' self.set_description[2] = FiniteSet(0, 1) else: x = randint(-2, 0) y = randint(x, 2) set_str += element+'\in ('+str(x)+', '+str(y)+')' temp_set = Interval(x, y, True, True) if isinstance(temp_set, FiniteSet): self.set_description[0] = 'empty_set' self.set_description[2] = EmptySet() else: self.set_description[2] = Interval(x, y, True, True) self.set_description[1] = str(self.set_description[2]) else: if choice([0, 1]) == 0: set_str += element+'\in \mathcal{P}('+uberset+') \wedge '+element+'^{2} = '+element if uberset == '\mathbb N_{> 0}': self.set_description[1] = '\{' self.set_description[2] = FiniteSet() self.set_description[0] = 'finite_set' else: set_str += element+'\leq 0' self.set_description[1] = '\mathbb{Z} \setminus \mathbb{N}' self.set_description[2] = 'ZNEG' elif self.set_type == 'empty_set': self.set_description[0] = 'empty_set' c = choice([0, 1]) if c == 0: set_str += element+'\in \mathcal{P}('+self.f_set_dict[choice(list(self.f_set_dict.keys()))]+')' elif c == 1: set_str += '\emptyset \in \ '+uberset self.set_description[1] = '\emptyset' self.set_description[2] = EmptySet() else: self.description[0] = 'empty_set' self.set_description[1] = '\emptyset' self.set_description[2] = EmptySet() """elif num == 1: if self.set_type == 'finite_set': self.set_description[0] = 'finite_set' if element == '\{x\}': if choice([0, 1]) == 0: set_str += ' \ | \ x \in \mathbb{N} \wedgde x \leq 0' self.set_description[1] = '\{1\}' self.set_description[2] = FiniteSet(0) else: set_str += ' \ | \ x \in \mathbb{Z} \wedge \mathcal{P}(\{x\}) = \{\emptyset, \{-1\}, \{1\}, \{-1, 1\}\}' self.set_description[1] = '\{\-1, 1}' self.set_description[2] = FiniteSet(-1, 1) elif element == '\{x+1\}': x = randint(-10, 10) set_str += ' \ | \ x \in \mathbb{Z} \wedge x = '+str(x) self.set_description[1] = '\{'+str(x+1)+'\}' self.set_description[2] = FiniteSet(x+1) elif element == '\lvert x \\rvert': set_str += ' \ | \ x \in \mathbb{Z} \wedge x \leq \pi' self.set_description[1] = '\{0, 1, 2, 3\}' self.set_description[2] = FiniteSet(0, 1, 2, 3) elif element == '-x': if choice([0, 1]) == 0: set_str += ' \ | \ x \in \mathbb{R} \wedge x^{2} = 2' self.set_description[1] = '-\sqrt{2}, \sqrt{2}' self.set_description[2] = 'sqrt1' else: set_str += ' \ | \ x \in \mathbb{R} \wedge x^{2} = 2 \wedge x \leq 0' self.set_description[1] = '\sqrt{2}' self.set_description[2] = 'sqrt2' elif element == '2x': x = randint(0, 50) set_str += ' \ | \ x \in \mathbb{Q} \wedge \\frac{x}{2} = '+str(x) self.set_description[1] = '\{'+str(x*2)+'\}' self.set_description[2] = FiniteSet(x*2) else: self.set_description[1] = FiniteSet(0, 0, 0) elif self.set_type == 'infinite_set': self.set_description[0] = 'infinite_set' if element == '\{x\}' or element == '\{x+1\}': temp_str = choice(['\mathbb{N}', '\mathbb{Z}']) set_str += ' \ | \ x \in'+temp_str self.set_description[1] = 'setofsets' self.set_description[2] = 'POW' elif element == '\lvert x \\rvert': set_str += ' \ | \ x \in \mathbb{Z}' self.set_description[1] = '\mathbb{N}' self.set_description[2] = 'N' elif element == '-x': set_str += ' \ | \ x \in \mathbb{Z}' self.set_description[1] = '\mathbb{Z}' self.set_description[2] = 'Z' elif element == '2x': temp_str = choice(['\mathbb{N}', '\mathbb{Z}', '\mathbb{Q}', '\mathbb{R}']) set_str += ' \ | \ x \in'+temp_str self.set_description[1] = ('MOD2') else: temp_str = choice(['\mathbb{N}', '\mathbb{Z}', '\mathbb{Q}', '\mathbb{R}']) set_str += ' \ | x \in '+temp_str self.set_description[1] = 'x^{2}' elif self.set_type == 'empty_set': self.set_description[0] = 'empty_set' if element == '\{x\}': if choice([0, 1]) == 0: set_str += ' \ | \ \{x\} \in \mathcal{P}(\mathbb{N}) \setminus \mathcal{P}(\mathbb{Z})' else: set_str += ' \in \mathbb{N}' elif element == '\{x+1\}': set_str += ' \ | \ x \in \mathbb{N} \wedge \ x \leq -2' elif element == '\lvert x \\rvert': set_str += ' \ | \ x \in \mathbb{N} \setminus \mathbb{Z}' elif element == '-x': set_str += ' \ | \ x \in \mathbb N_{> 0}' elif element == 'x+1': if choice([0, 1]) == 0: set_str += ' \ | \ x \in \mathbb{N} \wedge \ x \leq -2' else: set_str += '\ | | x \subseteq \{\pi\}' else: set_str += ' \ | \ x \in \mathbb{Z} \wedge x = \sqrt{2}' self.set_description[1] = '\emptyset' self.set_description[2] = EmptySet() else: pass""" self.final_set_string += set_str
def __init__(self): self.set_str = "" self.val_lst = [] self.interval = FiniteSet()
def test_difference(): assert Interval(1, 3) - Interval(1, 2) == Interval(2, 3, True) assert Interval(1, 3) - Interval(2, 3) == Interval(1, 2, False, True) assert Interval(1, 3, True) - Interval(2, 3) == Interval(1, 2, True, True) assert Interval(1, 3, True) - Interval(2, 3, True) == \ Interval(1, 2, True, False) assert Interval(0, 2) - FiniteSet(1) == \ Union(Interval(0, 1, False, True), Interval(1, 2, True, False)) assert FiniteSet(1, 2, 3) - FiniteSet(2) == FiniteSet(1, 3) assert FiniteSet('ham', 'eggs') - FiniteSet('eggs') == FiniteSet('ham') assert FiniteSet(1, 2, 3, 4) - Interval(2, 10, True, False) == \ FiniteSet(1, 2) assert FiniteSet(1, 2, 3, 4) - S.EmptySet == FiniteSet(1, 2, 3, 4) assert Union(Interval(0, 2), FiniteSet(2, 3, 4)) - Interval(1, 3) == \ Union(Interval(0, 1, False, True), FiniteSet(4)) assert -1 in S.Reals - S.Naturals
def test_Complement(): assert Complement(Interval(1, 3), Interval(1, 2)) == Interval(2, 3, True) assert Complement(FiniteSet(1, 3, 4), FiniteSet(3, 4)) == FiniteSet(1) assert Complement(Union(Interval(0, 2), FiniteSet(2, 3, 4)), Interval(1, 3)) == \ Union(Interval(0, 1, False, True), FiniteSet(4)) assert not 3 in Complement(Interval(0, 5), Interval(1, 4), evaluate=False) assert -1 in Complement(S.Reals, S.Naturals, evaluate=False) assert not 1 in Complement(S.Reals, S.Naturals, evaluate=False) assert Complement(S.Integers, S.UniversalSet) == EmptySet() assert S.UniversalSet.complement(S.Integers) == EmptySet() assert (not 0 in S.Reals.intersect(S.Integers - FiniteSet(0))) assert S.EmptySet - S.Integers == S.EmptySet assert (S.Integers - FiniteSet(0)) - FiniteSet(1) == S.Integers - FiniteSet(0, 1) assert S.Reals - Union(S.Naturals, FiniteSet(pi)) == \ Intersection(S.Reals - S.Naturals, S.Reals - FiniteSet(pi))
def test_is_proper_superset(): assert Interval(0, 1).is_proper_superset(Interval(0, 2)) is False assert Interval(0, 3).is_proper_superset(Interval(0, 2)) is True assert FiniteSet(1, 2, 3).is_proper_superset(S.EmptySet) is True raises(ValueError, lambda: Interval(0, 1).is_proper_superset(0))
def intersection_sets(a, b): try: return FiniteSet(*[el for el in a if el in b]) except TypeError: return None # could not evaluate `el in b` due to symbolic ranges.
def test_issue_9536(): from sympy.functions.elementary.exponential import log a = Symbol('a', real=True) assert FiniteSet(log(a)).intersect(S.Reals) == Intersection( S.Reals, FiniteSet(log(a)))
def test_is_open(): assert not Interval(0, 1, False, False).is_open assert not Interval(0, 1, True, False).is_open assert Interval(0, 1, True, True).is_open assert not FiniteSet(1, 2, 3).is_open
def test_intersect(): x = Symbol('x') assert Interval(0, 2).intersect(Interval(1, 2)) == Interval(1, 2) assert Interval(0, 2).intersect(Interval(1, 2, True)) == \ Interval(1, 2, True) assert Interval(0, 2, True).intersect(Interval(1, 2)) == \ Interval(1, 2, False, False) assert Interval(0, 2, True, True).intersect(Interval(1, 2)) == \ Interval(1, 2, False, True) assert Interval(0, 2).intersect(Union(Interval(0, 1), Interval(2, 3))) == \ Union(Interval(0, 1), Interval(2, 2)) assert FiniteSet(1, 2)._intersect((1, 2, 3)) == FiniteSet(1, 2) assert FiniteSet(1, 2, x).intersect(FiniteSet(x)) == FiniteSet(x) assert FiniteSet('ham', 'eggs').intersect(FiniteSet('ham')) == \ FiniteSet('ham') assert FiniteSet(1, 2, 3, 4, 5).intersect(S.EmptySet) == S.EmptySet assert Interval(0, 5).intersect(FiniteSet(1, 3)) == FiniteSet(1, 3) assert Interval(0, 1, True, True).intersect(FiniteSet(1)) == S.EmptySet assert Union(Interval(0, 1), Interval(2, 3)).intersect(Interval(1, 2)) == \ Union(Interval(1, 1), Interval(2, 2)) assert Union(Interval(0, 1), Interval(2, 3)).intersect(Interval(0, 2)) == \ Union(Interval(0, 1), Interval(2, 2)) assert Union(Interval(0, 1), Interval(2, 3)).intersect(Interval(1, 2, True, True)) == \ S.EmptySet assert Union(Interval(0, 1), Interval(2, 3)).intersect(S.EmptySet) == \ S.EmptySet assert Union(Interval(0, 5), FiniteSet('ham')).intersect(FiniteSet(2, 3, 4, 5, 6)) == \ Union(FiniteSet(2, 3, 4, 5), Intersection(FiniteSet(6), Union(Interval(0, 5), FiniteSet('ham')))) # issue 8217 assert Intersection(FiniteSet(x), FiniteSet(y)) == \ Intersection(FiniteSet(x), FiniteSet(y), evaluate=False) assert FiniteSet(x).intersect(S.Reals) == \ Intersection(S.Reals, FiniteSet(x), evaluate=False) # tests for the intersection alias assert Interval(0, 5).intersection(FiniteSet(1, 3)) == FiniteSet(1, 3) assert Interval(0, 1, True, True).intersection(FiniteSet(1)) == S.EmptySet assert Union(Interval(0, 1), Interval(2, 3)).intersection(Interval(1, 2)) == \ Union(Interval(1, 1), Interval(2, 2))
def test_is_closed(): assert Interval(0, 1, False, False).is_closed assert not Interval(0, 1, True, False).is_closed assert FiniteSet(1, 2, 3).is_closed
def intersection_sets(a, b): return FiniteSet(*(a._elements & b._elements))