Exemplo n.º 1
0
def test_real_imag():
    X, Z = symbols('X Z', commutative=False)
    a = Symbol('a', extended_real=True)
    assert (2 * a * x).as_real_imag() == (2 * a * re(x), 2 * a * im(x))

    # issue sympy/sympy#5395:
    assert (x * x.conjugate()).as_real_imag() == (abs(x)**2, 0)
    assert im(x * x.conjugate()) == 0
    assert im(x * y.conjugate() * z * y) == im(x * z) * abs(y)**2
    assert im(x * y.conjugate() * x * y) == im(x**2) * abs(y)**2
    assert im(Z * y.conjugate() * X * y) == im(Z * X) * abs(y)**2
    assert im(X * X.conjugate()) == im(X * X.conjugate(), evaluate=False)
    assert (sin(x)*sin(x).conjugate()).as_real_imag() == \
        (abs(sin(x))**2, 0)

    # issue sympy/sympy#6573:
    assert (x**2).as_real_imag() == (re(x)**2 - im(x)**2, 2 * re(x) * im(x))

    # issue sympy/sympy#6428:
    r = Symbol('r', extended_real=True)
    i = Symbol('i', imaginary=True)
    assert (i * r * x).as_real_imag() == (I * i * r * im(x),
                                          -I * i * r * re(x))
    assert (i * r * x *
            (y + 2)).as_real_imag() == (I * i * r * (re(y) + 2) * im(x) +
                                        I * i * r * re(x) * im(y),
                                        -I * i * r * (re(y) + 2) * re(x) +
                                        I * i * r * im(x) * im(y))

    # issue sympy/sympy#7106:
    assert ((1 + I) / (1 - I)).as_real_imag() == (0, 1)
    assert ((1 + 2 * I) * (1 + 3 * I)).as_real_imag() == (-5, 5)

    assert exp(x).as_real_imag(deep=False) == (re(exp(x)), im(exp(x)))
    assert (2**x).as_real_imag(deep=False) == (re(2**x), im(2**x))
Exemplo n.º 2
0
def test_conjugate():
    a = Symbol('a', extended_real=True)
    b = Symbol('b', imaginary=True)
    assert conjugate(a) == a
    assert conjugate(I * a) == -I * a
    assert conjugate(b) == -b
    assert conjugate(I * b) == I * b
    assert conjugate(a * b) == -a * b
    assert conjugate(I * a * b) == I * a * b

    x = Symbol('x')
    y = Symbol('y')
    assert conjugate(conjugate(x)) == x
    assert conjugate(x + y) == conjugate(x) + conjugate(y)
    assert conjugate(x - y) == conjugate(x) - conjugate(y)
    assert conjugate(x * y) == conjugate(x) * conjugate(y)
    assert conjugate(x / y) == conjugate(x) / conjugate(y)
    assert conjugate(-x) == -conjugate(x)

    a = Symbol('a', algebraic=True)
    t = Symbol('t', transcendental=True)
    assert re(a).is_algebraic
    assert re(x).is_algebraic is None
    assert re(t).is_algebraic is False

    assert conjugate(z).diff(z) == Derivative(conjugate(z), z)

    # issue sympy/sympy#4754
    x = Symbol('x', extended_real=True)
    y = Symbol('y', imaginary=True)
    f = Function('f')
    assert (f(x).conjugate()).diff(x) == (f(x).diff(x)).conjugate()
    assert (f(y).conjugate()).diff(y) == -(f(y).diff(y)).conjugate()
Exemplo n.º 3
0
def test_exp_assumptions():
    er = Symbol('er', extended_real=True)
    r = Symbol('r', real=True)
    i = Symbol('i', imaginary=True)
    c = Symbol('c', complex=True)
    for e in exp, exp_polar:
        assert e(x).is_extended_real is None
        assert e(x).is_imaginary is None
        assert e(i).is_extended_real is None
        assert e(i).is_imaginary is None
        assert e(er).is_extended_real is True
        assert e(re(x)).is_extended_real is True
        if e is not exp_polar:
            assert e(r).is_imaginary is False
            assert e(re(c)).is_imaginary is False

    assert exp(0, evaluate=False).is_algebraic

    a = Symbol('a', algebraic=True)
    an = Symbol('an', algebraic=True, nonzero=True)
    r = Symbol('r', rational=True)
    rn = Symbol('rn', rational=True, nonzero=True)
    assert exp(a).is_algebraic is None
    assert exp(an).is_algebraic is False
    assert exp(pi * r).is_algebraic is None
    assert exp(pi * rn).is_algebraic is False
Exemplo n.º 4
0
def test_messy():
    assert laplace_transform(Si(x), x, s) == ((-atan(s) + pi / 2) / s, 0, True)

    assert laplace_transform(Shi(x), x, s) == (acoth(s) / s, 1, True)

    # where should the logs be simplified?
    assert laplace_transform(Chi(x), x, s) == \
        ((log(s**(-2)) - log((s**2 - 1)/s**2))/(2*s), 1, True)

    # TODO maybe simplify the inequalities?
    assert laplace_transform(besselj(a, x), x, s)[1:] == \
        (0, And(Integer(0) < re(a/2) + Rational(1, 2), Integer(0) < re(a/2) + 1))

    # NOTE s < 0 can be done, but argument reduction is not good enough yet
    assert fourier_transform(besselj(1, x)/x, x, s, noconds=False) == \
        (Piecewise((0, 4*abs(pi**2*s**2) > 1),
                   (2*sqrt(-4*pi**2*s**2 + 1), True)), s > 0)
    # TODO FT(besselj(0,x)) - conditions are messy (but for acceptable reasons)
    #                       - folding could be better

    assert integrate(E1(x)*besselj(0, x), (x, 0, oo), meijerg=True) == \
        log(1 + sqrt(2))
    assert integrate(E1(x)*besselj(1, x), (x, 0, oo), meijerg=True) == \
        log(Rational(1, 2) + sqrt(2)/2)

    assert integrate(1/x/sqrt(1 - x**2), x, meijerg=True) == \
        Piecewise((-acosh(1/x), 1 < abs(x**(-2))), (I*asin(1/x), True))
Exemplo n.º 5
0
def test_real_imag():
    x, y, z = symbols('x, y, z')
    X, Y, Z = symbols('X, Y, Z', commutative=False)
    a = Symbol('a', extended_real=True)
    assert (2*a*x).as_real_imag() == (2*a*re(x), 2*a*im(x))

    # issue sympy/sympy#5395:
    assert (x*x.conjugate()).as_real_imag() == (Abs(x)**2, 0)
    assert im(x*x.conjugate()) == 0
    assert im(x*y.conjugate()*z*y) == im(x*z)*Abs(y)**2
    assert im(x*y.conjugate()*x*y) == im(x**2)*Abs(y)**2
    assert im(Z*y.conjugate()*X*y) == im(Z*X)*Abs(y)**2
    assert im(X*X.conjugate()) == im(X*X.conjugate(), evaluate=False)
    assert (sin(x)*sin(x).conjugate()).as_real_imag() == \
        (Abs(sin(x))**2, 0)

    # issue sympy/sympy#6573:
    assert (x**2).as_real_imag() == (re(x)**2 - im(x)**2, 2*re(x)*im(x))

    # issue sympy/sympy#6428:
    r = Symbol('r', extended_real=True)
    i = Symbol('i', imaginary=True)
    assert (i*r*x).as_real_imag() == (I*i*r*im(x), -I*i*r*re(x))
    assert (i*r*x*(y + 2)).as_real_imag() == (
        I*i*r*(re(y) + 2)*im(x) + I*i*r*re(x)*im(y),
        -I*i*r*(re(y) + 2)*re(x) + I*i*r*im(x)*im(y))

    # issue sympy/sympy#7106:
    assert ((1 + I)/(1 - I)).as_real_imag() == (0, 1)
    assert ((1 + 2*I)*(1 + 3*I)).as_real_imag() == (-5, 5)

    assert exp(x).as_real_imag(deep=False) == (re(exp(x)), im(exp(x)))
    assert (2**x).as_real_imag(deep=False) == (re(2**x), im(2**x))
Exemplo n.º 6
0
def test_messy():
    assert laplace_transform(Si(x), x, s) == ((-atan(s) + pi/2)/s, 0, True)

    assert laplace_transform(Shi(x), x, s) == (acoth(s)/s, 1, True)

    # where should the logs be simplified?
    assert laplace_transform(Chi(x), x, s) == \
        ((log(s**(-2)) - log((s**2 - 1)/s**2))/(2*s), 1, True)

    # TODO maybe simplify the inequalities?
    assert laplace_transform(besselj(a, x), x, s)[1:] == \
        (0, And(Integer(0) < re(a/2) + Rational(1, 2), Integer(0) < re(a/2) + 1))

    # NOTE s < 0 can be done, but argument reduction is not good enough yet
    assert fourier_transform(besselj(1, x)/x, x, s, noconds=False) == \
        (Piecewise((0, 4*abs(pi**2*s**2) > 1),
                   (2*sqrt(-4*pi**2*s**2 + 1), True)), s > 0)
    # TODO FT(besselj(0,x)) - conditions are messy (but for acceptable reasons)
    #                       - folding could be better

    assert integrate(E1(x)*besselj(0, x), (x, 0, oo), meijerg=True) == \
        log(1 + sqrt(2))
    assert integrate(E1(x)*besselj(1, x), (x, 0, oo), meijerg=True) == \
        log(Rational(1, 2) + sqrt(2)/2)

    assert integrate(1/x/sqrt(1 - x**2), x, meijerg=True) == \
        Piecewise((-acosh(1/x), 1 < abs(x**(-2))), (I*asin(1/x), True))
Exemplo n.º 7
0
def test_roots_mixed():
    f = -1936 - 5056 * x - 7592 * x**2 + 2704 * x**3 - 49 * x**4

    _re, _im = [], []
    p = Poly(f)
    for r in p.all_roots():
        c, (r, ) = r.as_coeff_mul()
        if r.is_real:
            r = r.interval
            _re.append((c * QQ.to_expr(r.a), c * QQ.to_expr(r.b)))
        else:
            r = r.interval
            _im.append((c * QQ.to_expr(r.ax) + c * I * QQ.to_expr(r.ay),
                        c * QQ.to_expr(r.bx) + c * I * QQ.to_expr(r.by)))

    _nroots = nroots(f)
    _sroots = roots(f, multiple=True)

    _re = [Interval(a, b) for (a, b) in _re]
    _im = [Interval(re(a), re(b)) * Interval(im(a), im(b)) for (a, b) in _im]

    _intervals = _re + _im
    _sroots = [r.evalf() for r in _sroots]

    _nroots = sorted(_nroots, key=lambda x: x.sort_key())
    _sroots = sorted(_sroots, key=lambda x: x.sort_key())

    for _roots in (_nroots, _sroots):
        for i, r in zip(_intervals, _roots):
            if r.is_extended_real:
                assert r in i
            else:
                assert (re(r), im(r)) in i
Exemplo n.º 8
0
def test_f_expand_complex():
    x = Symbol('x', extended_real=True)

    assert f(x).expand(complex=True) == I * im(f(x)) + re(f(x))
    assert exp(x).expand(complex=True) == exp(x)
    assert exp(I * x).expand(complex=True) == cos(x) + I * sin(x)
    assert exp(z).expand(complex=True) == cos(im(z))*exp(re(z)) + \
        I*sin(im(z))*exp(re(z))
Exemplo n.º 9
0
def test_f_expand_complex():
    x = Symbol('x', extended_real=True)

    assert f(x).expand(complex=True) == I*im(f(x)) + re(f(x))
    assert exp(x).expand(complex=True) == exp(x)
    assert exp(I*x).expand(complex=True) == cos(x) + I*sin(x)
    assert exp(z).expand(complex=True) == cos(im(z))*exp(re(z)) + \
        I*sin(im(z))*exp(re(z))
Exemplo n.º 10
0
def test_derivatives_issue_4757():
    x = Symbol('x', extended_real=True)
    y = Symbol('y', imaginary=True)
    f = Function('f')
    assert re(f(x)).diff(x) == re(f(x).diff(x))
    assert im(f(x)).diff(x) == im(f(x).diff(x))
    assert re(f(y)).diff(y) == -I * im(f(y).diff(y))
    assert im(f(y)).diff(y) == -I * re(f(y).diff(y))
    assert Abs(f(x)).diff(x).subs(f(x), 1 + I * x).doit() == x / sqrt(1 + x**2)
    assert arg(f(x)).diff(x).subs(f(x),
                                  1 + I * x**2).doit() == 2 * x / (1 + x**4)
    assert Abs(f(y)).diff(y).subs(f(y), 1 + y).doit() == -y / sqrt(1 - y**2)
    assert arg(f(y)).diff(y).subs(f(y), I + y**2).doit() == 2 * y / (1 + y**4)
Exemplo n.º 11
0
def test_real_imag():
    x, y, z = symbols('x, y, z')
    X, Y, Z = symbols('X, Y, Z', commutative=False)
    a = Symbol('a', extended_real=True)
    assert (2 * a * x).as_real_imag() == (2 * a * re(x), 2 * a * im(x))

    # issue 5395:
    assert (x * x.conjugate()).as_real_imag() == (Abs(x)**2, 0)
    assert im(x * x.conjugate()) == 0
    assert im(x * y.conjugate() * z * y) == im(x * z) * Abs(y)**2
    assert im(x * y.conjugate() * x * y) == im(x**2) * Abs(y)**2
    assert im(Z * y.conjugate() * X * y) == im(Z * X) * Abs(y)**2
    assert im(X * X.conjugate()) == im(X * X.conjugate(), evaluate=False)
    assert (sin(x)*sin(x).conjugate()).as_real_imag() == \
        (Abs(sin(x))**2, 0)

    # issue 6573:
    assert (x**2).as_real_imag() == (re(x)**2 - im(x)**2, 2 * re(x) * im(x))

    # issue 6428:
    r = Symbol('r', extended_real=True)
    i = Symbol('i', imaginary=True)
    assert (i * r * x).as_real_imag() == (I * i * r * im(x),
                                          -I * i * r * re(x))
    assert (i * r * x *
            (y + 2)).as_real_imag() == (I * i * r * (re(y) + 2) * im(x) +
                                        I * i * r * re(x) * im(y),
                                        -I * i * r * (re(y) + 2) * re(x) +
                                        I * i * r * im(x) * im(y))

    # issue 7106:
    assert ((1 + I) / (1 - I)).as_real_imag() == (0, 1)
    assert ((1 + 2 * I) * (1 + 3 * I)).as_real_imag() == (-5, 5)
Exemplo n.º 12
0
 def convergence_statement(self):
     """ Return a condition on z under which the series converges. """
     from diofant import And, Or, re, Ne, oo
     R = self.radius_of_convergence
     if R == 0:
         return False
     if R == oo:
         return True
     # The special functions and their approximations, page 44
     e = self.eta
     z = self.argument
     c1 = And(re(e) < 0, abs(z) <= 1)
     c2 = And(0 <= re(e), re(e) < 1, abs(z) <= 1, Ne(z, 1))
     c3 = And(re(e) >= 1, abs(z) < 1)
     return Or(c1, c2, c3)
Exemplo n.º 13
0
def test_catalan():
    n = Symbol('n', integer=True)
    m = Symbol('n', integer=True, positive=True)

    catalans = [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786]
    for i, c in enumerate(catalans):
        assert catalan(i) == c
        assert catalan(n).rewrite(factorial).subs({n: i}) == c
        assert catalan(n).rewrite(Product).subs({n: i}).doit() == c

    assert catalan(x) == catalan(x)
    assert catalan(2 *
                   x).rewrite(binomial) == binomial(4 * x, 2 * x) / (2 * x + 1)
    assert catalan(Rational(1, 2)).rewrite(gamma) == 8 / (3 * pi)
    assert catalan(Rational(1, 2)).rewrite(factorial).rewrite(gamma) ==\
        8 / (3 * pi)
    assert catalan(3 * x).rewrite(gamma) == 4**(
        3 * x) * gamma(3 * x + Rational(1, 2)) / (sqrt(pi) * gamma(3 * x + 2))
    assert catalan(x).rewrite(hyper) == hyper((-x + 1, -x), (2, ), 1)

    assert catalan(n).rewrite(factorial) == factorial(
        2 * n) / (factorial(n + 1) * factorial(n))
    assert isinstance(catalan(n).rewrite(Product), catalan)
    assert isinstance(catalan(m).rewrite(Product), Product)

    assert diff(catalan(x), x) == (polygamma(0, x + Rational(1, 2)) -
                                   polygamma(0, x + 2) + log(4)) * catalan(x)

    assert catalan(x).evalf() == catalan(x)
    c = catalan(Rational(1, 2)).evalf()
    assert str(c) == '0.848826363156775'
    c = catalan(I).evalf(3)
    assert sstr((re(c), im(c))) == '(0.398, -0.0209)'
Exemplo n.º 14
0
def test_catalan():
    n = Symbol('n', integer=True)
    m = Symbol('n', integer=True, positive=True)

    catalans = [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786]
    for i, c in enumerate(catalans):
        assert catalan(i) == c
        assert catalan(n).rewrite(factorial).subs({n: i}) == c
        assert catalan(n).rewrite(Product).subs({n: i}).doit() == c

    assert catalan(x) == catalan(x)
    assert catalan(2*x).rewrite(binomial) == binomial(4*x, 2*x)/(2*x + 1)
    assert catalan(Rational(1, 2)).rewrite(gamma) == 8/(3*pi)
    assert catalan(Rational(1, 2)).rewrite(factorial).rewrite(gamma) ==\
        8 / (3 * pi)
    assert catalan(3*x).rewrite(gamma) == 4**(
        3*x)*gamma(3*x + Rational(1, 2))/(sqrt(pi)*gamma(3*x + 2))
    assert catalan(x).rewrite(hyper) == hyper((-x + 1, -x), (2,), 1)

    assert catalan(n).rewrite(factorial) == factorial(2*n) / (factorial(n + 1)
                                                              * factorial(n))
    assert isinstance(catalan(n).rewrite(Product), catalan)
    assert isinstance(catalan(m).rewrite(Product), Product)

    assert diff(catalan(x), x) == (polygamma(
        0, x + Rational(1, 2)) - polygamma(0, x + 2) + log(4))*catalan(x)

    assert catalan(x).evalf() == catalan(x)
    c = catalan(Rational(1, 2)).evalf()
    assert str(c) == '0.848826363156775'
    c = catalan(I).evalf(3)
    assert sstr((re(c), im(c))) == '(0.398, -0.0209)'
Exemplo n.º 15
0
def test_AssocOp_Function():
    e = Min(-sqrt(3)*cos(pi/18)/6 +
            re(1/((Rational(-1, 2) - sqrt(3)*I/2)*cbrt(Rational(1, 6) +
                                                       sqrt(3)*I/18)))/3 + sin(pi/18)/2 + 2 +
            I*(-cos(pi/18)/2 - sqrt(3)*sin(pi/18)/6 +
               im(1/((Rational(-1, 2) - sqrt(3)*I/2)*cbrt(Rational(1, 6) +
                                                          sqrt(3)*I/18)))/3),
            re(1/((Rational(-1, 2) + sqrt(3)*I/2)*cbrt(Rational(1, 6) + sqrt(3)*I/18)))/3 -
            sqrt(3)*cos(pi/18)/6 - sin(pi/18)/2 + 2 +
            I*(im(1/((Rational(-1, 2) + sqrt(3)*I/2)*cbrt(Rational(1, 6) +
                                                          sqrt(3)*I/18)))/3 -
               sqrt(3)*sin(pi/18)/6 + cos(pi/18)/2))
    # the following should not raise a recursion error; it
    # should raise a value error because the first arg computes
    # a non-comparable (prec=1) imaginary part
    pytest.raises(ValueError, lambda: e.evalf(2, strict=False))
Exemplo n.º 16
0
def test_evalf_default():
    assert type(sin(4.0)) == Float
    assert type(re(sin(I + 1.0))) == Float
    assert type(im(sin(I + 1.0))) == Float
    assert type(sin(4)) == sin
    assert type(polygamma(2.0, 4.0)) == Float
    assert type(sin(Rational(1, 4))) == sin
Exemplo n.º 17
0
def test_evalf_default():
    assert type(sin(4.0)) == Float
    assert type(re(sin(I + 1.0))) == Float
    assert type(im(sin(I + 1.0))) == Float
    assert type(sin(4)) == sin
    assert type(polygamma(2.0, 4.0)) == Float
    assert type(sin(Rational(1, 4))) == sin
Exemplo n.º 18
0
def test_bugs():
    from diofant import polar_lift, re

    assert abs(re((1 + I)**2)) < 1e-15

    # anything that evalf's to 0 will do in place of polar_lift
    assert abs(polar_lift(0)).n() == 0
Exemplo n.º 19
0
def test_AssocOp_Function():
    e = Min(-sqrt(3)*cos(pi/18)/6 +
            re(1/((Rational(-1, 2) - sqrt(3)*I/2)*cbrt(Rational(1, 6) +
                                                       sqrt(3)*I/18)))/3 + sin(pi/18)/2 + 2 +
            I*(-cos(pi/18)/2 - sqrt(3)*sin(pi/18)/6 +
               im(1/((Rational(-1, 2) - sqrt(3)*I/2)*cbrt(Rational(1, 6) +
                                                          sqrt(3)*I/18)))/3),
            re(1/((Rational(-1, 2) + sqrt(3)*I/2)*cbrt(Rational(1, 6) + sqrt(3)*I/18)))/3 -
            sqrt(3)*cos(pi/18)/6 - sin(pi/18)/2 + 2 +
            I*(im(1/((Rational(-1, 2) + sqrt(3)*I/2)*cbrt(Rational(1, 6) +
                                                          sqrt(3)*I/18)))/3 -
               sqrt(3)*sin(pi/18)/6 + cos(pi/18)/2))
    # the following should not raise a recursion error; it
    # should raise a value error because the first arg computes
    # a non-comparable (prec=1) imaginary part
    pytest.raises(ValueError, lambda: e.evalf(2, strict=False))
Exemplo n.º 20
0
def test_evalf_default():
    from diofant.functions.special.gamma_functions import polygamma
    assert type(sin(4.0)) == Float
    assert type(re(sin(I + 1.0))) == Float
    assert type(im(sin(I + 1.0))) == Float
    assert type(sin(4)) == sin
    assert type(polygamma(2.0, 4.0)) == Float
    assert type(sin(Rational(1, 4))) == sin
Exemplo n.º 21
0
def test_inverse_mellin_transform():
    IMT = inverse_mellin_transform

    # test passing "None"
    assert IMT(1/(s**2 - 1), s, x, (-1, None)) == \
        -x*Heaviside(-x + 1)/2 - Heaviside(x - 1)/(2*x)
    assert IMT(1/(s**2 - 1), s, x, (None, 1)) == \
        (-x/2 + 1/(2*x))*Heaviside(-x + 1)

    def simp_pows(expr):
        return simplify(powsimp(expand_mul(expr, deep=False),
                                force=True)).replace(exp_polar, exp)

    assert simp_pows(IMT(d**c*d**(s - 1)*sin(pi*c)
                         * gamma(s)*gamma(s + c)*gamma(1 - s)*gamma(1 - s - c)/pi,
                         s, x, (Max(-re(c), 0), Min(1 - re(c), 1)))) \
        == (x**c - d**c)/(x - d)
Exemplo n.º 22
0
def test_abs_re_im(method):
    # issue diofant/diofant#662
    e1 = abs(sqrt(1 + sqrt(2 + I)))
    e2 = re(sqrt(I), evaluate=False)
    e3 = im(sqrt(I), evaluate=False)
    assert (minimal_polynomial(e1, method=method)(x) == x**16 - 4 * x**12 -
            12 * x**8 - 8 * x**4 + 4)
    assert minimal_polynomial(e2, method=method)(x) == 2 * x**2 - 1
    assert minimal_polynomial(e3, method=method)(x) == 2 * x**2 - 1
Exemplo n.º 23
0
def test_diofantissue_662():
    e1 = abs(sqrt(1 + sqrt(2 + I)))
    e2 = re(sqrt(I), evaluate=False)
    e3 = im(sqrt(I), evaluate=False)
    for meth in ('compose', 'groebner'):
        assert (minimal_polynomial(e1, method=meth)(x) ==
                x**16 - 4*x**12 - 12*x**8 - 8*x**4 + 4)
        assert minimal_polynomial(e2, method=meth)(x) == 2*x**2 - 1
        assert minimal_polynomial(e3, method=meth)(x) == 2*x**2 - 1
Exemplo n.º 24
0
def test_diofantissue_662():
    e1 = abs(sqrt(1 + sqrt(2 + I)))
    e2 = re(sqrt(I), evaluate=False)
    e3 = im(sqrt(I), evaluate=False)
    for meth in ('compose', 'groebner'):
        assert (minimal_polynomial(e1, method=meth)(x) == x**16 - 4 * x**12 -
                12 * x**8 - 8 * x**4 + 4)
        assert minimal_polynomial(e2, method=meth)(x) == 2 * x**2 - 1
        assert minimal_polynomial(e3, method=meth)(x) == 2 * x**2 - 1
Exemplo n.º 25
0
def test_expint():
    from diofant import E1, expint, Max, re, lerchphi, Symbol, simplify, Si, Ci, Ei
    aneg = Symbol('a', negative=True)
    u = Symbol('u', polar=True)

    assert mellin_transform(E1(x), x, s) == (gamma(s) / s, (0, oo), True)
    assert inverse_mellin_transform(gamma(s) / s, s, x,
                                    (0, oo)).rewrite(expint).expand() == E1(x)
    assert mellin_transform(expint(a, x), x, s) == \
        (gamma(s)/(a + s - 1), (Max(1 - re(a), 0), oo), True)
    # XXX IMT has hickups with complicated strips ...
    assert simplify(unpolarify(
                    inverse_mellin_transform(gamma(s)/(aneg + s - 1), s, x,
                  (1 - aneg, oo)).rewrite(expint).expand(func=True))) == \
        expint(aneg, x)

    assert mellin_transform(Si(x), x, s) == \
        (-2**s*sqrt(pi)*gamma(s/2 + Rational(1, 2))/(
        2*s*gamma(-s/2 + 1)), (-1, 0), True)
    assert inverse_mellin_transform(-2**s*sqrt(pi)*gamma((s + 1)/2)
                                    / (2*s*gamma(-s/2 + 1)), s, x, (-1, 0)) \
        == Si(x)

    assert mellin_transform(Ci(sqrt(x)), x, s) == \
        (-2**(2*s - 1)*sqrt(pi)*gamma(s)/(s*gamma(-s + Rational(1, 2))), (0, 1), True)
    assert inverse_mellin_transform(
        -4**s * sqrt(pi) * gamma(s) / (2 * s * gamma(-s + Rational(1, 2))), s,
        u, (0, 1)).expand() == Ci(sqrt(u))

    # TODO LT of Si, Shi, Chi is a mess ...
    assert laplace_transform(Ci(x), x, s) == (-log(1 + s**2) / 2 / s, 0, True)
    assert laplace_transform(expint(a, x), x, s) == \
        (lerchphi(s*polar_lift(-1), 1, a), 0, Integer(0) < re(a))
    assert laplace_transform(expint(1, x), x, s) == (log(s + 1) / s, 0, True)
    assert laplace_transform(expint(2, x), x, s) == \
        ((s - log(s + 1))/s**2, 0, True)

    assert inverse_laplace_transform(-log(1 + s**2)/2/s, s, u).expand() == \
        Heaviside(u)*Ci(u)
    assert inverse_laplace_transform(log(s + 1)/s, s, x).rewrite(expint) == \
        Heaviside(x)*E1(x)
    assert inverse_laplace_transform((s - log(s + 1))/s**2, s,
                x).rewrite(expint).expand() == \
        (expint(2, x)*Heaviside(x)).rewrite(Ei).rewrite(expint).expand()
Exemplo n.º 26
0
def test_expint():
    aneg = Symbol('a', negative=True)
    u = Symbol('u', polar=True)

    assert mellin_transform(E1(x), x, s) == (gamma(s)/s, (0, oo), True)
    assert inverse_mellin_transform(gamma(s)/s, s, x,
                                    (0, oo)).rewrite(expint).expand() == E1(x)
    assert mellin_transform(expint(a, x), x, s) == \
        (gamma(s)/(a + s - 1), (Max(1 - re(a), 0), oo), True)
    # XXX IMT has hickups with complicated strips ...
    assert simplify(unpolarify(
                    inverse_mellin_transform(gamma(s)/(aneg + s - 1), s, x,
                                             (1 - aneg, oo)).rewrite(expint).expand(func=True))) == \
        expint(aneg, x)

    assert mellin_transform(Si(x), x, s) == \
        (-2**s*sqrt(pi)*gamma(s/2 + Rational(1, 2))/(
            2*s*gamma(-s/2 + 1)), (-1, 0), True)
    assert inverse_mellin_transform(-2**s*sqrt(pi)*gamma((s + 1)/2)
                                    / (2*s*gamma(-s/2 + 1)), s, x, (-1, 0)) \
        == Si(x)

    assert mellin_transform(Ci(sqrt(x)), x, s) == \
        (-2**(2*s - 1)*sqrt(pi)*gamma(s)/(s*gamma(-s + Rational(1, 2))), (0, 1), True)
    assert inverse_mellin_transform(
        -4**s*sqrt(pi)*gamma(s)/(2*s*gamma(-s + Rational(1, 2))),
        s, u, (0, 1)).expand() == Ci(sqrt(u))

    # TODO LT of Si, Shi, Chi is a mess ...
    assert laplace_transform(Ci(x), x, s) == (-log(1 + s**2)/2/s, 0, True)
    assert laplace_transform(expint(a, x), x, s) == \
        (lerchphi(s*polar_lift(-1), 1, a), 0, Integer(0) < re(a))
    assert laplace_transform(expint(1, x), x, s) == (log(s + 1)/s, 0, True)
    assert laplace_transform(expint(2, x), x, s) == \
        ((s - log(s + 1))/s**2, 0, True)

    assert inverse_laplace_transform(-log(1 + s**2)/2/s, s, u).expand() == \
        Heaviside(u)*Ci(u)
    assert inverse_laplace_transform(log(s + 1)/s, s, x).rewrite(expint) == \
        Heaviside(x)*E1(x)
    assert inverse_laplace_transform((s - log(s + 1))/s**2, s,
                                     x).rewrite(expint).expand() == \
        (expint(2, x)*Heaviside(x)).rewrite(Ei).rewrite(expint).expand()
Exemplo n.º 27
0
def test_evalc():
    x = Symbol('x', extended_real=True)
    y = Symbol('y', extended_real=True)
    z = Symbol('z')
    assert ((x + I * y)**2).expand(complex=True) == x**2 + 2 * I * x * y - y**2
    assert expand_complex(z**(2 * I)) == (re(
        (re(z) + I * im(z))**(2 * I)) + I * im((re(z) + I * im(z))**(2 * I)))
    assert expand_complex(z**(2 * I),
                          deep=False) == I * im(z**(2 * I)) + re(z**(2 * I))

    assert exp(I * x) != cos(x) + I * sin(x)
    assert exp(I * x).expand(complex=True) == cos(x) + I * sin(x)
    assert exp(I * x +
               y).expand(complex=True) == exp(y) * cos(x) + I * sin(x) * exp(y)

    assert sin(I * x).expand(complex=True) == I * sinh(x)
    assert sin(x + I*y).expand(complex=True) == sin(x)*cosh(y) + \
        I * sinh(y) * cos(x)

    assert cos(I * x).expand(complex=True) == cosh(x)
    assert cos(x + I*y).expand(complex=True) == cos(x)*cosh(y) - \
        I * sinh(y) * sin(x)

    assert tan(I * x).expand(complex=True) == tanh(x) * I
    assert tan(x + I * y).expand(
        complex=True) == (sin(2 * x) / (cos(2 * x) + cosh(2 * y)) +
                          I * sinh(2 * y) / (cos(2 * x) + cosh(2 * y)))

    assert sinh(I * x).expand(complex=True) == I * sin(x)
    assert sinh(x + I*y).expand(complex=True) == sinh(x)*cos(y) + \
        I * sin(y) * cosh(x)

    assert cosh(I * x).expand(complex=True) == cos(x)
    assert cosh(x + I*y).expand(complex=True) == cosh(x)*cos(y) + \
        I * sin(y) * sinh(x)

    assert tanh(I * x).expand(complex=True) == tan(x) * I
    assert tanh(x + I * y).expand(
        complex=True) == ((sinh(x) * cosh(x) + I * cos(y) * sin(y)) /
                          (sinh(x)**2 + cos(y)**2)).expand()
Exemplo n.º 28
0
def test_as_real_imag():
    n = pi**1000
    # the special code for working out the real
    # and complex parts of a power with Integer exponent
    # should not run if there is no imaginary part, hence
    # this should not hang
    assert n.as_real_imag() == (n, 0)

    # issue sympy/sympy#6261
    assert sqrt(x).as_real_imag() == \
        (root(re(x)**2 + im(x)**2, 4)*cos(arg(re(x) + I*im(x))/2),
         root(re(x)**2 + im(x)**2, 4)*sin(arg(re(x) + I*im(x))/2))

    # issue sympy/sympy#3853
    a, b = symbols('a,b', extended_real=True)
    assert (((1 + sqrt(a + b*I))/2).as_real_imag() ==
            (root(a**2 + b**2, 4)*cos(arg(a + I*b)/2)/2 + Rational(1, 2),
             root(a**2 + b**2, 4)*sin(arg(a + I*b)/2)/2))

    assert sqrt(a**2).as_real_imag() == (sqrt(a**2), 0)
    i = symbols('i', imaginary=True)
    assert sqrt(i**2).as_real_imag() == (0, abs(i))
Exemplo n.º 29
0
def test_evalc():
    x = Symbol("x", extended_real=True)
    y = Symbol("y", extended_real=True)
    z = Symbol("z")
    assert ((x + I*y)**2).expand(complex=True) == x**2 + 2*I*x*y - y**2
    assert expand_complex(z**(2*I)) == (re((re(z) + I*im(z))**(2*I)) +
                                        I*im((re(z) + I*im(z))**(2*I)))
    assert expand_complex(
        z**(2*I), deep=False) == I*im(z**(2*I)) + re(z**(2*I))

    assert exp(I*x) != cos(x) + I*sin(x)
    assert exp(I*x).expand(complex=True) == cos(x) + I*sin(x)
    assert exp(I*x + y).expand(complex=True) == exp(y)*cos(x) + I*sin(x)*exp(y)

    assert sin(I*x).expand(complex=True) == I * sinh(x)
    assert sin(x + I*y).expand(complex=True) == sin(x)*cosh(y) + \
        I * sinh(y) * cos(x)

    assert cos(I*x).expand(complex=True) == cosh(x)
    assert cos(x + I*y).expand(complex=True) == cos(x)*cosh(y) - \
        I * sinh(y) * sin(x)

    assert tan(I*x).expand(complex=True) == tanh(x) * I
    assert tan(x + I*y).expand(complex=True) == (
        sin(2*x)/(cos(2*x) + cosh(2*y)) +
        I*sinh(2*y)/(cos(2*x) + cosh(2*y)))

    assert sinh(I*x).expand(complex=True) == I * sin(x)
    assert sinh(x + I*y).expand(complex=True) == sinh(x)*cos(y) + \
        I * sin(y) * cosh(x)

    assert cosh(I*x).expand(complex=True) == cos(x)
    assert cosh(x + I*y).expand(complex=True) == cosh(x)*cos(y) + \
        I * sin(y) * sinh(x)

    assert tanh(I*x).expand(complex=True) == tan(x) * I
    assert tanh(x + I*y).expand(complex=True) == (
        (sinh(x)*cosh(x) + I*cos(y)*sin(y)) /
        (sinh(x)**2 + cos(y)**2)).expand()
Exemplo n.º 30
0
def test_as_real_imag():
    n = pi**1000
    # the special code for working out the real
    # and complex parts of a power with Integer exponent
    # should not run if there is no imaginary part, hence
    # this should not hang
    assert n.as_real_imag() == (n, 0)

    # issue sympy/sympy#6261
    assert sqrt(x).as_real_imag() == \
        (root(re(x)**2 + im(x)**2, 4)*cos(arg(re(x) + I*im(x))/2),
         root(re(x)**2 + im(x)**2, 4)*sin(arg(re(x) + I*im(x))/2))

    # issue sympy/sympy#3853
    a, b = symbols('a,b', extended_real=True)
    assert (((1 + sqrt(a + b*I))/2).as_real_imag() ==
            (root(a**2 + b**2, 4)*cos(arg(a + I*b)/2)/2 + Rational(1, 2),
             root(a**2 + b**2, 4)*sin(arg(a + I*b)/2)/2))

    assert sqrt(a**2).as_real_imag() == (sqrt(a**2), 0)
    i = symbols('i', imaginary=True)
    assert sqrt(i**2).as_real_imag() == (0, abs(i))
Exemplo n.º 31
0
def test_conjugate():
    a = Symbol('a', extended_real=True)
    b = Symbol('b', imaginary=True)
    assert conjugate(a) == a
    assert conjugate(I * a) == -I * a
    assert conjugate(b) == -b
    assert conjugate(I * b) == I * b
    assert conjugate(a * b) == -a * b
    assert conjugate(I * a * b) == I * a * b

    assert conjugate(conjugate(x)) == x
    assert conjugate(x + y) == conjugate(x) + conjugate(y)
    assert conjugate(x - y) == conjugate(x) - conjugate(y)
    assert conjugate(x * y) == conjugate(x) * conjugate(y)
    assert conjugate(x / y) == conjugate(x) / conjugate(y)
    assert conjugate(-x) == -conjugate(x)

    a = Symbol('a', algebraic=True)
    t = Symbol('t', transcendental=True)
    assert re(a).is_algebraic
    assert re(x).is_algebraic is None
    assert re(t).is_algebraic is False
Exemplo n.º 32
0
def test_roots_mixed():
    f = -1936 - 5056*x - 7592*x**2 + 2704*x**3 - 49*x**4

    _re, _im = intervals(f, all=True)
    _nroots = nroots(f)
    _sroots = roots(f, multiple=True)

    _re = [ Interval(a, b) for (a, b), _ in _re ]
    _im = [ Interval(re(a), re(b))*Interval(im(a), im(b)) for (a, b),
            _ in _im ]

    _intervals = _re + _im
    _sroots = [ r.evalf() for r in _sroots ]

    _nroots = sorted(_nroots, key=lambda x: x.sort_key())
    _sroots = sorted(_sroots, key=lambda x: x.sort_key())

    for _roots in (_nroots, _sroots):
        for i, r in zip(_intervals, _roots):
            if r.is_extended_real:
                assert r in i
            else:
                assert (re(r), im(r)) in i
Exemplo n.º 33
0
def test_lambertw():
    k = Symbol('k')

    assert LambertW(x, 0) == LambertW(x)
    assert LambertW(x, 0, evaluate=False) != LambertW(x)
    assert LambertW(0) == 0
    assert LambertW(E) == 1
    assert LambertW(-1 / E) == -1
    assert LambertW(-log(2) / 2) == -log(2)
    assert LambertW(oo) == oo
    assert LambertW(0, 1) == -oo
    assert LambertW(0, 42) == -oo
    assert LambertW(-pi / 2, -1) == -I * pi / 2
    assert LambertW(-1 / E, -1) == -1
    assert LambertW(-2 * exp(-2), -1) == -2

    assert LambertW(
        x**2).diff(x) == 2 * LambertW(x**2) / x / (1 + LambertW(x**2))
    assert LambertW(x, k).diff(x) == LambertW(x, k) / x / (1 + LambertW(x, k))
    pytest.raises(ArgumentIndexError, lambda: LambertW(x).fdiff(3))
    pytest.raises(ArgumentIndexError, lambda: LambertW(x, k).fdiff(3))

    assert LambertW(sqrt(2)).evalf(30).epsilon_eq(
        Float('0.701338383413663009202120278965', 30), 1e-29)
    assert re(LambertW(2, -1)).evalf().epsilon_eq(Float('-0.834310366631110'))

    assert LambertW(-1).is_extended_real is False  # issue sympy/sympy#5215
    assert LambertW(2, evaluate=False).is_extended_real
    p = Symbol('p', positive=True)
    assert LambertW(p, evaluate=False).is_extended_real
    assert LambertW(p - 1, evaluate=False).is_extended_real is None
    assert LambertW(-p - 2 / E, evaluate=False).is_extended_real is False
    assert LambertW(Rational(1, 2), -1,
                    evaluate=False).is_extended_real is False
    assert LambertW(Rational(-1, 10), -1, evaluate=False).is_extended_real

    assert LambertW(0, evaluate=False).is_algebraic
    na = Symbol('na', nonzero=True, algebraic=True)
    assert LambertW(na).is_algebraic is False

    assert LambertW(x, -1).is_extended_real is None
    assert LambertW(x, 2).is_extended_real is None

    # See sympy/sympy#7259:
    assert LambertW(x).series(x) == x - x**2 + 3*x**3/2 - 8*x**4/3 + \
        125*x**5/24 + O(x**6)

    assert LambertW(x).series(x, n=0) == O(1, x)
    assert LambertW(x, k).series(x, x0=1,
                                 n=1) == (LambertW(1, k) + O(x - 1, (x, 1)))
Exemplo n.º 34
0
def test_mellin_transform_fail():
    MT = mellin_transform

    bpos = symbols('b', positive=True)

    expr = (sqrt(x + b**2) + b)**a / sqrt(x + b**2)
    # TODO does not work with bneg, argument wrong. Needs changes to matching.
    assert MT(expr.subs(b, -bpos), x, s) == \
        ((-1)**(a + 1)*2**(a + 2*s)*bpos**(a + 2*s - 1)*gamma(a + s)
         * gamma(1 - a - 2*s)/gamma(1 - s),
            (-re(a), -re(a)/2 + Rational(1, 2)), True)

    expr = (sqrt(x + b**2) + b)**a
    assert MT(expr.subs(b, -bpos), x, s) == \
        (
            2**(a + 2*s)*a*bpos**(a + 2*s)*gamma(-a - 2 *
                                                 s)*gamma(a + s)/gamma(-s + 1),
            (-re(a), -re(a)/2), True)

    # Test exponent 1:
    assert MT(expr.subs({b: -bpos, a: 1}), x, s) == \
        (-bpos**(2*s + 1)*gamma(s)*gamma(-s - Rational(1, 2))/(2*sqrt(pi)),
            (-1, -Rational(1, 2)), True)
Exemplo n.º 35
0
def test_matplotlib2():
    name = 'test2'

    plot_implicit(And(y > cos(x), Or(y > x, Eq(y, x))), show=False).save(tmp_file(name))

    # Test plots which cannot be rendered using the adaptive algorithm
    # TODO: catch the warning.
    plot_implicit(Eq(y, re(cos(x) + I*sin(x))), show=False).save(tmp_file(name))

    with warnings.catch_warnings(record=True) as w:
        plot_implicit(x**2 - 1, legend='An implicit plot', show=False).save(tmp_file(name))
        assert len(w) == 1
        assert issubclass(w[-1].category, UserWarning)
        assert 'No labelled objects found' in str(w[0].message)
Exemplo n.º 36
0
 def _eval_evalf(self, prec):
     """ Careful! any evalf of polar numbers is flaky """
     from diofant import im, pi, re
     i = im(self.exp)
     try:
         bad = (i <= -pi or i > pi)
     except TypeError:
         bad = True
     if bad:
         return self  # cannot evalf for this argument
     res = exp(self.exp).evalf(prec)
     if i > 0 and im(res) < 0:
         # i ~ pi, but exp(I*i) evaluated to argument slightly bigger than pi
         return re(res)
     return res
Exemplo n.º 37
0
def test_roots_mixed():
    f = -1936 - 5056 * x - 7592 * x**2 + 2704 * x**3 - 49 * x**4

    _re, _im = intervals(f, all=True)
    _nroots = nroots(f)
    _sroots = roots(f, multiple=True)

    _re = [Interval(a, b) for (a, b), _ in _re]
    _im = [
        Interval(re(a), re(b)) * Interval(im(a), im(b)) for (a, b), _ in _im
    ]

    _intervals = _re + _im
    _sroots = [r.evalf() for r in _sroots]

    _nroots = sorted(_nroots, key=lambda x: x.sort_key())
    _sroots = sorted(_sroots, key=lambda x: x.sort_key())

    for _roots in (_nroots, _sroots):
        for i, r in zip(_intervals, _roots):
            if r.is_extended_real:
                assert r in i
            else:
                assert (re(r), im(r)) in i
Exemplo n.º 38
0
def test_conjugate():
    a = Symbol('a', extended_real=True)
    b = Symbol('b', imaginary=True)
    assert conjugate(a) == a
    assert conjugate(I*a) == -I*a
    assert conjugate(b) == -b
    assert conjugate(I*b) == I*b
    assert conjugate(a*b) == -a*b
    assert conjugate(I*a*b) == I*a*b

    assert conjugate(conjugate(x)) == x
    assert conjugate(x + y) == conjugate(x) + conjugate(y)
    assert conjugate(x - y) == conjugate(x) - conjugate(y)
    assert conjugate(x * y) == conjugate(x) * conjugate(y)
    assert conjugate(x / y) == conjugate(x) / conjugate(y)
    assert conjugate(-x) == -conjugate(x)

    a = Symbol('a', algebraic=True)
    t = Symbol('t', transcendental=True)
    assert re(a).is_algebraic
    assert re(x).is_algebraic is None
    assert re(t).is_algebraic is False

    assert conjugate(z).diff(z) == Derivative(conjugate(z), z)
Exemplo n.º 39
0
def test_exp_assumptions():
    r = Symbol('r', extended_real=True)
    i = Symbol('i', imaginary=True)
    for e in exp, exp_polar:
        assert e(x).is_extended_real is None
        assert e(x).is_imaginary is None
        assert e(i).is_extended_real is None
        assert e(i).is_imaginary is None
        assert e(r).is_extended_real is True
        assert e(re(x)).is_extended_real is True
        if e is not exp_polar:
            assert e(r).is_imaginary is False
            assert e(re(x)).is_imaginary is False

    assert exp(0, evaluate=False).is_algebraic

    a = Symbol('a', algebraic=True)
    an = Symbol('an', algebraic=True, nonzero=True)
    r = Symbol('r', rational=True)
    rn = Symbol('rn', rational=True, nonzero=True)
    assert exp(a).is_algebraic is None
    assert exp(an).is_algebraic is False
    assert exp(pi*r).is_algebraic is None
    assert exp(pi*rn).is_algebraic is False
Exemplo n.º 40
0
def test_inverse_mellin_transform():
    IMT = inverse_mellin_transform

    # test passing "None"
    assert IMT(1/(s**2 - 1), s, x, (-1, None)) == \
        -x*Heaviside(-x + 1)/2 - Heaviside(x - 1)/(2*x)
    assert IMT(1/(s**2 - 1), s, x, (None, 1)) == \
        (-x/2 + 1/(2*x))*Heaviside(-x + 1)

    def simp_pows(expr):
        return simplify(powsimp(expand_mul(expr, deep=False),
                                force=True)).replace(exp_polar, exp)

    assert simp_pows(IMT(d**c*d**(s - 1)*sin(pi*c)
                         * gamma(s)*gamma(s + c)*gamma(1 - s)*gamma(1 - s - c)/pi,
                         s, x, (Max(-re(c), 0), Min(1 - re(c), 1)))) \
        == (x**c - d**c)/(x - d)

    # issue sympy/sympy#8882

    # This is the original test.
    # r = Symbol('r')
    # psi = 1/r*sin(r)*exp(-(a0*r))
    # h = -1/2*diff(psi, r, r) - 1/r*psi
    # f = 4*pi*psi*h*r**2
    # assert integrate(f, (r, -oo, 3), meijerg=True).has(Integral) == True

    # To save time, only the critical part is included.
    F = -a**(-s + 1)*(4 + 1/a**2)**(-s/2)*sqrt(1/a**2)*exp(-s*I*pi) * \
        sin(s*atan(sqrt(1/a**2)/2))*gamma(s)
    pytest.raises(
        IntegralTransformError, lambda: inverse_mellin_transform(
            F, s, x, (-1, oo), **{
                'as_meijerg': True,
                'needeval': True
            }))
Exemplo n.º 41
0
def test_lambertw():
    k = Symbol('k')

    assert LambertW(x, 0) == LambertW(x)
    assert LambertW(x, 0, evaluate=False) != LambertW(x)
    assert LambertW(0) == 0
    assert LambertW(E) == 1
    assert LambertW(-1/E) == -1
    assert LambertW(-log(2)/2) == -log(2)
    assert LambertW(oo) == oo
    assert LambertW(0, 1) == -oo
    assert LambertW(0, 42) == -oo
    assert LambertW(-pi/2, -1) == -I*pi/2
    assert LambertW(-1/E, -1) == -1
    assert LambertW(-2*exp(-2), -1) == -2

    assert LambertW(x**2).diff(x) == 2*LambertW(x**2)/x/(1 + LambertW(x**2))
    assert LambertW(x, k).diff(x) == LambertW(x, k)/x/(1 + LambertW(x, k))
    pytest.raises(ArgumentIndexError, lambda: LambertW(x).fdiff(3))
    pytest.raises(ArgumentIndexError, lambda: LambertW(x, k).fdiff(3))

    assert LambertW(sqrt(2)).evalf(30).epsilon_eq(
        Float("0.701338383413663009202120278965", 30), 1e-29)
    assert re(LambertW(2, -1)).evalf().epsilon_eq(Float("-0.834310366631110"))

    assert LambertW(-1).is_extended_real is False  # issue sympy/sympy#5215
    assert LambertW(2, evaluate=False).is_extended_real
    p = Symbol('p', positive=True)
    assert LambertW(p, evaluate=False).is_extended_real
    assert LambertW(p - 1, evaluate=False).is_extended_real is None
    assert LambertW(-p - 2/E, evaluate=False).is_extended_real is False
    assert LambertW(Rational(1, 2), -1, evaluate=False).is_extended_real is False
    assert LambertW(Rational(-1, 10), -1, evaluate=False).is_extended_real

    assert LambertW(0, evaluate=False).is_algebraic
    na = Symbol('na', nonzero=True, algebraic=True)
    assert LambertW(na).is_algebraic is False

    assert LambertW(x, -1).is_extended_real is None
    assert LambertW(x, 2).is_extended_real is None

    # See sympy/sympy#7259:
    assert LambertW(x).series(x) == x - x**2 + 3*x**3/2 - 8*x**4/3 + \
        125*x**5/24 + O(x**6)

    assert LambertW(x).series(x, n=0) == O(1, x)
    assert LambertW(x, k).series(x, x0=1, n=1) == (LambertW(1, k) +
                                                   O(x - 1, (x, 1)))
Exemplo n.º 42
0
def test_matplotlib2():
    name = 'test2'

    plot_implicit(And(y > cos(x), Or(y > x, Eq(y, x))),
                  show=False).save(tmp_file(name))

    # Test plots which cannot be rendered using the adaptive algorithm
    # TODO: catch the warning.
    plot_implicit(Eq(y, re(cos(x) + I * sin(x))),
                  show=False).save(tmp_file(name))

    with warnings.catch_warnings(record=True) as w:
        plot_implicit(x**2 - 1, legend='An implicit plot',
                      show=False).save(tmp_file(name))
        assert len(w) == 1
        assert issubclass(w[-1].category, UserWarning)
        assert 'No labelled objects found' in str(w[0].message)
Exemplo n.º 43
0
def test_catalan():
    n = Symbol('n', integer=True)
    m = Symbol('n', integer=True, positive=True)

    catalans = [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786]
    for i, c in enumerate(catalans):
        assert catalan(i) == c
        assert catalan(n).rewrite(factorial).subs({n: i}) == c
        assert catalan(n).rewrite(Product).subs({n: i}).doit() == c

    assert catalan(x) == catalan(x)
    assert catalan(2 *
                   x).rewrite(binomial) == binomial(4 * x, 2 * x) / (2 * x + 1)
    assert catalan(Rational(1, 2)).rewrite(gamma) == 8 / (3 * pi)
    assert catalan(Rational(1, 2)).rewrite(factorial).rewrite(gamma) ==\
        8 / (3 * pi)
    assert catalan(3 * x).rewrite(gamma) == 4**(
        3 * x) * gamma(3 * x + Rational(1, 2)) / (sqrt(pi) * gamma(3 * x + 2))
    assert catalan(x).rewrite(hyper) == hyper((-x + 1, -x), (2, ), 1)

    assert catalan(n).rewrite(factorial) == factorial(
        2 * n) / (factorial(n + 1) * factorial(n))
    assert isinstance(catalan(n).rewrite(Product), catalan)
    assert isinstance(catalan(m).rewrite(Product), Product)

    assert diff(catalan(x), x) == (polygamma(0, x + Rational(1, 2)) -
                                   polygamma(0, x + 2) + log(4)) * catalan(x)

    assert catalan(x).evalf() == catalan(x)
    c = catalan(Rational(1, 2)).evalf()
    assert str(c) == '0.848826363156775'
    c = catalan(I).evalf(3)
    assert sstr((re(c), im(c))) == '(0.398, -0.0209)'

    # issue sympy/sympy#8601
    n = Symbol('n', integer=True, negative=True)

    assert catalan(n - 1) == 0
    assert catalan(Rational(-1, 2)) == zoo
    assert catalan(-1) == Rational(-1, 2)
    c1 = catalan(-5.6).evalf(strict=False)
    assert str(c1) == '6.93334070531408e-5'
    c2 = catalan(-35.4).evalf(strict=False)
    assert str(c2) == '-4.14189164517449e-24'
Exemplo n.º 44
0
def test_derivatives_sympyissue_4757():
    x = Symbol('x', extended_real=True)
    y = Symbol('y', imaginary=True)
    f = Function('f')
    assert re(f(x)).diff(x) == re(f(x).diff(x))
    assert im(f(x)).diff(x) == im(f(x).diff(x))
    assert re(f(y)).diff(y) == -I*im(f(y).diff(y))
    assert im(f(y)).diff(y) == -I*re(f(y).diff(y))
    assert re(f(z)).diff(z) == Derivative(re(f(z)), z)
    assert im(f(z)).diff(z) == Derivative(im(f(z)), z)
    assert Abs(f(x)).diff(x).subs({f(x): 1 + I*x}).doit() == x/sqrt(1 + x**2)
    assert arg(f(x)).diff(x).subs({f(x): 1 + I*x**2}).doit() == 2*x/(1 + x**4)
    assert Abs(f(y)).diff(y).subs({f(y): 1 + y}).doit() == -y/sqrt(1 - y**2)
    assert arg(f(y)).diff(y).subs({f(y): I + y**2}).doit() == 2*y/(1 + y**4)
Exemplo n.º 45
0
def test_derivatives_sympyissue_4757():
    x = Symbol('x', extended_real=True)
    y = Symbol('y', imaginary=True)
    f = Function('f')
    assert re(f(x)).diff(x) == re(f(x).diff(x))
    assert im(f(x)).diff(x) == im(f(x).diff(x))
    assert re(f(y)).diff(y) == -I*im(f(y).diff(y))
    assert im(f(y)).diff(y) == -I*re(f(y).diff(y))
    assert re(f(z)).diff(z) == Derivative(re(f(z)), z)
    assert im(f(z)).diff(z) == Derivative(im(f(z)), z)
    assert abs(f(x)).diff(x).subs({f(x): 1 + I*x}).doit() == x/sqrt(1 + x**2)
    assert arg(f(x)).diff(x).subs({f(x): 1 + I*x**2}).doit() == 2*x/(1 + x**4)
    assert abs(f(y)).diff(y).subs({f(y): 1 + y}).doit() == -y/sqrt(1 - y**2)
    assert arg(f(y)).diff(y).subs({f(y): I + y**2}).doit() == 2*y/(1 + y**4)
Exemplo n.º 46
0
def plot_and_save(name):
    x = Symbol('x')
    y = Symbol('y')
    z = Symbol('z')
    # implicit plot tests
    plot_implicit(Eq(y, cos(x)), (x, -5, 5), (y, -2, 2)).save(tmp_file(name))
    plot_implicit(Eq(y**2, x**3 - x), (x, -5, 5),
                  (y, -4, 4)).save(tmp_file(name))
    plot_implicit(y > 1 / x, (x, -5, 5), (y, -2, 2)).save(tmp_file(name))
    plot_implicit(y < 1 / tan(x), (x, -5, 5), (y, -2, 2)).save(tmp_file(name))
    plot_implicit(y >= 2 * sin(x) * cos(x), (x, -5, 5),
                  (y, -2, 2)).save(tmp_file(name))
    plot_implicit(y <= x**2, (x, -3, 3), (y, -1, 5)).save(tmp_file(name))

    # Test all input args for plot_implicit
    plot_implicit(Eq(y**2, x**3 - x)).save(tmp_file())
    plot_implicit(Eq(y**2, x**3 - x), adaptive=False).save(tmp_file())
    plot_implicit(Eq(y**2, x**3 - x), adaptive=False,
                  points=500).save(tmp_file())
    plot_implicit(y > x, (x, -5, 5)).save(tmp_file())
    plot_implicit(And(y > exp(x), y > x + 2)).save(tmp_file())
    plot_implicit(Or(y > x, y > -x)).save(tmp_file())
    plot_implicit(x**2 - 1, (x, -5, 5)).save(tmp_file())
    plot_implicit(x**2 - 1).save(tmp_file())
    plot_implicit(y > x, depth=-5).save(tmp_file())
    plot_implicit(y > x, depth=5).save(tmp_file())
    plot_implicit(y > cos(x), adaptive=False).save(tmp_file())
    plot_implicit(y < cos(x), adaptive=False).save(tmp_file())
    plot_implicit(And(y > cos(x), Or(y > x, Eq(y, x)))).save(tmp_file())
    plot_implicit(y - cos(pi / x)).save(tmp_file())

    # Test plots which cannot be rendered using the adaptive algorithm
    # TODO: catch the warning.
    plot_implicit(Eq(y, re(cos(x) + I * sin(x)))).save(tmp_file(name))

    with warnings.catch_warnings(record=True) as w:
        plot_implicit(x**2 - 1, legend='An implicit plot').save(tmp_file())
        assert len(w) == 1
        assert issubclass(w[-1].category, UserWarning)
        assert 'No labeled objects found' in str(w[0].message)
Exemplo n.º 47
0
def test_meijerint():
    s, t, mu = symbols('s t mu', extended_real=True)
    assert integrate(meijerg([], [], [0], [], s*t)
                     * meijerg([], [], [mu/2], [-mu/2], t**2/4),
                     (t, 0, oo)).is_Piecewise
    s = symbols('s', positive=True)
    assert integrate(x**s*meijerg([[], []], [[0], []], x), (x, 0, oo)) == \
        gamma(s + 1)
    assert integrate(x**s*meijerg([[], []], [[0], []], x), (x, 0, oo),
                     meijerg=True) == gamma(s + 1)
    assert isinstance(integrate(x**s*meijerg([[], []], [[0], []], x),
                                (x, 0, oo), meijerg=False),
                      Integral)

    assert meijerint_indefinite(exp(x), x) == exp(x)

    # TODO what simplifications should be done automatically?
    # This tests "extra case" for antecedents_1.
    a, b = symbols('a b', positive=True)
    assert simplify(meijerint_definite(x**a, x, 0, b)[0]) == \
        b**(a + 1)/(a + 1)

    # This tests various conditions and expansions:
    meijerint_definite((x + 1)**3*exp(-x), x, 0, oo) == (16, True)

    # Again, how about simplifications?
    sigma, mu = symbols('sigma mu', positive=True)
    i, c = meijerint_definite(exp(-((x - mu)/(2*sigma))**2), x, 0, oo)
    assert simplify(i) == sqrt(pi)*sigma*(erf(mu/(2*sigma)) + 1)
    assert c

    i, _ = meijerint_definite(exp(-mu*x)*exp(sigma*x), x, 0, oo)
    # TODO it would be nice to test the condition
    assert simplify(i) == 1/(mu - sigma)

    # Test substitutions to change limits
    assert meijerint_definite(exp(x), x, -oo, 2) == (exp(2), True)
    # Note: causes a NaN in _check_antecedents
    assert expand(meijerint_definite(exp(x), x, 0, I)[0]) == exp(I) - 1
    assert expand(meijerint_definite(exp(-x), x, 0, x)[0]) == \
        1 - exp(-exp(I*arg(x))*abs(x))

    # Test -oo to oo
    assert meijerint_definite(exp(-x**2), x, -oo, oo) == (sqrt(pi), True)
    assert meijerint_definite(exp(-abs(x)), x, -oo, oo) == (2, True)
    assert meijerint_definite(exp(-(2*x - 3)**2), x, -oo, oo) == \
        (sqrt(pi)/2, True)
    assert meijerint_definite(exp(-abs(2*x - 3)), x, -oo, oo) == (1, True)
    assert meijerint_definite(exp(-((x - mu)/sigma)**2/2)/sqrt(2*pi*sigma**2),
                              x, -oo, oo) == (1, True)

    # Test one of the extra conditions for 2 g-functinos
    assert meijerint_definite(exp(-x)*sin(x), x, 0, oo) == (Rational(1, 2), True)

    # Test a bug
    def res(n):
        return (1/(1 + x**2)).diff(x, n).subs({x: 1})*(-1)**n
    for n in range(6):
        assert integrate(exp(-x)*sin(x)*x**n, (x, 0, oo), meijerg=True) == \
            res(n)

    # This used to test trigexpand... now it is done by linear substitution
    assert simplify(integrate(exp(-x)*sin(x + a), (x, 0, oo), meijerg=True)
                    ) == sqrt(2)*sin(a + pi/4)/2

    # Test the condition 14 from prudnikov.
    # (This is besselj*besselj in disguise, to stop the product from being
    #  recognised in the tables.)
    a, b, s = symbols('a b s')
    assert meijerint_definite(meijerg([], [], [a/2], [-a/2], x/4)
                              * meijerg([], [], [b/2], [-b/2], x/4)*x**(s - 1), x, 0, oo) == \
        (4*2**(2*s - 2)*gamma(-2*s + 1)*gamma(a/2 + b/2 + s)
         / (gamma(-a/2 + b/2 - s + 1)*gamma(a/2 - b/2 - s + 1)
            * gamma(a/2 + b/2 - s + 1)),
            And(0 < -2*re(4*s) + 8, 0 < re(a/2 + b/2 + s), re(2*s) < 1))

    # test a bug
    assert integrate(sin(x**a)*sin(x**b), (x, 0, oo), meijerg=True) == \
        Integral(sin(x**a)*sin(x**b), (x, 0, oo))

    # test better hyperexpand
    assert integrate(exp(-x**2)*log(x), (x, 0, oo), meijerg=True) == \
        (sqrt(pi)*polygamma(0, Rational(1, 2))/4).expand()

    # Test hyperexpand bug.
    n = symbols('n', integer=True)
    assert simplify(integrate(exp(-x)*x**n, x, meijerg=True)) == \
        lowergamma(n + 1, x)

    # Test a bug with argument 1/x
    alpha = symbols('alpha', positive=True)
    assert meijerint_definite((2 - x)**alpha*sin(alpha/x), x, 0, 2) == \
        (sqrt(pi)*alpha*gamma(alpha + 1)*meijerg(((), (alpha/2 + Rational(1, 2),
                                                       alpha/2 + 1)), ((0, 0, Rational(1, 2)), (-Rational(1, 2),)), alpha**2/16)/4, True)

    # test a bug related to 3016
    a, s = symbols('a s', positive=True)
    assert simplify(integrate(x**s*exp(-a*x**2), (x, -oo, oo))) == \
        a**(-s/2 - Rational(1, 2))*((-1)**s + 1)*gamma(s/2 + Rational(1, 2))/2
Exemplo n.º 48
0
def test_expand_function():
    assert expand(x + y) == x + y
    assert expand(x + y, complex=True) == I*im(x) + I*im(y) + re(x) + re(y)
    assert expand((x + y)**11, modulus=11) == x**11 + y**11
Exemplo n.º 49
0
def test_airyai():
    z = Symbol('z', extended_real=False)
    r = Symbol('r', extended_real=True)
    t = Symbol('t', negative=True)
    p = Symbol('p', positive=True)

    assert isinstance(airyai(z), airyai)

    assert airyai(0) == cbrt(3)/(3*gamma(Rational(2, 3)))
    assert airyai(oo) == 0
    assert airyai(-oo) == 0

    assert diff(airyai(z), z) == airyaiprime(z)

    assert series(airyai(z), z, 0, 3) == (
        3**Rational(5, 6)*gamma(Rational(1, 3))/(6*pi) - root(3, 6)*z*gamma(Rational(2, 3))/(2*pi) + O(z**3))

    l = Limit(airyai(I/x)/(exp(-Rational(2, 3)*(I/x)**Rational(3, 2))*sqrt(pi*sqrt(I/x))/2), x, 0)
    assert l.doit() == l  # cover _airyais._eval_aseries

    assert airyai(z).rewrite(hyper) == (
        -3**Rational(2, 3)*z*hyper((), (Rational(4, 3),), z**3/9)/(3*gamma(Rational(1, 3))) +
        cbrt(3)*hyper((), (Rational(2, 3),), z**3/9)/(3*gamma(Rational(2, 3))))

    assert isinstance(airyai(z).rewrite(besselj), airyai)
    assert airyai(t).rewrite(besselj) == (
        sqrt(-t)*(besselj(-Rational(1, 3), 2*(-t)**Rational(3, 2)/3) +
                  besselj(Rational(1, 3), 2*(-t)**Rational(3, 2)/3))/3)
    assert airyai(z).rewrite(besseli) == (
        -z*besseli(Rational(1, 3), 2*z**Rational(3, 2)/3)/(3*cbrt(z**Rational(3, 2))) +
        cbrt(z**Rational(3, 2))*besseli(-Rational(1, 3), 2*z**Rational(3, 2)/3)/3)
    assert airyai(p).rewrite(besseli) == (
        sqrt(p)*(besseli(-Rational(1, 3), 2*p**Rational(3, 2)/3) -
                 besseli(Rational(1, 3), 2*p**Rational(3, 2)/3))/3)

    assert expand_func(airyai(2*cbrt(3*z**5))) == (
        -sqrt(3)*(-1 + cbrt(z**5)/z**Rational(5, 3))*airybi(2*cbrt(3)*z**Rational(5, 3))/6 +
        (1 + cbrt(z**5)/z**Rational(5, 3))*airyai(2*cbrt(3)*z**Rational(5, 3))/2)
    assert expand_func(airyai(x*y)) == airyai(x*y)
    assert expand_func(airyai(log(x))) == airyai(log(x))
    assert expand_func(airyai(2*root(3*z**5, 5))) == airyai(2*root(3*z**5, 5))

    assert (airyai(r).as_real_imag() ==
            airyai(r).as_real_imag(deep=False) == (airyai(r), 0))
    assert airyai(x).as_real_imag() == airyai(x).as_real_imag(deep=False)
    assert (airyai(x).as_real_imag() ==
            (airyai(re(x) - I*re(x)*abs(im(x))/abs(re(x)))/2 +
             airyai(re(x) + I*re(x)*abs(im(x))/abs(re(x)))/2,
             I*(airyai(re(x) - I*re(x)*abs(im(x))/abs(re(x))) -
                airyai(re(x) + I*re(x)*abs(im(x))/Abs(re(x)))) *
             re(x)*abs(im(x))/(2*im(x)*abs(re(x)))))

    assert airyai(x).taylor_term(-1, x) == 0
Exemplo n.º 50
0
def test_re_im1652():
    x = Symbol('x')
    assert re(x) == re(conjugate(x))
    assert im(x) == - im(conjugate(x))
    assert im(x)*re(conjugate(x)) + im(conjugate(x)) * re(x) == 0
Exemplo n.º 51
0
def test_sympyissue_5084():
    x = Symbol('x')
    assert ((x + x*I)/(1 + I)).as_real_imag() == (re((x + I*x)/(1 + I)),
                                                  im((x + I*x)/(1 + I)))
Exemplo n.º 52
0
def test_fresnel():
    assert fresnels(0) == 0
    assert fresnels(+oo) == Rational(+1, 2)
    assert fresnels(-oo) == Rational(-1, 2)

    assert fresnels(z) == fresnels(z)
    assert fresnels(-z) == -fresnels(z)
    assert fresnels(I*z) == -I*fresnels(z)
    assert fresnels(-I*z) == I*fresnels(z)

    assert conjugate(fresnels(z)) == fresnels(conjugate(z))

    assert fresnels(z).diff(z) == sin(pi*z**2/2)

    assert fresnels(z).rewrite(erf) == (1 + I)/4 * (
        erf((1 + I)/2*sqrt(pi)*z) - I*erf((1 - I)/2*sqrt(pi)*z))

    assert fresnels(z).rewrite(hyper) == \
        pi*z**3/6 * hyper([Rational(3, 4)], [Rational(3, 2), Rational(7, 4)], -pi**2*z**4/16)

    assert fresnels(z).series(z, n=15) == \
        pi*z**3/6 - pi**3*z**7/336 + pi**5*z**11/42240 + O(z**15)

    assert fresnels(y/z).limit(z, 0) == fresnels(oo*sign(y))

    assert fresnels(x).taylor_term(-1, z) == 0
    assert fresnels(x).taylor_term(1, z, *(pi*z**3/6,)) == -pi**3*z**7/336
    assert fresnels(x).taylor_term(1, z) == -pi**3*z**7/336

    assert fresnels(w).is_extended_real is True
    assert fresnels(z).is_extended_real is None

    assert fresnels(z).as_real_imag() == \
        ((fresnels(re(z) - I*re(z)*Abs(im(z))/Abs(re(z)))/2 +
          fresnels(re(z) + I*re(z)*Abs(im(z))/Abs(re(z)))/2,
          I*(fresnels(re(z) - I*re(z)*Abs(im(z))/Abs(re(z))) -
             fresnels(re(z) + I*re(z)*Abs(im(z))/Abs(re(z)))) *
          re(z)*Abs(im(z))/(2*im(z)*Abs(re(z)))))
    assert fresnels(z).as_real_imag(deep=False) == fresnels(z).as_real_imag()
    assert fresnels(w).as_real_imag() == (fresnels(w), 0)
    assert fresnels(w).as_real_imag(deep=False) == fresnels(w).as_real_imag()
    assert (fresnels(I, evaluate=False).as_real_imag() ==
            (0, -erf(sqrt(pi)/2 + I*sqrt(pi)/2)/4 +
             I*(-erf(sqrt(pi)/2 + I*sqrt(pi)/2) + erf(sqrt(pi)/2 -
                I*sqrt(pi)/2))/4 - erf(sqrt(pi)/2 - I*sqrt(pi)/2)/4))

    assert fresnels(2 + 3*I).as_real_imag() == (
        fresnels(2 + 3*I)/2 + fresnels(2 - 3*I)/2,
        I*(fresnels(2 - 3*I) - fresnels(2 + 3*I))/2
    )

    assert expand_func(integrate(fresnels(z), z)) == \
        z*fresnels(z) + cos(pi*z**2/2)/pi

    assert fresnels(z).rewrite(meijerg) == sqrt(2)*pi*z**Rational(9, 4) * \
        meijerg(((), (1,)), ((Rational(3, 4),),
                             (Rational(1, 4), 0)), -pi**2*z**4/16)/(2*(-z)**Rational(3, 4)*(z**2)**Rational(3, 4))

    assert fresnelc(0) == 0
    assert fresnelc(+oo) == Rational(+1, 2)
    assert fresnelc(-oo) == Rational(-1, 2)

    assert fresnelc(z) == fresnelc(z)
    assert fresnelc(-z) == -fresnelc(z)
    assert fresnelc(I*z) == I*fresnelc(z)
    assert fresnelc(-I*z) == -I*fresnelc(z)

    assert conjugate(fresnelc(z)) == fresnelc(conjugate(z))

    assert fresnelc(z).diff(z) == cos(pi*z**2/2)
    pytest.raises(ArgumentIndexError, lambda: fresnels(z).fdiff(2))
    pytest.raises(ArgumentIndexError, lambda: fresnelc(z).fdiff(2))

    assert fresnelc(z).rewrite(erf) == (1 - I)/4 * (
        erf((1 + I)/2*sqrt(pi)*z) + I*erf((1 - I)/2*sqrt(pi)*z))

    assert fresnelc(z).rewrite(hyper) == \
        z * hyper([Rational(1, 4)], [Rational(1, 2), Rational(5, 4)], -pi**2*z**4/16)

    assert fresnelc(x).taylor_term(-1, z) == 0
    assert fresnelc(x).taylor_term(1, z, *(z,)) == -pi**2*z**5/40
    assert fresnelc(x).taylor_term(1, z) == -pi**2*z**5/40

    assert fresnelc(z).series(z, n=15) == \
        z - pi**2*z**5/40 + pi**4*z**9/3456 - pi**6*z**13/599040 + O(z**15)

    assert fresnelc(y/z).limit(z, 0) == fresnelc(oo*sign(y))

    # issue sympy/sympy#6510
    assert fresnels(z).series(z, oo) == \
        (-1/(pi**2*z**3) + O(z**(-6), (z, oo)))*sin(pi*z**2/2) + \
        (3/(pi**3*z**5) - 1/(pi*z) + O(z**(-6), (z, oo)))*cos(pi*z**2/2) + Rational(1, 2)
    assert fresnelc(z).series(z, oo) == \
        (-1/(pi**2*z**3) + O(z**(-6), (z, oo)))*cos(pi*z**2/2) + \
        (-3/(pi**3*z**5) + 1/(pi*z) + O(z**(-6), (z, oo)))*sin(pi*z**2/2) + Rational(1, 2)
    assert fresnels(1/z).series(z) == \
        (-z**3/pi**2 + O(z**6))*sin(pi/(2*z**2)) + (-z/pi + 3*z**5/pi**3 +
                                                    O(z**6))*cos(pi/(2*z**2)) + Rational(1, 2)
    assert fresnelc(1/z).series(z) == \
        (-z**3/pi**2 + O(z**6))*cos(pi/(2*z**2)) + (z/pi - 3*z**5/pi**3 +
                                                    O(z**6))*sin(pi/(2*z**2)) + Rational(1, 2)

    assert fresnelc(w).is_extended_real is True

    assert fresnelc(z).as_real_imag() == \
        ((fresnelc(re(z) - I*re(z)*Abs(im(z))/Abs(re(z)))/2 +
          fresnelc(re(z) + I*re(z)*Abs(im(z))/Abs(re(z)))/2,
          I*(fresnelc(re(z) - I*re(z)*Abs(im(z))/Abs(re(z))) -
             fresnelc(re(z) + I*re(z)*Abs(im(z))/Abs(re(z)))) *
          re(z)*Abs(im(z))/(2*im(z)*Abs(re(z)))))

    assert fresnelc(2 + 3*I).as_real_imag() == (
        fresnelc(2 - 3*I)/2 + fresnelc(2 + 3*I)/2,
        I*(fresnelc(2 - 3*I) - fresnelc(2 + 3*I))/2
    )

    assert expand_func(integrate(fresnelc(z), z)) == \
        z*fresnelc(z) - sin(pi*z**2/2)/pi

    assert fresnelc(z).rewrite(meijerg) == sqrt(2)*pi*z**Rational(3, 4) * \
        meijerg(((), (1,)), ((Rational(1, 4),),
                             (Rational(3, 4), 0)), -pi**2*z**4/16)/(2*root(-z, 4)*root(z**2, 4))

    verify_numerically(re(fresnels(z)), fresnels(z).as_real_imag()[0], z)
    verify_numerically(im(fresnels(z)), fresnels(z).as_real_imag()[1], z)
    verify_numerically(fresnels(z), fresnels(z).rewrite(hyper), z)
    verify_numerically(fresnels(z), fresnels(z).rewrite(meijerg), z)

    verify_numerically(re(fresnelc(z)), fresnelc(z).as_real_imag()[0], z)
    verify_numerically(im(fresnelc(z)), fresnelc(z).as_real_imag()[1], z)
    verify_numerically(fresnelc(z), fresnelc(z).rewrite(hyper), z)
    verify_numerically(fresnelc(z), fresnelc(z).rewrite(meijerg), z)
Exemplo n.º 53
0
def test_im():
    a, b = symbols('a,b', extended_real=True)

    r = Symbol('r', extended_real=True)
    i = Symbol('i', imaginary=True)

    assert im(nan) == nan

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

    assert im(0) == 0

    assert im(1) == 0
    assert im(-1) == 0

    assert im(E*I) == E
    assert im(-E*I) == -E

    assert im(x) == im(x)
    assert im(x*I) == re(x)
    assert im(r*I) == r
    assert im(r) == 0
    assert im(i*I) == 0
    assert im(i) == -I * i

    assert im(x + y) == im(x + y)
    assert im(x + r) == im(x)
    assert im(x + r*I) == im(x) + r

    assert im(im(x)*I) == im(x)

    assert im(2 + I) == 1
    assert im(x + I) == im(x) + 1

    assert im(x + y*I) == im(x) + re(y)
    assert im(x + r*I) == im(x) + r

    assert im(log(2*I)) == pi/2

    assert im((2 + I)**2).expand(complex=True) == 4

    assert im(conjugate(x)) == -im(x)
    assert conjugate(im(x)) == im(x)

    assert im(x).as_real_imag() == (im(x), 0)

    assert im(i*r*x).diff(r) == im(i*x)
    assert im(i*r*x).diff(i) == -I * re(r*x)

    assert im(sqrt(a + b*I)) == root(a**2 + b**2, 4)*sin(arg(a + I*b)/2)
    assert im(a * (2 + b*I)) == a*b

    assert im((1 + sqrt(a + b*I))/2) == root(a**2 + b**2, 4)*sin(arg(a + I*b)/2)/2

    assert im(x).rewrite(re) == -I*(x - re(x))  # sympy/sympy#10897
    assert (x + im(y)).rewrite(im, re) == x - I*(y - re(y))

    a = Symbol('a', algebraic=True)
    t = Symbol('t', transcendental=True)
    assert re(a).is_algebraic
    assert re(x).is_algebraic is None
    assert re(t).is_algebraic is False

    assert re(zoo) == nan
Exemplo n.º 54
0
def test_Abs():
    pytest.raises(TypeError, lambda: Abs(Interval(2, 3)))  # issue sympy/sympy#8717

    x, y = symbols('x,y')
    assert sign(sign(x)) == sign(x)
    assert isinstance(sign(x*y), sign)
    assert Abs(0) == 0
    assert Abs(1) == 1
    assert Abs(-1) == 1
    assert Abs(I) == 1
    assert Abs(-I) == 1
    assert Abs(nan) == nan
    assert Abs(I * pi) == pi
    assert Abs(-I * pi) == pi
    assert Abs(I * x) == Abs(x)
    assert Abs(-I * x) == Abs(x)
    assert Abs(-2*x) == 2*Abs(x)
    assert Abs(-2.0*x) == 2.0*Abs(x)
    assert Abs(2*pi*x*y) == 2*pi*Abs(x*y)
    assert Abs(conjugate(x)) == Abs(x)
    assert conjugate(Abs(x)) == Abs(x)

    a = cos(1)**2 + sin(1)**2 - 1
    assert Abs(a*x).series(x).simplify() == 0

    a = Symbol('a', positive=True)
    assert Abs(2*pi*x*a) == 2*pi*a*Abs(x)
    assert Abs(2*pi*I*x*a) == 2*pi*a*Abs(x)

    x = Symbol('x', extended_real=True)
    n = Symbol('n', integer=True)
    assert Abs((-1)**n) == 1
    assert x**(2*n) == Abs(x)**(2*n)
    assert Abs(x).diff(x) == sign(x)
    assert Abs(-x).fdiff() == sign(x)
    assert abs(x) == Abs(x)  # Python built-in
    assert Abs(x)**3 == x**2*Abs(x)
    assert Abs(x)**4 == x**4
    assert (
        Abs(x)**(3*n)).args == (Abs(x), 3*n)  # leave symbolic odd unchanged
    assert (1/Abs(x)).args == (Abs(x), -1)
    assert 1/Abs(x)**3 == 1/(x**2*Abs(x))
    assert Abs(x)**-3 == Abs(x)/(x**4)
    assert Abs(x**3) == x**2*Abs(x)
    assert Abs(x**pi) == Abs(x**pi, evaluate=False)

    x = Symbol('x', imaginary=True)
    assert Abs(x).diff(x) == -sign(x)

    pytest.raises(ArgumentIndexError, lambda: Abs(z).fdiff(2))

    eq = -sqrt(10 + 6*sqrt(3)) + sqrt(1 + sqrt(3)) + sqrt(3 + 3*sqrt(3))
    # if there is a fast way to know when you can and when you cannot prove an
    # expression like this is zero then the equality to zero is ok
    assert abs(eq) == 0
    q = 1 + sqrt(2) - 2*sqrt(3) + 1331*sqrt(6)
    p = cbrt(expand(q**3))
    d = p - q
    assert abs(d) == 0

    assert Abs(4*exp(pi*I/4)) == 4
    assert Abs(3**(2 + I)) == 9
    assert Abs((-3)**(1 - I)) == 3*exp(pi)

    assert Abs(oo) is oo
    assert Abs(-oo) is oo
    assert Abs(oo + I) is oo
    assert Abs(oo + I*oo) is oo

    a = Symbol('a', algebraic=True)
    t = Symbol('t', transcendental=True)
    x = Symbol('x')
    assert re(a).is_algebraic
    assert re(x).is_algebraic is None
    assert re(t).is_algebraic is False

    assert abs(sign(z)) == Abs(sign(z), evaluate=False)
Exemplo n.º 55
0
def test_erf():
    assert erf(nan) == nan

    assert erf(oo) == 1
    assert erf(-oo) == -1

    assert erf(0) == 0

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

    assert erf(-2) == -erf(2)
    assert erf(-x*y) == -erf(x*y)
    assert erf(-x - y) == -erf(x + y)

    assert erf(erfinv(x)) == x
    assert erf(erfcinv(x)) == 1 - x
    assert erf(erf2inv(0, x)) == x
    assert erf(erf2inv(0, erf(erfcinv(1 - erf(erfinv(x)))))) == x

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

    assert conjugate(erf(z)) == erf(conjugate(z))

    assert erf(x).as_leading_term(x) == 2*x/sqrt(pi)
    assert erf(1/x).as_leading_term(x) == erf(1/x)

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

    assert limit(exp(x)*exp(x**2)*(erf(x + 1/exp(x)) - erf(x)), x, oo) == \
        2/sqrt(pi)
    assert limit((1 - erf(z))*exp(z**2)*z, z, oo) == 1/sqrt(pi)
    assert limit((1 - erf(x))*exp(x**2)*sqrt(pi)*x, x, oo) == 1
    assert limit(((1 - erf(x))*exp(x**2)*sqrt(pi)*x - 1)*2*x**2, x, oo) == -1

    l = Limit((1 - erf(y/x))*exp(y**2/x**2), x, 0)
    assert l.doit() == l  # cover _erfs._eval_aseries

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

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

    assert erf(x).taylor_term(3, x, *(2*x/sqrt(pi), 0)) == -2*x**3/3/sqrt(pi)
Exemplo n.º 56
0
def test_sympyissue_7450():
    ans = integrate(exp(-(1 + I)*x), (x, 0, oo))
    assert re(ans) == Rational(1, 2) and im(ans) == Rational(-1, 2)
Exemplo n.º 57
0
def test_re():
    a, b = symbols('a,b', extended_real=True)

    r = Symbol('r', extended_real=True)
    i = Symbol('i', imaginary=True)

    assert re(nan) == nan

    assert re(oo) == oo
    assert re(-oo) == -oo

    assert re(0) == 0

    assert re(1) == 1
    assert re(-1) == -1

    assert re(E) == E
    assert re(-E) == -E

    assert re(x) == re(x)
    assert re(x*I) == -im(x)
    assert re(r*I) == 0
    assert re(r) == r
    assert re(i*I) == I * i
    assert re(i) == 0

    assert re(x + y) == re(x + y)
    assert re(x + r) == re(x) + r

    assert re(re(x)) == re(x)

    assert re(2 + I) == 2
    assert re(x + I) == re(x)

    assert re(x + y*I) == re(x) - im(y)
    assert re(x + r*I) == re(x)

    assert re(log(2*I)) == log(2)

    assert re((2 + I)**2).expand(complex=True) == 3

    assert re(conjugate(x)) == re(x)
    assert conjugate(re(x)) == re(x)

    assert re(x).as_real_imag() == (re(x), 0)

    assert re(i*r*x).diff(r) == re(i*x)
    assert re(i*r*x).diff(i) == I*r*im(x)

    assert re(sqrt(a + b*I)) == root(a**2 + b**2, 4)*cos(arg(a + I*b)/2)
    assert re(a * (2 + b*I)) == 2*a

    assert re((1 + sqrt(a + b*I))/2) == root(a**2 + b**2, 4)*cos(arg(a + I*b)/2)/2 + Rational(1, 2)

    assert re(x).rewrite(im) == x - I*im(x)  # issue sympy/sympy#10897
    assert (x + re(y)).rewrite(re, im) == x + y - I*im(y)

    a = Symbol('a', algebraic=True)
    t = Symbol('t', transcendental=True)
    assert re(a).is_algebraic
    assert re(x).is_algebraic is None
    assert re(t).is_algebraic is False

    assert re(zoo) == nan
Exemplo n.º 58
0
def test_bugs():
    assert abs(re((1 + I)**2)) < 1e-15

    # anything that evalf's to 0 will do in place of polar_lift
    assert abs(polar_lift(0)).evalf() == 0
Exemplo n.º 59
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)
Exemplo n.º 60
0
def test_erfi():
    assert erfi(nan) == nan

    assert erfi(+oo) == +oo
    assert erfi(-oo) == -oo

    assert erfi(0) == 0

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

    assert erfi(-x) == -erfi(x)

    assert erfi(I*erfinv(x)) == I*x
    assert erfi(I*erfcinv(x)) == I*(1 - x)
    assert erfi(I*erf2inv(0, x)) == I*x

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

    assert conjugate(erfi(z)) == erfi(conjugate(z))

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

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

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

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

    assert erfi(x).limit(x, oo) == oo