All files main.js

100% Statements 170/170
97.56% Branches 40/41
100% Functions 1/1
100% Lines 170/170

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 1711x 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 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 22x 22x 36x 36x 36x 36x 6x 6x 36x 9x 9x 9x 30x 36x 9x 9x 6x 6x 9x 9x 21x 21x 21x 36x 9x 9x 4509x 4509x 3006x 3006x 4509x 4500x 4500x 4509x 9x 2x 9x 7x 7x 9x 9x 12x 36x 4x 36x 8x 8x 12x 36x 9x 9x 36x 45x 15x 15x 11x 11x 45x 30x 30x 22x 22x 30x 45x 45x 27x 27x 45x 12x 36x 78x 78x 36x 4x 4x 12x 36x 9x 9x 36x 4x 36x 8x 8x 12x 12x 36x 36x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2018 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 join = require( 'path' ).join;
var readFile = require( '@stdlib/fs/read-file' ).sync;
var replace = require( '@stdlib/string/replace' );
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
var Float32Array = require( '@stdlib/array/float32' );
 
 
// VARIABLES //
 
var opts = {
	'encoding': 'utf8'
};
var dir = join( __dirname, 'templates' );
 
// Templates:
var SINGLE_COEFFICIENT_TEMPLATE = readFile( join( dir, 'single_coefficient.js.txt' ), opts ); // eslint-disable-line id-length
 
var EVALPOLY_TEMPLATE = readFile( join( dir, 'evalpoly.js.txt' ), opts );
var EVALPOLY_FLOAT32_TEMPLATE = readFile( join( dir, 'evalpoly.float32.js.txt' ), opts );
 
var EMPTY_TEMPLATE = readFile( join( dir, 'empty.js.txt' ), opts );
 
var LOOP_TEMPLATE = readFile( join( dir, 'loop.js.txt' ), opts );
var LOOP_FLOAT32_TEMPLATE = readFile( join( dir, 'loop.float32.js.txt' ), opts );
 
var MAX_CHARS = 68; // max-len (80) - chars already in line ('tab': 4, 'return ': 7, ';': 1)
 
 
// MAIN //
 
/**
* Compiles a module string which exports a function for evaluating a polynomial.
*
* @param {NumericArray} c - polynomial coefficients sorted in ascending degree
* @param {Options} [options] - function options
* @param {string} [options.dtype='float64'] - input value floating-point data type
* @returns {string} module string exporting a function for evaluating a polynomial
*
* @example
* var str = compile( [ 3.0, 2.0, 1.0 ] );
* // returns <string>
*/
function compile( c, options ) {
	var horner;
	var opts;
	var tmpl;
	var str;
	var n;
	var m;
	var i;
 
	opts = {
		'dtype': 'float64'
	};
	if ( arguments.length > 1 ) {
		opts.dtype = options.dtype || opts.dtype;
	}
	n = c.length;
 
	// If no coefficients, the function always returns 0...
	if ( n === 0 ) {
		return EMPTY_TEMPLATE;
	}
	if ( opts.dtype === 'float32' ) {
		// Ensure that coefficients have been converted to single-precision:
		c = new Float32Array( c );
	}
	// If only one coefficient, the function always returns that coefficient...
	if ( n === 1 ) {
		str = c[ 0 ].toString();
		if ( isInteger( c[ 0 ] ) ) {
			str += '.0';
		}
		return replace( SINGLE_COEFFICIENT_TEMPLATE, '{{coefficient}}', str );
	}
	m = n - 1;
 
	// Avoid exceeding the maximum stack size on V8 by using a simple loop :(. Note that the choice of `500` was empirically determined...
	if ( n > 500 ) {
		str = '';
		for ( i = 0; i < n; i++ ) {
			str += '\t' + c[ i ].toString();
			if ( isInteger( c[ i ] ) ) {
				str += '.0';
			}
			if ( i < m ) {
				str += ',\n';
			}
		}
		if ( opts.dtype === 'float32' ) {
			tmpl = LOOP_FLOAT32_TEMPLATE;
		} else {
			tmpl = LOOP_TEMPLATE;
		}
		return replace( tmpl, '{{coefficients}}', str );
	}
	// If more than one coefficient, apply Horner's method...
	if ( opts.dtype === 'float32' ) {
		horner = 'float64ToFloat32(';
	} else {
		horner = '';
	}
	horner += c[ 0 ].toString();
	if ( isInteger( c[ 0 ] ) ) {
		horner += '.0';
	}
	for ( i = 1; i < n; i++ ) {
		if ( opts.dtype === 'float32' ) {
			horner += ' + float64ToFloat32(x * ';
			if ( i < m ) {
				horner += 'float64ToFloat32(';
			}
		} else {
			horner += ' + (x * ';
			if ( i < m ) {
				horner += '(';
			}
		}
		horner += c[ i ].toString();
		if ( isInteger( c[ i ] ) ) {
			horner += '.0';
		}
	}
	// Close all the parentheses...
	for ( i = 0; i < (2*(n-1))-1; i++ ) {
		horner += ')';
	}
	if ( opts.dtype === 'float32' ) {
		horner += ')';
	}
	str = c[ 0 ].toString();
	if ( isInteger( c[ 0 ] ) ) {
		str += '.0';
	}
	if ( opts.dtype === 'float32' ) {
		tmpl = EVALPOLY_FLOAT32_TEMPLATE;
	} else {
		tmpl = EVALPOLY_TEMPLATE;
	}
	str = replace( tmpl, '{{coefficient}}', str );
	str = replace( str, '{{horner}}', horner );
	return replace( str, '{{eslint}}', ( horner.length > MAX_CHARS ) ? ' // eslint-disable-line max-len' : '' );
}
 
 
// EXPORTS //
 
module.exports = compile;