def test_manualintegrate_orthogonal_poly(): n = symbols('n') a, b = 7, Rational(5, 3) polys = [jacobi(n, a, b, x), gegenbauer(n, a, x), chebyshevt(n, x), chebyshevu(n, x), legendre(n, x), hermite(n, x), laguerre(n, x), assoc_laguerre(n, a, x)] for p in polys: integral = manualintegrate(p, x) for deg in [-2, -1, 0, 1, 3, 5, 8]: # some accept negative "degree", some do not try: p_subbed = p.subs(n, deg) except ValueError: continue assert (integral.subs(n, deg).diff(x) - p_subbed).expand() == 0 # can also integrate simple expressions with these polynomials q = x*p.subs(x, 2*x + 1) integral = manualintegrate(q, x) for deg in [2, 4, 7]: assert (integral.subs(n, deg).diff(x) - q.subs(n, deg)).expand() == 0 # cannot integrate with respect to any other parameter t = symbols('t') for i in range(len(p.args) - 1): new_args = list(p.args) new_args[i] = t assert isinstance(manualintegrate(p.func(*new_args), t), Integral)
def test_gegenbauer(): n = Symbol("n") a = Symbol("a") assert gegenbauer(0, a, x) == 1 assert gegenbauer(1, a, x) == 2 * a * x assert gegenbauer(2, a, x) == -a + x**2 * (2 * a**2 + 2 * a) assert gegenbauer(3, a, x) == \ x**3*(4*a**3/3 + 4*a**2 + a*Rational(8, 3)) + x*(-2*a**2 - 2*a) assert gegenbauer(-1, a, x) == 0 assert gegenbauer(n, S.Half, x) == legendre(n, x) assert gegenbauer(n, 1, x) == chebyshevu(n, x) assert gegenbauer(n, -1, x) == 0 X = gegenbauer(n, a, x) assert isinstance(X, gegenbauer) assert gegenbauer(n, a, -x) == (-1)**n * gegenbauer(n, a, x) assert gegenbauer(n, a, 0) == 2**n*sqrt(pi) * \ gamma(a + n/2)/(gamma(a)*gamma(-n/2 + S.Half)*gamma(n + 1)) assert gegenbauer(n, a, 1) == gamma(2 * a + n) / (gamma(2 * a) * gamma(n + 1)) assert gegenbauer(n, Rational(3, 4), -1) is zoo assert gegenbauer(n, Rational(1, 4), -1) == (sqrt(2) * cos(pi * (n + S.One / 4)) * gamma(n + S.Half) / (sqrt(pi) * gamma(n + 1))) m = Symbol("m", positive=True) assert gegenbauer(m, a, oo) == oo * RisingFactorial(a, m) assert unchanged(gegenbauer, n, a, oo) assert conjugate(gegenbauer(n, a, x)) == gegenbauer(n, conjugate(a), conjugate(x)) _k = Dummy('k') assert diff(gegenbauer(n, a, x), n) == Derivative(gegenbauer(n, a, x), n) assert diff(gegenbauer(n, a, x), a).dummy_eq( Sum((2 * (-1)**(-_k + n) + 2) * (_k + a) * gegenbauer(_k, a, x) / ((-_k + n) * (_k + 2 * a + n)) + ((2 * _k + 2) / ((_k + 2 * a) * (2 * _k + 2 * a + 1)) + 2 / (_k + 2 * a + n)) * gegenbauer(n, a, x), (_k, 0, n - 1))) assert diff(gegenbauer(n, a, x), x) == 2 * a * gegenbauer(n - 1, a + 1, x) assert gegenbauer(n, a, x).rewrite('polynomial').dummy_eq( Sum((-1)**_k * (2 * x)**(-2 * _k + n) * RisingFactorial(a, -_k + n) / (factorial(_k) * factorial(-2 * _k + n)), (_k, 0, floor(n / 2)))) raises(ArgumentIndexError, lambda: gegenbauer(n, a, x).fdiff(4))
def test_sympy__functions__special__polynomials__chebyshevu(): from sympy.functions.special.polynomials import chebyshevu assert _test_args(chebyshevu(x, 2))
def test_jacobi(): n = Symbol("n") a = Symbol("a") b = Symbol("b") 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(n, a, a, x) == RisingFactorial(a + 1, n) * gegenbauer( n, a + S.Half, 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, Rational(-1, 2), Rational(-1, 2), x) == RisingFactorial(S.Half, 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 unchanged(jacobi, n, a, b, oo) assert conjugate(jacobi(m, a, b, x)) == \ jacobi(m, conjugate(a), conjugate(b), conjugate(x)) _k = Dummy('k') assert diff(jacobi(n, a, b, x), n) == Derivative(jacobi(n, a, b, x), n) assert diff(jacobi(n, a, b, x), a).dummy_eq( Sum((jacobi(n, a, b, x) + (2 * _k + a + b + 1) * RisingFactorial(_k + b + 1, -_k + n) * jacobi(_k, a, b, x) / ((-_k + n) * RisingFactorial(_k + a + b + 1, -_k + n))) / (_k + a + b + n + 1), (_k, 0, n - 1))) assert diff(jacobi(n, a, b, x), b).dummy_eq( Sum(((-1)**(-_k + n) * (2 * _k + a + b + 1) * RisingFactorial(_k + a + 1, -_k + n) * jacobi(_k, a, b, x) / ((-_k + n) * RisingFactorial(_k + a + b + 1, -_k + n)) + jacobi(n, a, b, x)) / (_k + a + b + n + 1), (_k, 0, n - 1))) assert diff(jacobi(n, a, b, x), x) == \ (a/2 + b/2 + n/2 + S.Half)*jacobi(n - 1, a + 1, b + 1, x) 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)))) raises(ValueError, lambda: jacobi(-2.1, a, b, x)) raises(ValueError, lambda: jacobi(Dummy(positive=True, integer=True), 1, 2, oo)) assert jacobi(n, a, b, x).rewrite("polynomial").dummy_eq( Sum((S.Half - x / 2)**_k * RisingFactorial(-n, _k) * RisingFactorial(_k + a + 1, -_k + n) * RisingFactorial(a + b + n + 1, _k) / factorial(_k), (_k, 0, n)) / factorial(n)) raises(ArgumentIndexError, lambda: jacobi(n, a, b, x).fdiff(5))
def test_chebyshev(): assert chebyshevt(0, x) == 1 assert chebyshevt(1, x) == x assert chebyshevt(2, x) == 2 * x**2 - 1 assert chebyshevt(3, x) == 4 * x**3 - 3 * x for n in range(1, 4): for k in range(n): z = chebyshevt_root(n, k) assert chebyshevt(n, z) == 0 raises(ValueError, lambda: chebyshevt_root(n, n)) for n in range(1, 4): for k in range(n): z = chebyshevu_root(n, k) assert chebyshevu(n, z) == 0 raises(ValueError, lambda: chebyshevu_root(n, n)) n = Symbol("n") X = chebyshevt(n, x) assert isinstance(X, chebyshevt) assert unchanged(chebyshevt, n, x) assert chebyshevt(n, -x) == (-1)**n * chebyshevt(n, x) assert chebyshevt(-n, x) == chebyshevt(n, x) assert chebyshevt(n, 0) == cos(pi * n / 2) assert chebyshevt(n, 1) == 1 assert chebyshevt(n, oo) is oo assert conjugate(chebyshevt(n, x)) == chebyshevt(n, conjugate(x)) assert diff(chebyshevt(n, x), x) == n * chebyshevu(n - 1, x) X = chebyshevu(n, x) assert isinstance(X, chebyshevu) y = Symbol('y') assert chebyshevu(n, -x) == (-1)**n * chebyshevu(n, x) assert chebyshevu(-n, x) == -chebyshevu(n - 2, x) assert unchanged(chebyshevu, -n + y, x) assert chebyshevu(n, 0) == cos(pi * n / 2) assert chebyshevu(n, 1) == n + 1 assert chebyshevu(n, oo) is oo assert conjugate(chebyshevu(n, x)) == chebyshevu(n, conjugate(x)) assert diff(chebyshevu(n, x), x) == \ (-x*chebyshevu(n, x) + (n + 1)*chebyshevt(n + 1, x))/(x**2 - 1) _k = Dummy('k') assert chebyshevt(n, x).rewrite("polynomial").dummy_eq( Sum(x**(-2 * _k + n) * (x**2 - 1)**_k * binomial(n, 2 * _k), (_k, 0, floor(n / 2)))) assert chebyshevu(n, x).rewrite("polynomial").dummy_eq( Sum((-1)**_k * (2 * x)**(-2 * _k + n) * factorial(-_k + n) / (factorial(_k) * factorial(-2 * _k + n)), (_k, 0, floor(n / 2)))) raises(ArgumentIndexError, lambda: chebyshevt(n, x).fdiff(1)) raises(ArgumentIndexError, lambda: chebyshevt(n, x).fdiff(3)) raises(ArgumentIndexError, lambda: chebyshevu(n, x).fdiff(1)) raises(ArgumentIndexError, lambda: chebyshevu(n, x).fdiff(3))