mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-07 01:50:55 +00:00
Added type hints and changed to descriptive names
This commit is contained in:
parent
a3a71d2ef1
commit
02f7cfac25
|
@ -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]"
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user