From 02f7cfac25d29a6bf2e15444f89c714f27b1b1c2 Mon Sep 17 00:00:00 2001 From: Pablito Date: Wed, 4 Dec 2024 14:37:20 +0100 Subject: [PATCH] Added type hints and changed to descriptive names --- maths/polynomials/legendre.py | 36 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/maths/polynomials/legendre.py b/maths/polynomials/legendre.py index 72c320c9a..3681068e7 100644 --- a/maths/polynomials/legendre.py +++ b/maths/polynomials/legendre.py @@ -6,7 +6,7 @@ import pytest from numpy.polynomial import Polynomial -def legendre(n: int) -> list[float]: +def compute_legendre_polynomial_coefficients(n: int) -> [float]: """ Compute the coefficients of the nth Legendre polynomial. @@ -19,42 +19,34 @@ def legendre(n: int) -> list[float]: Returns: list[float]: Coefficients of the polynomial in ascending order of powers. """ - p = (1 / (factorial(n) * (2**n))) * (Polynomial([-1, 0, 1]) ** n) - return p.deriv(n).coef.tolist() + legendre_polynomial = (1 / (factorial(n) * (2**n))) * (Polynomial([-1, 0, 1]) ** n) + return legendre_polynomial.deriv(n).coef.tolist() -def test_legendre_0(): +def test_legendre_polynomial_degree_0() -> None: """Test the 0th Legendre polynomial.""" - assert legendre(0) == [1.0], "The 0th Legendre polynomial should be [1.0]" + assert compute_legendre_polynomial_coefficients(0) == [1.0], "The 0th Legendre polynomial should be [1.0]" -def test_legendre_1(): +def test_legendre_polynomial_degree_1() -> None: """Test the 1st Legendre polynomial.""" - assert legendre(1) == [0.0, 1.0], "The 1st Legendre polynomial should be [0.0, 1.0]" + assert compute_legendre_polynomial_coefficients(1) == [0.0, 1.0], "The 1st Legendre polynomial should be [0.0, 1.0]" -def test_legendre_2(): +def test_legendre_polynomial_degree_2() -> None: """Test the 2nd Legendre polynomial.""" - assert legendre(2) == [ - -0.5, - 0.0, - 1.5, - ], "The 2nd Legendre polynomial should be [-0.5, 0.0, 1.5]" + assert compute_legendre_polynomial_coefficients(2) == [-0.5, 0.0, 1.5], + "The 2nd Legendre polynomial should be [-0.5, 0.0, 1.5]" -def test_legendre_3(): +def test_legendre_polynomial_degree_3() -> None: """Test the 3rd Legendre polynomial.""" - assert legendre(3) == [ - 0.0, - -1.5, - 0.0, - 2.5, - ], "The 3rd Legendre polynomial should be [0.0, -1.5, 0.0, 2.5]" + 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]" -def test_legendre_4(): +def test_legendre_polynomial_degree_4() -> None: """Test the 4th Legendre polynomial.""" - assert legendre(4) == pytest.approx([0.375, 0.0, -3.75, 0.0, 4.375]) + assert compute_legendre_polynomial_coefficients(4) == pytest.approx([0.375, 0.0, -3.75, 0.0, 4.375]) "The 4th Legendre polynomial should be [0.375, 0.0, -3.75, 0.0, 4.375]"