All files factory.js

100% Statements 216/216
100% Branches 24/24
100% Functions 2/2
100% Lines 216/216

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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 2172x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 11x 11x 11x 11x 11x 11x 11x 11x 11x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 11x 1x 1x 9x 9x 2x 2x 7x 7x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 14x 14x 8x 8x 14x 14x 6x 6x 28x 28x 6x 6x 6x 6x 14x 14x 8x 8x 14x 14x 6x 6x 28x 28x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 14x 14x 8x 8x 14x 14x 6x 6x 28x 28x 6x 6x 6x 6x 6x 14x 14x 8x 8x 14x 14x 6x 6x 28x 28x 6x 6x 6x 6x 6x 6x 6x 6x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x 1x 11x 2x 2x 2x 2x 2x  
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 Fcn = require( '@stdlib/function/ctor' );
var evalrationalf = require( './main.js' );
 
 
// MAIN //
 
/**
* Generates a function for evaluating a rational function using single-precision floating-point arithmetic.
*
* ## Notes
*
* -   The compiled function uses [Horner's rule][horners-method] for efficient computation.
*
* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method
*
* @param {NumericArray} P - numerator polynomial coefficients sorted in ascending degree
* @param {NumericArray} Q - denominator polynomial coefficients sorted in ascending degree
* @returns {Function} function for evaluating a rational function
*
* @example
* var P = [ 20.0, 8.0, 3.0 ];
* var Q = [ 10.0, 9.0, 1.0 ];
*
* var rational = factory( P, Q );
*
* var v = rational( 10.0 ); // => (20*10^0 + 8*10^1 + 3*10^2) / (10*10^0 + 9*10^1 + 1*10^2) = (20+80+300)/(10+90+100)
* // returns 2.0
*
* v = rational( 2.0 ); // => (20*2^0 + 8*2^1 + 3*2^2) / (10*2^0 + 9*2^1 + 1*2^2) = (20+16+12)/(10+18+4)
* // returns 1.5
*/
function factory( P, Q ) {
	var f;
	var r;
	var n;
	var m;
	var i;
 
	// Avoid exceeding maximum stack size on V8 :(. Note that the value of `500` was empirically determined...
	if ( P.length > 500 ) {
		return rational;
	}
	// Code generation. Start with the function definition...
	f = 'return function evalrationalf(x){';
 
	// Create the function body...
	n = P.length;
 
	// Declare variables...
	f += 'var ax,s1,s2;';
 
	// If no coefficients, the function always returns NaN...
	if ( n === 0 ) {
		f += 'return NaN;';
	}
	// If P and Q have different lengths, the function always returns NaN...
	else if ( n !== Q.length ) {
		f += 'return NaN;';
	}
	// If P and Q have only one coefficient, the function always returns the ratio of the first coefficients...
	else if ( n === 1 ) {
		r = float64ToFloat32( P[ 0 ] / Q[ 0 ] );
		f += 'return ' + r + ';';
	}
	// If more than one coefficient, apply Horner's method to both the numerator and denominator...
	else {
		// If `x == 0`, return the ratio of the first coefficients...
		r = float64ToFloat32( P[ 0 ] / Q[ 0 ] );
		f += 'if(x===0.0){return ' + r + ';}';
 
		// Compute the absolute value of `x`...
		f += 'if(x<0.0){ax=-x;}else{ax=x;}';
 
		// If `abs(x) <= 1`, evaluate the numerator and denominator of the rational function using Horner's method...
		f += 'if(ax<=1.0){';
		f += 's1 = f64_to_f32(' + P[ 0 ];
		m = n - 1;
		for ( i = 1; i < n; i++ ) {
			f += '+f64_to_f32(x*';
			if ( i < m ) {
				f += '(';
			}
			f += P[ i ];
		}
		// Close all the parentheses...
		for ( i = 0; i < 2*m; i++ ) {
			f += ')';
		}
		f += ';';
		f += 's2 = f64_to_f32(' + Q[ 0 ];
		m = n - 1;
		for ( i = 1; i < n; i++ ) {
			f += '+f64_to_f32(x*';
			if ( i < m ) {
				f += '(';
			}
			f += Q[ i ];
		}
		// Close all the parentheses...
		for ( i = 0; i < 2*m; i++ ) {
			f += ')';
		}
		f += ';';
 
		// Close the if statement...
		f += '}else{';
 
		// If `abs(x) > 1`, evaluate the numerator and denominator via the inverse to avoid overflow...
		f += 'x = f64_to_f32(1.0/x);';
		m = n - 1;
		f += 's1 = f64_to_f32(' + P[ m ];
		for ( i = m - 1; i >= 0; i-- ) {
			f += '+f64_to_f32(x*';
			if ( i > 0 ) {
				f += '(';
			}
			f += P[ i ];
		}
		// Close all the parentheses...
		for ( i = 0; i < 2*m; i++ ) {
			f += ')';
		}
		f += ';';
 
		m = n - 1;
		f += 's2 = f64_to_f32(' + Q[ m ];
		for ( i = m - 1; i >= 0; i-- ) {
			f += '+f64_to_f32(x*';
			if ( i > 0 ) {
				f += '(';
			}
			f += Q[ i ];
		}
		// Close all the parentheses...
		for ( i = 0; i < 2*m; i++ ) {
			f += ')';
		}
		f += ';';
 
		// Close the else statement...
		f += '}';
 
		// Return the ratio of the two sums...
		f += 'return f64_to_f32(s1/s2);';
	}
	// Close the function:
	f += '}';
 
	// Add a source directive for debugging:
	f += '//# sourceURL=evalrationalf.factory.js';
 
	// Create the function in the global scope:
	return ( new Fcn( 'f64_to_f32', f ) )( float64ToFloat32 );
 
	/*
	*	function evalrationalf( x ) {
	*		var ax, s1, s2;
	*		if ( x === 0.0 ) {
	*			return f64_to_f32( P[0] / Q[0] );
	*		}
	*		if ( x < 0.0 ) {
	*			ax = -x;
	*		} else {
	*			ax = x;
	*		}
	*		if ( ax <= 1.0 ) {
	*			s1 = f64_to_f32(P[0]+f64_to_f32(x*f64_to_f32(P[1]+f64_to_f32(x*f64_to_f32(P[2]+f64_to_f32(x*f64_to_f32(P[3]+...+f64_to_f32(x*f64_to_f32(P[n-2]+f64_to_f32(x*P[n-1]))))))))));
	*			s2 = f64_to_f32(Q[0]+f64_to_f32(x*(Q[1]+f64_to_f32(x*(Q[2]+f64_to_f32(x*(Q[3]+...+f64_to_f32(x*(Q[n-2]+f64_to_f32(x*Q[n-1]))))))))));
	*		} else {
	*			x = 1.0/x;
	*			s1 = f64_to_f32(P[n-1]+f64_to_f32(x*(P[n-2]+f64_to_f32(x*(P[n-3]+f64_to_f32(x*(P[n-4]+...+f64_to_f32(x*(P[1]+f64_to_f32(x*P[0]))))))))));
	*			s2 = f64_to_f32(Q[n-1]+f64_to_f32(x*(Q[n-2]+f64_to_f32(x*(Q[n-3]+f64_to_f32(x*(Q[n-4]+...+f64_to_f32(x*(Q[1]+f64_to_f32(x*Q[0]))))))))));
	*		}
	*		return f64_to_f32( s1 / s2 );
	*	}
	*/
 
	/**
	* Evaluates a rational function.
	*
	* @private
	* @param {number} x - value at which to evaluate a rational function
	* @returns {number} evaluated rational function
	*/
	function rational( x ) {
		return evalrationalf( P, Q, x );
	}
}
 
 
// EXPORTS //
 
module.exports = factory;