Пример #1
0
def test_issue_9808():
    # See https://github.com/sympy/sympy/issues/16342
    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)
Пример #2
0
def test_Complement_as_relational_fail():
    x = Symbol('x')
    expr = Complement(Interval(0, 1), FiniteSet(2), evaluate=False)
    # XXX This example fails because 0 <= x changes to x >= 0
    # during the evaluation.
    assert expr.as_relational(x) == \
            (0 <= x) & (x <= 1) & Ne(x, 2)
Пример #3
0
def test_issue_9637():
    n = Symbol('n')
    a = FiniteSet(n)
    b = FiniteSet(2, n)
    assert Complement(S.Reals, a) == Complement(S.Reals, a, evaluate=False)
    assert Complement(Interval(1, 3), a) == Complement(Interval(1, 3), a, evaluate=False)
    assert Complement(Interval(1, 3), b) == \
        Complement(Union(Interval(1, 2, False, True), Interval(2, 3, True, False)), a)
    assert Complement(a, S.Reals) == Complement(a, S.Reals, evaluate=False)
    assert Complement(a, Interval(1, 3)) == Complement(a, Interval(1, 3), evaluate=False)
Пример #4
0
def test_issue_9447():
    a = Interval(0, 1) + Interval(2, 3)
    assert Complement(S.UniversalSet,
                      a) == Complement(S.UniversalSet,
                                       Union(Interval(0, 1), Interval(2, 3)),
                                       evaluate=False)
    assert Complement(S.Naturals,
                      a) == Complement(S.Naturals,
                                       Union(Interval(0, 1), Interval(2, 3)),
                                       evaluate=False)
Пример #5
0
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))
    # issue 12712
    assert Complement(FiniteSet(x, y, 2), Interval(-10, 10)) == \
            Complement(FiniteSet(x, y), Interval(-10, 10))
Пример #6
0
def test_issue_20291():
    from sympy import FiniteSet, Complement, Intersection, Reals, EmptySet
    a = Symbol('a')
    b = Symbol('b')
    A = FiniteSet(a, b)
    assert A.evalf(subs={a: 1, b: 2}) == FiniteSet(1.0, 2.0)
    B = FiniteSet(a - b, 1)
    assert B.evalf(subs={a: 1, b: 2}) == FiniteSet(-1.0, 1.0)

    sol = Complement(
        Intersection(FiniteSet(-b / 2 - sqrt(b**2 - 4 * pi) / 2), Reals),
        FiniteSet(0))
    assert sol.evalf(subs={b: 1}) == EmptySet
Пример #7
0
def test_complement():
    assert Interval(0, 1).complement(S.Reals) == \
        Union(Interval(-oo, 0, True, True), Interval(1, oo, True, True))
    assert Interval(0, 1, True, False).complement(S.Reals) == \
        Union(Interval(-oo, 0, True, False), Interval(1, oo, True, True))
    assert Interval(0, 1, False, True).complement(S.Reals) == \
        Union(Interval(-oo, 0, True, True), Interval(1, oo, False, True))
    assert Interval(0, 1, True, True).complement(S.Reals) == \
        Union(Interval(-oo, 0, True, False), Interval(1, oo, False, True))

    assert S.UniversalSet.complement(S.EmptySet) == S.EmptySet
    assert S.UniversalSet.complement(S.Reals) == S.EmptySet
    assert S.UniversalSet.complement(S.UniversalSet) == S.EmptySet

    assert S.EmptySet.complement(S.Reals) == S.Reals

    assert Union(Interval(0, 1), Interval(2, 3)).complement(S.Reals) == \
        Union(Interval(-oo, 0, True, True), Interval(1, 2, True, True),
              Interval(3, oo, True, True))

    assert FiniteSet(0).complement(S.Reals) ==  \
        Union(Interval(-oo, 0, True, True), Interval(0, oo, True, True))

    assert (FiniteSet(5) + Interval(S.NegativeInfinity,
                                    0)).complement(S.Reals) == \
        Interval(0, 5, True, True) + Interval(5, S.Infinity, True, True)

    assert FiniteSet(1, 2, 3).complement(S.Reals) == \
        Interval(S.NegativeInfinity, 1, True, True) + \
        Interval(1, 2, True, True) + Interval(2, 3, True, True) +\
        Interval(3, S.Infinity, True, True)

    assert FiniteSet(x).complement(S.Reals) == Complement(
        S.Reals, FiniteSet(x))

    assert FiniteSet(0, x).complement(S.Reals) == Complement(
        Interval(-oo, 0, True, True) + Interval(0, oo, True, True),
        FiniteSet(x),
        evaluate=False)

    square = Interval(0, 1) * Interval(0, 1)
    notsquare = square.complement(S.Reals * S.Reals)

    assert all(pt in square for pt in [(0, 0), (.5, .5), (1, 0), (1, 1)])
    assert not any(pt in notsquare
                   for pt in [(0, 0), (.5, .5), (1, 0), (1, 1)])
    assert not any(pt in square for pt in [(-1, 0), (1.5, .5), (10, 10)])
    assert all(pt in notsquare for pt in [(-1, 0), (1.5, .5), (10, 10)])
Пример #8
0
def to_interval(relational):
    if isinstance(relational, And):
        return Intersection([to_interval(i) for i in relational.args])
    elif isinstance(relational, Or):
        return Union([to_interval(i) for i in relational.args])
    elif isinstance(relational, Not):
        return Complement([to_interval(i) for i in relational.args])
    if relational == S.true:
        return Interval(S.NegativeInfinity,
                        S.Infinity,
                        left_open=True,
                        right_open=True)

    if len(relational.free_symbols) != 1:
        raise ValueError('Relational must only have one free symbol')
    if len(relational.args) != 2:
        raise ValueError('Relational must only have two arguments')
    free_symbol = list(relational.free_symbols)[0]
    lhs = relational.args[0]
    rhs = relational.args[1]
    if isinstance(relational, GreaterThan):
        if lhs == free_symbol:
            return Interval(rhs, S.Infinity, left_open=False)
        else:
            return Interval(S.NegativeInfinity, rhs, right_open=False)
    elif isinstance(relational, StrictGreaterThan):
        if lhs == free_symbol:
            return Interval(rhs, S.Infinity, left_open=True)
        else:
            return Interval(S.NegativeInfinity, rhs, right_open=True)
    elif isinstance(relational, LessThan):
        if lhs != free_symbol:
            return Interval(rhs, S.Infinity, left_open=False)
        else:
            return Interval(S.NegativeInfinity, rhs, right_open=False)
    elif isinstance(relational, StrictLessThan):
        if lhs != free_symbol:
            return Interval(rhs, S.Infinity, left_open=True)
        else:
            return Interval(S.NegativeInfinity, rhs, right_open=True)
    else:
        raise ValueError('Unsupported Relational: {}'.format(
            relational.__class__.__name__))
Пример #9
0
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)
Пример #10
0
def test_Complement():
    assert str(Complement(S.Reals, S.Naturals)) == 'Reals \\ Naturals'
Пример #11
0
def test_Complement():
    assert str(Complement(S.Reals, S.Naturals)) == '(-oo, oo) \ Naturals()'
Пример #12
0
def test_Complement():
    assert str(Complement(S.Reals, S.Naturals)) == 'Complement(Reals, Naturals)'
Пример #13
0
    def _regular_point_ellipse(self, a, b, c, d, e, f):
        D = 4 * a * c - b**2
        ok = D

        if not ok:
            raise ValueError("Rational Point on the conic does not exist")

        if a == 0 and c == 0:
            K = -1
            L = 4 * (d * e - b * f)
        elif c != 0:
            K = D
            L = 4 * c**2 * d**2 - 4 * b * c * d * e + 4 * a * c * e**2 + 4 * b**2 * c * f - 16 * a * c**2 * f
        else:
            K = D
            L = 4 * a**2 * e**2 - 4 * b * a * d * e + 4 * b**2 * a * f

        ok = L != 0 and not (K > 0 and L < 0)
        if not ok:
            raise ValueError("Rational Point on the conic does not exist")

        K = Rational(K).limit_denominator(10**12)
        L = Rational(L).limit_denominator(10**12)

        k1, k2 = K.p, K.q
        l1, l2 = L.p, L.q
        g = gcd(k2, l2)

        a1 = (l2 * k2) / g
        b1 = (k1 * l2) / g
        c1 = -(l1 * k2) / g
        a2 = sign(a1) * core(abs(a1), 2)
        r1 = sqrt(a1 / a2)
        b2 = sign(b1) * core(abs(b1), 2)
        r2 = sqrt(b1 / b2)
        c2 = sign(c1) * core(abs(c1), 2)
        r3 = sqrt(c1 / c2)

        g = gcd(gcd(a2, b2), c2)
        a2 = a2 / g
        b2 = b2 / g
        c2 = c2 / g

        g1 = gcd(a2, b2)
        a2 = a2 / g1
        b2 = b2 / g1
        c2 = c2 * g1

        g2 = gcd(a2, c2)
        a2 = a2 / g2
        b2 = b2 * g2
        c2 = c2 / g2

        g3 = gcd(b2, c2)
        a2 = a2 * g3
        b2 = b2 / g3
        c2 = c2 / g3

        x, y, z = symbols("x y z")
        eq = a2 * x**2 + b2 * y**2 + c2 * z**2

        solutions = diophantine(eq)

        if len(solutions) == 0:
            raise ValueError("Rational Point on the conic does not exist")

        flag = False
        for sol in solutions:
            syms = Tuple(*sol).free_symbols
            rep = {s: 3 for s in syms}
            sol_z = sol[2]

            if sol_z == 0:
                flag = True
                continue

            if not (isinstance(sol_z, Integer) or isinstance(sol_z, int)):
                syms_z = sol_z.free_symbols

                if len(syms_z) == 1:
                    p = next(iter(syms_z))
                    p_values = Complement(
                        S.Integers, solveset(Eq(sol_z, 0), p, S.Integers))
                    rep[p] = next(iter(p_values))

                if len(syms_z) == 2:
                    p, q = list(ordered(syms_z))

                    for i in S.Integers:
                        subs_sol_z = sol_z.subs(p, i)
                        q_values = Complement(
                            S.Integers,
                            solveset(Eq(subs_sol_z, 0), q, S.Integers))

                        if not q_values.is_empty:
                            rep[p] = i
                            rep[q] = next(iter(q_values))
                            break

                if len(syms) != 0:
                    x, y, z = tuple(s.subs(rep) for s in sol)
                else:
                    x, y, z = sol
                flag = False
                break

        if flag:
            raise ValueError("Rational Point on the conic does not exist")

        x = (x * g3) / r1
        y = (y * g2) / r2
        z = (z * g1) / r3
        x = x / z
        y = y / z

        if a == 0 and c == 0:
            x_reg = (x + y - 2 * e) / (2 * b)
            y_reg = (x - y - 2 * d) / (2 * b)
        elif c != 0:
            x_reg = (x - 2 * d * c + b * e) / K
            y_reg = (y - b * x_reg - e) / (2 * c)
        else:
            y_reg = (x - 2 * e * a + b * d) / K
            x_reg = (y - b * y_reg - d) / (2 * a)

        return x_reg, y_reg
Пример #14
0
def test_Complement_as_relational():
    x = Symbol('x')
    expr = Complement(Interval(0, 1), FiniteSet(2), evaluate=False)
    assert expr.as_relational(x) == \
        And(Le(0, x), Le(x, 1), Ne(x, 2))