Exemple #1
0
def test_bessel_eval():
    n, m, k = Symbol('n', integer=True), Symbol('m'), Symbol('k',
                                                             integer=True,
                                                             zero=False)

    for f in [besselj, besseli]:
        assert f(0, 0) == 1
        assert f(2.1, 0) == 0
        assert f(-3, 0) == 0
        assert f(-10.2, 0) == zoo
        assert f(1 + 3 * I, 0) == 0
        assert f(-3 + I, 0) == zoo
        assert f(-2 * I, 0) == nan
        assert f(n, 0) not in (0, 1)
        assert f(m, 0) not in (0, 1)
        assert f(k, 0) == 0

    assert bessely(0, 0) == -oo
    assert besselk(0, 0) == +oo
    for f in [bessely, besselk]:
        assert f(1 + I, 0) == zoo
        assert f(I, 0) == nan

    for x in (oo, -oo, I * oo, -I * oo):
        assert besselk(m, x) == 0

    for f in [besselj, bessely]:
        assert f(m, +oo) == 0
        assert f(m, -oo) == 0

    for f in [besseli, besselk]:
        assert f(m, +I * oo) == 0
        assert f(m, -I * oo) == 0

    for f in [besseli, besselk]:
        assert f(-4, z) == f(4, z)
        assert f(-3, z) == f(3, z)
        assert f(-n, z) == f(n, z)
        assert f(-m, z) != f(m, z)

    for f in [besselj, bessely]:
        assert f(-4, z) == f(4, z)
        assert f(-3, z) == -f(3, z)
        assert f(-n, z) == (-1)**n * f(n, z)
        assert f(-m, z) != (-1)**m * f(m, z)

    for f in [besselj, besseli]:
        assert f(m, -z) == (-z)**m * z**(-m) * f(m, z)

    assert besseli(2, -z) == besseli(2, z)
    assert besseli(3, -z) == -besseli(3, z)

    assert besselj(0, -z) == besselj(0, z)
    assert besselj(1, -z) == -besselj(1, z)

    assert besseli(0, I * z) == besselj(0, z)
    assert besseli(1, I * z) == I * besselj(1, z)
    assert besselj(3, I * z) == -I * besseli(3, z)
Exemple #2
0
def test_diff():
    assert besselj(n,
                   z).diff(z) == besselj(n - 1, z) / 2 - besselj(n + 1, z) / 2
    assert bessely(n,
                   z).diff(z) == bessely(n - 1, z) / 2 - bessely(n + 1, z) / 2
    assert besseli(n,
                   z).diff(z) == besseli(n - 1, z) / 2 + besseli(n + 1, z) / 2
    assert besselk(n,
                   z).diff(z) == -besselk(n - 1, z) / 2 - besselk(n + 1, z) / 2
    assert hankel1(n,
                   z).diff(z) == hankel1(n - 1, z) / 2 - hankel1(n + 1, z) / 2
    assert hankel2(n,
                   z).diff(z) == hankel2(n - 1, z) / 2 - hankel2(n + 1, z) / 2
Exemple #3
0
def test_diff():
    assert besselj(n, z).diff(z) == besselj(n - 1, z)/2 - besselj(n + 1, z)/2
    assert bessely(n, z).diff(z) == bessely(n - 1, z)/2 - bessely(n + 1, z)/2
    assert besseli(n, z).diff(z) == besseli(n - 1, z)/2 + besseli(n + 1, z)/2
    assert besselk(n, z).diff(z) == -besselk(n - 1, z)/2 - besselk(n + 1, z)/2
    assert hankel1(n, z).diff(z) == hankel1(n - 1, z)/2 - hankel1(n + 1, z)/2
    assert hankel2(n, z).diff(z) == hankel2(n - 1, z)/2 - hankel2(n + 1, z)/2

    pytest.raises(ArgumentIndexError, lambda: besselj(n, z).fdiff(3))
    pytest.raises(ArgumentIndexError, lambda: jn(n, z).fdiff(3))
    pytest.raises(ArgumentIndexError, lambda: airyai(z).fdiff(2))
    pytest.raises(ArgumentIndexError, lambda: airybi(z).fdiff(2))
    pytest.raises(ArgumentIndexError, lambda: airyaiprime(z).fdiff(2))
    pytest.raises(ArgumentIndexError, lambda: airybiprime(z).fdiff(2))
Exemple #4
0
def test_diff():
    assert besselj(n, z).diff(z) == besselj(n - 1, z)/2 - besselj(n + 1, z)/2
    assert bessely(n, z).diff(z) == bessely(n - 1, z)/2 - bessely(n + 1, z)/2
    assert besseli(n, z).diff(z) == besseli(n - 1, z)/2 + besseli(n + 1, z)/2
    assert besselk(n, z).diff(z) == -besselk(n - 1, z)/2 - besselk(n + 1, z)/2
    assert hankel1(n, z).diff(z) == hankel1(n - 1, z)/2 - hankel1(n + 1, z)/2
    assert hankel2(n, z).diff(z) == hankel2(n - 1, z)/2 - hankel2(n + 1, z)/2

    pytest.raises(ArgumentIndexError, lambda: besselj(n, z).fdiff(3))
    pytest.raises(ArgumentIndexError, lambda: jn(n, z).fdiff(3))
    pytest.raises(ArgumentIndexError, lambda: airyai(z).fdiff(2))
    pytest.raises(ArgumentIndexError, lambda: airybi(z).fdiff(2))
    pytest.raises(ArgumentIndexError, lambda: airyaiprime(z).fdiff(2))
    pytest.raises(ArgumentIndexError, lambda: airybiprime(z).fdiff(2))
Exemple #5
0
def test_bessel_eval():
    from diofant import I, Symbol
    n, m, k = Symbol('n', integer=True), Symbol('m'), Symbol('k',
                                                             integer=True,
                                                             zero=False)

    for f in [besselj, besseli]:
        assert f(0, 0) == S.One
        assert f(2.1, 0) == S.Zero
        assert f(-3, 0) == S.Zero
        assert f(-10.2, 0) == S.ComplexInfinity
        assert f(1 + 3 * I, 0) == S.Zero
        assert f(-3 + I, 0) == S.ComplexInfinity
        assert f(-2 * I, 0) == S.NaN
        assert f(n, 0) != S.One and f(n, 0) != S.Zero
        assert f(m, 0) != S.One and f(m, 0) != S.Zero
        assert f(k, 0) == S.Zero

    assert bessely(0, 0) == S.NegativeInfinity
    assert besselk(0, 0) == S.Infinity
    for f in [bessely, besselk]:
        assert f(1 + I, 0) == S.ComplexInfinity
        assert f(I, 0) == S.NaN

    for f in [besselj, bessely]:
        assert f(m, S.Infinity) == S.Zero
        assert f(m, S.NegativeInfinity) == S.Zero

    for f in [besseli, besselk]:
        assert f(m, I * S.Infinity) == S.Zero
        assert f(m, I * S.NegativeInfinity) == S.Zero

    for f in [besseli, besselk]:
        assert f(-4, z) == f(4, z)
        assert f(-3, z) == f(3, z)
        assert f(-n, z) == f(n, z)
        assert f(-m, z) != f(m, z)

    for f in [besselj, bessely]:
        assert f(-4, z) == f(4, z)
        assert f(-3, z) == -f(3, z)
        assert f(-n, z) == (-1)**n * f(n, z)
        assert f(-m, z) != (-1)**m * f(m, z)

    for f in [besselj, besseli]:
        assert f(m, -z) == (-z)**m * z**(-m) * f(m, z)

    assert besseli(2, -z) == besseli(2, z)
    assert besseli(3, -z) == -besseli(3, z)

    assert besselj(0, -z) == besselj(0, z)
    assert besselj(1, -z) == -besselj(1, z)

    assert besseli(0, I * z) == besselj(0, z)
    assert besseli(1, I * z) == I * besselj(1, z)
    assert besselj(3, I * z) == -I * besseli(3, z)
Exemple #6
0
def test_bessel_eval():
    n, m, k = Symbol('n', integer=True), Symbol('m'), Symbol('k', integer=True, zero=False)

    for f in [besselj, besseli]:
        assert f(0, 0) == 1
        assert f(2.1, 0) == 0
        assert f(-3, 0) == 0
        assert f(-10.2, 0) == zoo
        assert f(1 + 3*I, 0) == 0
        assert f(-3 + I, 0) == zoo
        assert f(-2*I, 0) == nan
        assert f(n, 0) not in (0, 1)
        assert f(m, 0) not in (0, 1)
        assert f(k, 0) == 0

    assert bessely(0, 0) == -oo
    assert besselk(0, 0) == +oo
    for f in [bessely, besselk]:
        assert f(1 + I, 0) == zoo
        assert f(I, 0) == nan

    for f in [besselj, bessely]:
        assert f(m, +oo) == 0
        assert f(m, -oo) == 0

    for f in [besseli, besselk]:
        assert f(m, +I*oo) == 0
        assert f(m, -I*oo) == 0

    for f in [besseli, besselk]:
        assert f(-4, z) == f(4, z)
        assert f(-3, z) == f(3, z)
        assert f(-n, z) == f(n, z)
        assert f(-m, z) != f(m, z)

    for f in [besselj, bessely]:
        assert f(-4, z) == f(4, z)
        assert f(-3, z) == -f(3, z)
        assert f(-n, z) == (-1)**n*f(n, z)
        assert f(-m, z) != (-1)**m*f(m, z)

    for f in [besselj, besseli]:
        assert f(m, -z) == (-z)**m*z**(-m)*f(m, z)

    assert besseli(2, -z) == besseli(2, z)
    assert besseli(3, -z) == -besseli(3, z)

    assert besselj(0, -z) == besselj(0, z)
    assert besselj(1, -z) == -besselj(1, z)

    assert besseli(0, I*z) == besselj(0, z)
    assert besseli(1, I*z) == I*besselj(1, z)
    assert besselj(3, I*z) == -I*besseli(3, z)
Exemple #7
0
def test_rewrite():
    from diofant import polar_lift, exp, I

    assert besselj(
        n, z).rewrite(jn) == sqrt(2 * z / pi) * jn(n - Rational(1, 2), z)
    assert bessely(
        n, z).rewrite(yn) == sqrt(2 * z / pi) * yn(n - Rational(1, 2), z)
    assert besseli(n, z).rewrite(besselj) == \
        exp(-I*n*pi/2)*besselj(n, polar_lift(I)*z)
    assert besselj(n, z).rewrite(besseli) == \
        exp(I*n*pi/2)*besseli(n, polar_lift(-I)*z)

    nu = randcplx()

    assert tn(besselj(nu, z), besselj(nu, z).rewrite(besseli), z)
    assert tn(besselj(nu, z), besselj(nu, z).rewrite(bessely), z)

    assert tn(besseli(nu, z), besseli(nu, z).rewrite(besselj), z)
    assert tn(besseli(nu, z), besseli(nu, z).rewrite(bessely), z)

    assert tn(bessely(nu, z), bessely(nu, z).rewrite(besselj), z)
    assert tn(bessely(nu, z), bessely(nu, z).rewrite(besseli), z)

    assert tn(besselk(nu, z), besselk(nu, z).rewrite(besselj), z)
    assert tn(besselk(nu, z), besselk(nu, z).rewrite(besseli), z)
    assert tn(besselk(nu, z), besselk(nu, z).rewrite(bessely), z)
Exemple #8
0
def test_mellin_transform_bessel():
    from diofant import Max
    MT = mellin_transform

    # 8.4.19
    assert MT(besselj(a, 2*sqrt(x)), x, s) == \
        (gamma(a/2 + s)/gamma(a/2 - s + 1), (-re(a)/2, Rational(3, 4)), True)
    assert MT(sin(sqrt(x))*besselj(a, sqrt(x)), x, s) == \
        (2**a*gamma(-2*s + Rational(1, 2))*gamma(a/2 + s + Rational(1, 2))/(
        gamma(-a/2 - s + 1)*gamma(a - 2*s + 1)), (
        -re(a)/2 - Rational(1, 2), Rational(1, 4)), True)
    assert MT(cos(sqrt(x))*besselj(a, sqrt(x)), x, s) == \
        (2**a*gamma(a/2 + s)*gamma(-2*s + Rational(1, 2))/(
        gamma(-a/2 - s + Rational(1, 2))*gamma(a - 2*s + 1)), (
        -re(a)/2, Rational(1, 4)), True)
    assert MT(besselj(a, sqrt(x))**2, x, s) == \
        (gamma(a + s)*gamma(Rational(1, 2) - s)
         / (sqrt(pi)*gamma(1 - s)*gamma(1 + a - s)),
            (-re(a), Rational(1, 2)), True)
    assert MT(besselj(a, sqrt(x))*besselj(-a, sqrt(x)), x, s) == \
        (gamma(s)*gamma(Rational(1, 2) - s)
         / (sqrt(pi)*gamma(1 - a - s)*gamma(1 + a - s)),
            (0, Rational(1, 2)), True)
    # NOTE: prudnikov gives the strip below as (1/2 - re(a), 1). As far as
    #       I can see this is wrong (since besselj(z) ~ 1/sqrt(z) for z large)
    assert MT(besselj(a - 1, sqrt(x))*besselj(a, sqrt(x)), x, s) == \
        (gamma(1 - s)*gamma(a + s - Rational(1, 2))
         / (sqrt(pi)*gamma(Rational(3, 2) - s)*gamma(a - s + Rational(1, 2))),
            (Rational(1, 2) - re(a), Rational(1, 2)), True)
    assert MT(besselj(a, sqrt(x))*besselj(b, sqrt(x)), x, s) == \
        (4**s*gamma(1 - 2*s)*gamma((a + b)/2 + s)
         / (gamma(1 - s + (b - a)/2)*gamma(1 - s + (a - b)/2)
            * gamma( 1 - s + (a + b)/2)),
            (-(re(a) + re(b))/2, Rational(1, 2)), True)
    assert MT(besselj(a, sqrt(x))**2 + besselj(-a, sqrt(x))**2, x, s)[1:] == \
        ((Max(re(a), -re(a)), Rational(1, 2)), True)

    # Section 8.4.20
    assert MT(bessely(a, 2*sqrt(x)), x, s) == \
        (-cos(pi*(a/2 - s))*gamma(s - a/2)*gamma(s + a/2)/pi,
            (Max(-re(a)/2, re(a)/2), Rational(3, 4)), True)
    assert MT(sin(sqrt(x))*bessely(a, sqrt(x)), x, s) == \
        (-4**s*sin(pi*(a/2 - s))*gamma(Rational(1, 2) - 2*s)
         * gamma((1 - a)/2 + s)*gamma((1 + a)/2 + s)
         / (sqrt(pi)*gamma(1 - s - a/2)*gamma(1 - s + a/2)),
            (Max(-(re(a) + 1)/2, (re(a) - 1)/2), Rational(1, 4)), True)
    assert MT(cos(sqrt(x))*bessely(a, sqrt(x)), x, s) == \
        (-4**s*cos(pi*(a/2 - s))*gamma(s - a/2)*gamma(s + a/2)*gamma(Rational(1, 2) - 2*s)
         / (sqrt(pi)*gamma(Rational(1, 2) - s - a/2)*gamma(Rational(1, 2) - s + a/2)),
            (Max(-re(a)/2, re(a)/2), Rational(1, 4)), True)
    assert MT(besselj(a, sqrt(x))*bessely(a, sqrt(x)), x, s) == \
        (-cos(pi*s)*gamma(s)*gamma(a + s)*gamma(Rational(1, 2) - s)
         / (pi**Rational(3, 2)*gamma(1 + a - s)),
            (Max(-re(a), 0), Rational(1, 2)), True)
    assert MT(besselj(a, sqrt(x))*bessely(b, sqrt(x)), x, s) == \
        (-4**s*cos(pi*(a/2 - b/2 + s))*gamma(1 - 2*s)
         * gamma(a/2 - b/2 + s)*gamma(a/2 + b/2 + s)
         / (pi*gamma(a/2 - b/2 - s + 1)*gamma(a/2 + b/2 - s + 1)),
            (Max((-re(a) + re(b))/2, (-re(a) - re(b))/2), Rational(1, 2)), True)
    # NOTE bessely(a, sqrt(x))**2 and bessely(a, sqrt(x))*bessely(b, sqrt(x))
    # are a mess (no matter what way you look at it ...)
    assert MT(bessely(a, sqrt(x))**2, x, s)[1:] == \
             ((Max(-re(a), 0, re(a)), Rational(1, 2)), True)

    # Section 8.4.22
    # TODO we can't do any of these (delicate cancellation)

    # Section 8.4.23
    assert MT(besselk(a, 2*sqrt(x)), x, s) == \
        (gamma(
         s - a/2)*gamma(s + a/2)/2, (Max(-re(a)/2, re(a)/2), oo), True)
    assert MT(
        besselj(a, 2 * sqrt(2 * sqrt(x))) * besselk(a, 2 * sqrt(2 * sqrt(x))),
        x, s) == (4**(-s) * gamma(2 * s) * gamma(a / 2 + s) /
                  (2 * gamma(a / 2 - s + 1)), (Max(0, -re(a) / 2), oo), True)
    # TODO bessely(a, x)*besselk(a, x) is a mess
    assert MT(besseli(a, sqrt(x))*besselk(a, sqrt(x)), x, s) == \
        (gamma(s)*gamma(
        a + s)*gamma(-s + Rational(1, 2))/(2*sqrt(pi)*gamma(a - s + 1)),
        (Max(-re(a), 0), Rational(1, 2)), True)
    assert MT(besseli(b, sqrt(x))*besselk(a, sqrt(x)), x, s) == \
        (2**(2*s - 1)*gamma(-2*s + 1)*gamma(-a/2 + b/2 + s) *
        gamma(a/2 + b/2 + s)/(gamma(-a/2 + b/2 - s + 1) *
        gamma(a/2 + b/2 - s + 1)), (Max(-re(a)/2 - re(b)/2,
        re(a)/2 - re(b)/2), Rational(1, 2)), True)

    # TODO products of besselk are a mess

    mt = MT(exp(-x / 2) * besselk(a, x / 2), x, s)
    mt0 = combsimp((trigsimp(combsimp(mt[0].expand(func=True)))))
    assert mt0 == 2 * pi**Rational(3, 2) * cos(
        pi * s) * gamma(-s + Rational(1, 2)) / (
            (cos(2 * pi * a) - cos(2 * pi * s)) * gamma(-a - s + 1) *
            gamma(a - s + 1))
    assert mt[1:] == ((Max(-re(a), re(a)), oo), True)
Exemple #9
0
def test_pole():
    pytest.raises(PoleError, lambda: sin(1 / x).series(x, 0, 5))
    pytest.raises(PoleError, lambda: sin(1 + 1 / x).series(x, 0, 5))
    pytest.raises(PoleError, lambda: (x * sin(1 / x)).series(x, 0, 5))
    pytest.raises(PoleError, lambda: besselk(0, x).series(x, 0, 2))
Exemple #10
0
def test_pole():
    pytest.raises(PoleError, lambda: sin(1/x).series(x, 0, 5))
    pytest.raises(PoleError, lambda: sin(1 + 1/x).series(x, 0, 5))
    pytest.raises(PoleError, lambda: (x*sin(1/x)).series(x, 0, 5))
    pytest.raises(PoleError, lambda: besselk(0, x).series(x, 0, 2))
Exemple #11
0
def test_expand():
    assert expand_func(besselj(Rational(1, 2), z).rewrite(jn)) == \
        sqrt(2)*sin(z)/(sqrt(pi)*sqrt(z))
    assert expand_func(bessely(Rational(1, 2), z).rewrite(yn)) == \
        -sqrt(2)*cos(z)/(sqrt(pi)*sqrt(z))
    assert expand_func(besselj(I, z)) == besselj(I, z)

    # Test simplify helper
    assert simplify(besselj(Rational(1, 2),
                            z)) == sqrt(2) * sin(z) / (sqrt(pi) * sqrt(z))

    # XXX: teach sin/cos to work around arguments like
    # x*exp_polar(I*pi*n/2).  Then change besselsimp -> expand_func
    assert besselsimp(besselj(Rational(1, 2),
                              z)) == sqrt(2) * sin(z) / (sqrt(pi) * sqrt(z))
    assert besselsimp(besselj(Rational(-1, 2),
                              z)) == sqrt(2) * cos(z) / (sqrt(pi) * sqrt(z))
    assert besselsimp(besselj(Rational(5, 2), z)) == \
        -sqrt(2)*(z**2*sin(z) + 3*z*cos(z) - 3*sin(z))/(sqrt(pi)*z**Rational(5, 2))
    assert besselsimp(besselj(-Rational(5, 2), z)) == \
        -sqrt(2)*(z**2*cos(z) - 3*z*sin(z) - 3*cos(z))/(sqrt(pi)*z**Rational(5, 2))

    assert besselsimp(bessely(Rational(1, 2), z)) == \
        -(sqrt(2)*cos(z))/(sqrt(pi)*sqrt(z))
    assert besselsimp(bessely(Rational(-1, 2),
                              z)) == sqrt(2) * sin(z) / (sqrt(pi) * sqrt(z))
    assert besselsimp(bessely(Rational(5, 2), z)) == \
        sqrt(2)*(z**2*cos(z) - 3*z*sin(z) - 3*cos(z))/(sqrt(pi)*z**Rational(5, 2))
    assert besselsimp(bessely(Rational(-5, 2), z)) == \
        -sqrt(2)*(z**2*sin(z) + 3*z*cos(z) - 3*sin(z))/(sqrt(pi)*z**Rational(5, 2))

    assert besselsimp(besseli(Rational(1, 2),
                              z)) == sqrt(2) * sinh(z) / (sqrt(pi) * sqrt(z))
    assert besselsimp(besseli(Rational(-1, 2), z)) == \
        sqrt(2)*cosh(z)/(sqrt(pi)*sqrt(z))
    assert besselsimp(besseli(Rational(5, 2), z)) == \
        sqrt(2)*(z**2*sinh(z) - 3*z*cosh(z) + 3*sinh(z))/(sqrt(pi)*z**Rational(5, 2))
    assert besselsimp(besseli(Rational(-5, 2), z)) == \
        sqrt(2)*(z**2*cosh(z) - 3*z*sinh(z) + 3*cosh(z))/(sqrt(pi)*z**Rational(5, 2))

    assert besselsimp(besselk(Rational(1, 2), z)) == \
        besselsimp(besselk(Rational(-1, 2), z)) == sqrt(pi)*exp(-z)/(sqrt(2)*sqrt(z))
    assert besselsimp(besselk(Rational(5, 2), z)) == \
        besselsimp(besselk(Rational(-5, 2), z)) == \
        sqrt(2)*sqrt(pi)*(z**2 + 3*z + 3)*exp(-z)/(2*z**Rational(5, 2))

    def check(eq, ans):
        return tn(eq, ans) and eq == ans

    rn = randcplx(a=1, b=0, d=0, c=2)

    for besselx in [besselj, bessely, besseli, besselk]:
        ri = Rational(2 * randint(-11, 10) + 1,
                      2)  # half integer in [-21/2, 21/2]
        assert tn(besselsimp(besselx(ri, z)), besselx(ri, z))

    assert check(expand_func(besseli(rn, x)),
                 besseli(rn - 2, x) - 2 * (rn - 1) * besseli(rn - 1, x) / x)
    assert check(expand_func(besseli(-rn, x)),
                 besseli(-rn + 2, x) + 2 * (-rn + 1) * besseli(-rn + 1, x) / x)

    assert check(expand_func(besselj(rn, x)),
                 -besselj(rn - 2, x) + 2 * (rn - 1) * besselj(rn - 1, x) / x)
    assert check(
        expand_func(besselj(-rn, x)),
        -besselj(-rn + 2, x) + 2 * (-rn + 1) * besselj(-rn + 1, x) / x)

    assert check(expand_func(besselk(rn, x)),
                 besselk(rn - 2, x) + 2 * (rn - 1) * besselk(rn - 1, x) / x)
    assert check(expand_func(besselk(-rn, x)),
                 besselk(-rn + 2, x) - 2 * (-rn + 1) * besselk(-rn + 1, x) / x)

    assert check(expand_func(bessely(rn, x)),
                 -bessely(rn - 2, x) + 2 * (rn - 1) * bessely(rn - 1, x) / x)
    assert check(
        expand_func(bessely(-rn, x)),
        -bessely(-rn + 2, x) + 2 * (-rn + 1) * bessely(-rn + 1, x) / x)

    n = Symbol('n', integer=True, positive=True)

    assert expand_func(besseli(n + 2, z)) == \
        besseli(n, z) + (-2*n - 2)*(-2*n*besseli(n, z)/z + besseli(n - 1, z))/z
    assert expand_func(besselj(n + 2, z)) == \
        -besselj(n, z) + (2*n + 2)*(2*n*besselj(n, z)/z - besselj(n - 1, z))/z
    assert expand_func(besselk(n + 2, z)) == \
        besselk(n, z) + (2*n + 2)*(2*n*besselk(n, z)/z + besselk(n - 1, z))/z
    assert expand_func(bessely(n + 2, z)) == \
        -bessely(n, z) + (2*n + 2)*(2*n*bessely(n, z)/z - bessely(n - 1, z))/z

    assert expand_func(besseli(n + Rational(1, 2), z).rewrite(jn)) == \
        (sqrt(2)*sqrt(z)*exp(-I*pi*(n + Rational(1, 2))/2) *
         exp_polar(I*pi/4)*jn(n, z*exp_polar(I*pi/2))/sqrt(pi))
    assert expand_func(besselj(n + Rational(1, 2), z).rewrite(jn)) == \
        sqrt(2)*sqrt(z)*jn(n, z)/sqrt(pi)

    r = Symbol('r', extended_real=True)
    p = Symbol('p', positive=True)
    i = Symbol('i', integer=True)

    for besselx in [besselj, bessely, besseli, besselk]:
        assert besselx(i, p).is_extended_real
        assert besselx(i, x).is_extended_real is None
        assert besselx(x, z).is_extended_real is None

    for besselx in [besselj, besseli]:
        assert besselx(i, r).is_extended_real
    for besselx in [bessely, besselk]:
        assert besselx(i, r).is_extended_real is None
Exemple #12
0
def test_sympyissue_14874():
    assert limit(besselk(0, x), x, oo) == 0
Exemple #13
0
def test_expand():
    assert expand_func(besselj(Rational(1, 2), z).rewrite(jn)) == \
        sqrt(2)*sin(z)/(sqrt(pi)*sqrt(z))
    assert expand_func(bessely(Rational(1, 2), z).rewrite(yn)) == \
        -sqrt(2)*cos(z)/(sqrt(pi)*sqrt(z))
    assert expand_func(besselj(I, z)) == besselj(I, z)

    # Test simplify helper
    assert simplify(besselj(Rational(1, 2), z)) == sqrt(2)*sin(z)/(sqrt(pi)*sqrt(z))

    # XXX: teach sin/cos to work around arguments like
    # x*exp_polar(I*pi*n/2).  Then change besselsimp -> expand_func
    assert besselsimp(besselj(Rational(1, 2), z)) == sqrt(2)*sin(z)/(sqrt(pi)*sqrt(z))
    assert besselsimp(besselj(Rational(-1, 2), z)) == sqrt(2)*cos(z)/(sqrt(pi)*sqrt(z))
    assert besselsimp(besselj(Rational(5, 2), z)) == \
        -sqrt(2)*(z**2*sin(z) + 3*z*cos(z) - 3*sin(z))/(sqrt(pi)*z**Rational(5, 2))
    assert besselsimp(besselj(-Rational(5, 2), z)) == \
        -sqrt(2)*(z**2*cos(z) - 3*z*sin(z) - 3*cos(z))/(sqrt(pi)*z**Rational(5, 2))

    assert besselsimp(bessely(Rational(1, 2), z)) == \
        -(sqrt(2)*cos(z))/(sqrt(pi)*sqrt(z))
    assert besselsimp(bessely(Rational(-1, 2), z)) == sqrt(2)*sin(z)/(sqrt(pi)*sqrt(z))
    assert besselsimp(bessely(Rational(5, 2), z)) == \
        sqrt(2)*(z**2*cos(z) - 3*z*sin(z) - 3*cos(z))/(sqrt(pi)*z**Rational(5, 2))
    assert besselsimp(bessely(Rational(-5, 2), z)) == \
        -sqrt(2)*(z**2*sin(z) + 3*z*cos(z) - 3*sin(z))/(sqrt(pi)*z**Rational(5, 2))

    assert besselsimp(besseli(Rational(1, 2), z)) == sqrt(2)*sinh(z)/(sqrt(pi)*sqrt(z))
    assert besselsimp(besseli(Rational(-1, 2), z)) == \
        sqrt(2)*cosh(z)/(sqrt(pi)*sqrt(z))
    assert besselsimp(besseli(Rational(5, 2), z)) == \
        sqrt(2)*(z**2*sinh(z) - 3*z*cosh(z) + 3*sinh(z))/(sqrt(pi)*z**Rational(5, 2))
    assert besselsimp(besseli(Rational(-5, 2), z)) == \
        sqrt(2)*(z**2*cosh(z) - 3*z*sinh(z) + 3*cosh(z))/(sqrt(pi)*z**Rational(5, 2))

    assert besselsimp(besselk(Rational(1, 2), z)) == \
        besselsimp(besselk(Rational(-1, 2), z)) == sqrt(pi)*exp(-z)/(sqrt(2)*sqrt(z))
    assert besselsimp(besselk(Rational(5, 2), z)) == \
        besselsimp(besselk(Rational(-5, 2), z)) == \
        sqrt(2)*sqrt(pi)*(z**2 + 3*z + 3)*exp(-z)/(2*z**Rational(5, 2))

    def check(eq, ans):
        return tn(eq, ans) and eq == ans

    rn = randcplx(a=1, b=0, d=0, c=2)

    for besselx in [besselj, bessely, besseli, besselk]:
        ri = Rational(2*randint(-11, 10) + 1, 2)  # half integer in [-21/2, 21/2]
        assert tn(besselsimp(besselx(ri, z)), besselx(ri, z))

    assert check(expand_func(besseli(rn, x)),
                 besseli(rn - 2, x) - 2*(rn - 1)*besseli(rn - 1, x)/x)
    assert check(expand_func(besseli(-rn, x)),
                 besseli(-rn + 2, x) + 2*(-rn + 1)*besseli(-rn + 1, x)/x)

    assert check(expand_func(besselj(rn, x)),
                 -besselj(rn - 2, x) + 2*(rn - 1)*besselj(rn - 1, x)/x)
    assert check(expand_func(besselj(-rn, x)),
                 -besselj(-rn + 2, x) + 2*(-rn + 1)*besselj(-rn + 1, x)/x)

    assert check(expand_func(besselk(rn, x)),
                 besselk(rn - 2, x) + 2*(rn - 1)*besselk(rn - 1, x)/x)
    assert check(expand_func(besselk(-rn, x)),
                 besselk(-rn + 2, x) - 2*(-rn + 1)*besselk(-rn + 1, x)/x)

    assert check(expand_func(bessely(rn, x)),
                 -bessely(rn - 2, x) + 2*(rn - 1)*bessely(rn - 1, x)/x)
    assert check(expand_func(bessely(-rn, x)),
                 -bessely(-rn + 2, x) + 2*(-rn + 1)*bessely(-rn + 1, x)/x)

    n = Symbol('n', integer=True, positive=True)

    assert expand_func(besseli(n + 2, z)) == \
        besseli(n, z) + (-2*n - 2)*(-2*n*besseli(n, z)/z + besseli(n - 1, z))/z
    assert expand_func(besselj(n + 2, z)) == \
        -besselj(n, z) + (2*n + 2)*(2*n*besselj(n, z)/z - besselj(n - 1, z))/z
    assert expand_func(besselk(n + 2, z)) == \
        besselk(n, z) + (2*n + 2)*(2*n*besselk(n, z)/z + besselk(n - 1, z))/z
    assert expand_func(bessely(n + 2, z)) == \
        -bessely(n, z) + (2*n + 2)*(2*n*bessely(n, z)/z - bessely(n - 1, z))/z

    assert expand_func(besseli(n + Rational(1, 2), z).rewrite(jn)) == \
        (sqrt(2)*sqrt(z)*exp(-I*pi*(n + Rational(1, 2))/2) *
         exp_polar(I*pi/4)*jn(n, z*exp_polar(I*pi/2))/sqrt(pi))
    assert expand_func(besselj(n + Rational(1, 2), z).rewrite(jn)) == \
        sqrt(2)*sqrt(z)*jn(n, z)/sqrt(pi)

    r = Symbol('r', extended_real=True)
    p = Symbol('p', positive=True)
    i = Symbol('i', integer=True)

    for besselx in [besselj, bessely, besseli, besselk]:
        assert besselx(i, p).is_extended_real
        assert besselx(i, x).is_extended_real is None
        assert besselx(x, z).is_extended_real is None

    for besselx in [besselj, besseli]:
        assert besselx(i, r).is_extended_real
    for besselx in [bessely, besselk]:
        assert besselx(i, r).is_extended_real is None
Exemple #14
0
def test_rewrite():
    assert besselj(n, z).rewrite(jn) == sqrt(2*z/pi)*jn(n - Rational(1, 2), z)
    assert bessely(n, z).rewrite(yn) == sqrt(2*z/pi)*yn(n - Rational(1, 2), z)
    assert besseli(n, z).rewrite(besselj) == \
        exp(-I*n*pi/2)*besselj(n, polar_lift(I)*z)
    assert besselj(n, z).rewrite(besseli) == \
        exp(I*n*pi/2)*besseli(n, polar_lift(-I)*z)
    assert besselj(2, z).rewrite(bessely) == besselj(2, z)
    assert bessely(2, z).rewrite(besselj) == bessely(2, z)
    assert bessely(2, z).rewrite(besseli) == bessely(2, z)
    assert besselk(2, z).rewrite(besseli) == besselk(2, z)
    assert besselk(2, z).rewrite(besselj) == besselk(2, z)
    assert besselk(2, z).rewrite(bessely) == besselk(2, z)

    nu = randcplx()

    assert tn(besselj(nu, z), besselj(nu, z).rewrite(besseli), z)
    assert tn(besselj(nu, z), besselj(nu, z).rewrite(bessely), z)

    assert tn(besseli(nu, z), besseli(nu, z).rewrite(besselj), z)
    assert tn(besseli(nu, z), besseli(nu, z).rewrite(bessely), z)

    assert tn(bessely(nu, z), bessely(nu, z).rewrite(besselj), z)
    assert tn(bessely(nu, z), bessely(nu, z).rewrite(besseli), z)

    assert tn(besselk(nu, z), besselk(nu, z).rewrite(besselj), z)
    assert tn(besselk(nu, z), besselk(nu, z).rewrite(besseli), z)
    assert tn(besselk(nu, z), besselk(nu, z).rewrite(bessely), z)
Exemple #15
0
def test_mellin_transform_bessel():
    MT = mellin_transform

    # 8.4.19
    assert MT(besselj(a, 2*sqrt(x)), x, s) == \
        (gamma(a/2 + s)/gamma(a/2 - s + 1), (-re(a)/2, Rational(3, 4)), True)
    assert MT(sin(sqrt(x))*besselj(a, sqrt(x)), x, s) == \
        (2**a*gamma(-2*s + Rational(1, 2))*gamma(a/2 + s + Rational(1, 2))/(
            gamma(-a/2 - s + 1)*gamma(a - 2*s + 1)), (
            -re(a)/2 - Rational(1, 2), Rational(1, 4)), True)
    assert MT(cos(sqrt(x))*besselj(a, sqrt(x)), x, s) == \
        (2**a*gamma(a/2 + s)*gamma(-2*s + Rational(1, 2))/(
            gamma(-a/2 - s + Rational(1, 2))*gamma(a - 2*s + 1)), (
            -re(a)/2, Rational(1, 4)), True)
    assert MT(besselj(a, sqrt(x))**2, x, s) == \
        (gamma(a + s)*gamma(Rational(1, 2) - s)
         / (sqrt(pi)*gamma(1 - s)*gamma(1 + a - s)),
            (-re(a), Rational(1, 2)), True)
    assert MT(besselj(a, sqrt(x))*besselj(-a, sqrt(x)), x, s) == \
        (gamma(s)*gamma(Rational(1, 2) - s)
         / (sqrt(pi)*gamma(1 - a - s)*gamma(1 + a - s)),
            (0, Rational(1, 2)), True)
    # NOTE: prudnikov gives the strip below as (1/2 - re(a), 1). As far as
    #       I can see this is wrong (since besselj(z) ~ 1/sqrt(z) for z large)
    assert MT(besselj(a - 1, sqrt(x))*besselj(a, sqrt(x)), x, s) == \
        (gamma(1 - s)*gamma(a + s - Rational(1, 2))
         / (sqrt(pi)*gamma(Rational(3, 2) - s)*gamma(a - s + Rational(1, 2))),
            (Rational(1, 2) - re(a), Rational(1, 2)), True)
    assert MT(besselj(a, sqrt(x))*besselj(b, sqrt(x)), x, s) == \
        (4**s*gamma(1 - 2*s)*gamma((a + b)/2 + s)
         / (gamma(1 - s + (b - a)/2)*gamma(1 - s + (a - b)/2)
            * gamma( 1 - s + (a + b)/2)),
            (-(re(a) + re(b))/2, Rational(1, 2)), True)
    assert MT(besselj(a, sqrt(x))**2 + besselj(-a, sqrt(x))**2, x, s)[1:] == \
        ((Max(re(a), -re(a)), Rational(1, 2)), True)

    # Section 8.4.20
    assert MT(bessely(a, 2*sqrt(x)), x, s) == \
        (-cos(pi*(a/2 - s))*gamma(s - a/2)*gamma(s + a/2)/pi,
            (Max(-re(a)/2, re(a)/2), Rational(3, 4)), True)
    assert MT(sin(sqrt(x))*bessely(a, sqrt(x)), x, s) == \
        (-4**s*sin(pi*(a/2 - s))*gamma(Rational(1, 2) - 2*s)
         * gamma((1 - a)/2 + s)*gamma((1 + a)/2 + s)
         / (sqrt(pi)*gamma(1 - s - a/2)*gamma(1 - s + a/2)),
            (Max(-(re(a) + 1)/2, (re(a) - 1)/2), Rational(1, 4)), True)
    assert MT(cos(sqrt(x))*bessely(a, sqrt(x)), x, s) == \
        (-4**s*cos(pi*(a/2 - s))*gamma(s - a/2)*gamma(s + a/2)*gamma(Rational(1, 2) - 2*s)
         / (sqrt(pi)*gamma(Rational(1, 2) - s - a/2)*gamma(Rational(1, 2) - s + a/2)),
            (Max(-re(a)/2, re(a)/2), Rational(1, 4)), True)
    assert MT(besselj(a, sqrt(x))*bessely(a, sqrt(x)), x, s) == \
        (-cos(pi*s)*gamma(s)*gamma(a + s)*gamma(Rational(1, 2) - s)
         / (pi**Rational(3, 2)*gamma(1 + a - s)),
            (Max(-re(a), 0), Rational(1, 2)), True)
    assert MT(besselj(a, sqrt(x))*bessely(b, sqrt(x)), x, s) == \
        (-4**s*cos(pi*(a/2 - b/2 + s))*gamma(1 - 2*s)
         * gamma(a/2 - b/2 + s)*gamma(a/2 + b/2 + s)
         / (pi*gamma(a/2 - b/2 - s + 1)*gamma(a/2 + b/2 - s + 1)),
            (Max((-re(a) + re(b))/2, (-re(a) - re(b))/2), Rational(1, 2)), True)
    # NOTE bessely(a, sqrt(x))**2 and bessely(a, sqrt(x))*bessely(b, sqrt(x))
    # are a mess (no matter what way you look at it ...)
    assert MT(bessely(a, sqrt(x))**2, x, s)[1:] == \
             ((Max(-re(a), 0, re(a)), Rational(1, 2)), True)

    # Section 8.4.22
    # TODO we can't do any of these (delicate cancellation)

    # Section 8.4.23
    assert MT(besselk(a, 2*sqrt(x)), x, s) == \
        (gamma(
         s - a/2)*gamma(s + a/2)/2, (Max(-re(a)/2, re(a)/2), oo), True)
    assert MT(besselj(a, 2*sqrt(2*sqrt(x)))*besselk(
        a, 2*sqrt(2*sqrt(x))), x, s) == (4**(-s)*gamma(2*s) *
                                         gamma(a/2 + s)/(2*gamma(a/2 - s + 1)), (Max(0, -re(a)/2), oo), True)
    # TODO bessely(a, x)*besselk(a, x) is a mess
    assert MT(besseli(a, sqrt(x))*besselk(a, sqrt(x)), x, s) == \
        (gamma(s)*gamma(
            a + s)*gamma(-s + Rational(1, 2))/(2*sqrt(pi)*gamma(a - s + 1)),
         (Max(-re(a), 0), Rational(1, 2)), True)
    assert MT(besseli(b, sqrt(x))*besselk(a, sqrt(x)), x, s) == \
        (2**(2*s - 1)*gamma(-2*s + 1)*gamma(-a/2 + b/2 + s) *
         gamma(a/2 + b/2 + s)/(gamma(-a/2 + b/2 - s + 1) *
                               gamma(a/2 + b/2 - s + 1)), (Max(-re(a)/2 - re(b)/2,
                                                               re(a)/2 - re(b)/2), Rational(1, 2)), True)

    # TODO products of besselk are a mess

    mt = MT(exp(-x/2)*besselk(a, x/2), x, s)
    mt0 = combsimp((trigsimp(combsimp(mt[0].expand(func=True)))))
    assert mt0 == 2*pi**Rational(3, 2)*cos(pi*s)*gamma(-s + Rational(1, 2))/(
        (cos(2*pi*a) - cos(2*pi*s))*gamma(-a - s + 1)*gamma(a - s + 1))
    assert mt[1:] == ((Max(-re(a), re(a)), oo), True)