_eval_apply() converted to canonize()
[sympy.git] / sympy / functions / special / polynomials.py
blob9b2bc2acbf6dbfd53b0f918a364d32512eaaf992
1 """
2 This module mainly implements special orthogonal polynomials.
4 See also functions.combinatorial.numbers which contains some
5 combinatorial polynomials.
7 """
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):
19 nofargs = 2
20 precedence = Basic.Apply_precedence
22 @classmethod
23 def canonize(cls, n, x):
24 if n.is_integer and n >= 0:
25 return cls.calc(int(n)).subs(_x, x)
26 if n.is_negative:
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):
36 """
37 chebyshevt(n, x) gives the nth Chebyshev polynomial (of the first
38 kind) of x, T_n(x)
40 The Chebyshev polynomials of the first kind are orthogonal on
41 [-1, 1] with respect to the weight 1/sqrt(1-x**2).
43 Examples
44 ========
45 >>> x = Symbol('x')
46 >>> chebyshevt(0, x)
48 >>> chebyshevt(1, x)
50 >>> chebyshevt(2, x)
51 (-1) + 2*x**2
53 References
54 ==========
55 * http://en.wikipedia.org/wiki/Chebyshev_polynomial
56 """
58 """
59 Chebyshev polynomial of the first kind, T_n(x)
60 """
61 @staticmethod
62 @recurrence_memo([Basic.One(), _x])
63 def calc(n, prev):
64 return (2*_x*prev[n-1] - prev[n-2]).expand()
67 class chebyshevu(PolynomialSequence):
68 """
69 chebyshevu(n, x) gives the nth Chebyshev polynomial of the second
70 kind of x, U_n(x)
72 The Chebyshev polynomials of the second kind are orthogonal on
73 [-1, 1] with respect to the weight sqrt(1-x**2).
75 Examples
76 ========
77 >>> x = Symbol('x')
78 >>> chebyshevu(0, x)
80 >>> chebyshevu(1, x)
81 2*x
82 >>> chebyshevu(2, x)
83 (-1) + 4*x**2
84 """
85 @staticmethod
86 @recurrence_memo([Basic.One(), 2*_x])
87 def calc(n, prev):
88 return (2*_x*prev[n-1] - prev[n-2]).expand()
91 class chebyshevt_root(SingleValuedFunction):
92 """
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.
97 Examples
98 ========
100 >>> chebyshevt_root(3, 2)
101 -1/2*3**(1/2)
102 >>> chebyshevt(3, chebyshevt_root(3, 2))
105 nofargs = 2
107 @classmethod
108 def canonize(cls, n, k):
109 if not 0 <= k < n:
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.
119 Examples
120 ========
122 >>> chebyshevu_root(3, 2)
123 -1/2*2**(1/2)
124 >>> chebyshevu(3, chebyshevu_root(3, 2))
127 nofargs = 2
129 @classmethod
130 def canonize(cls, n, k):
131 if not 0 <= k < n:
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
148 Examples
149 ========
150 >>> x = Symbol('x')
151 >>> legendre(0, x)
153 >>> legendre(1, x)
155 >>> legendre(2, x)
156 (-1/2) + (3/2)*x**2
158 References
159 ========
160 * http://en.wikipedia.org/wiki/Legendre_polynomial
162 @staticmethod
163 @recurrence_memo([Basic.One(), _x])
164 def calc(n, prev):
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).
179 Examples
180 ========
181 >>> x = Symbol('x')
182 >>> hermite(0, x)
184 >>> hermite(1, x)
186 >>> hermite(2, x)
187 (-2) + 4*x**2
189 References
190 ==========
191 * http://mathworld.wolfram.com/HermitePolynomial.html
193 @staticmethod
194 @recurrence_memo([Basic.One(), 2*_x])
195 def calc(n, prev):
196 return (2*_x*prev[n-1]-2*(n-1)*prev[n-2]).expand()