]>
gitweb.michael.orlitzky.com - sage.d.git/blob - mjo/orthogonal_polynomials.py
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))
66 Solve a simple least squares problem over `[-\pi, \pi]`::
70 sage: def inner_product(v1, v2):
71 ... return integrate(v1*v2, x, a, b)
74 ... return sqrt(inner_product(v,v))
76 sage: def project(basis, v):
77 ... return sum([ inner_product(v, b)*b/norm(b)**2
81 sage: legendre_basis = [ legendre_p(k, x, a, b) for k in range(0,4) ]
82 sage: proj = project(legendre_basis, f)
83 sage: proj.simplify_trig()
84 5/2*(7*(pi^2 - 15)*x^3 - 3*(pi^4 - 21*pi^2)*x)/pi^6
88 We should agree with Maxima for all `n`::
90 sage: eq = lambda k: bool(legendre_p(k,x) == legendre_P(k,x))
91 sage: all([eq(k) for k in range(0,20) ]) # long time
94 We can evaluate the result of the zeroth polynomial::
96 sage: f = legendre_p(0,x)
100 We should have |P(a)| = |P(b)| = 1 for all a,b::
102 sage: a = RR.random_element()
103 sage: b = RR.random_element()
104 sage: k = ZZ.random_element(20)
105 sage: P = legendre_p(k, x, a, b)
106 sage: abs(P(x=a)) # abs tol 1e-12
108 sage: abs(P(x=b)) # abs tol 1e-12
111 Two different polynomials should be orthogonal with respect to the
112 inner product over `[a,b]`. Note that this test can fail if QQ is
113 replaced with RR, since integrate() can return NaN::
115 sage: a = QQ.random_element()
116 sage: b = QQ.random_element()
117 sage: j = ZZ.random_element(20)
119 sage: Pj = legendre_p(j, x, a, b)
120 sage: Pk = legendre_p(k, x, a, b)
121 sage: integrate(Pj*Pk, x, a, b) # abs tol 1e-12
124 The first few polynomials shifted to [0,1] are known to be::
128 sage: p2 = 6*x^2 - 6*x + 1
129 sage: p3 = 20*x^3 - 30*x^2 + 12*x - 1
130 sage: bool(legendre_p(0, x, 0, 1) == p0)
132 sage: bool(legendre_p(1, x, 0, 1) == p1)
134 sage: bool(legendre_p(2, x, 0, 1) == p2)
136 sage: bool(legendre_p(3, x, 0, 1) == p3)
139 The zeroth polynomial is an element of the ring that we're working
142 sage: legendre_p(0, MatrixSpace(ZZ, 2)([1, 2, -4, 7]))
148 raise TypeError('n must be a natural number')
151 raise ValueError('n must be nonnegative')
153 if not (a
in RR
and b
in RR
):
154 raise TypeError('both `a` and `b` must be real numbers')
157 # Easy base case, save time. Attempt to return a value in the
158 # same field/ring as `x`.
161 # Even though we know a,b are real we use a different ring. We
162 # prefer ZZ so that we can support division of finite field
163 # elements by (a-b). Eventually this should be supported for QQ as
164 # well, although it does not work at the moment. The preference of
165 # SR over RR is to return something attractive when e.g. a=pi.
176 # Ensure that (2**n) is an element of ZZ. This is used later --
177 # we can divide finite field elements by integers but we can't
178 # multiply them by rationals at the moment.
182 # This is an affine map from [a,b] into [-1,1] and so
183 # preserves orthogonality.
184 return (a
+ b
- 2*t
)/(a
- b
)
187 return binomial(n
,m
)*binomial(n
, n
-m
)
190 # As given in A&S, but with `x` replaced by `phi(x)`.
191 return ( ((phi(x
) - 1)**(n
-m
)) * (phi(x
) + 1)**m
)
193 # From Abramowitz & Stegun, (22.3.2) with alpha = beta = 0.
194 # Also massaged to support finite field elements.
195 P
= sum([ c(m
)*g(m
) for m
in range(0,n
+1) ])/(2**n
)