Example #1
0
def test_Range():
    assert Range(5) == Range(0, 5) == Range(0, 5, 1)

    r = Range(10, 20, 2)
    assert 12 in r
    assert 8 not in r
    assert 11 not in r
    assert 30 not in r

    assert list(Range(0, 5)) == list(range(5))
    assert list(Range(5, 0, -1)) == list(range(1, 6))

    assert Range(0, 10, -1) == S.EmptySet

    assert Range(5, 15).sup == 14
    assert Range(5, 15).inf == 5
    assert Range(15, 5, -1).sup == 15
    assert Range(15, 5, -1).inf == 6
    assert Range(10, 67, 10).sup == 60
    assert Range(60, 7, -10).inf == 10

    assert len(Range(10, 38, 10)) == 3
    assert Range(0, 0, 5) == S.EmptySet

    assert Range(1, 1) == S.EmptySet
    pytest.raises(ValueError, lambda: Range(0, oo, oo))
    pytest.raises(ValueError, lambda: Range(0, pi, 1))

    assert 5 in Range(0, oo, 5)
    assert -5 in Range(-oo, 0, 5)

    assert Range(0, oo)
    assert Range(-oo, 0)
    assert Range(0, -oo, -1)
    assert Range(1, oo)
    assert Range(-oo, oo)

    assert Range(0, oo, 2)._last_element is oo
    assert Range(-oo, 1, 1)._last_element is Integer(0)

    it = iter(Range(-oo, 0, 2))
    assert (next(it), next(it)) == (-2, -4)

    assert Range(-1, 10, 1).intersection(S.Integers) == Range(-1, 10, 1)
    assert Range(-1, 10, 1).intersection(S.Naturals) == Range(1, 10, 1)
    assert (Range(-1, 10,
                  1).intersection(Set(x)) == Intersection(Range(-1, 10, 1),
                                                          Set(x),
                                                          evaluate=False))

    assert Range(1, 10, 1)._ith_element(5) == 6  # the index starts from zero
    assert Range(1, 10, 1)._last_element == 9

    assert Range(1, 10, 1).boundary == Range(1, 10, 1)

    assert Range(range(10)) == Range(10)
    assert Range(range(1, 10)) == Range(1, 10)
    assert Range(range(1, 10, 2)) == Range(1, 10, 2)
    assert Range(range(1000000000000)) == Range(1000000000000)
Example #2
0
def test_naturals():
    N = S.Naturals
    assert 5 in N
    assert -5 not in N
    assert 5.5 not in N
    ni = iter(N)
    a, b, c, d = next(ni), next(ni), next(ni), next(ni)
    assert (a, b, c, d) == (1, 2, 3, 4)
    assert isinstance(a, Basic)

    assert N.intersection(Interval(-5, 5)) == Range(1, 6)
    assert N.intersection(Interval(-5, 5, True, True)) == Range(1, 5)
    assert N.intersection(Set(x)) == Intersection(N, Set(x), evaluate=False)

    assert N.boundary == N

    assert N.inf == 1
    assert N.sup == oo
Example #3
0
def test_integers():
    Z = S.Integers
    assert 5 in Z
    assert -5 in Z
    assert 5.5 not in Z
    zi = iter(Z)
    a, b, c, d = next(zi), next(zi), next(zi), next(zi)
    assert (a, b, c, d) == (0, 1, -1, 2)
    assert isinstance(a, Basic)

    assert Z.intersection(Interval(-5, 5)) == Range(-5, 6)
    assert Z.intersection(Interval(-5, 5, True, True)) == Range(-4, 5)
    assert Z.intersection(Set(x)) == Intersection(Z, Set(x), evaluate=False)

    assert Z.inf == -oo
    assert Z.sup == oo

    assert Z.boundary == Z

    assert imageset(Lambda((x, y), x * y),
                    Z) == ImageSet(Lambda((x, y), x * y), Z)
Example #4
0
def test_SymmetricDifference():
    assert (SymmetricDifference(FiniteSet(0, 1, 2, 3, 4, 5),
                                FiniteSet(2, 4, 6, 8, 10)) == FiniteSet(
                                    0, 1, 3, 5, 6, 8, 10))
    assert (SymmetricDifference(FiniteSet(2, 3, 4),
                                FiniteSet(2, 3, 4, 5)) == FiniteSet(5))
    assert FiniteSet(2).symmetric_difference(FiniteSet(2, 5)) == FiniteSet(5)
    assert (FiniteSet(1, 2, 3, 4, 5) ^ FiniteSet(1, 2, 5, 6) == FiniteSet(
        3, 4, 6))
    assert (Set(Integer(1), Integer(2), Integer(3))
            ^ Set(Integer(2), Integer(3), Integer(4)) == Union(
                Set(Integer(1), Integer(2), Integer(3)) -
                Set(Integer(2), Integer(3), Integer(4)),
                Set(Integer(2), Integer(3), Integer(4)) -
                Set(Integer(1), Integer(2), Integer(3))))
    assert (Interval(0, 4) ^ Interval(2, 5) == Union(
        Interval(0, 4) - Interval(2, 5),
        Interval(2, 5) - Interval(0, 4)))

    class TestSet(Set):
        def _symmetric_difference(self, other):
            return

    t1, t2 = TestSet(1), TestSet(2)
    assert t1.symmetric_difference(t2) == SymmetricDifference(t1,
                                                              t2,
                                                              evaluate=False)
Example #5
0
def test_SymmetricDifference():
    assert (SymmetricDifference(FiniteSet(0, 1, 2, 3, 4, 5),
                                FiniteSet(2, 4, 6, 8, 10)) == FiniteSet(
                                    0, 1, 3, 5, 6, 8, 10))
    assert (SymmetricDifference(FiniteSet(2, 3, 4),
                                FiniteSet(2, 3, 4, 5)) == FiniteSet(5))
    assert (FiniteSet(1, 2, 3, 4, 5) ^ FiniteSet(1, 2, 5, 6) == FiniteSet(
        3, 4, 6))
    assert (Set(1, 2, 3) ^ Set(2, 3, 4) == Union(
        Set(1, 2, 3) - Set(2, 3, 4),
        Set(2, 3, 4) - Set(1, 2, 3)))
    assert (Interval(0, 4) ^ Interval(2, 5) == Union(
        Interval(0, 4) - Interval(2, 5),
        Interval(2, 5) - Interval(0, 4)))
Example #6
0
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)

    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.intersection(Y)
    assert 2 in X and 3 in X and 3 in XandY
    assert XandY.is_subset(X) and XandY.is_subset(Y)

    pytest.raises(TypeError, lambda: Union(1, 2, 3))

    assert X.is_iterable is False
    Z = Union(FiniteSet(1, 2)*FiniteSet(3, 4), FiniteSet(1, 2, 3, 4))
    assert Z.is_iterable

    # issue sympy/sympy#7843
    assert Union(S.EmptySet, FiniteSet(-sqrt(-I), sqrt(-I))) == FiniteSet(-sqrt(-I), sqrt(-I))

    assert Union(ProductSet(FiniteSet(1), FiniteSet(2)), Interval(0, 1)).is_Union
    assert Union(ProductSet(FiniteSet(1), FiniteSet(2)),
                 ProductSet(FiniteSet(1), FiniteSet(2), FiniteSet(3))).is_Union

    assert list(Union(FiniteSet(1, 2), FiniteSet(3, 4), evaluate=False)) == [1, 3, 2, 4]
    pytest.raises(TypeError, lambda: iter(Union(FiniteSet(1, 2), Interval(0, 1))))

    assert (Union(FiniteSet(E), FiniteSet(pi), evaluate=False).evalf() ==
            FiniteSet(Float('2.7182818284590451', prec=15),
                      Float('3.1415926535897931', prec=15)))
Example #7
0
def test_is_number():
    assert Interval(0, 1).is_number is False
    assert Set().is_number is False
Example #8
0
def test_ProductSet_iter():
    pytest.raises(TypeError, lambda: iter(ProductSet(Set(1), Set(2))))
Example #9
0
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)

    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)

    pytest.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))