]>
gitweb.michael.orlitzky.com - sage.d.git/blob - orthogonal_polynomials.py
0da947cd6f0bf21705f501ec4523868135ff658f
3 def legendre_p(n
, x
, a
= -1, b
= 1):
5 Returns the ``n``th Legendre polynomial of the first kind over the
6 interval [a, b] with respect to ``x``.
8 When [a,b] is not [-1,1], we scale the standard Legendre
9 polynomial (which is defined over [-1,1]) via an affine map. The
10 resulting polynomials are still orthogonal and possess the
11 property that `P(a) = P(b) = 1`.
15 * ``n`` -- The index of the polynomial.
17 * ``x`` -- Either the variable to use as the independent
18 variable in the polynomial, or a point at which to evaluate
21 * ``a`` -- The "left" endpoint of the interval. Must be a real number.
23 * ``b`` -- The "right" endpoint of the interval. Must be a real number.
27 If ``x`` is a variable, a polynomial (symbolic expression) will be
28 returned. Otherwise, the value of the ``n``th polynomial at ``x``
33 Create the standard Legendre polynomials in `x`::
40 Reuse the variable from a polynomial ring::
45 If ``x`` is a real number, the result should be as well::
47 sage: legendre_p(3, 1.1)
50 Similarly for complex numbers::
52 sage: legendre_p(3, 1 + I)
57 sage: legendre_p(3, MatrixSpace(ZZ, 2)([1, 2, -4, 7]))
61 And finite field elements::
63 sage: legendre_P(3, GF(11)(5))
68 We should agree with Maxima for all `n`::
70 sage: eq = lambda k: bool(legendre_p(k,x) == legendre_P(k,x))
71 sage: all([eq(k) for k in range(0,20) ]) # long time
74 We can evaluate the result of the zeroth polynomial::
76 sage: f = legendre_p(0,x)
80 We should have |P(a)| = |P(b)| = 1 for all a,b::
82 sage: a = RR.random_element()
83 sage: b = RR.random_element()
84 sage: k = ZZ.random_element(20)
85 sage: P = legendre_p(k, x, a, b)
86 sage: abs(P(x=a)) # abs tol 1e-12
88 sage: abs(P(x=b)) # abs tol 1e-12
91 Two different polynomials should be orthogonal with respect to the
92 inner product over `[a,b]`. Note that this test can fail if QQ is
93 replaced with RR, since integrate() can return NaN::
95 sage: a = QQ.random_element()
96 sage: b = QQ.random_element()
97 sage: j = ZZ.random_element(20)
99 sage: Pj = legendre_p(j, x, a, b)
100 sage: Pk = legendre_p(k, x, a, b)
101 sage: integrate(Pj*Pk, x, a, b) # abs tol 1e-12
104 The first few polynomials shifted to [0,1] are known to be::
108 sage: p2 = 6*x^2 - 6*x + 1
109 sage: p3 = 20*x^3 - 30*x^2 + 12*x - 1
110 sage: bool(legendre_p(0, x, 0, 1) == p0)
112 sage: bool(legendre_p(1, x, 0, 1) == p1)
114 sage: bool(legendre_p(2, x, 0, 1) == p2)
116 sage: bool(legendre_p(3, x, 0, 1) == p3)
119 The zeroth polynomial is an element of the ring that we're working
122 sage: legendre_p(0, MatrixSpace(ZZ, 2)([1, 2, -4, 7]))
128 raise TypeError('n must be a natural number')
131 raise ValueError('n must be nonnegative')
133 if not (a
in RR
and b
in RR
):
134 raise TypeError('both `a` and `b` must be real numbers')
137 # Easy base case, save time. Attempt to return a value in the
138 # same field/ring as `x`.
141 # Even though we know a,b are real we use a different ring. We
142 # prefer ZZ so that we can support division of finite field
143 # elements by (a-b). Eventually this should be supported for QQ as
144 # well, although it does not work at the moment. The preference of
145 # SR over RR is to return something attractive when e.g. a=pi.
156 # Ensure that (2**n) is an element of ZZ. This is used later --
157 # we can divide finite field elements by integers but we can't
158 # multiply them by rationals at the moment.
162 # This is an affine map from [a,b] into [-1,1] and so
163 # preserves orthogonality.
164 return (a
+ b
- 2*t
)/(a
- b
)
167 return binomial(n
,m
)*binomial(n
, n
-m
)
170 # As given in A&S, but with `x` replaced by `phi(x)`.
171 return ( ((phi(x
) - 1)**(n
-m
)) * (phi(x
) + 1)**m
)
173 # From Abramowitz & Stegun, (22.3.2) with alpha = beta = 0.
174 # Also massaged to support finite field elements.
175 P
= sum([ c(m
)*g(m
) for m
in range(0,n
+1) ])/(2**n
)