Orthogonal Polynomials

  • The Chebyshev polynomial of the first kind arises as a solution to the differential equation

    (1x2)yxy+n2y=0

    and those of the second kind as a solution to

    (1x2)y3xy+n(n+2)y=0.

    The Chebyshev polynomials of the first kind are defined by the recurrence relation

    T0(x)=1T1(x)=xTn+1(x)=2xTn(x)Tn1(x).

    The Chebyshev polynomials of the second kind are defined by the recurrence relation

    U0(x)=1U1(x)=2xUn+1(x)=2xUn(x)Un1(x).

    For integers m,n, they satisfy the orthogonality relations

    11Tn(x)Tm(x)dx1x2={0:nm     π:n=m=0π/2:n=m0

    and

    11Un(x)Um(x)1x2dx=π2δm,n.

    They are named after Pafnuty Chebyshev (alternative transliterations: Tchebyshef or Tschebyscheff).

  • The Hermite polynomials are defined either by

    Hn(x)=(1)nex2/2dndxnex2/2

    (the “probabilists’ Hermite polynomials”), or by

    Hn(x)=(1)nex2dndxnex2

    (the “physicists’ Hermite polynomials”). Sage (via Maxima) implements the latter flavor. These satisfy the orthogonality relation

    Hn(x)Hm(x)ex2dx=n!2nπδnm

    They are named in honor of Charles Hermite.

  • Each Legendre polynomial Pn(x) is an n-th degree polynomial. It may be expressed using Rodrigues’ formula:

    Pn(x)=(2nn!)1dndxn[(x21)n].

    These are solutions to Legendre’s differential equation:

    ddx[(1x2)ddxP(x)]+n(n+1)P(x)=0.

    and satisfy the orthogonality relation

    11Pm(x)Pn(x)dx=22n+1δmn

    The Legendre function of the second kind Qn(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 Pm(x) can be given in terms of the “usual” Legendre polynomials by

    Pm(x)=(1)m(1x2)m/2dmdxmP(x)=(1)m2!(1x2)m/2d+mdx+m(x21).

    Assuming 0m, they satisfy the orthogonality relation:

    11P(m)kP(m)dx=2(+m)!(2+1)(m)! δk,,

    where δk, is the Kronecker delta.

    The associated Legendre functions of the second kind Qm(x) can be given in terms of the “usual” Legendre polynomials by

    Qm(x)=(1)m(1x2)m/2dmdxmQ(x).

    They are named after Adrien-Marie Legendre.

  • Laguerre polynomials may be defined by the Rodrigues formula

    Ln(x)=exn!dndxn(exxn).

    They are solutions of Laguerre’s equation:

    xy+(1x)y+ny=0

    and satisfy the orthogonality relation

    0Lm(x)Ln(x)exdx=δmn.

    The generalized Laguerre polynomials may be defined by the Rodrigues formula:

    L(α)n(x)=xαexn!dndxn(exxn+α).

    (These are also sometimes called the associated Laguerre polynomials.) The simple Laguerre polynomials are recovered from the generalized polynomials by setting α=0.

    They are named after Edmond Laguerre.

  • Jacobi polynomials are a class of orthogonal polynomials. They are obtained from hypergeometric series in cases where the series is in fact finite:

    P(α,β)n(z)=(α+1)nn!2F1(n,1+α+β+n;α+1;1z2),

    where ()n is Pochhammer’s symbol (for the rising factorial), (Abramowitz and Stegun p561.) and thus have the explicit expression

    P(α,β)n(z)=Γ(α+n+1)n!Γ(α+β+n+1)nm=0(nm)Γ(α+β+n+m+1)Γ(α+m+1)(z12)m.

    They are named after Carl Jacobi.

  • Ultraspherical or Gegenbauer polynomials are given in terms of the Jacobi polynomials P(α,β)n(x) with α=β=a1/2 by

    C(a)n(x)=Γ(a+1/2)Γ(2a)Γ(n+2a)Γ(n+a+1/2)P(a1/2,a1/2)n(x).

    They satisfy the orthogonality relation

    11(1x2)a1/2C(a)m(x)C(a)n(x)dx=δmn212aπΓ(n+2a)(n+a)Γ2(a)Γ(n+1),

    for a>1/2. They are obtained from hypergeometric series in cases where the series is in fact finite:

    C(a)n(z)=(2a)n_n!2F1(n,2a+n;a+12;1z2)

    where n_ is the falling factorial. (See Abramowitz and Stegun p561)

    They are named for Leopold Gegenbauer (1849-1903).

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”

(x)n=x(x+1)(x+2)(x+n1)=(x+n1)!(x1)!.

On the other hand, the “falling factorial” or “lower factorial” is

xn_=x!(xn)!,

in the notation of Ronald L. Graham, Donald E. Knuth and Oren Patashnik in their book Concrete Mathematics.

Todo

Implement Zernike polynomials. Wikipedia article Zernike_polynomials

REFERENCES:

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.

class sage.functions.orthogonal_polys.ChebyshevFunction(name, nargs=2, latex_name=None, conversions={})

Bases: sage.functions.orthogonal_polys.OrthogonalFunction

Abstract base class for Chebyshev polynomials of the first and second kind.

EXAMPLES:

sage: chebyshev_T(3,x)
4*x^3 - 3*x
class sage.functions.orthogonal_polys.Func_assoc_legendre_P

Bases: sage.symbolic.function.BuiltinFunction

EXAMPLES:

sage: loads(dumps(gen_legendre_P))
gen_legendre_P
sage: maxima(gen_legendre_P(20,6,x, hold=True))._sage_().expand().coefficient(x,10)
2508866163428625/128
eval_poly(n, m, arg, **kwds)

Return the associated Legendre P(n, m, arg) polynomial for integers n>1,m>1.

EXAMPLES:

sage: gen_legendre_P(7,4,x)
3465/2*(13*x^3 - 3*x)*(x^2 - 1)^2
sage: gen_legendre_P(3,1,sqrt(x))
-3/2*(5*x - 1)*sqrt(-x + 1)

REFERENCE:

class sage.functions.orthogonal_polys.Func_assoc_legendre_Q

Bases: sage.symbolic.function.BuiltinFunction

EXAMPLES:

sage: loads(dumps(gen_legendre_Q))
gen_legendre_Q
sage: maxima(gen_legendre_Q(2,1,3, hold=True))._sage_().simplify_full()
1/4*sqrt(2)*(36*pi - 36*I*log(2) + 25*I)
eval_recursive(n, m, x, **kwds)

Return the associated Legendre Q(n, m, arg) function for integers n>1,m>1.

EXAMPLES:

sage: gen_legendre_Q(3,4,x)
48/(x^2 - 1)^2
sage: gen_legendre_Q(4,5,x)
-384/((x^2 - 1)^2*sqrt(-x^2 + 1))
sage: gen_legendre_Q(0,1,x)
-1/sqrt(-x^2 + 1)
sage: gen_legendre_Q(0,2,x)
-1/2*((x + 1)^2 - (x - 1)^2)/(x^2 - 1)
sage: gen_legendre_Q(2,2,x).subs(x=2).expand()
9/2*I*pi - 9/2*log(3) + 14/3
class sage.functions.orthogonal_polys.Func_chebyshev_T

Bases: sage.functions.orthogonal_polys.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)
eval_algebraic(n, x)

Evaluate 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)
eval_formula(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
class sage.functions.orthogonal_polys.Func_chebyshev_U

Bases: sage.functions.orthogonal_polys.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
eval_algebraic(n, x)

Evaluate chebyshev_U 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_U.eval_algebraic(5,x)
-2*((2*x + 1)*(2*x - 1)*x - 4*(2*x^2 - 1)*x)*(2*x + 1)*(2*x - 1)
sage: parent(chebyshev_U(3, Mod(8,9)))
Ring of integers modulo 9
sage: parent(chebyshev_U(3, Mod(1,9)))
Ring of integers modulo 9
sage: chebyshev_U(-3,x) + chebyshev_U(1,x)
0
sage: chebyshev_U(-1,Mod(5,8))
0
sage: parent(chebyshev_U(-1,Mod(5,8)))
Ring of integers modulo 8
sage: R.<t> = ZZ[]
sage: chebyshev_U.eval_algebraic(-2, t)
-1
sage: chebyshev_U.eval_algebraic(-1, t)
0
sage: chebyshev_U.eval_algebraic(0, t)
1
sage: chebyshev_U.eval_algebraic(1, t)
2*t
sage: n = 97; x = RIF(pi/n)
sage: chebyshev_U(n-1, cos(x)).contains_zero()
True
sage: R.<t> = Zp(2, 6, 'capped-abs')[]
sage: chebyshev_U(10^6+1, t)
(2 + O(2^6))*t + O(2^6)
eval_formula(n, x)

Evaluate chebyshev_U using an explicit formula.

See [AS1964] 227 (p. 782) for details on the recursions. See also [Koe1999] for the recursion formulas.

INPUT:

  • n – an integer
  • x – a value to evaluate the polynomial at (this can be any ring element)

EXAMPLES:

sage: chebyshev_U.eval_formula(10, x)
1024*x^10 - 2304*x^8 + 1792*x^6 - 560*x^4 + 60*x^2 - 1
sage: chebyshev_U.eval_formula(-2, x)
-1
sage: chebyshev_U.eval_formula(-1, x)
0
sage: chebyshev_U.eval_formula(0, x)
1
sage: chebyshev_U.eval_formula(1, x)
2*x
sage: chebyshev_U.eval_formula(2,0.1) == chebyshev_U._evalf_(2,0.1)
True
class sage.functions.orthogonal_polys.Func_gen_laguerre

Bases: sage.functions.orthogonal_polys.OrthogonalFunction

REFERENCE:

  • [AS1964] 22.5.16, page 778 and page 789.
class sage.functions.orthogonal_polys.Func_hermite

Bases: sage.symbolic.function.GinacFunction

Returns the Hermite polynomial for integers n>1.

REFERENCE:

  • [AS1964] 22.5.40 and 22.5.41, page 779.

EXAMPLES:

sage: x = PolynomialRing(QQ, 'x').gen()
sage: hermite(2,x)
4*x^2 - 2
sage: hermite(3,x)
8*x^3 - 12*x
sage: hermite(3,2)
40
sage: S.<y> = PolynomialRing(RR)
sage: hermite(3,y)
8.00000000000000*y^3 - 12.0000000000000*y
sage: R.<x,y> = QQ[]
sage: hermite(3,y^2)
8*y^6 - 12*y^2
sage: w = var('w')
sage: hermite(3,2*w)
64*w^3 - 24*w
sage: hermite(5,3.1416)
5208.69733891963
sage: hermite(5,RealField(100)(pi))
5208.6167627118104649470287166

Check that trac ticket #17192 is fixed:

sage: x = PolynomialRing(QQ, 'x').gen()
sage: hermite(0,x)
1

sage: hermite(-1,x)
Traceback (most recent call last):
...
RuntimeError: hermite_eval: The index n must be a nonnegative integer

sage: hermite(-7,x)
Traceback (most recent call last):
...
RuntimeError: hermite_eval: The index n must be a nonnegative integer

sage: _ = var('m x')
sage: hermite(m, x).diff(m)
Traceback (most recent call last):
...
RuntimeError: derivative w.r.t. to the index is not supported yet
class sage.functions.orthogonal_polys.Func_jacobi_P

Bases: sage.functions.orthogonal_polys.OrthogonalFunction

Return the Jacobi polynomial P(a,b)n(x) for integers n>1 and a and b symbolic or a>1 and b>1. The Jacobi polynomials are actually defined for all a and b. However, the Jacobi polynomial weight (1x)a(1+x)b isn’t integrable for a1 or b1.

REFERENCE:

EXAMPLES:

sage: x = PolynomialRing(QQ, 'x').gen()
sage: jacobi_P(2,0,0,x)
3/2*x^2 - 1/2
sage: jacobi_P(2,1,2,1.2)
5.01000000000000
class sage.functions.orthogonal_polys.Func_laguerre

Bases: sage.functions.orthogonal_polys.OrthogonalFunction

REFERENCE:

  • [AS1964] 22.5.16, page 778 and page 789.
class sage.functions.orthogonal_polys.Func_legendre_P

Bases: sage.symbolic.function.GinacFunction

EXAMPLES:

sage: legendre_P(4, 2.0)
55.3750000000000
sage: legendre_P(1, x)
x
sage: legendre_P(4, x+1)
35/8*(x + 1)^4 - 15/4*(x + 1)^2 + 3/8
sage: legendre_P(1/2, I+1.)
1.05338240025858 + 0.359890322109665*I
sage: legendre_P(0, SR(1)).parent()
Symbolic Ring

sage: legendre_P(0, 0)
1
sage: legendre_P(1, x)
x

sage: legendre_P(4, 2.)
55.3750000000000
sage: legendre_P(5.5,1.00001)
1.00017875754114
sage: legendre_P(1/2, I+1).n()
1.05338240025858 + 0.359890322109665*I
sage: legendre_P(1/2, I+1).n(59)
1.0533824002585801 + 0.35989032210966539*I
sage: legendre_P(42, RR(12345678))
2.66314881466753e309
sage: legendre_P(42, Reals(20)(12345678))
2.6632e309
sage: legendre_P(201/2, 0).n()
0.0561386178630179
sage: legendre_P(201/2, 0).n(100)
0.056138617863017877699963095883

sage: R.<x> = QQ[]
sage: legendre_P(4,x)
35/8*x^4 - 15/4*x^2 + 3/8
sage: legendre_P(10000,x).coefficient(x,1)
0
sage: var('t,x')
(t, x)
sage: legendre_P(-5,t)
35/8*t^4 - 15/4*t^2 + 3/8
sage: legendre_P(4, x+1)
35/8*(x + 1)^4 - 15/4*(x + 1)^2 + 3/8
sage: legendre_P(4, sqrt(2))
83/8
sage: legendre_P(4, I*e)
35/8*e^4 + 15/4*e^2 + 3/8

sage: n = var('n')
sage: derivative(legendre_P(n,x), x)
(n*x*legendre_P(n, x) - n*legendre_P(n - 1, x))/(x^2 - 1)
sage: derivative(legendre_P(3,x), x)
15/2*x^2 - 3/2
sage: derivative(legendre_P(n,x), n)
Traceback (most recent call last):
...
RuntimeError: derivative w.r.t. to the index is not supported yet
class sage.functions.orthogonal_polys.Func_legendre_Q

Bases: sage.symbolic.function.BuiltinFunction

EXAMPLES:

sage: loads(dumps(legendre_Q))
legendre_Q
sage: maxima(legendre_Q(20,x, hold=True))._sage_().coefficient(x,10)
-29113619535/131072*log(-(x + 1)/(x - 1))
eval_formula(n, arg, **kwds)

Return expanded Legendre Q(n, arg) function expression.

REFERENCE:

EXAMPLES:

sage: legendre_Q.eval_formula(1, x)
1/2*x*(log(x + 1) - log(-x + 1)) - 1
sage: legendre_Q.eval_formula(2,x).expand().collect(log(1+x)).collect(log(1-x))
1/4*(3*x^2 - 1)*log(x + 1) - 1/4*(3*x^2 - 1)*log(-x + 1) - 3/2*x
sage: legendre_Q.eval_formula(20,x).coefficient(x,10)
-29113619535/131072*log(x + 1) + 29113619535/131072*log(-x + 1)
sage: legendre_Q(0, 2)
-1/2*I*pi + 1/2*log(3)
sage: legendre_Q(0, 2.)
0.549306144334055 - 1.57079632679490*I
eval_recursive(n, arg, **kwds)

Return expanded Legendre Q(n, arg) function expression.

EXAMPLES:

sage: legendre_Q.eval_recursive(2,x)
3/4*x^2*(log(x + 1) - log(-x + 1)) - 3/2*x - 1/4*log(x + 1) + 1/4*log(-x + 1)
sage: legendre_Q.eval_recursive(20,x).expand().coefficient(x,10)
-29113619535/131072*log(x + 1) + 29113619535/131072*log(-x + 1)
class sage.functions.orthogonal_polys.Func_ultraspherical

Bases: sage.symbolic.function.GinacFunction

Return the ultraspherical (or Gegenbauer) polynomial gegenbauer(n,a,x),

Can(x)=n/2k=0(1)kΓ(nk+a)Γ(a)k!(n2k)!(2x)n2k.

When n is a nonnegative integer, this formula gives a polynomial in z of degree n, but all parameters are permitted to be complex numbers. When a=1/2, the Gegenbauer polynomial reduces to a Legendre polynomial.

Computed using Pynac.

For numerical evaluation, consider using the mpmath library,, as it also allows complex numbers (and negative n as well); see the examples below.

REFERENCE:

EXAMPLES:

sage: ultraspherical(8, 101/11, x)
795972057547264/214358881*x^8 - 62604543852032/19487171*x^6...
sage: x = PolynomialRing(QQ, 'x').gen()
sage: ultraspherical(2,3/2,x)
15/2*x^2 - 3/2
sage: ultraspherical(1,1,x)
2*x
sage: t = PolynomialRing(RationalField(),"t").gen()
sage: gegenbauer(3,2,t)
32*t^3 - 12*t
sage: _ = var('x')
sage: for N in range(100):
....:     n = ZZ.random_element().abs() + 5
....:     a = QQ.random_element().abs() + 5
....:     assert ((n+1)*ultraspherical(n+1,a,x) - 2*x*(n+a)*ultraspherical(n,a,x) + (n+2*a-1)*ultraspherical(n-1,a,x)).expand().is_zero()
sage: ultraspherical(5,9/10,3.1416)
6949.55439044240
sage: ultraspherical(5,9/10,RealField(100)(pi))
6949.4695419382702451843080687

sage: _ = var('a n')
sage: gegenbauer(2,a,x)
2*(a + 1)*a*x^2 - a
sage: gegenbauer(3,a,x)
4/3*(a + 2)*(a + 1)*a*x^3 - 2*(a + 1)*a*x
sage: gegenbauer(3,a,x).expand()
4/3*a^3*x^3 + 4*a^2*x^3 + 8/3*a*x^3 - 2*a^2*x - 2*a*x
sage: gegenbauer(10,a,x).expand().coefficient(x,2)
1/12*a^6 + 5/4*a^5 + 85/12*a^4 + 75/4*a^3 + 137/6*a^2 + 10*a
sage: ex = gegenbauer(100,a,x)
sage: (ex.subs(a==55/98) - gegenbauer(100,55/98,x)).is_trivial_zero()
True

sage: gegenbauer(2,-3,x)
12*x^2 + 3
sage: gegenbauer(120,-99/2,3)
1654502372608570682112687530178328494861923493372493824
sage: gegenbauer(5,9/2,x)
21879/8*x^5 - 6435/4*x^3 + 1287/8*x
sage: gegenbauer(15,3/2,5)
3903412392243800

sage: derivative(gegenbauer(n,a,x),x)
2*a*gegenbauer(n - 1, a + 1, x)
sage: derivative(gegenbauer(3,a,x),x)
4*(a + 2)*(a + 1)*a*x^2 - 2*(a + 1)*a
sage: derivative(gegenbauer(n,a,x),a)
Traceback (most recent call last):
...
RuntimeError: derivative w.r.t. to the second index is not supported yet

Numerical evaluation with the mpmath library:

sage: from mpmath import gegenbauer as gegenbauer_mp
sage: from mpmath import mp
sage: mp.pretty = True; mp.dps=25
sage: gegenbauer_mp(-7,0.5,0.3)
0.1291811875
sage: gegenbauer_mp(2+3j, -0.75, -1000j)
(-5038991.358609026523401901 + 9414549.285447104177860806j)
class sage.functions.orthogonal_polys.OrthogonalFunction(name, nargs=2, latex_name=None, conversions={})

Bases: sage.symbolic.function.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.

eval_formula(*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