0

I'm kinda new to python. I want to write a code which can produce the legendre polinomials for me. Here is my code:

import sys
sys.setrecursionlimit(10000)
print(sys.getrecursionlimit())
from scipy import integrate
p=int(input("p="))
arr = [0]*(p+1)
a = [0]*(p+1)
arr[0]=lambda x: 1
arr[1]=lambda x: x
for i in range(2,p+1):
    arr[i]=lambda x:2*i*x*arr[i-1](x)-(i-1)*arr[i-2](x)/i
    a[i]=arr[i](0)
print(a[p])

The code does not give any errors or warnings. I wanted to check if the code calculates well, so I tried to print one of the legendre polinomials with an exact value (zero). It works well if p<3, but if p>3 the code does not print anything, but I don't get errors or warnings, so I don't know what hte problem is. p is the degree of the legendre polinomial. If p=3 I should get the first four legendre polinomial(because they also start from zero, like the indexing of lists).I also tried printing the value directly without the list a, but that didn't work either. What should I do?

Zsombor
  • 1
  • 1
  • The `i` is bound to the *last* value of `i`, i.e.`p`. That is not what you want. Use `lambda x, i=i: ...` to capture the *current* value of `i`. – MisterMiyagi Jun 19 '22 at 16:08
  • Does this answer your question? [What do lambda function closures capture?](https://stackoverflow.com/questions/2295290/what-do-lambda-function-closures-capture) – MisterMiyagi Jun 19 '22 at 16:09

0 Answers0