-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add JS implementation of math/base/special/legendre-polynomial
#10932
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
Draft
officiallyanee
wants to merge
5
commits into
stdlib-js:develop
Choose a base branch
from
officiallyanee:feat/legendre-polynomial
base: develop
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.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
933e762
feat: add JS implementation of math/base/special/legendre-polynomial
officiallyanee 3b5e828
chore: update copyright years
stdlib-bot 860a13b
fix: lint in runner.py
officiallyanee 0b158eb
fix: use gamma instead of beta for small-x power series branch & add …
officiallyanee ece1250
Merge remote-tracking branch 'upstream/develop' into feat/legendre-po…
stdlib-bot 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
160 changes: 160 additions & 0 deletions
160
lib/node_modules/@stdlib/math/base/special/legendre-polynomial/README.md
This file contains hidden or 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,160 @@ | ||
| <!-- | ||
|
|
||
| @license Apache-2.0 | ||
|
|
||
| Copyright (c) 2026 The Stdlib Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| --> | ||
|
|
||
| # Legendre Polynomial | ||
|
|
||
| > Evaluate a [Legendre polynomial][legendre-polynomial]. | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| The [Legendre polynomials][legendre-polynomial] $P_n(x)$ are classical orthogonal polynomials which are solutions to the Legendre differential equation | ||
|
|
||
| <!-- <equation class="equation" label="eq:legendre_ode" align="center" raw="(1 - x^2) y'' - 2x y' + n(n+1) y = 0" alt="Legendre differential equation"> --> | ||
|
|
||
| ```math | ||
| (1 - x^2) P_n''(x) - 2x P_n'(x) + n(n+1) P_n(x) = 0 | ||
| ``` | ||
|
|
||
| <!-- <div class="equation" align="center" data-raw-text="(1 - x^2) P_n''(x) - 2x P_n'(x) + n(n+1) P_n(x) = 0" data-equation="eq:legendre_ode"> | ||
| <img src="docs/img/equation_legendre_ode.svg" alt="Legendre differential equation"> | ||
| <br> | ||
| </div> --> | ||
|
|
||
| <!-- </equation> --> | ||
|
|
||
| For **integer** non-negative $n$, they satisfy the three-term recurrence relation | ||
|
|
||
| <!-- <equation class="equation" label="eq:legendre_recurrence" align="center" raw="(n+1) P_{n+1}(x) = (2n+1)\, x\, P_n(x) - n\, P_{n-1}(x)" alt="Legendre polynomial recurrence relation"> --> | ||
|
|
||
| ```math | ||
| (n+1) P_{n+1}(x) = (2n+1)\, x\, P_n(x) - n\, P_{n-1}(x) | ||
| ``` | ||
|
|
||
| <!-- <div class="equation" align="center" data-raw-text="(n+1) P_{n+1}(x) = (2n+1)\, x\, P_n(x) - n\, P_{n-1}(x)" data-equation="eq:legendre_recurrence"> | ||
| <img src="docs/img/equation_legendre_recurrence.svg" alt="Legendre polynomial recurrence relation"> | ||
| <br> | ||
| </div> --> | ||
|
|
||
| <!-- </equation> --> | ||
|
|
||
| with $P_0(x) = 1$ and $P_1(x) = x$. For **non-integer** $n$, the polynomial is generalized via the relation to the Gauss hypergeometric function | ||
|
|
||
| <!-- <equation class="equation" label="eq:legendre_hyp2f1" align="center" raw="P_n(x) = {}_2F_1 \left( -n,\, n+1;\, 1;\, \tfrac{1-x}{2} \right)" alt="Legendre polynomial via hypergeometric function"> --> | ||
|
|
||
| ```math | ||
| P_n(x) = {}_2F_1 \left( -n,\, n+1;\, 1;\, \tfrac{1-x}{2} \right) | ||
| ``` | ||
|
|
||
| <!-- <div class="equation" align="center" data-raw-text="P_n(x) = {}_2F_1 \left( -n,\, n+1;\, 1;\, \tfrac{1-x}{2} \right)" data-equation="eq:legendre_hyp2f1"> | ||
| <img src="docs/img/equation_legendre_hyp2f1.svg" alt="Legendre polynomial via hypergeometric function"> | ||
| <br> | ||
| </div> --> | ||
|
|
||
| <!-- </equation> --> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var legendrepoly = require( '@stdlib/math/base/special/legendre-polynomial' ); | ||
| ``` | ||
|
|
||
| #### legendrepoly( n, x ) | ||
|
|
||
| Evaluates a [Legendre polynomial][legendre-polynomial] of degree `n` at a point `x`. | ||
|
|
||
| ```javascript | ||
| var v = legendrepoly( 1.0, 0.5 ); | ||
| // returns 0.5 | ||
|
|
||
| v = legendrepoly( 2.0, 0.5 ); | ||
| // returns -0.125 | ||
| ``` | ||
|
|
||
| If `n` is not an integer, the polynomial is evaluated using the Gauss hypergeometric function. | ||
|
|
||
| ```javascript | ||
| var v = legendrepoly( 1.5, 0.5 ); | ||
| // returns ~0.172 | ||
| ``` | ||
|
|
||
| If provided `NaN` for any argument, the function returns `NaN`. | ||
|
|
||
| ```javascript | ||
| var v = legendrepoly( NaN, 0.5 ); | ||
| // returns NaN | ||
|
|
||
| v = legendrepoly( 1.0, NaN ); | ||
| // returns NaN | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var uniform = require( '@stdlib/random/base/uniform' ); | ||
| var legendrepoly = require( '@stdlib/math/base/special/legendre-polynomial' ); | ||
|
|
||
| var x; | ||
| var n; | ||
| var v; | ||
| var i; | ||
|
|
||
| for ( i = 0; i < 100; i++ ) { | ||
| n = uniform( -50.0, 50.0 ); | ||
| x = uniform( -1.0, 1.0 ); | ||
| v = legendrepoly( n, x ); | ||
| console.log( 'P_%d(%d) = %d', n, x, v ); | ||
| } | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.related --> | ||
|
|
||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| [legendre-polynomial]: https://en.wikipedia.org/wiki/Legendre_polynomials | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> |
54 changes: 54 additions & 0 deletions
54
lib/node_modules/@stdlib/math/base/special/legendre-polynomial/benchmark/benchmark.js
This file contains hidden or 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 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var bench = require( '@stdlib/bench' ); | ||
| var uniform = require( '@stdlib/random/array/uniform' ); | ||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var legendrepoly = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var n; | ||
| var x; | ||
| var y; | ||
| var i; | ||
|
|
||
| n = uniform( 100, -50.0, 50.0 ); | ||
| x = uniform( 100, -1.0, 1.0 ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| y = legendrepoly( n[ i%n.length ], x[ i%x.length ] ); | ||
| if ( isnan( y ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( isnan( y ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
45 changes: 45 additions & 0 deletions
45
lib/node_modules/@stdlib/math/base/special/legendre-polynomial/docs/types/index.d.ts
This file contains hidden or 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,45 @@ | ||
| /* | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| // TypeScript Version: 4.1 | ||
|
|
||
| /** | ||
| * Evaluates a Legendre polynomial. | ||
| * | ||
| * @param n - degree of the polynomial | ||
| * @param x - evaluation point | ||
| * @returns polynomial value | ||
| * | ||
| * @example | ||
| * var v = legendrepoly( 1.0, 0.5 ); | ||
| * // returns 0.5 | ||
| * | ||
| * @example | ||
| * var v = legendrepoly( 2.0, 0.5 ); | ||
| * // returns -0.125 | ||
| * | ||
| * @example | ||
| * var v = legendrepoly( 1.5, 0.5 ); | ||
| * // returns ~0.172 | ||
| */ | ||
| declare function legendrepoly( n: number, x: number ): number; | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| export = legendrepoly; |
52 changes: 52 additions & 0 deletions
52
lib/node_modules/@stdlib/math/base/special/legendre-polynomial/docs/types/test.ts
This file contains hidden or 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,52 @@ | ||
| /* | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import legendrepoly = require( './index' ); | ||
|
|
||
|
|
||
| // TESTS // | ||
|
|
||
| // The function returns a number... | ||
| { | ||
| legendrepoly( 1.0, 0.5 ); // $ExpectType number | ||
| legendrepoly( 2.0, 0.5 ); // $ExpectType number | ||
| legendrepoly( 1.5, 0.5 ); // $ExpectType number | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is provided values other than two numbers... | ||
| { | ||
| legendrepoly( true, 0.5 ); // $ExpectError | ||
| legendrepoly( false, 0.5 ); // $ExpectError | ||
| legendrepoly( '5', 0.5 ); // $ExpectError | ||
| legendrepoly( [], 0.5 ); // $ExpectError | ||
| legendrepoly( {}, 0.5 ); // $ExpectError | ||
| legendrepoly( ( x: number ): number => x, 0.5 ); // $ExpectError | ||
|
|
||
| legendrepoly( 1.0, true ); // $ExpectError | ||
| legendrepoly( 1.0, false ); // $ExpectError | ||
| legendrepoly( 1.0, '5' ); // $ExpectError | ||
| legendrepoly( 1.0, [] ); // $ExpectError | ||
| legendrepoly( 1.0, {} ); // $ExpectError | ||
| legendrepoly( 1.0, ( x: number ): number => x ); // $ExpectError | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is provided insufficient arguments... | ||
| { | ||
| legendrepoly(); // $ExpectError | ||
| legendrepoly( 1.0 ); // $ExpectError | ||
| } |
34 changes: 34 additions & 0 deletions
34
lib/node_modules/@stdlib/math/base/special/legendre-polynomial/examples/index.js
This file contains hidden or 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,34 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| var uniform = require( '@stdlib/random/base/uniform' ); | ||
| var legendrepoly = require( './../lib' ); | ||
|
|
||
| var x; | ||
| var n; | ||
| var v; | ||
| var i; | ||
|
|
||
| for ( i = 0; i < 100; i++ ) { | ||
| n = uniform( -50.0, 50.0 ); | ||
| x = uniform( -1.0, 1.0 ); | ||
| v = legendrepoly( n, x ); | ||
| console.log( 'P_%d(%d) = %d', n, x, v ); | ||
| } | ||
46 changes: 46 additions & 0 deletions
46
lib/node_modules/@stdlib/math/base/special/legendre-polynomial/lib/index.js
This file contains hidden or 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,46 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Evaluate a Legendre polynomial. | ||
| * | ||
| * @module @stdlib/math/base/special/legendre-polynomial | ||
| * | ||
| * @example | ||
| * var legendrepoly = require( '@stdlib/math/base/special/legendre-polynomial' ); | ||
| * | ||
| * var v = legendrepoly( 1.0, 0.5 ); | ||
| * // returns 0.5 | ||
| * | ||
| * v = legendrepoly( 0.0, 0.5 ); | ||
| * // returns 1.0 | ||
| * | ||
| * v = legendrepoly( 2.0, 0.5 ); | ||
| * // returns -0.125 | ||
| */ | ||
|
|
||
| // MODULES // | ||
|
|
||
| var main = require( './main.js' ); | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| module.exports = 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.
Apparently we have moved away from using a
forto a recursive method using:-Re:- https://github.com/stdlib-js/stdlib/blob/develop/lib/node_modules/%40stdlib/math/base/special/atan2d/examples/index.js
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.
Ditto for makdown!!!