This repository was archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathorthogonal_polys.py
3032 lines (2431 loc) · 96.4 KB
/
orthogonal_polys.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
r"""
Orthogonal polynomials
Chebyshev polynomials
---------------------
The Chebyshev polynomial of the first kind arises as a solution
to the differential equation
.. MATH::
(1-x^2)\,y'' - x\,y' + n^2\,y = 0
and those of the second kind as a solution to
.. MATH::
(1-x^2)\,y'' - 3x\,y' + n(n+2)\,y = 0.
The Chebyshev polynomials of the first kind are defined by the
recurrence relation
.. MATH::
T_0(x) = 1, \qquad T_1(x) = x, \qquad T_{n+1}(x) = 2xT_n(x) - T_{n-1}(x).
The Chebyshev polynomials of the second kind are defined by the
recurrence relation
.. MATH::
U_0(x) = 1, \qquad U_1(x) = 2x, \qquad U_{n+1}(x) = 2xU_n(x) - U_{n-1}(x).
For integers `m,n`, they satisfy the orthogonality relations
.. MATH::
\int_{-1}^1 T_n(x)T_m(x)\,\frac{dx}{\sqrt{1-x^2}} =
\left\{ \begin{array}{cl} 0 & \text{if } n\neq m, \\ \pi & \text{if } n=m=0, \\ \pi/2 & \text{if } n = m \neq 0, \end{array} \right.
and
.. MATH::
\int_{-1}^1 U_n(x)U_m(x)\sqrt{1-x^2}\,dx =\frac{\pi}{2}\delta_{m,n}.
They are named after Pafnuty Chebyshev (1821-1894, alternative
transliterations: Tchebyshef or Tschebyscheff).
Hermite polynomials
-------------------
The *Hermite polynomials* are defined either by
.. MATH::
H_n(x) = (-1)^n e^{x^2/2} \frac{d^n}{dx^n} e^{-x^2/2}
(the "probabilists' Hermite polynomials"), or by
.. MATH::
H_n(x) = (-1)^n e^{x^2}\frac{d^n}{dx^n}e^{-x^2}
(the "physicists' Hermite polynomials"). Sage (via Maxima) implements
the latter flavor. These satisfy the orthogonality relation
.. MATH::
\int_{-\infty}^{\infty} H_n(x) H_m(x) \, e^{-x^2} \, dx
= \sqrt{\pi} n! 2^n \delta_{nm}.
They are named in honor of Charles Hermite (1822-1901), but were first
introduced by Laplace in 1810 and also studied by Chebyshev in 1859.
Legendre polynomials
--------------------
Each *Legendre polynomial* `P_n(x)` is an `n`-th degree polynomial.
It may be expressed using Rodrigues' formula:
.. MATH::
P_n(x) = (2^n n!)^{-1} {\frac{d^n}{dx^n} } \left[ (x^2 -1)^n \right].
These are solutions to Legendre's differential equation:
.. MATH::
\frac{d}{dx} \left[ (1-x^2) {\frac{d}{dx}} P(x) \right] + n(n+1)P(x) = 0
and satisfy the orthogonality relation
.. MATH::
\int_{-1}^{1} P_m(x) P_n(x)\,dx = {\frac{2}{2n + 1}} \delta_{mn}.
The *Legendre function of the second kind* `Q_n(x)` is another
(linearly independent) solution to the Legendre differential equation.
It is not an "orthogonal polynomial" however.
The associated Legendre functions of the first kind `P_\ell^m(x)` can
be given in terms of the "usual" Legendre polynomials by
.. MATH::
\begin{aligned}
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{aligned}
Assuming `0 \le m \le \ell`, they satisfy the orthogonality relation:
.. MATH::
\int_{-1}^{1} P_k^{(m)} P_{\ell}^{(m)} dx
= \frac{2(\ell+m)!}{(2\ell+1)(\ell-m)!}\ \delta _{k,\ell},
where `\delta _{k,\ell}` is the Kronecker delta.
The associated Legendre functions of the second kind
`Q_\ell^m(x)` can be given in terms of the "usual"
Legendre polynomials by
.. MATH::
Q_{\ell}^m(x) = (-1)^m (1-x^2)^{m/2} \frac{d^m}{dx^m} Q_{\ell}(x).
They are named after Adrien-Marie Legendre (1752-1833).
Laguerre polynomials
--------------------
*Laguerre polynomials* may be defined by the Rodrigues formula
.. MATH::
L_n(x) = \frac{e^x}{n!} \frac{d^n}{dx^n} \left( e^{-x} x^n \right).
They are solutions of Laguerre's equation:
.. MATH::
x\,y'' + (1 - x)\,y' + n\,y = 0
and satisfy the orthogonality relation
.. MATH::
\int_0^{\infty} L_m(x) L_n(x) e^{-x} \, dx = \delta_{mn}.
The generalized Laguerre polynomials may be defined by the Rodrigues formula:
.. MATH::
L_n^{(\alpha)}(x) = \frac{x^{-\alpha} e^x}{n!} \frac{d^n}{dx^n}
\left(e^{-x} x^{n+\alpha}\right).
(These are also sometimes called the associated Laguerre
polynomials.) The simple Laguerre polynomials are recovered from
the generalized polynomials by setting `\alpha = 0`.
They are named after Edmond Laguerre (1834-1886).
Jacobi polynomials
------------------
*Jacobi polynomials* are a class of orthogonal polynomials. They
are obtained from hypergeometric series in cases where the series
is in fact finite:
.. MATH::
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),
where `()_n` is Pochhammer's symbol (for the rising factorial),
(Abramowitz and Stegun p561.) and thus have the explicit expression
.. MATH::
P_n^{(\alpha,\beta)} (z) = \frac{\Gamma(\alpha+n+1)}{n!\Gamma(\alpha+\beta+n+1)}
\sum_{m=0}^n \binom{n}{m} \frac{\Gamma(\alpha+\beta+n+m+1)}{\Gamma(\alpha+m+1)}
\left(\frac{z-1}{2}\right)^m.
They are named after Carl Gustav Jaboc Jacobi (1804-1851).
Gegenbauer polynomials
----------------------
*Ultraspherical* or *Gegenbauer polynomials* are given in terms of
the Jacobi polynomials `P_n^{(\alpha,\beta)}(x)` with
`\alpha = \beta = a - 1/2` by
.. MATH::
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).
They satisfy the orthogonality relation
.. MATH::
\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)},
for `a > -1/2`. They are obtained from hypergeometric series
in cases where the series is in fact finite:
.. MATH::
C_n^{(a)}(z) = \frac{(2a)^{\underline{n}}}{n!}
\,_2F_1\left(-n,2a+n; a+\frac{1}{2}; \frac{1-z}{2}\right)
where `\underline{n}` is the falling factorial. (See
Abramowitz and Stegun p561.)
They are named for Leopold Gegenbauer (1849-1903).
Krawtchouk polynomials
----------------------
The *Krawtchouk polynomials* are discrete orthogonal polynomials that
are given by the hypergeometric series
.. MATH::
K_j(x; n, p) = (-1)^j \binom{n}{j} p^j
\,_{2}F_1\left(-j,-x; -n; p^{-1}\right).
Since they are discrete orthogonal polynomials, they satisfy an orthogonality
relation defined on a discrete (in this case finite) set of points:
.. MATH::
\sum_{m=0}^n K_i(m; n, p) K_j(m; n, p) \, \binom{n}{m} p^m q^{n-m}
= \binom{n}{j} (pq)^j \delta_{ij},
where `q = 1 - p`. They can also be described by the recurrence relation
.. MATH::
j K_j(x; n, p) = (x - (n-j+1) p - (j-1) q) K_{j-1}(x; n, p)
- p q (n - j + 2) K_{j-2}(x; n, p),
where `K_0(x; n, p) = 1` and `K_1(x; n, p) = x - n p`.
They are named for Mykhailo Krawtchouk (1892-1942).
Meixner polynomials
-------------------
The *Meixner polynomials* are discrete orthogonal polynomials that
are given by the hypergeometric series
.. MATH::
M_n(x; n, p) = (-1)^j \binom{n}{j} p^j
\,_{2}F_1\left(-j,-x; -n; p^{-1}\right).
They satisfy an orthogonality relation:
.. MATH::
\sum_{k=0}^{\infty} \tilde{M}_n(k; b, c) \tilde{M}_m(k; b, c) \, \frac{(b)_k}{k!} c^k
= \frac{c^{-n} n!}{(b)_n (1-c)^b} \delta_{mn},
where `\tilde{M}_n(x; b, c) = M_n(x; b, c) / (b)_x`, for `b > 0 ` and
`0 < c < 1`. They can also be described by the recurrence relation
.. MATH::
\begin{aligned}
c (n-1+b) M_n(x; b, c) & = ((c-1) x + n-1 + c (n-1+b)) (b+n-1) M_{n-1}(x; b, c)
\\ & \qquad - (b+n-1) (b+n-2) (n-1) M_{n-2}(x; b, c),
\end{aligned}
where `M_0(x; b, c) = 0` and `M_1(x; b, c) = (1 - c^{-1}) x + b`.
They are named for Josef Meixner (1908-1994).
Hahn polynomials
----------------
The *Hahn polynomials* are discrete orthogonal polynomials that
are given by the hypergeometric series
.. MATH::
Q_k(x; a, b, n) = \,_{3}F_2\left(-k,k+a+b+1,-x; a+1,-n; 1\right).
They satisfy an orthogonality relation:
.. MATH::
\sum_{k=0}^{n-1} Q_i(k; a, b, n) Q_j(k; a, b, n) \, \rho(k)
= \frac{\delta_{ij}}{\pi_i},
where
.. MATH::
\begin{aligned}
\rho(k) &= \binom{a+k}{k} \binom{b+n-k}{n-k},
\\
\pi_i &= \delta_{ij} \frac{(-1)^i i! (b+1)_i (i+a+b+1)_{n+1}}{n! (2i+a+b+1) (-n)_i (a+1)_i}.
\end{aligned}
They can also be described by the recurrence relation
.. MATH::
A Q_k(x; a,b,n) = (-x + A + C) Q_{k-1}(x; a,b,n) - C Q_{k-2}(x; a,b,n),
where `Q_0(x; a,b,n) = 1` and `Q_1(x; a,b,n) = 1 - \frac{a+b+2}{(a+1)n} x` and
.. MATH::
A = \frac{(k+a+b) (k+a) (n-k+1)}{(2k+a+b-1) (2k+a+b)},
\qquad
C = \frac{(k-1) (k+b-1) (k+a+b+n)}{(2k+a+b-2) (2k+a+b-1)}.
They are named for Wolfgang Hahn (1911-1998), although they were first
introduced by Chebyshev in 1875.
Pochhammer symbol
-----------------
For completeness, the *Pochhammer symbol*, introduced by Leo August
Pochhammer, `(x)_n`, is used in the theory of special
functions to represent the "rising factorial" or "upper factorial"
.. MATH::
(x)_n = x(x+1)(x+2) \cdots (x+n-1) = \frac{(x+n-1)!}{(x-1)!}.
On the other hand, the *falling factorial* or *lower factorial* is
.. MATH::
x^{\underline{n}} = \frac{x!}{(x-n)!},
in the notation of Ronald L. Graham, Donald E. Knuth and Oren Patashnik
in their book Concrete Mathematics.
.. TODO::
Implement Zernike polynomials.
:wikipedia:`Zernike_polynomials`
REFERENCES:
- [AS1964]_
- :wikipedia:`Chebyshev_polynomials`
- :wikipedia:`Legendre_polynomials`
- :wikipedia:`Hermite_polynomials`
- http://mathworld.wolfram.com/GegenbauerPolynomial.html
- :wikipedia:`Jacobi_polynomials`
- :wikipedia:`Laguerre_polynomia`
- :wikipedia:`Associated_Legendre_polynomials`
- :wikipedia:`Kravchuk_polynomials`
- :wikipedia:`Meixner_polynomials`
- :wikipedia:`Hahn_polynomials`
- Roelof Koekeok and René F. Swarttouw, :arxiv:`math/9602214`
- [Koe1999]_
AUTHORS:
- David Joyner (2006-06)
- Stefan Reiterer (2010-)
- Ralf Stephan (2015-)
The original module wrapped some of the orthogonal/special functions
in the Maxima package "orthopoly" and was written by Barton
Willis of the University of Nebraska at Kearney.
"""
# ****************************************************************************
# Copyright (C) 2006 William Stein <[email protected]>
# 2006 David Joyner <[email protected]>
# 2010 Stefan Reiterer <[email protected]>
#
# Distributed under the terms of the GNU General Public License (GPL)
#
# This code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# The full text of the GPL is available at:
#
# https://www.gnu.org/licenses/
# ****************************************************************************
import warnings
from sage.misc.latex import latex
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ
from sage.rings.real_mpfr import RR
from sage.rings.cc import CC
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
import sage.rings.abc
from sage.symbolic.function import BuiltinFunction, GinacFunction
from sage.symbolic.expression import Expression
from sage.symbolic.ring import SR
from sage.functions.other import factorial, binomial
from sage.structure.element import parent
from sage.arith.misc import rising_factorial
class OrthogonalFunction(BuiltinFunction):
"""
Base class for orthogonal polynomials.
This class is an abstract base class for all orthogonal polynomials since
they share similar properties. The evaluation as a polynomial
is either done via maxima, or with pynac.
Convention: The first argument is always the order of the polynomial,
the others are other values or parameters where the polynomial is
evaluated.
"""
def __init__(self, name, nargs=2, latex_name=None, conversions=None):
"""
:class:`OrthogonalFunction` class needs the same input parameter as
it's parent class.
EXAMPLES::
sage: from sage.functions.orthogonal_polys import OrthogonalFunction
sage: new = OrthogonalFunction('testo_P')
sage: new
testo_P
"""
self._maxima_name = None
if conversions:
try:
self._maxima_name = conversions['maxima']
except KeyError:
pass
super(OrthogonalFunction, self).__init__(name=name, nargs=nargs,
latex_name=latex_name,
conversions=conversions)
def eval_formula(self, *args):
"""
Evaluate this polynomial using an explicit formula.
EXAMPLES::
sage: from sage.functions.orthogonal_polys import OrthogonalFunction
sage: P = OrthogonalFunction('testo_P')
sage: P.eval_formula(1,2.0)
Traceback (most recent call last):
...
NotImplementedError: no explicit calculation of values implemented
"""
raise NotImplementedError("no explicit calculation of values implemented")
def _eval_special_values_(self, *args):
"""
Evaluate the polynomial explicitly for special values.
EXAMPLES::
sage: var('n')
n
sage: chebyshev_T(n,-1)
(-1)^n
"""
raise ValueError("no special values known")
def _eval_(self, n, *args):
"""
The :meth:`_eval_()` method decides which evaluation suits best
for the given input, and returns a proper value.
EXAMPLES::
sage: var('n,x')
(n, x)
sage: chebyshev_T(5,x)
16*x^5 - 20*x^3 + 5*x
"""
return None
def __call__(self, *args, **kwds):
"""
This overides the call method from SageObject to avoid problems with coercions,
since the _eval_ method is able to handle more data types than symbolic functions
would normally allow.
Thus we have the distinction between algebraic objects (if n is an integer),
and else as symbolic function.
EXAMPLES::
sage: chebyshev_T(5, x)
16*x^5 - 20*x^3 + 5*x
sage: chebyshev_T(5, x, algorithm='pari')
16*x^5 - 20*x^3 + 5*x
sage: chebyshev_T(5, x, algorithm='maxima')
16*x^5 - 20*x^3 + 5*x
sage: chebyshev_T(5, x, algorithm='recursive')
16*x^5 - 20*x^3 + 5*x
"""
algorithm = kwds.get('algorithm', None)
if algorithm == 'pari':
return self.eval_pari(*args, **kwds)
elif algorithm == 'recursive':
return self.eval_recursive(*args, **kwds)
elif algorithm == 'maxima':
from sage.calculus.calculus import maxima
kwds['hold'] = True
return maxima(self._eval_(*args, **kwds))._sage_()
return super(OrthogonalFunction, self).__call__(*args, **kwds)
class ChebyshevFunction(OrthogonalFunction):
"""
Abstract base class for Chebyshev polynomials of the first and second kind.
EXAMPLES::
sage: chebyshev_T(3,x)
4*x^3 - 3*x
"""
def __call__(self, n, *args, **kwds):
"""
This overides the call method from SageObject to avoid problems with coercions,
since the _eval_ method is able to handle more data types than symbolic functions
would normally allow.
Thus we have the distinction between algebraic objects (if n is an integer),
and else as symbolic function.
EXAMPLES::
sage: K.<a> = NumberField(x^3-x-1)
sage: chebyshev_T(5, a)
16*a^2 + a - 4
sage: chebyshev_T(5,MatrixSpace(ZZ, 2)([1, 2, -4, 7]))
[-40799 44162]
[-88324 91687]
sage: R.<x> = QQ[]
sage: parent(chebyshev_T(5, x))
Univariate Polynomial Ring in x over Rational Field
sage: chebyshev_T(5, 2, hold=True)
chebyshev_T(5, 2)
sage: chebyshev_T(1,2,3)
Traceback (most recent call last):
...
TypeError: Symbolic function chebyshev_T takes exactly 2 arguments (3 given)
"""
# If n is an integer: consider the polynomial as an algebraic (not symbolic) object
if n in ZZ and not kwds.get('hold', False):
try:
return self._eval_(n, *args)
except Exception:
pass
return super(ChebyshevFunction, self).__call__(n, *args, **kwds)
def _eval_(self, n, x):
"""
The :meth:`_eval_()` method decides which evaluation suits best
for the given input, and returns a proper value.
EXAMPLES::
sage: var('n,x')
(n, x)
sage: chebyshev_T(5,x)
16*x^5 - 20*x^3 + 5*x
sage: chebyshev_T(64, x)
2*(2*(2*(2*(2*(2*x^2 - 1)^2 - 1)^2 - 1)^2 - 1)^2 - 1)^2 - 1
sage: chebyshev_T(n,-1)
(-1)^n
sage: chebyshev_T(-7,x)
64*x^7 - 112*x^5 + 56*x^3 - 7*x
sage: chebyshev_T(3/2,x)
chebyshev_T(3/2, x)
sage: R.<t> = QQ[]
sage: chebyshev_T(2,t)
2*t^2 - 1
sage: chebyshev_U(2,t)
4*t^2 - 1
sage: parent(chebyshev_T(4, RIF(5)))
Real Interval Field with 53 bits of precision
sage: RR2 = RealField(5)
sage: chebyshev_T(100000,RR2(2))
8.9e57180
sage: chebyshev_T(5,Qp(3)(2))
2 + 3^2 + 3^3 + 3^4 + 3^5 + O(3^20)
sage: chebyshev_T(100001/2, 2)
...chebyshev_T(100001/2, 2)
sage: chebyshev_U._eval_(1.5, Mod(8,9)) is None
True
"""
# n is an integer => evaluate algebraically (as polynomial)
if n in ZZ:
n = ZZ(n)
# Expanded symbolic expression only for small values of n
if isinstance(x, Expression) and n.abs() < 32:
return self.eval_formula(n, x)
return self.eval_algebraic(n, x)
if isinstance(x, Expression) or isinstance(n, Expression):
# Check for known identities
try:
return self._eval_special_values_(n, x)
except ValueError:
# Don't evaluate => keep symbolic
return None
# n is not an integer and neither n nor x is symbolic.
# We assume n and x are real/complex and evaluate numerically
try:
import sage.libs.mpmath.all as mpmath
return self._evalf_(n, x)
except mpmath.NoConvergence:
warnings.warn("mpmath failed, keeping expression unevaluated",
RuntimeWarning)
return None
except Exception:
# Numerical evaluation failed => keep symbolic
return None
class Func_chebyshev_T(ChebyshevFunction):
"""
Chebyshev polynomials of the first kind.
REFERENCE:
- [AS1964]_ 22.5.31 page 778 and 6.1.22 page 256.
EXAMPLES::
sage: chebyshev_T(5,x)
16*x^5 - 20*x^3 + 5*x
sage: var('k')
k
sage: test = chebyshev_T(k,x)
sage: test
chebyshev_T(k, x)
"""
def __init__(self):
"""
Init method for the chebyshev polynomials of the first kind.
EXAMPLES::
sage: var('n, x')
(n, x)
sage: from sage.functions.orthogonal_polys import Func_chebyshev_T
sage: chebyshev_T2 = Func_chebyshev_T()
sage: chebyshev_T2(1,x)
x
sage: chebyshev_T(x, x)._sympy_()
chebyshevt(x, x)
sage: maxima(chebyshev_T(1,x, hold=True))
_SAGE_VAR_x
sage: maxima(chebyshev_T(n, chebyshev_T(n, x)))
chebyshev_t(_SAGE_VAR_n,chebyshev_t(_SAGE_VAR_n,_SAGE_VAR_x))
"""
ChebyshevFunction.__init__(self, 'chebyshev_T', nargs=2,
conversions=dict(maxima='chebyshev_t',
mathematica='ChebyshevT',
sympy='chebyshevt',
giac='tchebyshev1'))
def _latex_(self):
r"""
TESTS::
sage: latex(chebyshev_T)
T_n
"""
return r"T_n"
def _print_latex_(self, n, z):
r"""
TESTS::
sage: latex(chebyshev_T(3, x, hold=True))
T_{3}\left(x\right)
"""
return r"T_{{{}}}\left({}\right)".format(latex(n), latex(z))
def _eval_special_values_(self, n, x):
"""
Values known for special values of x.
For details see [AS1964]_ 22.4 (p. 777)
EXAMPLES::
sage: var('n')
n
sage: chebyshev_T(n,1)
1
sage: chebyshev_T(n,0)
1/2*(-1)^(1/2*n)*((-1)^n + 1)
sage: chebyshev_T(n,-1)
(-1)^n
sage: chebyshev_T._eval_special_values_(3/2,x)
Traceback (most recent call last):
...
ValueError: no special value found
sage: chebyshev_T._eval_special_values_(n, 0.1)
Traceback (most recent call last):
...
ValueError: no special value found
"""
if x == 1:
return x
if x == -1:
return x**n
if x == 0:
return (1+(-1)**n)*(-1)**(n/2)/2
raise ValueError("no special value found")
def _evalf_(self, n, x, **kwds):
"""
Evaluates :class:`chebyshev_T` numerically with mpmath.
EXAMPLES::
sage: chebyshev_T._evalf_(10,3)
2.26195370000000e7
sage: chebyshev_T._evalf_(10,3,parent=RealField(75))
2.261953700000000000000e7
sage: chebyshev_T._evalf_(10,I)
-3363.00000000000
sage: chebyshev_T._evalf_(5,0.3)
0.998880000000000
sage: chebyshev_T(1/2, 0)
0.707106781186548
sage: chebyshev_T(1/2, 3/2)
1.11803398874989
sage: chebyshev_T._evalf_(1.5, Mod(8,9))
Traceback (most recent call last):
...
TypeError: cannot evaluate chebyshev_T with parent Ring of integers modulo 9
This simply evaluates using :class:`RealField` or :class:`ComplexField`::
sage: chebyshev_T(1234.5, RDF(2.1))
5.48174256255782e735
sage: chebyshev_T(1234.5, I)
-1.21629397684152e472 - 1.21629397684152e472*I
For large values of ``n``, mpmath fails (but the algebraic formula
still works)::
sage: chebyshev_T._evalf_(10^6, 0.1)
Traceback (most recent call last):
...
NoConvergence: Hypergeometric series converges too slowly. Try increasing maxterms.
sage: chebyshev_T(10^6, 0.1)
0.636384327171504
"""
try:
real_parent = kwds['parent']
except KeyError:
real_parent = parent(x)
if not isinstance(real_parent, (sage.rings.abc.RealField, sage.rings.abc.ComplexField)):
# parent is not a real or complex field: figure out a good parent
if x in RR:
x = RR(x)
real_parent = RR
elif x in CC:
x = CC(x)
real_parent = CC
if not isinstance(real_parent, (sage.rings.abc.RealField, sage.rings.abc.ComplexField)):
raise TypeError("cannot evaluate chebyshev_T with parent {}".format(real_parent))
from sage.libs.mpmath.all import call as mpcall
from sage.libs.mpmath.all import chebyt as mpchebyt
return mpcall(mpchebyt, n, x, parent=real_parent)
def eval_formula(self, n, x):
"""
Evaluate ``chebyshev_T`` using an explicit formula.
See [AS1964]_ 227 (p. 782) for details for the recursions.
See also [Koe1999]_ for fast evaluation techniques.
INPUT:
- ``n`` -- an integer
- ``x`` -- a value to evaluate the polynomial at (this can be
any ring element)
EXAMPLES::
sage: chebyshev_T.eval_formula(-1,x)
x
sage: chebyshev_T.eval_formula(0,x)
1
sage: chebyshev_T.eval_formula(1,x)
x
sage: chebyshev_T.eval_formula(2,0.1) == chebyshev_T._evalf_(2,0.1)
True
sage: chebyshev_T.eval_formula(10,x)
512*x^10 - 1280*x^8 + 1120*x^6 - 400*x^4 + 50*x^2 - 1
sage: chebyshev_T.eval_algebraic(10,x).expand()
512*x^10 - 1280*x^8 + 1120*x^6 - 400*x^4 + 50*x^2 - 1
"""
if n < 0:
return self.eval_formula(-n, x)
elif n == 0:
return parent(x).one()
res = parent(x).zero()
for j in range(n // 2 + 1):
f = factorial(n-1-j) / factorial(j) / factorial(n-2*j)
res += (-1)**j * (2*x)**(n-2*j) * f
res *= n/2
return res
def eval_algebraic(self, n, x):
"""
Evaluate :class:`chebyshev_T` as polynomial, using a recursive
formula.
INPUT:
- ``n`` -- an integer
- ``x`` -- a value to evaluate the polynomial at (this can be
any ring element)
EXAMPLES::
sage: chebyshev_T.eval_algebraic(5, x)
2*(2*(2*x^2 - 1)*x - x)*(2*x^2 - 1) - x
sage: chebyshev_T(-7, x) - chebyshev_T(7,x)
0
sage: R.<t> = ZZ[]
sage: chebyshev_T.eval_algebraic(-1, t)
t
sage: chebyshev_T.eval_algebraic(0, t)
1
sage: chebyshev_T.eval_algebraic(1, t)
t
sage: chebyshev_T(7^100, 1/2)
1/2
sage: chebyshev_T(7^100, Mod(2,3))
2
sage: n = 97; x = RIF(pi/2/n)
sage: chebyshev_T(n, cos(x)).contains_zero()
True
sage: R.<t> = Zp(2, 8, 'capped-abs')[]
sage: chebyshev_T(10^6+1, t)
(2^7 + O(2^8))*t^5 + O(2^8)*t^4 + (2^6 + O(2^8))*t^3 + O(2^8)*t^2 + (1 + 2^6 + O(2^8))*t + O(2^8)
"""
if n == 0:
return parent(x).one()
if n < 0:
return self._eval_recursive_(-n, x)[0]
return self._eval_recursive_(n, x)[0]
def _eval_recursive_(self, n, x, both=False):
"""
If ``both=True``, compute ``(T(n,x), T(n-1,x))`` using a
recursive formula.
If ``both=False``, return instead a tuple ``(T(n,x), False)``.
EXAMPLES::
sage: chebyshev_T._eval_recursive_(5, x)
(2*(2*(2*x^2 - 1)*x - x)*(2*x^2 - 1) - x, False)
sage: chebyshev_T._eval_recursive_(5, x, True)
(2*(2*(2*x^2 - 1)*x - x)*(2*x^2 - 1) - x, 2*(2*x^2 - 1)^2 - 1)
"""
if n == 1:
return x, parent(x).one()
assert n >= 2
a, b = self._eval_recursive_((n+1)//2, x, both or n % 2)
if n % 2 == 0:
return 2*a*a - 1, both and 2*a*b - x
else:
return 2*a*b - x, both and 2*b*b - 1
def _eval_numpy_(self, n, x):
"""
Evaluate ``self`` using numpy.
EXAMPLES::
sage: import numpy
sage: z = numpy.array([1,2])
sage: z2 = numpy.array([[1,2],[1,2]])
sage: z3 = numpy.array([1,2,3.])
sage: chebyshev_T(1,z)
array([1., 2.])
sage: chebyshev_T(1,z2)
array([[1., 2.],
[1., 2.]])
sage: chebyshev_T(1,z3)
array([1., 2., 3.])
sage: chebyshev_T(z,0.1)
array([ 0.1 , -0.98])
"""
from scipy.special import eval_chebyt
return eval_chebyt(n, x)
def _derivative_(self, n, x, diff_param):
"""
Return the derivative of :class:`chebyshev_T` in form of the Chebyshev
polynomial of the second kind :class:`chebyshev_U`.
EXAMPLES::
sage: var('k')
k
sage: derivative(chebyshev_T(k,x),x)
k*chebyshev_U(k - 1, x)
sage: derivative(chebyshev_T(3,x),x)
12*x^2 - 3
sage: derivative(chebyshev_T(k,x),k)
Traceback (most recent call last):
...
NotImplementedError: derivative w.r.t. to the index is not supported yet
"""
if diff_param == 0:
raise NotImplementedError("derivative w.r.t. to the index is not supported yet")
elif diff_param == 1:
return n*chebyshev_U(n-1, x)
raise ValueError("illegal differentiation parameter {}".format(diff_param))
chebyshev_T = Func_chebyshev_T()
class Func_chebyshev_U(ChebyshevFunction):
"""
Class for the Chebyshev polynomial of the second kind.
REFERENCE:
- [AS1964]_ 22.8.3 page 783 and 6.1.22 page 256.
EXAMPLES::
sage: R.<t> = QQ[]
sage: chebyshev_U(2,t)
4*t^2 - 1
sage: chebyshev_U(3,t)
8*t^3 - 4*t
"""
def __init__(self):
"""
Init method for the chebyshev polynomials of the second kind.
EXAMPLES::
sage: var('n, x')
(n, x)
sage: from sage.functions.orthogonal_polys import Func_chebyshev_U
sage: chebyshev_U2 = Func_chebyshev_U()
sage: chebyshev_U2(1,x)
2*x
sage: chebyshev_U(x, x)._sympy_()
chebyshevu(x, x)
sage: maxima(chebyshev_U(2,x, hold=True))
3*((-(8*(1-_SAGE_VAR_x))/3)+(4*(1-_SAGE_VAR_x)^2)/3+1)
sage: maxima(chebyshev_U(n,x, hold=True))
chebyshev_u(_SAGE_VAR_n,_SAGE_VAR_x)
"""
ChebyshevFunction.__init__(self, 'chebyshev_U', nargs=2,
conversions=dict(maxima='chebyshev_u',
mathematica='ChebyshevU',
sympy='chebyshevu',
giac='tchebyshev2'))
def _latex_(self):
r"""
TESTS::
sage: latex(chebyshev_U)
U_n
"""
return r"U_n"
def _print_latex_(self, n, z):
r"""
TESTS::
sage: latex(chebyshev_U(3, x, hold=True))