From 7036c84cb5d9f92f343d3c9dcb8ee9eddb9b5f75 Mon Sep 17 00:00:00 2001 From: Pablito Date: Tue, 3 Dec 2024 11:19:20 +0100 Subject: [PATCH 01/11] Implemented Legendre polynomial computation algorithm Added an algorithm that calculates the coefficients of the Legendre polynomial of degree n using the recurrence relation --- maths/polynomials/legendre.py | 60 +++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 maths/polynomials/legendre.py diff --git a/maths/polynomials/legendre.py b/maths/polynomials/legendre.py new file mode 100644 index 000000000..8bd3fdb56 --- /dev/null +++ b/maths/polynomials/legendre.py @@ -0,0 +1,60 @@ +from numpy.polynomial import Polynomial +from math import factorial +import pytest + + +def legendre(n: int) -> list[float]: + """ + Compute the coefficients of the nth Legendre polynomial. + + The Legendre polynomials are solutions to Legendre's differential equation + and are widely used in physics and engineering. + + Parameters: + n (int): The order of the Legendre polynomial. + + 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() + + +def jsp(): + print(legendre(1)) + print(legendre(2)) + print(legendre(3)) + print(legendre(4)) + + +jsp() + + +def test_legendre_0(): + """Test the 0th Legendre polynomial.""" + assert legendre(0) == [1.0], "The 0th Legendre polynomial should be [1.0]" + + +def test_legendre_1(): + """Test the 1st Legendre polynomial.""" + assert legendre(1) == [0.0, 1.0], "The 1st Legendre polynomial should be [0.0, 1.0]" + + +def test_legendre_2(): + """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]" + + +def test_legendre_3(): + """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]" + + +def test_legendre_4(): + """Test the 4th Legendre polynomial.""" + assert legendre(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]" + + +if __name__ == '__main__': + pytest.main() From 08d1f15d1bf51971f0a032b6f57ff1cad441e6da Mon Sep 17 00:00:00 2001 From: Nanoemm Date: Tue, 3 Dec 2024 10:19:40 +0000 Subject: [PATCH 02/11] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index f0a34a553..885436989 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -719,6 +719,7 @@ * [Pollard Rho](maths/pollard_rho.py) * [Polynomial Evaluation](maths/polynomial_evaluation.py) * Polynomials + * [Legendre](maths/polynomials/legendre.py) * [Single Indeterminate Operations](maths/polynomials/single_indeterminate_operations.py) * [Power Using Recursion](maths/power_using_recursion.py) * [Prime Check](maths/prime_check.py) From bb1d79fed86866fd512ed107e4d737ba1b9ca8c7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Dec 2024 10:22:47 +0000 Subject: [PATCH 03/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/polynomials/legendre.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/maths/polynomials/legendre.py b/maths/polynomials/legendre.py index 8bd3fdb56..74865729f 100644 --- a/maths/polynomials/legendre.py +++ b/maths/polynomials/legendre.py @@ -16,7 +16,7 @@ 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) + p = (1 / (factorial(n) * (2**n))) * (Polynomial([-1, 0, 1]) ** n) return p.deriv(n).coef.tolist() @@ -42,12 +42,21 @@ def test_legendre_1(): def test_legendre_2(): """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 legendre(2) == [ + -0.5, + 0.0, + 1.5, + ], "The 2nd Legendre polynomial should be [-0.5, 0.0, 1.5]" def test_legendre_3(): """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 legendre(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(): @@ -56,5 +65,5 @@ def test_legendre_4(): "The 4th Legendre polynomial should be [0.375, 0.0, -3.75, 0.0, 4.375]" -if __name__ == '__main__': +if __name__ == "__main__": pytest.main() From be2ab89982d311ec415e50462ddd5bfd7e2e5e67 Mon Sep 17 00:00:00 2001 From: Pablito Date: Tue, 3 Dec 2024 11:25:47 +0100 Subject: [PATCH 04/11] Changed the import block --- maths/polynomials/legendre.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/maths/polynomials/legendre.py b/maths/polynomials/legendre.py index 74865729f..a0d63c2a2 100644 --- a/maths/polynomials/legendre.py +++ b/maths/polynomials/legendre.py @@ -1,6 +1,9 @@ -from numpy.polynomial import Polynomial +# Imports de bibliothèques standard from math import factorial + +# Imports de bibliothèques tierces import pytest +from numpy.polynomial import Polynomial def legendre(n: int) -> list[float]: From a3a71d2ef13a792e1bfc1f61556de6691635e362 Mon Sep 17 00:00:00 2001 From: Pablito Date: Tue, 3 Dec 2024 11:27:22 +0100 Subject: [PATCH 05/11] Removed an unused function --- maths/polynomials/legendre.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/maths/polynomials/legendre.py b/maths/polynomials/legendre.py index a0d63c2a2..72c320c9a 100644 --- a/maths/polynomials/legendre.py +++ b/maths/polynomials/legendre.py @@ -23,16 +23,6 @@ def legendre(n: int) -> list[float]: return p.deriv(n).coef.tolist() -def jsp(): - print(legendre(1)) - print(legendre(2)) - print(legendre(3)) - print(legendre(4)) - - -jsp() - - def test_legendre_0(): """Test the 0th Legendre polynomial.""" assert legendre(0) == [1.0], "The 0th Legendre polynomial should be [1.0]" From 02f7cfac25d29a6bf2e15444f89c714f27b1b1c2 Mon Sep 17 00:00:00 2001 From: Pablito Date: Wed, 4 Dec 2024 14:37:20 +0100 Subject: [PATCH 06/11] 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]" From dddc326d0da73da2d821f6ca93eda1ba2e65e837 Mon Sep 17 00:00:00 2001 From: Pablito Date: Wed, 4 Dec 2024 14:42:13 +0100 Subject: [PATCH 07/11] Returned to previous commit with a variable name changed --- maths/polynomials/legendre.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/maths/polynomials/legendre.py b/maths/polynomials/legendre.py index 3681068e7..b2ebce0f9 100644 --- a/maths/polynomials/legendre.py +++ b/maths/polynomials/legendre.py @@ -6,7 +6,7 @@ import pytest from numpy.polynomial import Polynomial -def compute_legendre_polynomial_coefficients(n: int) -> [float]: +def legendre(n: int) -> [float]: """ Compute the coefficients of the nth Legendre polynomial. @@ -23,30 +23,38 @@ def compute_legendre_polynomial_coefficients(n: int) -> [float]: return legendre_polynomial.deriv(n).coef.tolist() -def test_legendre_polynomial_degree_0() -> None: +def test_legendre_0(): """Test the 0th Legendre polynomial.""" - assert compute_legendre_polynomial_coefficients(0) == [1.0], "The 0th Legendre polynomial should be [1.0]" + assert legendre(0) == [1.0], "The 0th Legendre polynomial should be [1.0]" -def test_legendre_polynomial_degree_1() -> None: +def test_legendre_1(): """Test the 1st Legendre polynomial.""" - assert compute_legendre_polynomial_coefficients(1) == [0.0, 1.0], "The 1st Legendre polynomial should be [0.0, 1.0]" + assert legendre(1) == [0.0, 1.0], "The 1st Legendre polynomial should be [0.0, 1.0]" -def test_legendre_polynomial_degree_2() -> None: +def test_legendre_2(): """Test the 2nd Legendre polynomial.""" - assert compute_legendre_polynomial_coefficients(2) == [-0.5, 0.0, 1.5], - "The 2nd Legendre polynomial should be [-0.5, 0.0, 1.5]" + assert legendre(2) == [ + -0.5, + 0.0, + 1.5, + ], "The 2nd Legendre polynomial should be [-0.5, 0.0, 1.5]" -def test_legendre_polynomial_degree_3() -> None: +def test_legendre_3(): """Test the 3rd Legendre polynomial.""" - 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]" + 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]" -def test_legendre_polynomial_degree_4() -> None: +def test_legendre_4(): """Test the 4th Legendre polynomial.""" - assert compute_legendre_polynomial_coefficients(4) == pytest.approx([0.375, 0.0, -3.75, 0.0, 4.375]) + assert legendre(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]" From 20c3200a629a5a4ed2ff6132ee51cc862ddca267 Mon Sep 17 00:00:00 2001 From: Pablito Date: Wed, 4 Dec 2024 14:44:57 +0100 Subject: [PATCH 08/11] Minor changes to some lists in the tests --- maths/polynomials/legendre.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/maths/polynomials/legendre.py b/maths/polynomials/legendre.py index b2ebce0f9..5bfa86409 100644 --- a/maths/polynomials/legendre.py +++ b/maths/polynomials/legendre.py @@ -35,21 +35,14 @@ def test_legendre_1(): def test_legendre_2(): """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 legendre(2) == [-0.5, 0.0, 1.5] + "The 2nd Legendre polynomial should be [-0.5, 0.0, 1.5]" def test_legendre_3(): """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 legendre(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(): From 60ad95847264473ec7749f216b6d5863bc391d64 Mon Sep 17 00:00:00 2001 From: Pablito Date: Wed, 4 Dec 2024 14:46:18 +0100 Subject: [PATCH 09/11] Added type hints for tests --- maths/polynomials/legendre.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/maths/polynomials/legendre.py b/maths/polynomials/legendre.py index 5bfa86409..716b0d845 100644 --- a/maths/polynomials/legendre.py +++ b/maths/polynomials/legendre.py @@ -23,29 +23,29 @@ def legendre(n: int) -> [float]: return legendre_polynomial.deriv(n).coef.tolist() -def test_legendre_0(): +def test_legendre_0() -> None: """Test the 0th Legendre polynomial.""" assert legendre(0) == [1.0], "The 0th Legendre polynomial should be [1.0]" -def test_legendre_1(): +def test_legendre_1() -> None: """Test the 1st Legendre polynomial.""" assert legendre(1) == [0.0, 1.0], "The 1st Legendre polynomial should be [0.0, 1.0]" -def test_legendre_2(): +def test_legendre_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]" -def test_legendre_3(): +def test_legendre_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]" -def test_legendre_4(): +def test_legendre_4() -> None: """Test the 4th Legendre polynomial.""" assert legendre(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]" From 555b54cbbcafcc223d2f5372227153de07476fe9 Mon Sep 17 00:00:00 2001 From: Pablito Date: Wed, 4 Dec 2024 14:49:54 +0100 Subject: [PATCH 10/11] Minor changes to type hints --- maths/polynomials/legendre.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maths/polynomials/legendre.py b/maths/polynomials/legendre.py index 716b0d845..a359deabd 100644 --- a/maths/polynomials/legendre.py +++ b/maths/polynomials/legendre.py @@ -1,12 +1,13 @@ # Imports de bibliothèques standard from math import factorial +from typing import List # Imports de bibliothèques tierces import pytest from numpy.polynomial import Polynomial -def legendre(n: int) -> [float]: +def legendre(n: int) -> List[float]: """ Compute the coefficients of the nth Legendre polynomial. From 33266f262f5c45fd9adf9ad3b7d02fb378d0b05b Mon Sep 17 00:00:00 2001 From: Pablito Date: Wed, 4 Dec 2024 14:51:02 +0100 Subject: [PATCH 11/11] Minor changes to type hints --- maths/polynomials/legendre.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/maths/polynomials/legendre.py b/maths/polynomials/legendre.py index a359deabd..d5cbb2564 100644 --- a/maths/polynomials/legendre.py +++ b/maths/polynomials/legendre.py @@ -1,13 +1,12 @@ # Imports de bibliothèques standard from math import factorial -from typing import List # Imports de bibliothèques tierces import pytest from numpy.polynomial import Polynomial -def legendre(n: int) -> List[float]: +def legendre(n: int) -> list[float]: """ Compute the coefficients of the nth Legendre polynomial.