Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2526x 2526x 2526x 2526x 2526x 2526x 2526x 2526x 2526x 2526x 2526x 2526x 2526x 3x 3x 2523x 2523x 2523x 2523x 2526x 1005x 1005x 1005x 1005x 1005x 1005x 1518x 1518x 2526x 1x 1x 1518x 2526x 37x 37x 2526x 36x 36x 1445x 2526x 10x 10x 10x 10x 10x 6x 10x 4x 4x 10x 10x 29x 29x 29x 2x 2x 29x 10x 10x 1435x 1435x 1435x 2526x 35324x 35324x 35324x 35324x 1435x 2526x 1x 1x 1x 1x 1x | /**
* @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.
*
*
* ## Notice
*
* The following copyright, license, and long description were part of the original implementation available as part of [SciPy]{@link https://github.com/scipy/scipy/blob/fe5fd89f1d13791f8bee620e424fe8f91fbecb02/scipy/special/orthogonal_eval.pxd#L365}. The implementation has been modified for JavaScript.
*
* ```text
* Copyright (c) 2001-2002 Enthought, Inc. 2003-2025, SciPy Developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ```
*/
'use strict';
// MODULES //
var isnan = require( '@stdlib/assert/is-nan' );
var hyp2f1 = require( '@stdlib/math/base/special/hyp2f1' );
var gamma = require( '@stdlib/math/base/special/gamma' );
var abs = require( '@stdlib/math/base/special/abs' );
var floor = require( '@stdlib/math/base/special/floor' );
// MAIN //
/**
* Evaluates a Legendre polynomial.
*
* @param {number} n - degree of the polynomial
* @param {number} x - evaluation point
* @returns {number} 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.0, 0.5 );
* // returns 1.0
*
* @example
* var v = legendrepoly( NaN, 0.5 );
* // returns NaN
*/
function legendrepoly( n, x ) {
var nFloor;
var isInt;
var kk;
var d;
var g;
var a;
var b;
var c;
var p;
var k;
if ( isnan( n ) || isnan( x ) ) {
return NaN;
}
nFloor = floor( n );
isInt = ( n === nFloor );
if ( !isInt ) {
a = -n;
b = n + 1.0;
c = 1.0;
g = 0.5 * ( 1.0 - x );
return hyp2f1( a, b, c, g );
}
// For integer n, we use the recurrence relation...
if ( n < 0.0 ) {
n = -n - 1.0;
}
if ( n === 0.0 ) {
return 1.0;
}
if ( n === 1.0 ) {
return x;
}
if ( abs( x ) < 1e-5 ) {
// Power series rather than recurrence due to loss of precision
// http://functions.wolfram.com/Polynomials/LegendreP/02/
a = floor( n / 2.0 );
d = ( a % 2 === 0 ) ? 1.0 : -1.0;
if ( n === ( 2.0 * a ) ) {
d *= -2.0 / ( ( gamma( a + 1.0 ) * gamma( -0.5 ) ) / gamma( a + 0.5 ) );
} else {
d *= ( 2.0 * x ) / ( ( gamma( a + 1.0 ) * gamma( 0.5 ) ) / gamma( a + 1.5 ) );
}
p = 0.0;
for ( kk = 0; kk <= a; kk++ ) {
p += d;
d *= -2.0 * x * x * ( a - kk ) * ( ( 2.0 * n ) + 1.0 - ( 2.0 * a ) + ( 2.0 * kk ) ) / ( ( n + 1.0 - ( 2.0 * a ) + ( 2.0 * kk ) ) * ( n + 2.0 - ( 2.0 * a ) + ( 2.0 * kk ) ) );
if ( abs( d ) === 1e-20 * abs( p ) ) {
break;
}
}
return p;
}
d = x - 1.0;
p = x;
for ( kk = 0; kk < n - 1; kk++ ) {
k = kk + 1.0;
d = ( ( ( ( 2.0 * k ) + 1.0 ) / ( k + 1.0 ) ) * ( x - 1.0 ) * p ) + ( ( k / ( k + 1.0 ) ) * d );
p += d;
}
return p;
}
// EXPORTS //
module.exports = legendrepoly;
|