6
6
from numpy .polynomial import Polynomial
7
7
8
8
9
- def legendre (n : int ) -> list [float ]:
9
+ def compute_legendre_polynomial_coefficients (n : int ) -> [float ]:
10
10
"""
11
11
Compute the coefficients of the nth Legendre polynomial.
12
12
@@ -19,42 +19,34 @@ def legendre(n: int) -> list[float]:
19
19
Returns:
20
20
list[float]: Coefficients of the polynomial in ascending order of powers.
21
21
"""
22
- p = (1 / (factorial (n ) * (2 ** n ))) * (Polynomial ([- 1 , 0 , 1 ]) ** n )
23
- return p .deriv (n ).coef .tolist ()
22
+ legendre_polynomial = (1 / (factorial (n ) * (2 ** n ))) * (Polynomial ([- 1 , 0 , 1 ]) ** n )
23
+ return legendre_polynomial .deriv (n ).coef .tolist ()
24
24
25
25
26
- def test_legendre_0 () :
26
+ def test_legendre_polynomial_degree_0 () -> None :
27
27
"""Test the 0th Legendre polynomial."""
28
- assert legendre (0 ) == [1.0 ], "The 0th Legendre polynomial should be [1.0]"
28
+ assert compute_legendre_polynomial_coefficients (0 ) == [1.0 ], "The 0th Legendre polynomial should be [1.0]"
29
29
30
30
31
- def test_legendre_1 () :
31
+ def test_legendre_polynomial_degree_1 () -> None :
32
32
"""Test the 1st Legendre polynomial."""
33
- assert legendre (1 ) == [0.0 , 1.0 ], "The 1st Legendre polynomial should be [0.0, 1.0]"
33
+ assert compute_legendre_polynomial_coefficients (1 ) == [0.0 , 1.0 ], "The 1st Legendre polynomial should be [0.0, 1.0]"
34
34
35
35
36
- def test_legendre_2 () :
36
+ def test_legendre_polynomial_degree_2 () -> None :
37
37
"""Test the 2nd Legendre polynomial."""
38
- assert legendre (2 ) == [
39
- - 0.5 ,
40
- 0.0 ,
41
- 1.5 ,
42
- ], "The 2nd Legendre polynomial should be [-0.5, 0.0, 1.5]"
38
+ assert compute_legendre_polynomial_coefficients (2 ) == [- 0.5 , 0.0 , 1.5 ],
39
+ "The 2nd Legendre polynomial should be [-0.5, 0.0, 1.5]"
43
40
44
41
45
- def test_legendre_3 () :
42
+ def test_legendre_polynomial_degree_3 () -> None :
46
43
"""Test the 3rd Legendre polynomial."""
47
- assert legendre (3 ) == [
48
- 0.0 ,
49
- - 1.5 ,
50
- 0.0 ,
51
- 2.5 ,
52
- ], "The 3rd Legendre polynomial should be [0.0, -1.5, 0.0, 2.5]"
44
+ assert compute_legendre_polynomial_coefficients (3 ) == [0.0 , - 1.5 , 0.0 , 2.5 ], "The 3rd Legendre polynomial should be [0.0, -1.5, 0.0, 2.5]"
53
45
54
46
55
- def test_legendre_4 () :
47
+ def test_legendre_polynomial_degree_4 () -> None :
56
48
"""Test the 4th Legendre polynomial."""
57
- assert legendre (4 ) == pytest .approx ([0.375 , 0.0 , - 3.75 , 0.0 , 4.375 ])
49
+ assert compute_legendre_polynomial_coefficients (4 ) == pytest .approx ([0.375 , 0.0 , - 3.75 , 0.0 , 4.375 ])
58
50
"The 4th Legendre polynomial should be [0.375, 0.0, -3.75, 0.0, 4.375]"
59
51
60
52
0 commit comments