All files factory.js

100% Statements 123/123
100% Branches 11/11
100% Functions 2/2
100% Lines 123/123

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 1243x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 6x 1x 1x 4x 4x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 5x 5x 5x 5x 3x 3x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 6x 3x 3x 3x 3x 3x  
/**
* @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 float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
var Float32Array = require( '@stdlib/array/float32' );
var Fcn = require( '@stdlib/function/ctor' );
var chebyshevSeriesf = require( './main.js' );
 
 
// MAIN //
 
/**
* Generates a function for evaluating a Chebyshev series.
*
* ## Notes
*
* -   The compiled function uses [Clenshaw's algorithm][clenshaw-algorithm] for efficient computation.
*
* [clenshaw-algorithm]: https://en.wikipedia.org/wiki/Clenshaw_algorithm
*
* @param {NumericArray} c - Chebyshev series coefficients in descending degree order
* @returns {Function} function for evaluating a Chebyshev series
*
* @example
* var Float32Array = require( '@stdlib/array/float32' );
*
* var polyval = factory( new Float32Array( [ 1.0, 0.5 ] ) );
*
* var v = polyval( 1.0 ); // 1*T_0(1/2) + 0.5*T_1(1/2)
* // returns 0.75
*
* v = polyval( 0.0 ); // 1*T_0(0) + 0.5*T_1(0)
* // returns 0.25
*/
function factory( c ) {
	var f;
	var n;
	var i;
 
	// Explicitly copy in order to ensure single-precision:
	c = new Float32Array( c );
 
	// Use the general implementation if the number of coefficients is large:
	if ( c.length > 500 ) {
		return evaluate;
	}
 
	// Code generation. Start with the function definition...
	f = 'return function chebyshevSeriesf(x){';
	f += 'var b0;';
	f += 'var b1;';
	f += 'var b2;';
 
	// Create the function body...
	n = c.length;
 
	// If no coefficients, the function always returns zero...
	if ( n === 0 ) {
		f += 'return 0.0;';
	}
	// If only one coefficient, the function always returns that coefficient divided by two...
	else if ( n === 1 ) {
		f += 'return f64_to_f32(0.5 * ' + c[ 0 ] + ');';
	}
	// If more than one coefficient, apply Clenshaw's algorithm...
	else {
		// Initialize:
		f += 'b0 = ' + c[ 0 ] + ';';
		f += 'b1 = 0.0;';
 
		// Recurrence:
		for ( i = 1; i < n; i++ ) {
			f += 'b2 = b1;';
			f += 'b1 = b0;';
			f += 'b0 = f64_to_f32(f64_to_f32(f64_to_f32(x*b1) - b2) + ' + c[ i ] + ');';
		}
		f += 'return f64_to_f32(0.5 * f64_to_f32(b0 - b2));';
	}
 
	// Close the function:
	f += '};';
 
	// Add a source directive for debugging:
	f += '//# sourceURL=chebyshevSeriesf.factory.js';
 
	// Create the function in the global scope:
	return ( new Fcn( 'f64_to_f32', f ) )( float64ToFloat32 );
 
	/**
	* Evaluates a Chebyshev series.
	*
	* @private
	* @param {number} x - evaluation point
	* @returns {number} evaluated series
	*/
	function evaluate( x ) {
		return chebyshevSeriesf( x, c );
	}
}
 
 
// EXPORTS //
 
module.exports = factory;