Compare commits

...

12 Commits

Author SHA1 Message Date
Nanoemm
c7c9b325ea
Merge 33266f262f5c45fd9adf9ad3b7d02fb378d0b05b into e59d819d091efdb30e385f4ecfe9ab5d36c3be71 2025-02-08 03:45:26 +08:00
Pablito
33266f262f Minor changes to type hints 2024-12-04 14:51:02 +01:00
Pablito
555b54cbbc Minor changes to type hints 2024-12-04 14:49:54 +01:00
Pablito
60ad958472 Added type hints for tests 2024-12-04 14:46:18 +01:00
Pablito
20c3200a62 Minor changes to some lists in the tests 2024-12-04 14:44:57 +01:00
Pablito
dddc326d0d Returned to previous commit with a variable name changed 2024-12-04 14:42:13 +01:00
Pablito
02f7cfac25 Added type hints and changed to descriptive names 2024-12-04 14:37:20 +01:00
Pablito
a3a71d2ef1 Removed an unused function 2024-12-03 11:27:22 +01:00
Pablito
be2ab89982 Changed the import block 2024-12-03 11:26:06 +01:00
pre-commit-ci[bot]
bb1d79fed8 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-12-03 10:22:48 +00:00
Nanoemm
08d1f15d1b updating DIRECTORY.md 2024-12-03 10:19:40 +00:00
Pablito
7036c84cb5 Implemented Legendre polynomial computation algorithm
Added an algorithm that calculates the coefficients of the Legendre polynomial of degree n using the recurrence relation
2024-12-03 11:19:20 +01:00
2 changed files with 56 additions and 0 deletions

View File

@ -726,6 +726,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)

View File

@ -0,0 +1,55 @@
# 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]:
"""
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_0() -> None:
"""Test the 0th Legendre polynomial."""
assert legendre(0) == [1.0], "The 0th Legendre polynomial should be [1.0]"
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() -> 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() -> 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() -> 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]"
if __name__ == "__main__":
pytest.main()