2 This module mainly implements special orthogonal polynomials.
4 See also functions.combinatorial.numbers which contains some
5 combinatorial polynomials.
9 from sympy
.core
.basic
import Basic
, S
10 from sympy
.core
import Rational
, Symbol
11 from sympy
.core
.function
import SingleValuedFunction
12 from sympy
.utilities
.memoization
import recurrence_memo
15 _x
= Basic
.Symbol('x', dummy
=True)
17 class PolynomialSequence(SingleValuedFunction
):
20 precedence
= Basic
.Apply_precedence
23 def canonize(cls
, n
, x
):
24 if n
.is_integer
and n
>= 0:
25 return cls
.calc(int(n
)).subs(_x
, x
)
27 raise ValueError("%s index must be nonnegative integer (got %r)" % (self
, i
))
31 #----------------------------------------------------------------------------
32 # Chebyshev polynomials of first and second kind
35 class chebyshevt(PolynomialSequence
):
37 chebyshevt(n, x) gives the nth Chebyshev polynomial (of the first
40 The Chebyshev polynomials of the first kind are orthogonal on
41 [-1, 1] with respect to the weight 1/sqrt(1-x**2).
55 * http://en.wikipedia.org/wiki/Chebyshev_polynomial
59 Chebyshev polynomial of the first kind, T_n(x)
62 @recurrence_memo([Basic
.One(), _x
])
64 return (2*_x
*prev
[n
-1] - prev
[n
-2]).expand()
67 class chebyshevu(PolynomialSequence
):
69 chebyshevu(n, x) gives the nth Chebyshev polynomial of the second
72 The Chebyshev polynomials of the second kind are orthogonal on
73 [-1, 1] with respect to the weight sqrt(1-x**2).
86 @recurrence_memo([Basic
.One(), 2*_x
])
88 return (2*_x
*prev
[n
-1] - prev
[n
-2]).expand()
91 class chebyshevt_root(SingleValuedFunction
):
93 chebyshev_root(n, k) returns the kth root (indexed from zero) of
94 the nth Chebyshev polynomial of the first kind; that is, if
95 0 <= k < n, chebyshevt(n, chebyshevt_root(n, k)) == 0.
100 >>> chebyshevt_root(3, 2)
102 >>> chebyshevt(3, chebyshevt_root(3, 2))
108 def canonize(cls
, n
, k
):
110 raise ValueError, "must have 0 <= k < n"
111 return Basic
.cos(S
.Pi
*(2*k
+1)/(2*n
))
113 class chebyshevu_root(SingleValuedFunction
):
115 chebyshevu_root(n, k) returns the kth root (indexed from zero) of the
116 nth Chebyshev polynomial of the second kind; that is, if 0 <= k < n,
117 chebyshevu(n, chebyshevu_root(n, k)) == 0.
122 >>> chebyshevu_root(3, 2)
124 >>> chebyshevu(3, chebyshevu_root(3, 2))
130 def canonize(cls
, n
, k
):
132 raise ValueError, "must have 0 <= k < n"
133 return Basic
.cos(S
.Pi
*(k
+1)/(n
+1))
136 #----------------------------------------------------------------------------
137 # Legendre polynomials
140 class legendre(PolynomialSequence
):
142 legendre(n, x) gives the nth Legendre polynomial of x, P_n(x)
144 The Legendre polynomials are orthogonal on [-1, 1] with respect to
145 the constant weight 1. They satisfy P_n(1) = 1 for all n; further,
146 P_n is odd for odd n and even for even n
160 * http://en.wikipedia.org/wiki/Legendre_polynomial
163 @recurrence_memo([Basic
.One(), _x
])
165 return (((2*n
-1)*_x
*prev
[n
-1] - (n
-1)*prev
[n
-2])/n
).expand()
168 #----------------------------------------------------------------------------
169 # Hermite polynomials
172 class hermite(PolynomialSequence
):
174 hermite(n, x) gives the nth Hermite polynomial in x, H_n(x)
176 The Hermite polynomials are orthogonal on (-oo, oo) with respect to
177 the weight exp(-x**2/2).
191 * http://mathworld.wolfram.com/HermitePolynomial.html
194 @recurrence_memo([Basic
.One(), 2*_x
])
196 return (2*_x
*prev
[n
-1]-2*(n
-1)*prev
[n
-2]).expand()