Skip to content
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
54 changes: 54 additions & 0 deletions maths/polynomials/legendre.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Imports de bibliothèques standard
from math import factorial

# Imports de bibliothèques tierces
import pytest
from numpy.polynomial import Polynomial


def compute_legendre_polynomial_coefficients(n: int) -> [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.
"""
legendre_polynomial = (1 / (factorial(n) * (2**n))) * (Polynomial([-1, 0, 1]) ** n)
return legendre_polynomial.deriv(n).coef.tolist()


def test_legendre_polynomial_degree_0() -> None:
"""Test the 0th Legendre polynomial."""
assert compute_legendre_polynomial_coefficients(0) == [1.0], "The 0th Legendre polynomial should be [1.0]"

Check failure on line 28 in maths/polynomials/legendre.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/polynomials/legendre.py:28:89: E501 Line too long (110 > 88)


def test_legendre_polynomial_degree_1() -> None:
"""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]"

Check failure on line 33 in maths/polynomials/legendre.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/polynomials/legendre.py:33:89: E501 Line too long (120 > 88)


def test_legendre_polynomial_degree_2() -> None:
"""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]"

Check failure on line 39 in maths/polynomials/legendre.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

maths/polynomials/legendre.py:38:76: SyntaxError: Expected an expression

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An error occurred while parsing the file: maths/polynomials/legendre.py

Traceback (most recent call last):
  File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
    reports = lint_file(
              ^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 40:1.
parser error: error at 39:4: expected one of +, -, ..., AWAIT, False, NAME, NUMBER, None, True, lambda, not, ~

    "The 2nd Legendre polynomial should be [-0.5, 0.0, 1.5]"
                                                           ^


def test_legendre_polynomial_degree_3() -> None:
"""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]"

Check failure on line 44 in maths/polynomials/legendre.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/polynomials/legendre.py:44:89: E501 Line too long (142 > 88)


def test_legendre_polynomial_degree_4() -> None:
"""Test the 4th Legendre polynomial."""
assert compute_legendre_polynomial_coefficients(4) == pytest.approx([0.375, 0.0, -3.75, 0.0, 4.375])

Check failure on line 49 in maths/polynomials/legendre.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

maths/polynomials/legendre.py:49:89: E501 Line too long (104 > 88)
"The 4th Legendre polynomial should be [0.375, 0.0, -3.75, 0.0, 4.375]"


if __name__ == "__main__":
pytest.main()
Loading