Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 -->
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();
});
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;
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
}
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 );
}
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;
Loading