Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/functions/orthogonal_polys.py
4056 views
1
r"""
2
Orthogonal Polynomials
3
4
This module wraps some of the orthogonal/special functions in the
5
Maxima package "orthopoly". This package was written by Barton
6
Willis of the University of Nebraska at Kearney. It is released
7
under the terms of the General Public License (GPL). Send
8
Maxima-related bug reports and comments on this module to
9
[email protected]. In your report, please include Maxima and specfun
10
version information.
11
12
13
- The Chebyshev polynomial of the first kind arises as a solution
14
to the differential equation
15
16
.. math::
17
18
(1-x^2)\,y'' - x\,y' + n^2\,y = 0
19
20
21
and those of the second kind as a solution to
22
23
.. math::
24
25
(1-x^2)\,y'' - 3x\,y' + n(n+2)\,y = 0.
26
27
28
The Chebyshev polynomials of the first kind are defined by the
29
recurrence relation
30
31
.. math::
32
33
T_0(x) = 1 \, T_1(x) = x \, T_{n+1}(x) = 2xT_n(x) - T_{n-1}(x). \,
34
35
36
The Chebyshev polynomials of the second kind are defined by the
37
recurrence relation
38
39
.. math::
40
41
U_0(x) = 1 \, U_1(x) = 2x \, U_{n+1}(x) = 2xU_n(x) - U_{n-1}(x). \,
42
43
44
45
For integers `m,n`, they satisfy the orthogonality
46
relations
47
48
.. math::
49
50
\int_{-1}^1 T_n(x)T_m(x)\,\frac{dx}{\sqrt{1-x^2}} =\left\{ \begin{matrix} 0 &: n\ne m~~~~~\\ \pi &: n=m=0\\ \pi/2 &: n=m\ne 0 \end{matrix} \right.
51
52
53
and
54
55
56
.. math::
57
58
\int_{-1}^1 U_n(x)U_m(x)\sqrt{1-x^2}\,dx =\frac{\pi}{2}\delta_{m,n}.
59
60
61
62
They are named after Pafnuty Chebyshev (alternative
63
transliterations: Tchebyshef or Tschebyscheff).
64
65
- The Hermite polynomials are defined either by
66
67
.. math::
68
69
H_n(x)=(-1)^n e^{x^2/2}\frac{d^n}{dx^n}e^{-x^2/2}
70
71
72
(the "probabilists' Hermite polynomials"), or by
73
74
75
.. math::
76
77
H_n(x)=(-1)^n e^{x^2}\frac{d^n}{dx^n}e^{-x^2}
78
79
80
(the "physicists' Hermite polynomials"). Sage (via Maxima)
81
implements the latter flavor. These satisfy the orthogonality
82
relation
83
84
.. math::
85
86
\int_{-\infty}^\infty H_n(x)H_m(x)\,e^{-x^2}\,dx ={n!2^n}{\sqrt{\pi}}\delta_{nm}
87
88
89
90
They are named in honor of Charles Hermite.
91
92
- Each *Legendre polynomial* `P_n(x)` is an `n`-th degree polynomial.
93
It may be expressed using Rodrigues' formula:
94
95
.. math::
96
97
P_n(x) = (2^n n!)^{-1} {\frac{d^n}{dx^n} } \left[ (x^2 -1)^n \right].
98
99
These are solutions to Legendre's differential equation:
100
101
.. math::
102
103
{\frac{d}{dx}} \left[ (1-x^2) {\frac{d}{dx}} P(x) \right] + n(n+1)P(x) = 0.
104
105
and satisfy the orthogonality relation
106
107
.. math::
108
109
\int_{-1}^{1} P_m(x) P_n(x)\,dx = {\frac{2}{2n + 1}} \delta_{mn}
110
111
The *Legendre function of the second kind* `Q_n(x)` is another
112
(linearly independent) solution to the Legendre differential equation.
113
It is not an "orthogonal polynomial" however.
114
115
The associated Legendre functions of the first kind
116
`P_\ell^m(x)` can be given in terms of the "usual"
117
Legendre polynomials by
118
119
.. math::
120
121
\begin{array}{ll} P_\ell^m(x) &= (-1)^m(1-x^2)^{m/2}\frac{d^m}{dx^m}P_\ell(x) \\ &= \frac{(-1)^m}{2^\ell \ell!} (1-x^2)^{m/2}\frac{d^{\ell+m}}{dx^{\ell+m}}(x^2-1)^\ell. \end{array}
122
123
124
Assuming `0 \le m \le \ell`, they satisfy the orthogonality
125
relation:
126
127
.. math::
128
129
\int_{-1}^{1} P_k ^{(m)} P_\ell ^{(m)} dx = \frac{2 (\ell+m)!}{(2\ell+1)(\ell-m)!}\ \delta _{k,\ell},
130
131
132
where `\delta _{k,\ell}` is the Kronecker delta.
133
134
The associated Legendre functions of the second kind
135
`Q_\ell^m(x)` can be given in terms of the "usual"
136
Legendre polynomials by
137
138
139
.. math::
140
141
Q_\ell^m(x) = (-1)^m(1-x^2)^{m/2}\frac{d^m}{dx^m}Q_\ell(x).
142
143
144
145
They are named after Adrien-Marie Legendre.
146
147
- Laguerre polynomials may be defined by the Rodrigues formula
148
149
.. math::
150
151
L_n(x)=\frac{e^x}{n!}\frac{d^n}{dx^n}\left(e^{-x} x^n\right).
152
153
154
They are solutions of Laguerre's equation:
155
156
157
.. math::
158
159
x\,y'' + (1 - x)\,y' + n\,y = 0\,
160
161
and satisfy the orthogonality relation
162
163
164
.. math::
165
166
\int_0^\infty L_m(x) L_n(x) e^{-x}\,dx = \delta_{mn}.
167
168
169
170
The generalized Laguerre polynomials may be defined by the
171
Rodrigues formula:
172
173
174
.. math::
175
176
L_n^{(\alpha)}(x) = {\frac{x^{-\alpha} e^x}{n!}}{\frac{d^n}{dx^n}} \left(e^{-x} x^{n+\alpha}\right) .
177
178
179
(These are also sometimes called the associated Laguerre
180
polynomials.) The simple Laguerre polynomials are recovered from
181
the generalized polynomials by setting `\alpha =0`.
182
183
They are named after Edmond Laguerre.
184
185
- Jacobi polynomials are a class of orthogonal polynomials. They
186
are obtained from hypergeometric series in cases where the series
187
is in fact finite:
188
189
.. math::
190
191
P_n^{(\alpha,\beta)}(z) =\frac{(\alpha+1)_n}{n!} \,_2F_1\left(-n,1+\alpha+\beta+n;\alpha+1;\frac{1-z}{2}\right) ,
192
193
194
where `()_n` is Pochhammer's symbol (for the rising
195
factorial), (Abramowitz and Stegun p561.) and thus have the
196
explicit expression
197
198
199
.. math::
200
201
P_n^{(\alpha,\beta)} (z) = \frac{\Gamma (\alpha+n+1)}{n!\Gamma (\alpha+\beta+n+1)} \sum_{m=0}^n {n\choose m} \frac{\Gamma (\alpha + \beta + n + m + 1)}{\Gamma (\alpha + m + 1)} \left(\frac{z-1}{2}\right)^m .
202
203
204
205
They are named after Carl Jacobi.
206
207
- Ultraspherical or Gegenbauer polynomials are given in terms of
208
the Jacobi polynomials `P_n^{(\alpha,\beta)}(x)` with
209
`\alpha=\beta=a-1/2` by
210
211
212
.. math::
213
214
C_n^{(a)}(x)= \frac{\Gamma(a+1/2)}{\Gamma(2a)}\frac{\Gamma(n+2a)}{\Gamma(n+a+1/2)} P_n^{(a-1/2,a-1/2)}(x).
215
216
217
They satisfy the orthogonality relation
218
219
.. math::
220
221
\int_{-1}^1(1-x^2)^{a-1/2}C_m^{(a)}(x)C_n^{(a)}(x)\, dx =\delta_{mn}2^{1-2a}\pi \frac{\Gamma(n+2a)}{(n+a)\Gamma^2(a)\Gamma(n+1)} ,
222
223
224
for `a>-1/2`. They are obtained from hypergeometric series
225
in cases where the series is in fact finite:
226
227
228
.. math::
229
230
C_n^{(a)}(z) =\frac{(2a)^{\underline{n}}}{n!} \,_2F_1\left(-n,2a+n;a+\frac{1}{2};\frac{1-z}{2}\right)
231
232
233
where `\underline{n}` is the falling factorial. (See
234
Abramowitz and Stegun p561)
235
236
They are named for Leopold Gegenbauer (1849-1903).
237
238
239
For completeness, the Pochhammer symbol, introduced by Leo August
240
Pochhammer, `(x)_n`, is used in the theory of special
241
functions to represent the "rising factorial" or "upper factorial"
242
243
.. math::
244
245
(x)_n=x(x+1)(x+2)\cdots(x+n-1)=\frac{(x+n-1)!}{(x-1)!}.
246
247
248
On the other hand, the "falling factorial" or "lower factorial" is
249
250
.. math::
251
252
x^{\underline{n}}=\frac{x!}{(x-n)!} ,
253
254
255
in the notation of Ronald L. Graham, Donald E. Knuth and Oren
256
Patashnik in their book Concrete Mathematics.
257
258
.. note::
259
260
The first call of any of these will usually cost a bit extra
261
(it loads "specfun", but I'm not sure if that is the real reason).
262
The next call is usually faster but not always.
263
264
TODO: Implement associated Legendre polynomials and Zernike
265
polynomials. (Neither is in Maxima.)
266
http://en.wikipedia.org/wiki/Associated_Legendre_polynomials
267
http://en.wikipedia.org/wiki/Zernike_polynomials
268
269
REFERENCES:
270
271
- Abramowitz and Stegun: Handbook of Mathematical Functions,
272
http://www.math.sfu.ca/ cbm/aands/
273
274
- http://en.wikipedia.org/wiki/Chebyshev_polynomials
275
276
- http://en.wikipedia.org/wiki/Legendre_polynomials
277
278
- http://en.wikipedia.org/wiki/Hermite_polynomials
279
280
- http://mathworld.wolfram.com/GegenbauerPolynomial.html
281
282
- http://en.wikipedia.org/wiki/Jacobi_polynomials
283
284
- http://en.wikipedia.org/wiki/Laguerre_polynomia
285
286
- http://en.wikipedia.org/wiki/Associated_Legendre_polynomials
287
288
289
AUTHORS:
290
291
- David Joyner (2006-06)
292
"""
293
294
#*****************************************************************************
295
# Copyright (C) 2006 William Stein <[email protected]>
296
# 2006 David Joyner <[email protected]>
297
#
298
# Distributed under the terms of the GNU General Public License (GPL)
299
#
300
# This code is distributed in the hope that it will be useful,
301
# but WITHOUT ANY WARRANTY; without even the implied warranty of
302
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
303
# General Public License for more details.
304
#
305
# The full text of the GPL is available at:
306
#
307
# http://www.gnu.org/licenses/
308
#*****************************************************************************
309
310
from sage.misc.sage_eval import sage_eval
311
from sage.rings.all import ZZ
312
from sage.calculus.calculus import maxima
313
314
_done = False
315
def _init():
316
"""
317
Internal function which checks if Maxima has loaded the
318
"orthopoly" package. All functions using this in this
319
file should call this function first.
320
321
TEST:
322
323
The global starts ``False``::
324
325
sage: sage.functions.orthogonal_polys._done
326
False
327
328
Then after using one of these functions, it changes::
329
330
sage: from sage.functions.orthogonal_polys import chebyshev_T
331
sage: chebyshev_T(2,x)
332
2*(x - 1)^2 + 4*x - 3
333
sage: sage.functions.orthogonal_polys._done
334
True
335
336
Note that because here we use a Pynac variable ``x``,
337
the representation of the function is different from
338
its actual doctest, where a polynomial indeterminate
339
``x`` is used.
340
"""
341
global _done
342
if _done:
343
return
344
maxima.eval('load("orthopoly");')
345
# TODO -- make it possible to use the intervals returned
346
# instead of just discarding this info!
347
maxima.eval('orthopoly_returns_intervals:false;')
348
_done = True
349
350
351
def chebyshev_T(n,x):
352
"""
353
Returns the Chebyshev function of the first kind for integers
354
`n>-1`.
355
356
REFERENCE:
357
358
- AS 22.5.31 page 778 and AS 6.1.22 page 256.
359
360
EXAMPLES::
361
362
sage: x = PolynomialRing(QQ, 'x').gen()
363
sage: chebyshev_T(2,x)
364
2*x^2 - 1
365
"""
366
_init()
367
return sage_eval(maxima.eval('chebyshev_t(%s,x)'%ZZ(n)), locals={'x':x})
368
369
def chebyshev_U(n,x):
370
"""
371
Returns the Chebyshev function of the second kind for integers `n>-1`.
372
373
REFERENCE:
374
375
- AS, 22.8.3 page 783 and AS 6.1.22 page 256.
376
377
EXAMPLES::
378
379
sage: x = PolynomialRing(QQ, 'x').gen()
380
sage: chebyshev_U(2,x)
381
4*x^2 - 1
382
"""
383
_init()
384
return sage_eval(maxima.eval('chebyshev_u(%s,x)'%ZZ(n)), locals={'x':x})
385
386
def gen_laguerre(n,a,x):
387
"""
388
Returns the generalized Laguerre polynomial for integers `n > -1`.
389
Typically, a = 1/2 or a = -1/2.
390
391
REFERENCE:
392
393
- table on page 789 in AS.
394
395
EXAMPLES::
396
397
sage: x = PolynomialRing(QQ, 'x').gen()
398
sage: gen_laguerre(2,1,x)
399
1/2*x^2 - 3*x + 3
400
sage: gen_laguerre(2,1/2,x)
401
1/2*x^2 - 5/2*x + 15/8
402
sage: gen_laguerre(2,-1/2,x)
403
1/2*x^2 - 3/2*x + 3/8
404
sage: gen_laguerre(2,0,x)
405
1/2*x^2 - 2*x + 1
406
sage: gen_laguerre(3,0,x)
407
-1/6*x^3 + 3/2*x^2 - 3*x + 1
408
"""
409
from sage.functions.all import sqrt
410
_init()
411
return sage_eval(maxima.eval('gen_laguerre(%s,%s,x)'%(ZZ(n),a)), locals={'x':x})
412
413
def gen_legendre_P(n,m,x):
414
r"""
415
Returns the generalized (or associated) Legendre function of the
416
first kind for integers `n > -1, m > -1`.
417
418
The awkward code for when m is odd and 1 results from the fact that
419
Maxima is happy with, for example, `(1 - t^2)^3/2`, but
420
Sage is not. For these cases the function is computed from the
421
(m-1)-case using one of the recursions satisfied by the Legendre
422
functions.
423
424
REFERENCE:
425
426
- Gradshteyn and Ryzhik 8.706 page 1000.
427
428
EXAMPLES::
429
430
sage: P.<t> = QQ[]
431
sage: gen_legendre_P(2, 0, t)
432
3/2*t^2 - 1/2
433
sage: gen_legendre_P(2, 0, t) == legendre_P(2, t)
434
True
435
sage: gen_legendre_P(3, 1, t)
436
-3/2*sqrt(-t^2 + 1)*(5*t^2 - 1)
437
sage: gen_legendre_P(4, 3, t)
438
105*sqrt(-t^2 + 1)*(t^2 - 1)*t
439
sage: gen_legendre_P(7, 3, I).expand()
440
-16695*sqrt(2)
441
sage: gen_legendre_P(4, 1, 2.5)
442
-583.562373654533*I
443
"""
444
from sage.functions.all import sqrt
445
_init()
446
if m.mod(2).is_zero() or m.is_one():
447
return sage_eval(maxima.eval('assoc_legendre_p(%s,%s,x)'%(ZZ(n),ZZ(m))), locals={'x':x})
448
else:
449
return sqrt(1-x**2)*(((n-m+1)*x*gen_legendre_P(n,m-1,x)-(n+m-1)*gen_legendre_P(n-1,m-1,x))/(1-x**2))
450
451
def gen_legendre_Q(n,m,x):
452
"""
453
Returns the generalized (or associated) Legendre function of the
454
second kind for integers `n>-1`, `m>-1`.
455
456
Maxima restricts m = n. Hence the cases m n are computed using the
457
same recursion used for gen_legendre_P(n,m,x) when m is odd and
458
1.
459
460
EXAMPLES::
461
462
sage: P.<t> = QQ[]
463
sage: gen_legendre_Q(2,0,t)
464
3/4*t^2*log(-(t + 1)/(t - 1)) - 3/2*t - 1/4*log(-(t + 1)/(t - 1))
465
sage: gen_legendre_Q(2,0,t) - legendre_Q(2, t)
466
0
467
sage: gen_legendre_Q(3,1,0.5)
468
2.49185259170895
469
sage: gen_legendre_Q(0, 1, x)
470
-1/sqrt(-x^2 + 1)
471
sage: gen_legendre_Q(2, 4, x).factor()
472
48*x/((x - 1)^2*(x + 1)^2)
473
"""
474
from sage.functions.all import sqrt
475
if m <= n:
476
_init()
477
return sage_eval(maxima.eval('assoc_legendre_q(%s,%s,x)'%(ZZ(n),ZZ(m))), locals={'x':x})
478
if m == n + 1 or n == 0:
479
if m.mod(2).is_zero():
480
denom = (1 - x**2)**(m/2)
481
else:
482
denom = sqrt(1 - x**2)*(1 - x**2)**((m-1)/2)
483
if m == n + 1:
484
return (-1)**m*(m-1).factorial()*2**n/denom
485
else:
486
return (-1)**m*(m-1).factorial()*((x+1)**m - (x-1)**m)/(2*denom)
487
else:
488
return ((n-m+1)*x*gen_legendre_Q(n,m-1,x)-(n+m-1)*gen_legendre_Q(n-1,m-1,x))/sqrt(1-x**2)
489
490
def hermite(n,x):
491
"""
492
Returns the Hermite polynomial for integers `n > -1`.
493
494
REFERENCE:
495
496
- AS 22.5.40 and 22.5.41, page 779.
497
498
EXAMPLES::
499
500
sage: x = PolynomialRing(QQ, 'x').gen()
501
sage: hermite(2,x)
502
4*x^2 - 2
503
sage: hermite(3,x)
504
8*x^3 - 12*x
505
sage: hermite(3,2)
506
40
507
sage: S.<y> = PolynomialRing(RR)
508
sage: hermite(3,y)
509
8.00000000000000*y^3 - 12.0000000000000*y
510
sage: R.<x,y> = QQ[]
511
sage: hermite(3,y^2)
512
8*y^6 - 12*y^2
513
sage: w = var('w')
514
sage: hermite(3,2*w)
515
8*(8*w^2 - 3)*w
516
"""
517
_init()
518
return sage_eval(maxima.eval('hermite(%s,x)'%ZZ(n)), locals={'x':x})
519
520
def jacobi_P(n,a,b,x):
521
r"""
522
Returns the Jacobi polynomial `P_n^{(a,b)}(x)` for
523
integers `n > -1` and a and b symbolic or `a > -1`
524
and `b > -1`. The Jacobi polynomials are actually defined
525
for all a and b. However, the Jacobi polynomial weight
526
`(1-x)^a(1+x)^b` isn't integrable for `a \leq -1`
527
or `b \leq -1`.
528
529
REFERENCE:
530
531
- table on page 789 in AS.
532
533
EXAMPLES::
534
535
sage: x = PolynomialRing(QQ, 'x').gen()
536
sage: jacobi_P(2,0,0,x)
537
3/2*x^2 - 1/2
538
sage: jacobi_P(2,1,2,1.2) # random output of low order bits
539
5.009999999999998
540
"""
541
_init()
542
return sage_eval(maxima.eval('jacobi_p(%s,%s,%s,x)'%(ZZ(n),a,b)), locals={'x':x})
543
544
def laguerre(n,x):
545
"""
546
Returns the Laguerre polynomial for integers `n > -1`.
547
548
REFERENCE:
549
550
- AS 22.5.16, page 778 and AS page 789.
551
552
EXAMPLES::
553
554
sage: x = PolynomialRing(QQ, 'x').gen()
555
sage: laguerre(2,x)
556
1/2*x^2 - 2*x + 1
557
sage: laguerre(3,x)
558
-1/6*x^3 + 3/2*x^2 - 3*x + 1
559
sage: laguerre(2,2)
560
-1
561
"""
562
_init()
563
return sage_eval(maxima.eval('laguerre(%s,x)'%ZZ(n)), locals={'x':x})
564
565
def legendre_P(n,x):
566
"""
567
Returns the Legendre polynomial of the first kind for integers
568
`n > -1`.
569
570
REFERENCE:
571
572
- AS 22.5.35 page 779.
573
574
EXAMPLES::
575
576
sage: P.<t> = QQ[]
577
sage: legendre_P(2,t)
578
3/2*t^2 - 1/2
579
sage: legendre_P(3, 1.1)
580
1.67750000000000
581
sage: legendre_P(3, 1 + I)
582
7/2*I - 13/2
583
sage: legendre_P(3, MatrixSpace(ZZ, 2)([1, 2, -4, 7]))
584
[-179 242]
585
[-484 547]
586
sage: legendre_P(3, GF(11)(5))
587
8
588
"""
589
_init()
590
return sage_eval(maxima.eval('legendre_p(%s,x)'%ZZ(n)), locals={'x':x})
591
592
def legendre_Q(n,x):
593
"""
594
Returns the Legendre function of the second kind for integers
595
`n>-1`.
596
597
Computed using Maxima.
598
599
EXAMPLES::
600
601
sage: P.<t> = QQ[]
602
sage: legendre_Q(2, t)
603
3/4*t^2*log(-(t + 1)/(t - 1)) - 3/2*t - 1/4*log(-(t + 1)/(t - 1))
604
sage: legendre_Q(3, 0.5)
605
-0.198654771479482
606
sage: legendre_Q(4, 2)
607
443/16*I*pi + 443/16*log(3) - 365/12
608
sage: legendre_Q(4, 2.0)
609
0.00116107583162324 + 86.9828465962674*I
610
"""
611
_init()
612
return sage_eval(maxima.eval('legendre_q(%s,x)'%ZZ(n)), locals={'x':x})
613
614
def ultraspherical(n,a,x):
615
"""
616
Returns the ultraspherical (or Gegenbauer) polynomial for integers
617
`n > -1`.
618
619
Computed using Maxima.
620
621
REFERENCE:
622
623
- AS 22.5.27
624
625
EXAMPLES::
626
627
sage: x = PolynomialRing(QQ, 'x').gen()
628
sage: ultraspherical(2,3/2,x)
629
15/2*x^2 - 3/2
630
sage: ultraspherical(2,1/2,x)
631
3/2*x^2 - 1/2
632
sage: ultraspherical(1,1,x)
633
2*x
634
sage: t = PolynomialRing(RationalField(),"t").gen()
635
sage: gegenbauer(3,2,t)
636
32*t^3 - 12*t
637
"""
638
_init()
639
return sage_eval(maxima.eval('ultraspherical(%s,%s,x)'%(ZZ(n),a)), locals={'x':x})
640
641
gegenbauer = ultraspherical
642
643