Skip to content

Commit 02f7cfa

Browse files
committed
Added type hints and changed to descriptive names
1 parent a3a71d2 commit 02f7cfa

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

maths/polynomials/legendre.py

+14-22
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from numpy.polynomial import Polynomial
77

88

9-
def legendre(n: int) -> list[float]:
9+
def compute_legendre_polynomial_coefficients(n: int) -> [float]:
1010
"""
1111
Compute the coefficients of the nth Legendre polynomial.
1212
@@ -19,42 +19,34 @@ def legendre(n: int) -> list[float]:
1919
Returns:
2020
list[float]: Coefficients of the polynomial in ascending order of powers.
2121
"""
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()
2424

2525

26-
def test_legendre_0():
26+
def test_legendre_polynomial_degree_0() -> None:
2727
"""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]"
2929

3030

31-
def test_legendre_1():
31+
def test_legendre_polynomial_degree_1() -> None:
3232
"""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]"
3434

3535

36-
def test_legendre_2():
36+
def test_legendre_polynomial_degree_2() -> None:
3737
"""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]"
4340

4441

45-
def test_legendre_3():
42+
def test_legendre_polynomial_degree_3() -> None:
4643
"""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]"
5345

5446

55-
def test_legendre_4():
47+
def test_legendre_polynomial_degree_4() -> None:
5648
"""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])
5850
"The 4th Legendre polynomial should be [0.375, 0.0, -3.75, 0.0, 4.375]"
5951

6052

0 commit comments

Comments
 (0)