Juan Hortez
Juan Hortez

Reputation: 27

Legendre polynomials in python

How to generate ORTHONORMAL basis of Legendre polynomials on [0,1]?

I tried using many modules, but they usually don’t provide normalisation and change of interval. Is there some general normalizing factor? Or formula for change of the interval?

Upvotes: -2

Views: 418

Answers (1)

Stéphane Laurent
Stéphane Laurent

Reputation: 84659

You can get the Legendre polynomials on [-1, 1] with scipy.special.legendre. They are orthogonal but not orthonormal. But it is known that the integral of Ln(x)*Ln(x) over [-1, 1] is 2 / (2*n +1), where Ln denotes the Legendre polynomial of degree n. So you can normalize the Legendre polynomials as follows:

import scipy.integrate as integrate
from scipy.special import legendre
from math import sqrt

l3 = legendre(3, monic=False)

integrate.quad(lambda x: l3(x)*l3(x), -1, 1) # = 2 / (2*degree + 1)

def l3normalized(x):
    return sqrt((2*3 + 1) / 2) * l3(x)

integrate.quad(lambda x: l3normalized(x)*l3normalized(x), -1, 1) # = 1

Now, to change the interval, you can do a linear change of variables: define Pn on [0, 1] by Pn(x) = sqrt(2) * Ln_normalized(2*x-1). Then the Pn are orthonormal.

import scipy.integrate as integrate
from scipy.special import legendre
from math import sqrt

l3 = legendre(3, monic=False)

integrate.quad(lambda x: l3(x)*l3(x), -1, 1) # = 2 / (2*degree + 1)

def l3normalized(x):
    return sqrt((2*3 + 1) / 2) * l3(x)

integrate.quad(lambda x: l3normalized(x)*l3normalized(x), -1, 1) # = 1

l4 = legendre(4, monic=False)
def l4normalized(x):
    return sqrt((2*4 + 1) / 2) * l4(x)

def p3(x):
    return sqrt(2) * l3normalized(2*x-1)

def p4(x):
    return sqrt(2) * l4normalized(2*x-1)

integrate.quad(lambda x: p3(x)*p3(x), 0, 1) # = 1
integrate.quad(lambda x: p3(x)*p4(x), 0, 1) # = 0

Upvotes: 0

Related Questions