Skip to content
DeepForgeby svx
All problems
EasyCalculusca-102

Legendre Polynomial Value

Problem

Evaluate the Legendre polynomial P_n(x) with the Bonnet recurrence:

P_0 = 1, P_1 = x, and (k+1) * P_{k+1} = (2k+1) * x * P_k - k * P_{k-1}

Assume n >= 0.

Hint

Carry the two previous polynomials and apply the three-term recurrence.

Test cases

case 1
in: [0,0.5]
expected: 1
case 2
in: [1,0.5]
expected: 0.5
case 3
in: [2,0.5]
expected: -0.125
case 4
in: [3,1]
expected: 1
case 5
in: [4,0.5]
expected: -0.2890625
Starter codePython
def legendre_p(n, x):
    # Your code here
    pass

5 test cases · run your code against them in the editor