Exemple #1
0
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
Exemple #2
0
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
Exemple #3
0
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)
Exemple #4
0
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)
Exemple #5
0
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)
Exemple #6
0
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
Exemple #7
0
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
Exemple #8
0
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)+'\}'
Exemple #9
0
 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
Exemple #10
0
 def __init__(self):
     self.set_str = ""
     self.val_lst = []
     self.interval = FiniteSet()
Exemple #11
0
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))
Exemple #12
0
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)
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)
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)