-
-
Notifications
You must be signed in to change notification settings - Fork 46.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added an implementation of Legendre polynomial computation algorithm #12407
Open
Nanoemm
wants to merge
11
commits into
TheAlgorithms:master
Choose a base branch
from
Miranda13:legendre
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7036c84
Implemented Legendre polynomial computation algorithm
Nanoemm 08d1f15
updating DIRECTORY.md
Nanoemm bb1d79f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] be2ab89
Changed the import block
Nanoemm a3a71d2
Removed an unused function
Nanoemm 02f7cfa
Added type hints and changed to descriptive names
Nanoemm dddc326
Returned to previous commit with a variable name changed
Nanoemm 20c3200
Minor changes to some lists in the tests
Nanoemm 60ad958
Added type hints for tests
Nanoemm 555b54c
Minor changes to type hints
Nanoemm 33266f2
Minor changes to type hints
Nanoemm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]" | ||
|
||
|
||
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]" | ||
|
||
|
||
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]" | ||
|
||
|
||
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]" | ||
|
||
|
||
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]) | ||
"The 4th Legendre polynomial should be [0.375, 0.0, -3.75, 0.0, 4.375]" | ||
|
||
|
||
if __name__ == "__main__": | ||
pytest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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