-
-
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
base: master
Are you sure you want to change the base?
Changes from 5 commits
7036c84
08d1f15
bb1d79f
be2ab89
a3a71d2
02f7cfa
dddc326
20c3200
60ad958
555b54c
33266f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# 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]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: |
||
""" | ||
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. | ||
""" | ||
p = (1 / (factorial(n) * (2**n))) * (Polynomial([-1, 0, 1]) ** n) | ||
return p.deriv(n).coef.tolist() | ||
|
||
|
||
def test_legendre_0(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: |
||
"""Test the 0th Legendre polynomial.""" | ||
assert legendre(0) == [1.0], "The 0th Legendre polynomial should be [1.0]" | ||
|
||
|
||
def test_legendre_1(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: |
||
"""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(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: |
||
"""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]" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An error occurred while parsing the file: 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_3(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: |
||
"""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(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: |
||
"""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() |
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.
Please provide descriptive name for the parameter:
n