All files main.js

100% Statements 91/91
100% Branches 12/12
100% Functions 1/1
100% Lines 91/91

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 921x 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 3688x 3688x 6x 6x 3688x 2674x 2143x 2143x 2674x 14x 14x 517x 517x 1008x 3688x 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.
*/
 
'use strict';
 
// MODULES //
 
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var isInteger = require( '@stdlib/math/base/assert/is-integer' );
var isInfinite = require( '@stdlib/math/base/assert/is-infinite' );
var recurrence = require( './recurrence.js' );
var closedForm = require( './closed_form.js' );
 
 
// MAIN //
 
/**
* Evaluates a Chebyshev polynomial of the second kind for a degree `n` at a value `x`.
*
* ## Notes
*
* -   For integer `n`, the function uses the three-term recurrence relation. Negative integer degrees are handled using the reflection formula \\( U_{-n}(x) = -U_{n-2}(x) \\).
* -   For non-integer `n`, the function is defined as \\( (n+1) \\, {}_2F_1\left( -n, n+2; \tfrac{3}{2}; \tfrac{1}{2}(1-x) \right) \\). We evaluate it using the equivalent closed form \\( \sin\left( (n+1)\theta \right) / \sin\theta \\), where \\( x = \cos\theta \\) (and its hyperbolic analog for \\( x > 1 \\)), as evaluating the hypergeometric function directly loses accuracy for larger \\( \|n\| \\) when `x` is negative. For \\( x < -1 \\), the function is complex-valued, and the function returns `NaN`.
*
* @param {number} n - degree
* @param {number} x - input value
* @returns {number} function value
*
* @example
* var y = chebyshevupoly( 0.0, 0.5 );
* // returns 1.0
*
* @example
* var y = chebyshevupoly( 1.0, -0.5 );
* // returns -1.0
*
* @example
* var y = chebyshevupoly( 3.0, 0.5 );
* // returns -1.0
*
* @example
* var y = chebyshevupoly( -3.0, 0.5 );
* // returns -1.0
*
* @example
* var y = chebyshevupoly( 0.5, 0.5 );
* // returns ~1.155
*
* @example
* var y = chebyshevupoly( NaN, 0.5 );
* // returns NaN
*
* @example
* var y = chebyshevupoly( 2.0, NaN );
* // returns NaN
*/
function chebyshevupoly( n, x ) {
	if ( isnan( n ) || isnan( x ) || isInfinite( n ) ) {
		return NaN;
	}
	if ( isInteger( n ) ) {
		if ( n >= 0.0 ) {
			return recurrence( n, x );
		}
		if ( n === -1.0 ) {
			return 0.0;
		}
		return -recurrence( -n-2.0, x );
	}
	return closedForm( n, x );
}
 
 
// EXPORTS //
 
module.exports = chebyshevupoly;