Beispiel #1
0
def test_li():
    z = Symbol("z")
    zr = Symbol("z", extended_real=True)
    zp = Symbol("z", positive=True)
    zn = Symbol("z", negative=True)

    assert li(0) == 0
    assert li(1) == -oo
    assert li(oo) == oo

    assert isinstance(li(z), li)

    assert diff(li(z), z) == 1/log(z)

    assert conjugate(li(z)) == li(conjugate(z))
    assert conjugate(li(-zr)) == li(-zr)
    assert conjugate(li(-zp)) == conjugate(li(-zp))
    assert conjugate(li(zn)) == conjugate(li(zn))

    assert li(z).rewrite(Li) == Li(z) + li(2)
    assert li(z).rewrite(Ei) == Ei(log(z))
    assert li(z).rewrite(uppergamma) == (-log(1/log(z))/2 - log(-log(z)) +
                                         log(log(z))/2 - expint(1, -log(z)))
    assert li(z).rewrite(Si) == (-log(I*log(z)) - log(1/log(z))/2 +
                                 log(log(z))/2 + Ci(I*log(z)) + Shi(log(z)))
    assert li(z).rewrite(Ci) == (-log(I*log(z)) - log(1/log(z))/2 +
                                 log(log(z))/2 + Ci(I*log(z)) + Shi(log(z)))
    assert li(z).rewrite(Shi) == (-log(1/log(z))/2 + log(log(z))/2 +
                                  Chi(log(z)) - Shi(log(z)))
    assert li(z).rewrite(Chi) == (-log(1/log(z))/2 + log(log(z))/2 +
                                  Chi(log(z)) - Shi(log(z)))
    assert li(z).rewrite(hyper) == (log(z)*hyper((1, 1), (2, 2), log(z)) -
                                   log(1/log(z))/2 + log(log(z))/2 + EulerGamma)
    assert li(z).rewrite(meijerg) == (-log(1/log(z))/2 - log(-log(z)) + log(log(z))/2 -
                                      meijerg(((), (1,)), ((0, 0), ()), -log(z)))
Beispiel #2
0
def test_expint():
    """ Test various exponential integrals. """
    from diofant import (expint, unpolarify, Symbol, Ci, Si, Shi, Chi, sin,
                         cos, sinh, cosh, Ei)
    assert simplify(
        unpolarify(
            integrate(exp(-z * x) / x**y, (x, 1, oo),
                      meijerg=True,
                      conds='none').rewrite(expint).expand(
                          func=True))) == expint(y, z)

    assert integrate(exp(-z*x)/x, (x, 1, oo), meijerg=True,
                     conds='none').rewrite(expint).expand() == \
        expint(1, z)
    assert integrate(exp(-z*x)/x**2, (x, 1, oo), meijerg=True,
                     conds='none').rewrite(expint).expand() == \
        expint(2, z).rewrite(Ei).rewrite(expint)
    assert integrate(exp(-z*x)/x**3, (x, 1, oo), meijerg=True,
                     conds='none').rewrite(expint).expand() == \
        expint(3, z).rewrite(Ei).rewrite(expint).expand()

    t = Symbol('t', positive=True)
    assert integrate(-cos(x) / x, (x, t, oo), meijerg=True).expand() == Ci(t)
    assert integrate(-sin(x)/x, (x, t, oo), meijerg=True).expand() == \
        Si(t) - pi/2
    assert integrate(sin(x) / x, (x, 0, z), meijerg=True) == Si(z)
    assert integrate(sinh(x) / x, (x, 0, z), meijerg=True) == Shi(z)
    assert integrate(exp(-x)/x, x, meijerg=True).expand().rewrite(expint) == \
        I*pi - expint(1, x)
    assert integrate(exp(-x)/x**2, x, meijerg=True).rewrite(expint).expand() \
        == expint(1, x) - exp(-x)/x - I*pi

    u = Symbol('u', polar=True)
    assert integrate(cos(u)/u, u, meijerg=True).expand().as_independent(u)[1] \
        == Ci(u)
    assert integrate(cosh(u)/u, u, meijerg=True).expand().as_independent(u)[1] \
        == Chi(u)

    assert integrate(
        expint(1, x), x,
        meijerg=True).rewrite(expint).expand() == x * expint(1, x) - exp(-x)
    assert integrate(expint(2, x), x, meijerg=True
            ).rewrite(expint).expand() == \
        -x**2*expint(1, x)/2 + x*exp(-x)/2 - exp(-x)/2
    assert simplify(unpolarify(integrate(expint(y, x), x,
                 meijerg=True).rewrite(expint).expand(func=True))) == \
        -expint(y + 1, x)

    assert integrate(Si(x), x, meijerg=True) == x * Si(x) + cos(x)
    assert integrate(Ci(u), u, meijerg=True).expand() == u * Ci(u) - sin(u)
    assert integrate(Shi(x), x, meijerg=True) == x * Shi(x) - cosh(x)
    assert integrate(Chi(u), u, meijerg=True).expand() == u * Chi(u) - sinh(u)

    assert integrate(Si(x) * exp(-x), (x, 0, oo), meijerg=True) == pi / 4
    assert integrate(expint(1, x) * sin(x), (x, 0, oo),
                     meijerg=True) == log(2) / 2
Beispiel #3
0
def test_specfun():
    for f in [besselj, bessely, besseli, besselk]:
        assert octave_code(f(n, x)) == f.__name__ + '(n, x)'
    assert octave_code(hankel1(n, x)) == 'besselh(n, 1, x)'
    assert octave_code(hankel2(n, x)) == 'besselh(n, 2, x)'
    assert octave_code(airyai(x)) == 'airy(0, x)'
    assert octave_code(airyaiprime(x)) == 'airy(1, x)'
    assert octave_code(airybi(x)) == 'airy(2, x)'
    assert octave_code(airybiprime(x)) == 'airy(3, x)'
    assert octave_code(uppergamma(n, x)) == "gammainc(x, n, 'upper')"
    assert octave_code(lowergamma(n, x)) == "gammainc(x, n, 'lower')"
    assert octave_code(jn(
        n, x)) == 'sqrt(2)*sqrt(pi)*sqrt(1./x).*besselj(n + 1/2, x)/2'
    assert octave_code(yn(
        n, x)) == 'sqrt(2)*sqrt(pi)*sqrt(1./x).*bessely(n + 1/2, x)/2'
    assert octave_code(Chi(x)) == 'coshint(x)'
    assert octave_code(Ci(x)) == 'cosint(x)'
    assert octave_code(laguerre(n, x)) == 'laguerreL(n, x)'
    assert octave_code(li(x)) == 'logint(x)'
    assert octave_code(loggamma(x)) == 'gammaln(x)'
    assert octave_code(polygamma(n, x)) == 'psi(n, x)'
    assert octave_code(Shi(x)) == 'sinhint(x)'
    assert octave_code(Si(x)) == 'sinint(x)'
    assert octave_code(LambertW(x)) == 'lambertw(x)'
    assert octave_code(LambertW(x, n)) == 'lambertw(n, x)'
    assert octave_code(zeta(x)) == 'zeta(x)'
    assert octave_code(zeta(
        x, y)) == '% Not supported in Octave:\n% zeta\nzeta(x, y)'
def test_ei():
    pos = Symbol('p', positive=True)
    neg = Symbol('n', negative=True)
    assert Ei(0) == -oo
    assert Ei(+oo) == oo
    assert Ei(-oo) == 0
    assert Ei(-pos) == Ei(polar_lift(-1) * pos) - I * pi
    assert Ei(neg) == Ei(polar_lift(neg)) - I * pi
    assert tn_branch(Ei)
    assert mytd(Ei(x), exp(x) / x, x)
    assert mytn(Ei(x),
                Ei(x).rewrite(uppergamma),
                -uppergamma(0, x * polar_lift(-1)) - I * pi, x)
    assert mytn(Ei(x),
                Ei(x).rewrite(expint), -expint(1, x * polar_lift(-1)) - I * pi,
                x)
    assert Ei(x).rewrite(expint).rewrite(Ei) == Ei(x)
    assert Ei(x * exp_polar(2 * I * pi)) == Ei(x) + 2 * I * pi
    assert Ei(x * exp_polar(-2 * I * pi)) == Ei(x) - 2 * I * pi

    assert mytn(Ei(x), Ei(x).rewrite(Shi), Chi(x) + Shi(x), x)
    assert mytn(Ei(x * polar_lift(I)),
                Ei(x * polar_lift(I)).rewrite(Si),
                Ci(x) + I * Si(x) + I * pi / 2, x)

    assert Ei(log(x)).rewrite(li) == li(x)
    assert Ei(2 * log(x)).rewrite(li) == li(x**2)

    assert Ei(x).series(x) == (EulerGamma + log(x) + x + x**2 / 4 + x**3 / 18 +
                               x**4 / 96 + x**5 / 600 + O(x**6))
    assert Ei(1 + x).series(x) == (Ei(1) + E * x + E * x**3 / 6 -
                                   E * x**4 / 12 + 3 * E * x**5 / 40 + O(x**6))

    pytest.raises(ArgumentIndexError, lambda: Ei(x).fdiff(2))
Beispiel #5
0
def test_ei():
    pos = Symbol('p', positive=True)
    neg = Symbol('n', negative=True)
    assert Ei(-pos) == Ei(polar_lift(-1) * pos) - I * pi
    assert Ei(neg) == Ei(polar_lift(neg)) - I * pi
    assert tn_branch(Ei)
    assert mytd(Ei(x), exp(x) / x, x)
    assert mytn(Ei(x),
                Ei(x).rewrite(uppergamma),
                -uppergamma(0, x * polar_lift(-1)) - I * pi, x)
    assert mytn(Ei(x),
                Ei(x).rewrite(expint), -expint(1, x * polar_lift(-1)) - I * pi,
                x)
    assert Ei(x).rewrite(expint).rewrite(Ei) == Ei(x)
    assert Ei(x * exp_polar(2 * I * pi)) == Ei(x) + 2 * I * pi
    assert Ei(x * exp_polar(-2 * I * pi)) == Ei(x) - 2 * I * pi

    assert mytn(Ei(x), Ei(x).rewrite(Shi), Chi(x) + Shi(x), x)
    assert mytn(Ei(x * polar_lift(I)),
                Ei(x * polar_lift(I)).rewrite(Si),
                Ci(x) + I * Si(x) + I * pi / 2, x)

    assert Ei(log(x)).rewrite(li) == li(x)
    assert Ei(2 * log(x)).rewrite(li) == li(x**2)

    assert Ei(x).series(x) == EulerGamma + log(x) + x + x**2/4 + \
        x**3/18 + x**4/96 + x**5/600 + O(x**6)
Beispiel #6
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()
Beispiel #7
0
def test_expint():
    assert mytn(expint(x, y),
                expint(x, y).rewrite(uppergamma),
                y**(x - 1) * uppergamma(1 - x, y), x)
    assert mytd(expint(x, y),
                -y**(x - 1) * meijerg([], [1, 1], [0, 0, 1 - x], [], y), x)
    assert mytd(expint(x, y), -expint(x - 1, y), y)
    assert mytn(expint(1, x),
                expint(1, x).rewrite(Ei), -Ei(x * polar_lift(-1)) + I * pi, x)

    assert expint(-4, x) == exp(-x)/x + 4*exp(-x)/x**2 + 12*exp(-x)/x**3 \
        + 24*exp(-x)/x**4 + 24*exp(-x)/x**5
    assert expint(-Rational(3, 2), x) == \
        exp(-x)/x + 3*exp(-x)/(2*x**2) - 3*sqrt(pi)*erf(sqrt(x))/(4*x**Rational(5, 2)) \
        + 3*sqrt(pi)/(4*x**Rational(5, 2))

    assert tn_branch(expint, 1)
    assert tn_branch(expint, 2)
    assert tn_branch(expint, 3)
    assert tn_branch(expint, 1.7)
    assert tn_branch(expint, pi)

    assert expint(y, x*exp_polar(2*I*pi)) == \
        x**(y - 1)*(exp(2*I*pi*y) - 1)*gamma(-y + 1) + expint(y, x)
    assert expint(y, x*exp_polar(-2*I*pi)) == \
        x**(y - 1)*(exp(-2*I*pi*y) - 1)*gamma(-y + 1) + expint(y, x)
    assert expint(2,
                  x * exp_polar(2 * I * pi)) == 2 * I * pi * x + expint(2, x)
    assert expint(2, x *
                  exp_polar(-2 * I * pi)) == -2 * I * pi * x + expint(2, x)
    assert expint(1, x).rewrite(Ei).rewrite(expint) == expint(1, x)

    assert mytn(E1(x), E1(x).rewrite(Shi), Shi(x) - Chi(x), x)
    assert mytn(E1(polar_lift(I) * x),
                E1(polar_lift(I) * x).rewrite(Si),
                -Ci(x) + I * Si(x) - I * pi / 2, x)

    assert mytn(expint(2, x),
                expint(2, x).rewrite(Ei).rewrite(expint), -x * E1(x) + exp(-x),
                x)
    assert mytn(expint(3, x),
                expint(3, x).rewrite(Ei).rewrite(expint),
                x**2 * E1(x) / 2 + (1 - x) * exp(-x) / 2, x)

    assert expint(Rational(3, 2), z).nseries(z, n=10) == \
        2 + 2*z - z**2/3 + z**3/15 - z**4/84 + z**5/540 - \
        2*sqrt(pi)*sqrt(z) + O(z**6)

    assert E1(z).series(z) == -EulerGamma - log(z) + z - \
        z**2/4 + z**3/18 - z**4/96 + z**5/600 + O(z**6)

    assert expint(4, z).series(z) == Rational(1, 3) - z/2 + z**2/2 + \
        z**3*(log(z)/6 - Rational(11, 36) + EulerGamma/6) - z**4/24 + \
        z**5/240 + O(z**6)
Beispiel #8
0
def test_cosine_transform():
    from diofant import Si, Ci

    t = symbols("t")
    w = symbols("w")
    a = symbols("a")
    f = Function("f")

    # Test unevaluated form
    assert cosine_transform(f(t), t, w) == CosineTransform(f(t), t, w)
    assert inverse_cosine_transform(f(w), w,
                                    t) == InverseCosineTransform(f(w), w, t)

    assert cosine_transform(1 / sqrt(t), t, w) == 1 / sqrt(w)
    assert inverse_cosine_transform(1 / sqrt(w), w, t) == 1 / sqrt(t)

    assert cosine_transform(1 / (a**2 + t**2), t,
                            w) == sqrt(2) * sqrt(pi) * exp(-a * w) / (2 * a)

    assert cosine_transform(
        t**(-a), t, w) == 2**(-a + Rational(1, 2)) * w**(a - 1) * gamma(
            (-a + 1) / 2) / gamma(a / 2)
    assert inverse_cosine_transform(
        2**(-a + Rational(1, 2)) * w**(a - 1) *
        gamma(-a / 2 + Rational(1, 2)) / gamma(a / 2), w, t) == t**(-a)

    assert cosine_transform(exp(-a * t), t,
                            w) == sqrt(2) * a / (sqrt(pi) * (a**2 + w**2))
    assert inverse_cosine_transform(
        sqrt(2) * a / (sqrt(pi) * (a**2 + w**2)), w, t) == exp(-a * t)

    assert cosine_transform(exp(-a * sqrt(t)) * cos(a * sqrt(t)), t,
                            w) == a * exp(-a**2 /
                                          (2 * w)) / (2 * w**Rational(3, 2))

    assert cosine_transform(
        1 / (a + t), t,
        w) == sqrt(2) * ((-2 * Si(a * w) + pi) * sin(a * w) / 2 -
                         cos(a * w) * Ci(a * w)) / sqrt(pi)
    assert inverse_cosine_transform(
        sqrt(2) * meijerg(((Rational(1, 2), 0), ()),
                          ((Rational(1, 2), 0, 0),
                           (Rational(1, 2), )), a**2 * w**2 / 4) / (2 * pi), w,
        t) == 1 / (a + t)

    assert cosine_transform(1 / sqrt(a**2 + t**2), t, w) == sqrt(2) * meijerg(
        ((Rational(1, 2), ), ()),
        ((0, 0), (Rational(1, 2), )), a**2 * w**2 / 4) / (2 * sqrt(pi))
    assert inverse_cosine_transform(
        sqrt(2) * meijerg(
            ((Rational(1, 2), ), ()),
            ((0, 0), (Rational(1, 2), )), a**2 * w**2 / 4) / (2 * sqrt(pi)), w,
        t) == 1 / (a * sqrt(1 + t**2 / a**2))
Beispiel #9
0
def test_meijerg_lookup():
    assert hyperexpand(meijerg([a], [], [b, a], [], z)) == \
        z**b*exp(z)*gamma(-a + b + 1)*uppergamma(a - b, z)
    assert hyperexpand(meijerg([0], [], [0, 0], [], z)) == \
        exp(z)*uppergamma(0, z)
    assert can_do_meijer([a], [], [b, a + 1], [])
    assert can_do_meijer([a], [], [b + 2, a], [])
    assert can_do_meijer([a], [], [b - 2, a], [])

    assert hyperexpand(meijerg([a], [], [a, a, a - Rational(1, 2)], [], z)) == \
        -sqrt(pi)*z**(a - Rational(1, 2))*(2*cos(2*sqrt(z))*(Si(2*sqrt(z)) - pi/2)
                                           - 2*sin(2*sqrt(z))*Ci(2*sqrt(z))) == \
        hyperexpand(meijerg([a], [], [a, a - Rational(1, 2), a], [], z)) == \
        hyperexpand(meijerg([a], [], [a - Rational(1, 2), a, a], [], z))
    assert can_do_meijer([a - 1], [], [a + 2, a - Rational(3, 2), a + 1], [])
def test_ci():
    m1 = exp_polar(I * pi)
    m1_ = exp_polar(-I * pi)
    pI = exp_polar(I * pi / 2)
    mI = exp_polar(-I * pi / 2)

    assert Ci(m1 * x) == Ci(x) + I * pi
    assert Ci(m1_ * x) == Ci(x) - I * pi
    assert Ci(pI * x) == Chi(x) + I * pi / 2
    assert Ci(mI * x) == Chi(x) - I * pi / 2
    assert Chi(m1 * x) == Chi(x) + I * pi
    assert Chi(m1_ * x) == Chi(x) - I * pi
    assert Chi(pI * x) == Ci(x) + I * pi / 2
    assert Chi(mI * x) == Ci(x) - I * pi / 2
    assert Ci(exp_polar(2 * I * pi) * x) == Ci(x) + 2 * I * pi
    assert Chi(exp_polar(-2 * I * pi) * x) == Chi(x) - 2 * I * pi
    assert Chi(exp_polar(2 * I * pi) * x) == Chi(x) + 2 * I * pi
    assert Ci(exp_polar(-2 * I * pi) * x) == Ci(x) - 2 * I * pi

    assert Ci(oo) == 0
    assert Ci(-oo) == I * pi
    assert Chi(oo) == oo
    assert Chi(-oo) == oo

    assert mytd(Ci(x), cos(x) / x, x)
    assert mytd(Chi(x), cosh(x) / x, x)

    assert mytn(
        Ci(x),
        Ci(x).rewrite(Ei),
        Ei(x * exp_polar(-I * pi / 2)) / 2 + Ei(x * exp_polar(I * pi / 2)) / 2,
        x)
    assert mytn(Chi(x),
                Chi(x).rewrite(Ei),
                Ei(x) / 2 + Ei(x * exp_polar(I * pi)) / 2 - I * pi / 2, x)

    assert tn_arg(Ci)
    assert tn_arg(Chi)

    assert Ci(x).nseries(x, n=4) == \
        EulerGamma + log(x) - x**2/4 + x**4/96 + O(x**6)
    assert Chi(x).nseries(x, n=4) == \
        EulerGamma + log(x) + x**2/4 + x**4/96 + O(x**6)
    assert limit(log(x) - Ci(2 * x), x, 0) == -log(2) - EulerGamma
Beispiel #11
0
def test_limit_bug():
    z = Symbol('z', nonzero=True)
    assert integrate(sin(x*y*z), (x, 0, pi), (y, 0, pi)) == \
        (log(z**2) + 2*EulerGamma + 2*log(pi))/(2*z) - \
        (-log(pi*z) + log(pi**2*z**2)/2 + Ci(pi**2*z))/z + log(pi)/z