mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-11 17:19:48 +00:00
Compare commits
13 Commits
c7c9b325ea
...
5930fc906a
Author | SHA1 | Date | |
---|---|---|---|
|
5930fc906a | ||
|
338cbafe0d | ||
|
33266f262f | ||
|
555b54cbbc | ||
|
60ad958472 | ||
|
20c3200a62 | ||
|
dddc326d0d | ||
|
02f7cfac25 | ||
|
a3a71d2ef1 | ||
|
be2ab89982 | ||
|
bb1d79fed8 | ||
|
08d1f15d1b | ||
|
7036c84cb5 |
@ -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)
|
||||
|
@ -1,4 +1,4 @@
|
||||
def actual_power(a: int, b: int):
|
||||
def actual_power(a: int, b: int) -> int:
|
||||
"""
|
||||
Function using divide and conquer to calculate a^b.
|
||||
It only works for integer a,b.
|
||||
@ -19,10 +19,12 @@ def actual_power(a: int, b: int):
|
||||
"""
|
||||
if b == 0:
|
||||
return 1
|
||||
half = actual_power(a, b // 2)
|
||||
|
||||
if (b % 2) == 0:
|
||||
return actual_power(a, int(b / 2)) * actual_power(a, int(b / 2))
|
||||
return half * half
|
||||
else:
|
||||
return a * actual_power(a, int(b / 2)) * actual_power(a, int(b / 2))
|
||||
return a * half * half
|
||||
|
||||
|
||||
def power(a: int, b: int) -> float:
|
||||
@ -43,9 +45,9 @@ def power(a: int, b: int) -> float:
|
||||
-0.125
|
||||
"""
|
||||
if b < 0:
|
||||
return 1 / actual_power(a, b)
|
||||
return 1 / actual_power(a, -b)
|
||||
return actual_power(a, b)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(power(-2, -3))
|
||||
print(power(-2, -3)) # output -0.125
|
||||
|
55
maths/polynomials/legendre.py
Normal file
55
maths/polynomials/legendre.py
Normal 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()
|
Loading…
x
Reference in New Issue
Block a user