Exemple #1
0
def test_symbol():
    a, b, c, p, q = map(Wild, 'abcpq')

    e = x
    assert e.match(x) == {}
    assert e.match(a) == {a: x}

    e = Integer(5)
    assert e.match(c) == {c: 5}
    assert e.match(e) == {}
    assert e.match(e + 1) is None
Exemple #2
0
def test_erfc():
    assert erfc(nan) == nan

    assert erfc(oo) == 0
    assert erfc(-oo) == 2

    assert erfc(0) == 1

    assert erfc(I * oo) == -oo * I
    assert erfc(-I * oo) == oo * I

    assert erfc(-x) == Integer(2) - erfc(x)
    assert erfc(erfcinv(x)) == x
    assert erfc(erfinv(x)) == 1 - x

    assert erfc(I).is_extended_real is False
    assert erfc(w).is_extended_real is True
    assert erfc(z).is_extended_real is None

    assert conjugate(erfc(z)) == erfc(conjugate(z))

    assert erfc(x).as_leading_term(x) == 1
    assert erfc(1 / x).as_leading_term(x) == erfc(1 / x)

    assert erfc(z).rewrite('erf') == 1 - erf(z)
    assert erfc(z).rewrite('erfi') == 1 + I * erfi(I * z)
    assert erfc(z).rewrite('fresnels') == 1 - (1 + I) * (
        fresnelc(z * (1 - I) / sqrt(pi)) - I * fresnels(z *
                                                        (1 - I) / sqrt(pi)))
    assert erfc(z).rewrite('fresnelc') == 1 - (1 + I) * (
        fresnelc(z * (1 - I) / sqrt(pi)) - I * fresnels(z *
                                                        (1 - I) / sqrt(pi)))
    assert erfc(z).rewrite('hyper') == 1 - 2 * z * hyper(
        [Rational(1, 2)], [Rational(3, 2)], -z**2) / sqrt(pi)
    assert erfc(z).rewrite('meijerg') == 1 - z * meijerg(
        [Rational(1, 2)], [], [0], [Rational(-1, 2)], z**2) / sqrt(pi)
    assert erfc(z).rewrite(
        'uppergamma') == 1 - sqrt(z**2) * erf(sqrt(z**2)) / z
    assert erfc(z).rewrite('expint') == 1 - sqrt(z**2) / z + z * expint(
        Rational(1, 2), z**2) / sqrt(pi)

    assert erfc(x).as_real_imag() == \
        ((erfc(re(x) - I*re(x)*abs(im(x))/abs(re(x)))/2 +
          erfc(re(x) + I*re(x)*abs(im(x))/abs(re(x)))/2,
          I*(erfc(re(x) - I*re(x)*abs(im(x))/abs(re(x))) -
             erfc(re(x) + I*re(x)*abs(im(x))/abs(re(x)))) *
          re(x)*abs(im(x))/(2*im(x)*abs(re(x)))))
    assert erfc(x).as_real_imag(deep=False) == erfc(x).as_real_imag()
    assert erfc(w).as_real_imag() == (erfc(w), 0)
    assert erfc(w).as_real_imag(deep=False) == erfc(w).as_real_imag()
    assert erfc(I).as_real_imag() == (1, -erfi(1))

    pytest.raises(ArgumentIndexError, lambda: erfc(x).fdiff(2))

    assert erfc(x).taylor_term(3, x, *(-2 * x / sqrt(pi),
                                       0)) == 2 * x**3 / 3 / sqrt(pi)

    assert erfc(x).limit(x, oo) == 0

    assert erfc(x).diff(x) == -2 * exp(-x**2) / sqrt(pi)
Exemple #3
0
def test_Xor():

    assert Xor() is false
    assert Xor(A) == A
    assert Xor(A, A) is false
    assert Xor(True, A, A) is true
    assert Xor(A, A, A, A, A) == A
    assert Xor(True, False, False, A, B) == ~Xor(A, B)
    assert Xor(True) is true
    assert Xor(False) is false
    assert Xor(True, True ) is false
    assert Xor(True, False) is true
    assert Xor(False, False) is false
    assert Xor(True, A) == ~A
    assert Xor(False, A) == A
    assert Xor(True, False, False) is true
    assert Xor(True, False, A) == ~A
    assert Xor(False, False, A) == A
    assert isinstance(Xor(A, B), Xor)
    assert Xor(A, B, Xor(C, D)) == Xor(A, B, C, D)
    assert Xor(A, B, Xor(B, C)) == Xor(A, C)
    assert Xor(A < 1, A >= 1, B) == Xor(0, 1, B) == Xor(1, 0, B)
    e = A > 1
    assert Xor(e, e.canonical) == Xor(0, 0) == Xor(1, 1)
    e = Integer(1) < A
    assert e != e.canonical and Xor(e, e.canonical) is false
    assert Xor(A > 1, B > C) == Xor(A > 1, B > C, evaluate=False)
Exemple #4
0
def test_conversion2():
    a = 2 * list2numpy([x**2, x])
    b = list2numpy([2 * x**2, 2 * x])
    assert (a == b).all()

    one = Integer(1)
    zero = Integer(0)
    X = list2numpy([one, zero, zero])
    Y = one * X
    X = list2numpy([Symbol('a') + Rational(1, 2)])
    Y = X + X
    assert Y == numpy.array([1 + 2 * Symbol('a')])
    Y = Y + 1
    assert Y == numpy.array([2 + 2 * Symbol('a')])
    Y = X - X
    assert Y == numpy.array([0])
Exemple #5
0
def test_sympyissue_5223():
    assert next(Integer(0).lseries(x)) == 0
    assert cos(x).series() == cos(x).series(x)

    e = cos(x).series(x, 1, n=None)
    assert [next(e) for i in range(2)] == [cos(1), -((x - 1)*sin(1))]
    e = cos(x).series(x, 1, n=None, dir='-')
    assert [next(e) for i in range(2)] == [cos(1), (1 - x)*sin(1)]
    # the following test is exact so no need for x -> x - 1 replacement
    assert abs(x).series(x, 1, dir='-') == x
    assert exp(x).series(x, 1, dir='-', n=3).removeO() == \
        E - E*(-x + 1) + E*(-x + 1)**2/2

    D = Derivative
    assert next(D(cos(x), x).lseries()) == D(1, x)
    assert D(exp(x), x).series(n=3) == (D(1, x) + D(x, x) + D(x**2/2, x) +
                                        D(x**3/6, x) + O(x**3))

    assert Integral(x, (x, 1, 3), (y, 1, x)).series(x) == -4 + 4*x

    assert (1 + x + O(x**2)).getn() == 2

    assert ((sin(x))**y).nseries(x, n=1) == x**y + O(x**(y + 2), x)

    assert sin(1/x).series(x, oo, n=5) == 1/x - 1/(6*x**3) + O(x**(-5), (x, oo))

    assert exp(x*log(x)).series(n=3) == \
        1 + x*log(x) + x**2*log(x)**2/2 + x**3*log(x)**3/6 + O(x**3)

    p = Symbol('p', positive=True)
    assert exp(sqrt(p)**3*log(p)).series(n=3) == \
        1 + p**3*log(p)**2/2 + p**Rational(3, 2)*log(p) + O(p**3)

    assert exp(sin(x)*log(x)).series(n=2) == \
        1 + x*log(x) + x**2*log(x)**2/2 + O(x**2)
Exemple #6
0
    def eval(cls, nu, z):
        if z.is_zero:
            if nu.is_zero:
                return S.One
            elif (nu.is_integer and nu.is_zero is False) or re(nu).is_positive:
                return S.Zero
            elif re(nu).is_negative and not (nu.is_integer is True):
                return S.ComplexInfinity
            elif nu.is_imaginary:
                return S.NaN
        if z is S.Infinity or (z is S.NegativeInfinity):
            return S.Zero

        if z.could_extract_minus_sign():
            return (z)**nu * (-z)**(-nu) * besselj(nu, -z)
        if nu.is_integer:
            if nu.could_extract_minus_sign():
                return Integer(-1)**(-nu) * besselj(-nu, z)
            newz = z.extract_multiplicatively(I)
            if newz:  # NOTE we don't want to change the function if z==0
                return I**(nu) * besseli(nu, newz)

        # branch handling:
        from diofant import unpolarify, exp
        if nu.is_integer:
            newz = unpolarify(z)
            if newz != z:
                return besselj(nu, newz)
        else:
            newz, n = z.extract_branch_factor()
            if n != 0:
                return exp(2 * n * pi * nu * I) * besselj(nu, newz)
        nnu = unpolarify(nu)
        if nu != nnu:
            return besselj(nnu, z)
Exemple #7
0
def test_as_content_primitive():
    # although the _as_content_primitive methods do not alter the underlying structure,
    # the as_content_primitive function will touch up the expression and join
    # bases that would otherwise have not been joined.
    assert ((x*(2 + 2*x)*(3*x + 3)**2)).as_content_primitive() == \
        (18, x*(x + 1)**3)
    assert (2 + 2*x + 2*y*(3 + 3*y)).as_content_primitive() == \
        (2, x + 3*y*(y + 1) + 1)
    assert ((2 + 6*x)**2).as_content_primitive() == (4, (3*x + 1)**2)
    assert ((2 + 6*x)**(2*y)).as_content_primitive() == \
        (1, (_keep_coeff(Integer(2), (3*x + 1)))**(2*y))
    assert (5 + 10*x + 2*y*(3 + 3*y)).as_content_primitive() == \
        (1, 10*x + 6*y*(y + 1) + 5)
    assert ((5*(x*(1 + y)) + 2*x*(3 + 3*y))).as_content_primitive() == \
        (11, x*(y + 1))
    assert ((5*(x*(1 + y)) + 2*x*(3 + 3*y))**2).as_content_primitive() == \
        (121, x**2*(y + 1)**2)
    assert (y**2).as_content_primitive() == (1, y**2)
    assert (S.Infinity).as_content_primitive() == (1, oo)
    eq = x**(2 + y)
    assert (eq).as_content_primitive() == (1, eq)
    assert (S.Half**(2 + x)).as_content_primitive() == (Rational(1, 4), 2**-x)
    assert ((-S.Half)**(2 + x)).as_content_primitive() == \
           (Rational(1, 4), (-S.Half)**x)
    assert ((-S.Half)**(2 + x)).as_content_primitive() == \
           (Rational(1, 4), (-S.Half)**x)
    assert (4**((1 + y)/2)).as_content_primitive() == (2, 4**(y/2))
    assert (3**((1 + y)/2)).as_content_primitive() == \
           (1, 3**(Mul(Rational(1, 2), 1 + y, evaluate=False)))
    assert (5**Rational(3, 4)).as_content_primitive() == (1, 5**Rational(3, 4))
    assert (5**Rational(7, 4)).as_content_primitive() == (5, 5**Rational(3, 4))
    assert Add(5*z/7, 0.5*x, 3*y/2, evaluate=False).as_content_primitive() == \
              (Rational(1, 14), 7.0*x + 21*y + 10*z)
    assert (2**Rational(3, 4) + 2**Rational(1, 4)*sqrt(3)).as_content_primitive(radical=True) == \
           (1, 2**Rational(1, 4)*(sqrt(2) + sqrt(3)))
Exemple #8
0
def test__dict_from_expr_no_gens():
    pytest.raises(GeneratorsNeeded, lambda: dict_from_expr(Integer(17)))

    assert dict_from_expr(x) == ({(1, ): 1}, (x, ))
    assert dict_from_expr(y) == ({(1, ): 1}, (y, ))

    assert dict_from_expr(x * y) == ({(1, 1): 1}, (x, y))
    assert dict_from_expr(x + y) == ({(1, 0): 1, (0, 1): 1}, (x, y))

    assert dict_from_expr(sqrt(2)) == ({(1, ): 1}, (sqrt(2), ))
    pytest.raises(GeneratorsNeeded,
                  lambda: dict_from_expr(sqrt(2), greedy=False))

    assert dict_from_expr(x * y, domain=ZZ.poly_ring(x)) == ({(1, ): x}, (y, ))
    assert dict_from_expr(x * y, domain=ZZ.poly_ring(y)) == ({(1, ): y}, (x, ))

    assert dict_from_expr(3 * sqrt(2) * pi * x * y, extension=None) == ({
        (1, 1, 1, 1):
        3
    }, (x, y, pi, sqrt(2)))
    assert dict_from_expr(3 * sqrt(2) * pi * x * y, extension=True) == ({
        (1, 1, 1):
        3 * sqrt(2)
    }, (x, y, pi))

    f = cos(x) * sin(x) + cos(x) * sin(y) + cos(y) * sin(x) + cos(y) * sin(y)

    assert dict_from_expr(f) == ({
        (0, 1, 0, 1): 1,
        (0, 1, 1, 0): 1,
        (1, 0, 0, 1): 1,
        (1, 0, 1, 0): 1
    }, (cos(x), cos(y), sin(x), sin(y)))
Exemple #9
0
def test_deltasummation_mul_x_add_y_kd():
    assert ds(x*(y + Kd(i, j)), (j, 1, 3)) == \
        Piecewise((3*x*y + x, And(Integer(1) <= i, i <= 3)), (3*x*y, True))
    assert ds(x*(y + Kd(i, j)), (j, 1, 1)) == \
        Piecewise((x*y + x, Eq(i, 1)), (x*y, True))
    assert ds(x*(y + Kd(i, j)), (j, 2, 2)) == \
        Piecewise((x*y + x, Eq(i, 2)), (x*y, True))
    assert ds(x*(y + Kd(i, j)), (j, 3, 3)) == \
        Piecewise((x*y + x, Eq(i, 3)), (x*y, True))
    assert ds(x*(y + Kd(i, j)), (j, 1, k)) == \
        Piecewise((k*x*y + x, And(Integer(1) <= i, i <= k)), (k*x*y, True))
    assert ds(x*(y + Kd(i, j)), (j, k, 3)) == \
        Piecewise(((4 - k)*x*y + x, And(k <= i, i <= 3)), ((4 - k)*x*y, True))
    assert ds(x * (y + Kd(i, j)), (j, k, l)) == Piecewise(
        ((l - k + 1) * x * y + x, And(k <= i, i <= l)),
        ((l - k + 1) * x * y, True))
Exemple #10
0
def test_systematic_basic():
    def s(diofant_object, numpy_array):
        x = diofant_object + numpy_array
        x = numpy_array + diofant_object
        x = diofant_object - numpy_array
        x = numpy_array - diofant_object
        x = diofant_object * numpy_array
        x = numpy_array * diofant_object
        x = diofant_object / numpy_array
        x = numpy_array / diofant_object
        x = diofant_object**numpy_array
        x = numpy_array**diofant_object

    x = Symbol("x")
    y = Symbol("y")
    diofant_objs = [
        Rational(2, 3),
        Float("1.3"),
        x,
        y,
        pow(x, y) * y,
        Integer(5),
        Float(5.5),
    ]
    numpy_objs = [
        array([1]),
        array([3, 8, -1]),
        array([x, x**2, Rational(5)]),
        array([x / y * sin(y), 5, Rational(5)]),
    ]
    for x in diofant_objs:
        for y in numpy_objs:
            s(x, y)
Exemple #11
0
def test_Domain_interface():
    pytest.raises(TypeError, lambda: DomainElement().parent)

    assert RR(1).parent is RR
    assert CC(1).parent is CC

    assert RR.has_default_precision
    assert CC.has_default_precision

    RR3 = RealField(prec=53, dps=3)
    assert str(RR3(1.7611107002)) == '1.76'

    assert RealField(tol=3).tolerance == 3.0
    assert RealField(tol=0.1).tolerance == 0.1
    assert RealField(tol='0.1').tolerance == 0.1
    pytest.raises(ValueError, lambda: RealField(tol=object()))

    pytest.raises(AttributeError, lambda: CC.ring)
    pytest.raises(DomainError, lambda: CC.get_exact())

    assert str(EX(1)) == 'EX(1)'

    assert EX(1).as_expr() == Integer(1)
    assert bool(EX(1)) is True
    assert bool(EX(0)) is False
Exemple #12
0
def test_deltaproduct_mul_add_x_y_add_y_kd():
    assert dp((x + y)*(y + Kd(i, j)), (j, 1, 3)) == ((x + y)*y)**3 + \
        (x + y)*((x + y)*y)**2*Kd(i, 1) + \
        (x + y)*y*(x + y)**2*y*Kd(i, 2) + \
        ((x + y)*y)**2*(x + y)*Kd(i, 3)
    assert dp((x + y) * (y + Kd(i, j)), (j, 1, 1)) == (x + y) * (y + Kd(i, 1))
    assert dp((x + y) * (y + Kd(i, j)), (j, 2, 2)) == (x + y) * (y + Kd(i, 2))
    assert dp((x + y) * (y + Kd(i, j)), (j, 3, 3)) == (x + y) * (y + Kd(i, 3))
    assert dp((x + y)*(y + Kd(i, j)), (j, 1, k)) == \
        ((x + y)*y)**k + Piecewise(
            (((x + y)*y)**(i - 1)*(x + y)*((x + y)*y)**(k - i),
             And(Integer(1) <= i, i <= k)),
            (0, True)
    )
    assert dp((x + y)*(y + Kd(i, j)), (j, k, 3)) == \
        ((x + y)*y)**(-k + 4) + Piecewise(
            (((x + y)*y)**(i - k)*(x + y)*((x + y)*y)**(3 - i),
             And(k <= i, i <= 3)),
            (0, True)
    )
    assert dp((x + y)*(y + Kd(i, j)), (j, k, l)) == \
        ((x + y)*y)**(-k + l + 1) + Piecewise(
            (((x + y)*y)**(i - k)*(x + y)*((x + y)*y)**(l - i),
             And(k <= i, i <= l)),
            (0, True)
    )
Exemple #13
0
def test_power():
    p, q, r = map(Wild, 'pqr')

    e = (x + y)**a
    assert e.match(p**q) == {p: x + y, q: a}
    assert e.match(p**p) is None

    e = (x + y)**(x + y)
    assert e.match(p**p) == {p: x + y}
    assert e.match(p**q) == {p: x + y, q: x + y}

    e = (2*x)**2
    assert e.match(p*q**r) == {p: 4, q: x, r: 2}

    e = Integer(1)
    assert e.match(x**p) == {p: 0}
Exemple #14
0
def test_reduce_piecewise_inequalities():
    e = abs(x - 5) < 3
    ans = And(Lt(2, x), Lt(x, 8))
    assert reduce_inequalities(e) == ans
    assert reduce_inequalities(e, x) == ans
    assert reduce_inequalities(abs(x - 5)) == Eq(x, 5)
    assert reduce_inequalities(
        abs(2*x + 3) >= 8) == Or(And(Le(Rational(5, 2), x), Lt(x, oo)),
                                 And(Le(x, -Rational(11, 2)), Lt(-oo, x)))
    assert reduce_inequalities(abs(x - 4) + abs(
        3*x - 5) < 7) == And(Lt(Rational(1, 2), x), Lt(x, 4))
    assert reduce_inequalities(abs(x - 4) + abs(3*abs(x) - 5) < 7) == \
        Or(And(Integer(-2) < x, x < -1), And(Rational(1, 2) < x, x < 4))

    nr = Symbol('nr', extended_real=False)
    pytest.raises(TypeError, lambda: reduce_inequalities(abs(nr - 5) < 3))

    # sympy/sympy#10198
    assert reduce_inequalities(-1 + 1/abs(1/x - 1) < 0) == \
        Or(And(Lt(0, x), x < Rational(1, 2)), And(-oo < x, x < 0))

    # sympy/sympy#10255
    assert reduce_inequalities(Piecewise((1, x < 1), (3, True)) > 1) == \
        And(Le(1, x), x < oo)
    assert reduce_inequalities(Piecewise((x**2, x < 0), (2*x, x >= 0)) < 1) == \
        And(Lt(-1, x), x < Rational(1, 2))
Exemple #15
0
def test_canonical():
    one = Integer(1)

    def unchanged(v):
        c = v.canonical
        return v.is_Relational and c.is_Relational and v == c

    def isreversed(v):
        return v.canonical == v.reversed

    assert unchanged(x < one)
    assert unchanged(x <= one)
    assert isreversed(Eq(one, x, evaluate=False))
    assert unchanged(Eq(x, one, evaluate=False))
    assert isreversed(Ne(one, x, evaluate=False))
    assert unchanged(Ne(x, one, evaluate=False))
    assert unchanged(x >= one)
    assert unchanged(x > one)

    assert unchanged(x < y)
    assert unchanged(x <= y)
    assert isreversed(Eq(y, x, evaluate=False))
    assert unchanged(Eq(x, y, evaluate=False))
    assert isreversed(Ne(y, x, evaluate=False))
    assert unchanged(Ne(x, y, evaluate=False))
    assert isreversed(x >= y)
    assert isreversed(x > y)
    assert (-x < 1).canonical == (x > -1)
    assert isreversed(-x > y)
Exemple #16
0
def test_deltaproduct_mul_add_x_kd_add_y_kd():
    assert dp((x + Kd(i, k))*(y + Kd(i, j)), (j, 1, 3)) == \
        Kd(i, 1)*(Kd(i, k) + x)*((Kd(i, k) + x)*y)**2 + \
        Kd(i, 2)*(Kd(i, k) + x)*y*(Kd(i, k) + x)**2*y + \
        Kd(i, 3)*((Kd(i, k) + x)*y)**2*(Kd(i, k) + x) + \
        ((Kd(i, k) + x)*y)**3
    assert dp((x + Kd(i, k))*(y + Kd(i, j)), (j, 1, 1)) == \
        (x + Kd(i, k))*(y + Kd(i, 1))
    assert dp((x + Kd(i, k))*(y + Kd(i, j)), (j, 2, 2)) == \
        (x + Kd(i, k))*(y + Kd(i, 2))
    assert dp((x + Kd(i, k))*(y + Kd(i, j)), (j, 3, 3)) == \
        (x + Kd(i, k))*(y + Kd(i, 3))
    assert dp((x + Kd(i, k))*(y + Kd(i, j)), (j, 1, k)) == \
        ((x + Kd(i, k))*y)**k + Piecewise(
            (((x + Kd(i, k))*y)**(i - 1)*(x + Kd(i, k)) *
             ((x + Kd(i, k))*y)**(-i + k), And(Integer(1) <= i, i <= k)),
            (0, True)
    )
    assert dp((x + Kd(i, k))*(y + Kd(i, j)), (j, k, 3)) == \
        ((x + Kd(i, k))*y)**(4 - k) + Piecewise(
            (((x + Kd(i, k))*y)**(i - k)*(x + Kd(i, k)) *
             ((x + Kd(i, k))*y)**(-i + 3), And(k <= i, i <= 3)),
            (0, True)
    )
    assert dp((x + Kd(i, k))*(y + Kd(i, j)), (j, k, l)) == \
        ((x + Kd(i, k))*y)**(-k + l + 1) + Piecewise(
            (((x + Kd(i, k))*y)**(i - k)*(x + Kd(i, k)) *
             ((x + Kd(i, k))*y)**(-i + l), And(k <= i, i <= l)),
            (0, True)
    )
Exemple #17
0
def test_diff_args():
    # issue sympy/sympy#5399
    args = [x, y, Integer(2), Rational(1, 2)]

    def ok(a):
        """Return True if the input args for diff are ok"""
        if not a:
            return False
        if a[0].is_Symbol is False:
            return False
        s_at = [i for i in range(len(a)) if a[i].is_Symbol]
        n_at = [i for i in range(len(a)) if not a[i].is_Symbol]
        # every symbol is followed by symbol or int
        # every number is followed by a symbol
        return (all(a[i + 1].is_Symbol or a[i + 1].is_Integer
                    for i in s_at if i + 1 < len(a))
                and all(a[i + 1].is_Symbol for i in n_at if i + 1 < len(a)))

    eq = x**10 * y**8
    for a in subsets(args):
        for v in variations(a, len(a)):
            if ok(v):
                eq.diff(*v)  # not raises
            else:
                pytest.raises(ValueError, lambda: eq.diff(*v))
Exemple #18
0
def test_Lambda():
    e = Lambda(x, x**2)
    assert e(4) == 16
    assert e(x) == x**2
    assert e(y) == y**2

    assert Lambda(x, x**2) == Lambda(x, x**2)
    assert Lambda(x, x**2) == Lambda(y, y**2)
    assert Lambda(x, x**2) != Lambda(y, y**2 + 1)
    assert Lambda((x, y), x**y) == Lambda((y, x), y**x)
    assert Lambda((x, y), x**y) != Lambda((x, y), y**x)

    assert Lambda((x, y), x**y)(x, y) == x**y
    assert Lambda((x, y), x**y)(3, 3) == 3**3
    assert Lambda((x, y), x**y)(x, 3) == x**3
    assert Lambda((x, y), x**y)(3, y) == 3**y
    assert Lambda(x, f(x))(x) == f(x)
    assert Lambda(x, x**2)(e(x)) == x**4
    assert e(e(x)) == x**4

    assert Lambda((x, y), x + y).nargs == FiniteSet(2)

    p = x, y, z, t
    assert Lambda(p, t * (x + y + z))(*p) == t * (x + y + z)

    assert Lambda(x, 2 * x) + Lambda(y, 2 * y) == 2 * Lambda(x, 2 * x)
    assert Lambda(x, 2 * x) not in [Lambda(x, x)]
    pytest.raises(TypeError, lambda: Lambda(1, x))
    assert Lambda(x, 1)(1) is Integer(1)

    assert (2 * x).canonical_variables == {}
    assert Lambda(x, 2 * x).canonical_variables == {x: Symbol('0_')}
    x_ = Symbol('x_')
    assert Lambda(x_, 2 * x_).canonical_variables == {x_: Symbol('0__')}
Exemple #19
0
def test_power():
    p, q, r = map(Wild, 'pqr')

    e = (x + y)**a
    assert e.match(p**q) == {p: x + y, q: a}
    assert e.match(p**p) is None

    e = (x + y)**(x + y)
    assert e.match(p**p) == {p: x + y}
    assert e.match(p**q) == {p: x + y, q: x + y}

    e = (2 * x)**2
    assert e.match(p * q**r) == {p: 4, q: x, r: 2}

    e = Integer(1)
    assert e.match(x**p) == {p: 0}
Exemple #20
0
def test_leading_order2():
    assert set(
        (2 + pi + x**2).extract_leading_order(x)) == {(pi, O(1, x)),
                                                      (Integer(2), O(1, x))}
    assert set(
        (2 * x + pi * x + x**2).extract_leading_order(x)) == {(2 * x, O(x)),
                                                              (x * pi, O(x))}
Exemple #21
0
def test_prime():
    assert Integer(-1).is_prime is False
    assert Integer(-2).is_prime is False
    assert Integer(-4).is_prime is False
    assert Integer(0).is_prime is False
    assert Integer(1).is_prime is False
    assert Integer(2).is_prime is True
    assert Integer(17).is_prime is True
    assert Integer(4).is_prime is False
Exemple #22
0
def test_gcd_terms():
    f = 2*(x + 1)*(x + 4)/(5*x**2 + 5) + (2*x + 2)*(x + 5)/(x**2 + 1)/5 + \
        (2*x + 2)*(x + 6)/(5*x**2 + 5)

    assert _gcd_terms(f) == (Rational(6, 5)*((1 + x)/(1 + x**2)), 5 + x, 1)
    assert _gcd_terms(Add.make_args(f)) == \
        (Rational(6, 5)*((1 + x)/(1 + x**2)), 5 + x, 1)

    newf = Rational(6, 5)*((1 + x)*(5 + x)/(1 + x**2))
    assert gcd_terms(f) == newf
    args = Add.make_args(f)
    # non-Basic sequences of terms treated as terms of Add
    assert gcd_terms(list(args)) == newf
    assert gcd_terms(tuple(args)) == newf
    assert gcd_terms(set(args)) == newf
    # but a Basic sequence is treated as a container
    assert gcd_terms(Tuple(*args)) != newf
    assert gcd_terms(Basic(Tuple(1, 3*y + 3*x*y), Tuple(1, 3))) == \
        Basic((1, 3*y*(x + 1)), (1, 3))
    # but we shouldn't change keys of a dictionary or some may be lost
    assert gcd_terms(Dict((x*(1 + y), 2), (x + x*y, y + x*y))) == \
        Dict({x*(y + 1): 2, x + x*y: y*(1 + x)})

    assert gcd_terms((2*x + 2)**3 + (2*x + 2)**2) == 4*(x + 1)**2*(2*x + 3)

    assert gcd_terms(0) == 0
    assert gcd_terms(1) == 1
    assert gcd_terms(x) == x
    assert gcd_terms(2 + 2*x) == Mul(2, 1 + x, evaluate=False)
    arg = x*(2*x + 4*y)
    garg = 2*x*(x + 2*y)
    assert gcd_terms(arg) == garg
    assert gcd_terms(sin(arg)) == sin(garg)

    # issue sympy/sympy#6139-like
    alpha, alpha1, alpha2, alpha3 = symbols('alpha:4')
    a = alpha**2 - alpha*x**2 + alpha + x**3 - x*(alpha + 1)
    rep = (alpha, (1 + sqrt(5))/2 + alpha1*x + alpha2*x**2 + alpha3*x**3)
    s = (a/(x - alpha)).subs(*rep).series(x, 0, 1)
    assert simplify(collect(s, x)) == -sqrt(5)/2 - Rational(3, 2) + O(x)

    # issue sympy/sympy#5917
    assert _gcd_terms([Integer(0), Integer(0)]) == (0, 0, 1)
    assert _gcd_terms([2*x + 4]) == (2, x + 2, 1)

    eq = x/(x + 1/x)
    assert gcd_terms(eq, fraction=False) == eq
Exemple #23
0
def test_sympyissue_4559():
    e = Symbol('e')
    w = Wild('w', exclude=[x])
    y = Wild('y')

    # this is as it should be

    assert (3 / x).match(w / y) == {w: 3, y: x}
    assert (3 * x).match(w * y) == {w: 3, y: x}
    assert (x / 3).match(y / w) == {w: 3, y: x}
    assert (3 * x).match(y / w) == {w: Rational(1, 3), y: x}

    # these could be allowed to fail

    assert (x / 3).match(w / y) == {w: Rational(1, 3), y: 1 / x}
    assert (3 * x).match(w / y) == {w: 3, y: 1 / x}
    assert (3 / x).match(w * y) == {w: 3, y: 1 / x}

    # Note that solve will give
    # multiple roots but match only gives one:
    #
    # >>> solve(x**r - y**2, y)
    # [{y: -x**(r/2)}, {y: x**(r/2)}]

    r = Symbol('r', rational=True)
    assert (x**r).match(y**2) == {y: x**(r / 2)}
    assert (x**e).match(y**2) == {y: sqrt(x**e)}

    # since (x**i = y) -> x = y**(1/i) where i is an integer
    # the following should also be valid as long as y is not
    # zero when i is negative.

    a = Wild('a')

    e = Integer(0)
    assert e.match(a) == {a: e}
    assert e.match(1 / a) is None
    assert e.match(a**.3) is None

    e = Integer(3)
    assert e.match(1 / a) == {a: 1 / e}
    assert e.match(1 / a**2) == {a: 1 / sqrt(e)}
    e = pi
    assert e.match(1 / a) == {a: 1 / e}
    assert e.match(1 / a**2) == {a: 1 / sqrt(e)}
    assert (-e).match(sqrt(a)) is None
    assert (-e).match(a**2) == {a: I * sqrt(pi)}
Exemple #24
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)
Exemple #25
0
def test__parallel_dict_from_expr_if_gens():
    assert parallel_dict_from_expr([x + 2*y + 3*z, Integer(7)], gens=(x,)) == \
        ([{(1,): 1, (0,): 2*y + 3*z}, {(0,): 7}], (x,))
    assert parallel_dict_from_expr((Mul(x, x**2, evaluate=False),), gens=(x,)) == \
        ([{(3,): 1}], (x,))

    pytest.raises(PolynomialError, lambda: parallel_dict_from_expr(
        (A * x, ), gens=(x, )))
Exemple #26
0
def test_inequalities_cant_sympify_other():
    # see issue sympy/sympy#7833

    bar = "foo"

    for a in (x, Integer(0), Rational(1, 3), pi, I, zoo, oo, -oo, nan):
        for op in (lt, gt, le, ge):
            pytest.raises(TypeError, lambda: op(a, bar))
Exemple #27
0
def test_simplify():
    assert simplify(x*(y + 1) - x*y - x + 1 < x) == (x > 1)
    assert simplify(Integer(1) < -x) == (x < -1)

    # issue sympy/sympy#10304
    d = -(3*2**pi)**(1/pi) + 2*3**(1/pi)
    assert d.is_real
    assert simplify(Eq(1 + I*d, 0)) is False
Exemple #28
0
def test_deltasummation_basic_symbolic():
    assert ds(Kd(exp(i), 0), (i, 1, 3)) == 0
    assert ds(Kd(exp(i), 0), (i, -1, 3)) == 0
    assert ds(Kd(exp(i), 1), (i, 0, 3)) == 1
    assert ds(Kd(exp(i), 1), (i, 1, 3)) == 0
    assert ds(Kd(exp(i), 1), (i, -10, 3)) == 1
    assert ds(Kd(i, j), (j, 1, 3)) == \
        Piecewise((1, And(Integer(1) <= i, i <= 3)), (0, True))
    assert ds(Kd(i, j), (j, 1, 1)) == Piecewise((1, Eq(i, 1)), (0, True))
    assert ds(Kd(i, j), (j, 2, 2)) == Piecewise((1, Eq(i, 2)), (0, True))
    assert ds(Kd(i, j), (j, 3, 3)) == Piecewise((1, Eq(i, 3)), (0, True))
    assert ds(Kd(i, j), (j, 1, k)) == \
        Piecewise((1, And(Integer(1) <= i, i <= k)), (0, True))
    assert ds(Kd(i, j), (j, k, 3)) == \
        Piecewise((1, And(k <= i, i <= 3)), (0, True))
    assert ds(Kd(i, j), (j, k, l)) == \
        Piecewise((1, And(k <= i, i <= l)), (0, True))
Exemple #29
0
def dotest(s):
    x = Symbol('x')
    y = Symbol('y')
    l = [Integer(2), Float('1.3'), x, y, pow(x, y) * y, 5, 5.5]
    for x in l:
        for y in l:
            s(x, y)
    return True
Exemple #30
0
def test_dice():
    # TODO: Make iid method!
    X, Y, Z = Die('X', 6), Die('Y', 6), Die('Z', 6)
    a, b = symbols('a b')

    assert E(X) == 3 + S.Half
    assert variance(X) == Rational(35, 12)
    assert E(X + Y) == 7
    assert E(X + X) == 7
    assert E(a * X + b) == a * E(X) + b
    assert variance(X + Y) == variance(X) + variance(Y) == cmoment(X + Y, 2)
    assert variance(X + X) == 4 * variance(X) == cmoment(X + X, 2)
    assert cmoment(X, 0) == 1
    assert cmoment(4 * X, 3) == 64 * cmoment(X, 3)
    assert covariance(X, Y) == S.Zero
    assert covariance(X, X + Y) == variance(X)
    assert density(Eq(cos(X * S.Pi), 1))[True] == S.Half
    assert correlation(X, Y) == 0
    assert correlation(X, Y) == correlation(Y, X)
    assert smoment(X + Y, 3) == skewness(X + Y)
    assert smoment(X, 0) == 1
    assert P(X > 3) == S.Half
    assert P(2 * X > 6) == S.Half
    assert P(X > Y) == Rational(5, 12)
    assert P(Eq(X, Y)) == P(Eq(X, 1))

    assert E(X, X > 3) == 5 == moment(X, 1, 0, X > 3)
    assert E(X, Y > 3) == E(X) == moment(X, 1, 0, Y > 3)
    assert E(X + Y, Eq(X, Y)) == E(2 * X)
    assert moment(X, 0) == 1
    assert moment(5 * X, 2) == 25 * moment(X, 2)

    assert P(X > 3, X > 3) == S.One
    assert P(X > Y, Eq(Y, 6)) == S.Zero
    assert P(Eq(X + Y, 12)) == S.One / 36
    assert P(Eq(X + Y, 12), Eq(X, 6)) == S.One / 6

    assert density(X + Y) == density(Y + Z) != density(X + X)
    d = density(2 * X + Y**Z)
    assert d[Integer(22)] == S.One / 108 and d[Integer(
        4100)] == S.One / 216 and Integer(3130) not in d

    assert pspace(X).domain.as_boolean() == Or(
        *[Eq(X.symbol, i) for i in [1, 2, 3, 4, 5, 6]])

    assert where(X > 3).set == FiniteSet(4, 5, 6)
Exemple #31
0
def test_diofantissue_453():
    x = Symbol('x', real=True)
    assert isolve(abs((x - 1) / (x - 5)) <= Rational(1, 3),
                  x) == And(Integer(-1) <= x, x <= 2)
    assert solve(abs((x - 1) / (x - 5)) - Rational(1, 3), x) == [{
        x: -1
    }, {
        x: 2
    }]
Exemple #32
0
def test_sympify_gmpy():
    if HAS_GMPY:
        import gmpy2 as gmpy

        value = sympify(gmpy.mpz(1000001))
        assert value == Integer(1000001) and type(value) is Integer

        value = sympify(gmpy.mpq(101, 127))
        assert value == Rational(101, 127) and type(value) is Rational
Exemple #33
0
def test_match_exclude():
    p = Wild("p")
    q = Wild("q")
    r = Wild("r")

    e = Integer(6)
    assert e.match(2*p) == {p: 3}

    e = 3/(4*x + 5)
    assert e.match(3/(p*x + q)) == {p: 4, q: 5}

    e = 3/(4*x + 5)
    assert e.match(p/(q*x + r)) == {p: 3, q: 4, r: 5}

    e = 2/(x + 1)
    assert e.match(p/(q*x + r)) == {p: 2, q: 1, r: 1}

    e = 1/(x + 1)
    assert e.match(p/(q*x + r)) == {p: 1, q: 1, r: 1}

    e = 4*x + 5
    assert e.match(p*x + q) == {p: 4, q: 5}

    e = 4*x + 5*y + 6
    assert e.match(p*x + q*y + r) == {p: 4, q: 5, r: 6}

    a = Wild('a', exclude=[x])

    e = 3*x
    assert e.match(p*x) == {p: 3}
    assert e.match(a*x) == {a: 3}

    e = 3*x**2
    assert e.match(p*x) == {p: 3*x}
    assert e.match(a*x) is None

    e = 3*x + 3 + 6/x
    assert e.match(p*x**2 + p*x + 2*p) == {p: 3/x}
    assert e.match(a*x**2 + a*x + 2*a) is None