Example #1
0
def test_jacobi():
    assert jacobi(0, a, b, x) == 1
    assert jacobi(1, a, b, x) == a / 2 - b / 2 + x * (a / 2 + b / 2 + 1)
    assert (jacobi(2, a, b, x) == a**2 / 8 - a * b / 4 - a / 8 + b**2 / 8 -
            b / 8 + x**2 * (a**2 / 8 + a * b / 4 + 7 * a / 8 + b**2 / 8 +
                            7 * b / 8 + Rational(3, 2)) + x *
            (a**2 / 4 + 3 * a / 4 - b**2 / 4 - 3 * b / 4) - S.Half)

    assert jacobi(n, a, a, x) == RisingFactorial(a + 1, n) * gegenbauer(
        n, a + Rational(1, 2), x) / RisingFactorial(2 * a + 1, n)
    assert jacobi(n, a, -a,
                  x) == ((-1)**a * (-x + 1)**(-a / 2) * (x + 1)**(a / 2) *
                         assoc_legendre(n, a, x) * factorial(-a + n) *
                         gamma(a + n + 1) / (factorial(a + n) * gamma(n + 1)))
    assert jacobi(n, -b, b, x) == ((-x + 1)**(b / 2) * (x + 1)**(-b / 2) *
                                   assoc_legendre(n, b, x) *
                                   gamma(-b + n + 1) / gamma(n + 1))
    assert jacobi(n, 0, 0, x) == legendre(n, x)
    assert jacobi(n, S.Half, S.Half, x) == RisingFactorial(Rational(
        3, 2), n) * chebyshevu(n, x) / factorial(n + 1)
    assert jacobi(n, -S.Half, -S.Half, x) == RisingFactorial(
        Rational(1, 2), n) * chebyshevt(n, x) / factorial(n)

    X = jacobi(n, a, b, x)
    assert isinstance(X, jacobi)

    assert jacobi(n, a, b, -x) == (-1)**n * jacobi(n, b, a, x)
    assert jacobi(n, a, b, 0) == 2**(-n) * gamma(a + n + 1) * hyper(
        (-b - n, -n), (a + 1, ), -1) / (factorial(n) * gamma(a + 1))
    assert jacobi(n, a, b, 1) == RisingFactorial(a + 1, n) / factorial(n)

    m = Symbol("m", positive=True)
    assert jacobi(m, a, b, oo) == oo * RisingFactorial(a + b + m + 1, m)

    assert conjugate(jacobi(m, a, b, x)) == \
        jacobi(m, conjugate(a), conjugate(b), conjugate(x))

    assert diff(jacobi(n, a, b, x), n) == Derivative(jacobi(n, a, b, x), n)
    assert diff(jacobi(n, a, b, x), x) == \
        (a/2 + b/2 + n/2 + Rational(1, 2))*jacobi(n - 1, a + 1, b + 1, x)

    # XXX see issue sympy/sympy#5539
    assert str(jacobi(n, a, b, x).diff(a)) == \
        ("Sum((jacobi(n, a, b, x) + (a + b + 2*_k + 1)*RisingFactorial(b + "
         "_k + 1, n - _k)*jacobi(_k, a, b, x)/((n - _k)*RisingFactorial(a + "
         "b + _k + 1, n - _k)))/(a + b + n + _k + 1), (_k, 0, n - 1))")
    assert str(jacobi(n, a, b, x).diff(b)) == \
        ("Sum(((-1)**(n - _k)*(a + b + 2*_k + 1)*RisingFactorial(a + "
         "_k + 1, n - _k)*jacobi(_k, a, b, x)/((n - _k)*RisingFactorial(a + "
         "b + _k + 1, n - _k)) + jacobi(n, a, b, x))/(a + b + n + "
         "_k + 1), (_k, 0, n - 1))")

    assert jacobi_normalized(n, a, b, x) == \
           (jacobi(n, a, b, x)/sqrt(2**(a + b + 1)*gamma(a + n + 1)*gamma(b + n + 1)
                                    / ((a + b + 2*n + 1)*factorial(n)*gamma(a + b + n + 1))))

    pytest.raises(ValueError, lambda: jacobi(-2.1, a, b, x))
    pytest.raises(ValueError,
                  lambda: jacobi(Dummy(positive=True, integer=True), 1, 2, oo))
    pytest.raises(ArgumentIndexError, lambda: jacobi(n, a, b, x).fdiff(5))
def test_jacobi():
    assert jacobi(0, a, b, x) == 1
    assert jacobi(1, a, b, x) == a/2 - b/2 + x*(a/2 + b/2 + 1)
    assert (jacobi(2, a, b, x) == a**2/8 - a*b/4 - a/8 + b**2/8 - b/8 +
            x**2*(a**2/8 + a*b/4 + 7*a/8 + b**2/8 + 7*b/8 + Rational(3, 2)) +
            x*(a**2/4 + 3*a/4 - b**2/4 - 3*b/4) - Rational(1, 2))

    assert jacobi(n, a, a, x) == RisingFactorial(
        a + 1, n)*gegenbauer(n, a + Rational(1, 2), x)/RisingFactorial(2*a + 1, n)
    assert jacobi(n, a, -a, x) == ((-1)**a*(-x + 1)**(-a/2)*(x + 1)**(a/2)*assoc_legendre(n, a, x) *
                                   factorial(-a + n)*gamma(a + n + 1)/(factorial(a + n)*gamma(n + 1)))
    assert jacobi(n, -b, b, x) == ((-x + 1)**(b/2)*(x + 1)**(-b/2)*assoc_legendre(n, b, x) *
                                   gamma(-b + n + 1)/gamma(n + 1))
    assert jacobi(n, 0, 0, x) == legendre(n, x)
    assert jacobi(n, Rational(1, 2), Rational(1, 2), x) == RisingFactorial(
        Rational(3, 2), n)*chebyshevu(n, x)/factorial(n + 1)
    assert jacobi(n, Rational(-1, 2), Rational(-1, 2), x) == RisingFactorial(
        Rational(1, 2), n)*chebyshevt(n, x)/factorial(n)

    X = jacobi(n, a, b, x)
    assert isinstance(X, jacobi)

    assert jacobi(n, a, b, -x) == (-1)**n*jacobi(n, b, a, x)
    assert jacobi(n, a, b, 0) == 2**(-n)*gamma(a + n + 1)*hyper(
        (-b - n, -n), (a + 1,), -1)/(factorial(n)*gamma(a + 1))
    assert jacobi(n, a, b, 1) == RisingFactorial(a + 1, n)/factorial(n)

    m = Symbol("m", positive=True)
    assert jacobi(m, a, b, oo) == oo*RisingFactorial(a + b + m + 1, m)
    assert jacobi(n, a, b, oo) == jacobi(n, a, b, oo, evaluate=False)

    assert conjugate(jacobi(m, a, b, x)) == \
        jacobi(m, conjugate(a), conjugate(b), conjugate(x))

    assert diff(jacobi(n, a, b, x), n) == Derivative(jacobi(n, a, b, x), n)
    assert diff(jacobi(n, a, b, x), x) == \
        (a/2 + b/2 + n/2 + Rational(1, 2))*jacobi(n - 1, a + 1, b + 1, x)

    # XXX see issue sympy/sympy#5539
    assert str(jacobi(n, a, b, x).diff(a)) == \
        ("Sum((jacobi(n, a, b, x) + (a + b + 2*_k + 1)*RisingFactorial(b + "
         "_k + 1, n - _k)*jacobi(_k, a, b, x)/((n - _k)*RisingFactorial(a + "
         "b + _k + 1, n - _k)))/(a + b + n + _k + 1), (_k, 0, n - 1))")
    assert str(jacobi(n, a, b, x).diff(b)) == \
        ("Sum(((-1)**(n - _k)*(a + b + 2*_k + 1)*RisingFactorial(a + "
         "_k + 1, n - _k)*jacobi(_k, a, b, x)/((n - _k)*RisingFactorial(a + "
         "b + _k + 1, n - _k)) + jacobi(n, a, b, x))/(a + b + n + "
         "_k + 1), (_k, 0, n - 1))")

    assert jacobi_normalized(n, a, b, x) == \
        (jacobi(n, a, b, x)/sqrt(2**(a + b + 1)*gamma(a + n + 1)*gamma(b + n + 1)
                                 / ((a + b + 2*n + 1)*factorial(n)*gamma(a + b + n + 1))))

    pytest.raises(ValueError, lambda: jacobi(-2.1, a, b, x))
    pytest.raises(ValueError, lambda: jacobi(Dummy(positive=True, integer=True), 1, 2, oo))
    pytest.raises(ArgumentIndexError, lambda: jacobi(n, a, b, x).fdiff(5))
Example #3
0
def test_Ynm():
    # http://en.wikipedia.org/wiki/Spherical_harmonics
    th, ph = Symbol("theta", extended_real=True), Symbol("phi",
                                                         extended_real=True)
    from diofant.abc import n, m

    assert Ynm(0, 0, th, ph).expand(func=True) == 1 / (2 * sqrt(pi))
    assert Ynm(1, -1, th, ph) == -exp(-2 * I * ph) * Ynm(1, 1, th, ph)
    assert Ynm(1, -1, th, ph).expand(
        func=True) == sqrt(6) * sin(th) * exp(-I * ph) / (4 * sqrt(pi))
    assert Ynm(1, -1, th, ph).expand(
        func=True) == sqrt(6) * sin(th) * exp(-I * ph) / (4 * sqrt(pi))
    assert Ynm(1, 0, th,
               ph).expand(func=True) == sqrt(3) * cos(th) / (2 * sqrt(pi))
    assert Ynm(1, 1, th, ph).expand(
        func=True) == -sqrt(6) * sin(th) * exp(I * ph) / (4 * sqrt(pi))
    assert Ynm(2, 0, th, ph).expand(
        func=True
    ) == 3 * sqrt(5) * cos(th)**2 / (4 * sqrt(pi)) - sqrt(5) / (4 * sqrt(pi))
    assert Ynm(2, 1, th, ph).expand(func=True) == -sqrt(30) * sin(th) * exp(
        I * ph) * cos(th) / (4 * sqrt(pi))
    assert Ynm(
        2, -2, th,
        ph).expand(func=True) == (-sqrt(30) * exp(-2 * I * ph) * cos(th)**2 /
                                  (8 * sqrt(pi)) +
                                  sqrt(30) * exp(-2 * I * ph) / (8 * sqrt(pi)))
    assert Ynm(
        2, 2, th,
        ph).expand(func=True) == (-sqrt(30) * exp(2 * I * ph) * cos(th)**2 /
                                  (8 * sqrt(pi)) + sqrt(30) * exp(2 * I * ph) /
                                  (8 * sqrt(pi)))

    assert diff(Ynm(
        n, m, th, ph), th) == (m * cot(th) * Ynm(n, m, th, ph) + sqrt(
            (-m + n) * (m + n + 1)) * exp(-I * ph) * Ynm(n, m + 1, th, ph))
    assert diff(Ynm(n, m, th, ph), ph) == I * m * Ynm(n, m, th, ph)
    pytest.raises(ArgumentIndexError, lambda: Ynm(n, m, th, ph).fdiff(1))

    assert conjugate(
        Ynm(n, m, th,
            ph)) == (-1)**(2 * m) * exp(-2 * I * m * ph) * Ynm(n, m, th, ph)

    assert Ynm(n, m, -th, ph) == Ynm(n, m, th, ph)
    assert Ynm(n, m, th, -ph) == exp(-2 * I * m * ph) * Ynm(n, m, th, ph)
    assert Ynm(n, -m, th,
               ph) == (-1)**m * exp(-2 * I * m * ph) * Ynm(n, m, th, ph)

    assert (Ynm(n, m, th, ph).rewrite(sin) == Ynm(
        n, m, th, ph).rewrite(cos) == exp(I * m * ph) * sqrt(
            (2 * n + 1) * factorial(-m + n) / factorial(m + n)) *
            assoc_legendre(n, m, cos(th)) / (2 * sqrt(pi)))
    assert (Ynm(n, m, th, ph).as_real_imag() == (
        sqrt((2 * n + 1) * factorial(-m + n) / factorial(m + n)) *
        cos(m * ph) * assoc_legendre(n, m, cos(th)) / (2 * sqrt(pi)),
        sqrt((2 * n + 1) * factorial(-m + n) / factorial(m + n)) *
        sin(m * ph) * assoc_legendre(n, m, cos(th)) / (2 * sqrt(pi))))
def test_Ynm():
    # https//en.wikipedia.org/wiki/Spherical_harmonics
    th, ph = Symbol("theta", extended_real=True), Symbol("phi", extended_real=True)

    assert Ynm(0, 0, th, ph).expand(func=True) == 1/(2*sqrt(pi))
    assert Ynm(1, -1, th, ph) == -exp(-2*I*ph)*Ynm(1, 1, th, ph)
    assert Ynm(1, -1, th, ph).expand(func=True) == sqrt(6)*sin(th)*exp(-I*ph)/(4*sqrt(pi))
    assert Ynm(1, -1, th, ph).expand(func=True) == sqrt(6)*sin(th)*exp(-I*ph)/(4*sqrt(pi))
    assert Ynm(1, 0, th, ph).expand(func=True) == sqrt(3)*cos(th)/(2*sqrt(pi))
    assert Ynm(1, 1, th, ph).expand(func=True) == -sqrt(6)*sin(th)*exp(I*ph)/(4*sqrt(pi))
    assert Ynm(2, 0, th, ph).expand(func=True) == 3*sqrt(5)*cos(th)**2/(4*sqrt(pi)) - sqrt(5)/(4*sqrt(pi))
    assert Ynm(2, 1, th, ph).expand(func=True) == -sqrt(30)*sin(th)*exp(I*ph)*cos(th)/(4*sqrt(pi))
    assert Ynm(2, -2, th, ph).expand(func=True) == (-sqrt(30)*exp(-2*I*ph)*cos(th)**2/(8*sqrt(pi))
                                                    + sqrt(30)*exp(-2*I*ph)/(8*sqrt(pi)))
    assert Ynm(2, 2, th, ph).expand(func=True) == (-sqrt(30)*exp(2*I*ph)*cos(th)**2/(8*sqrt(pi))
                                                   + sqrt(30)*exp(2*I*ph)/(8*sqrt(pi)))

    assert diff(Ynm(n, m, th, ph), th) == (m*cot(th)*Ynm(n, m, th, ph)
                                           + sqrt((-m + n)*(m + n + 1))*exp(-I*ph)*Ynm(n, m + 1, th, ph))
    assert diff(Ynm(n, m, th, ph), ph) == I*m*Ynm(n, m, th, ph)
    pytest.raises(ArgumentIndexError, lambda: Ynm(n, m, th, ph).fdiff(1))

    assert conjugate(Ynm(n, m, th, ph)) == (-1)**(2*m)*exp(-2*I*m*ph)*Ynm(n, m, th, ph)

    assert Ynm(n, m, -th, ph) == Ynm(n, m, th, ph)
    assert Ynm(n, m, th, -ph) == exp(-2*I*m*ph)*Ynm(n, m, th, ph)
    assert Ynm(n, -m, th, ph) == (-1)**m*exp(-2*I*m*ph)*Ynm(n, m, th, ph)

    assert (Ynm(n, m, th, ph).rewrite(sin) ==
            Ynm(n, m, th, ph).rewrite(cos) ==
            exp(I*m*ph)*sqrt((2*n + 1)*factorial(-m + n)/factorial(m + n)) *
            assoc_legendre(n, m, cos(th))/(2*sqrt(pi)))
    assert (Ynm(n, m, th, ph).as_real_imag() ==
            (sqrt((2*n + 1)*factorial(-m + n)/factorial(m + n))*cos(m*ph) *
             assoc_legendre(n, m, cos(th))/(2*sqrt(pi)),
             sqrt((2*n + 1)*factorial(-m + n)/factorial(m + n))*sin(m*ph) *
             assoc_legendre(n, m, cos(th))/(2*sqrt(pi))))
Example #5
0
def test_assoc_legendre():
    Plm = assoc_legendre
    Q = sqrt(1 - x**2)

    assert Plm(0, 0, x) == 1
    assert Plm(1, 0, x) == x
    assert Plm(1, 1, x) == -Q
    assert Plm(2, 0, x) == (3 * x**2 - 1) / 2
    assert Plm(2, 1, x) == -3 * x * Q
    assert Plm(2, 2, x) == 3 * Q**2
    assert Plm(3, 0, x) == (5 * x**3 - 3 * x) / 2
    assert Plm(3, 1,
               x).expand() == ((3 * (1 - 5 * x**2) / 2).expand() * Q).expand()
    assert Plm(3, 2, x) == 15 * x * Q**2
    assert Plm(3, 3, x) == -15 * Q**3

    # negative m
    assert Plm(1, -1, x) == -Plm(1, 1, x) / 2
    assert Plm(2, -2, x) == Plm(2, 2, x) / 24
    assert Plm(2, -1, x) == -Plm(2, 1, x) / 6
    assert Plm(3, -3, x) == -Plm(3, 3, x) / 720
    assert Plm(3, -2, x) == Plm(3, 2, x) / 120
    assert Plm(3, -1, x) == -Plm(3, 1, x) / 12

    X = Plm(n, m, x)
    assert isinstance(X, assoc_legendre)

    assert Plm(n, 0, x) == legendre(n, x)
    assert Plm(n, m, 0) == 2**m * sqrt(pi) / (gamma(
        (1 - m - n) / 2) * gamma(1 - (m - n) / 2))

    pytest.raises(ValueError, lambda: Plm(-1, 0, x))
    pytest.raises(ValueError, lambda: Plm(0, 1, x))
    pytest.raises(ValueError, lambda: Plm(-1, 2, x))

    assert conjugate(assoc_legendre(n, m, x)) == \
        assoc_legendre(n, conjugate(m), conjugate(x))

    assert assoc_legendre(n, m, x).diff(x) == \
        (n*x*assoc_legendre(n, m, x) -
         (m + n)*assoc_legendre(n - 1, m, x))/(x**2 - 1)

    pytest.raises(ArgumentIndexError, lambda: assoc_legendre(n, m, x).fdiff(1))

    assert (str(assoc_laguerre(
        n, m,
        x).diff(m)) == 'Sum(assoc_laguerre(_k, m, x)/(-m + n), (_k, 0, n - 1))'
            )
def test_assoc_legendre():
    Plm = assoc_legendre
    Q = sqrt(1 - x**2)

    assert Plm(0, 0, x) == 1
    assert Plm(1, 0, x) == x
    assert Plm(1, 1, x) == -Q
    assert Plm(2, 0, x) == (3*x**2 - 1)/2
    assert Plm(2, 1, x) == -3*x*Q
    assert Plm(2, 2, x) == 3*Q**2
    assert Plm(3, 0, x) == (5*x**3 - 3*x)/2
    assert Plm(3, 1, x).expand() == (( 3*(1 - 5*x**2)/2 ).expand() * Q).expand()
    assert Plm(3, 2, x) == 15*x * Q**2
    assert Plm(3, 3, x) == -15 * Q**3

    # negative m
    assert Plm(1, -1, x) == -Plm(1, 1, x)/2
    assert Plm(2, -2, x) == Plm(2, 2, x)/24
    assert Plm(2, -1, x) == -Plm(2, 1, x)/6
    assert Plm(3, -3, x) == -Plm(3, 3, x)/720
    assert Plm(3, -2, x) == Plm(3, 2, x)/120
    assert Plm(3, -1, x) == -Plm(3, 1, x)/12

    X = Plm(n, m, x)
    assert isinstance(X, assoc_legendre)

    assert Plm(n, 0, x) == legendre(n, x)
    assert Plm(n, m, 0) == 2**m*sqrt(pi)/(gamma((1 - m - n)/2)*gamma(1 - (m - n)/2))

    pytest.raises(ValueError, lambda: Plm(-1, 0, x))
    pytest.raises(ValueError, lambda: Plm(0, 1, x))
    pytest.raises(ValueError, lambda: Plm(-1, 2, x))

    assert conjugate(assoc_legendre(n, m, x)) == \
        assoc_legendre(n, conjugate(m), conjugate(x))

    assert assoc_legendre(n, m, x).diff(x) == \
        (n*x*assoc_legendre(n, m, x) -
         (m + n)*assoc_legendre(n - 1, m, x))/(x**2 - 1)

    pytest.raises(ArgumentIndexError, lambda: assoc_legendre(n, m, x).fdiff(1))

    assert (str(assoc_laguerre(n, m, x).diff(m)) ==
            'Sum(assoc_laguerre(_k, m, x)/(-m + n), (_k, 0, n - 1))')