diff --git a/base/tools/evalpoly-compile/README.md b/base/tools/evalpoly-compile/README.md index d2e6baa38..1109e5beb 100644 --- a/base/tools/evalpoly-compile/README.md +++ b/base/tools/evalpoly-compile/README.md @@ -36,16 +36,20 @@ limitations under the License. var compile = require( '@stdlib/math/base/tools/evalpoly-compile' ); ``` -#### compile( c ) +#### compile( c\[, options] ) -Compiles a module `string` containing an exported function which evaluates a [polynomial][@stdlib/math/base/tools/evalpoly] having coefficients `c`. +Compiles a module string containing an exported function which evaluates a [polynomial][@stdlib/math/base/tools/evalpoly] having coefficients `c`. ```javascript var str = compile( [ 3.0, 2.0, 1.0 ] ); // returns ``` -In the example above, the output `string` would correspond to the following module: +The function supports the following `options`: + +- **dtype**: input argument floating-point data type (e.g., `float64` or `float32`). Default: `'float64'`. + +In the example above, the output string would correspond to the following module: @@ -71,7 +75,7 @@ function evalpoly( x ) { if ( x === 0.0 ) { return 3.0; } - return 3.0 + (x * (2.0 + (x * 1.0))); // eslint-disable-line max-len + return 3.0 + (x * (2.0 + (x * 1.0))); } @@ -82,6 +86,55 @@ module.exports = evalpoly; The coefficients should be ordered in **ascending** degree, thus matching summation notation. +By default, the function assumes double-precision floating-point arithmetic. To emulate single-precision floating-point arithmetic, set the `dtype` option to `'float32'`. + +```javascript +var str = compile( [ 3.0, 2.0, 1.0 ], { + 'dtype': 'float32' +}); +// returns +``` + +In the previous example, the output string would correspond to the following module: + + + +```javascript +'use strict'; + +// MODULES // + +var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); + + +// MAIN // + +/** +* Evaluates a polynomial. +* +* ## Notes +* +* - The implementation uses [Horner's rule][horners-method] for efficient computation. +* +* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method +* +* @private +* @param {number} x - value at which to evaluate the polynomial +* @returns {number} evaluated polynomial +*/ +function evalpoly( x ) { + if ( x === 0.0 ) { + return 3.0; + } + return float64ToFloat32(3.0 + float64ToFloat32(x * float64ToFloat32(2.0 + float64ToFloat32(x * 1.0)))); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = evalpoly; +``` + @@ -103,29 +156,14 @@ The coefficients should be ordered in **ascending** degree, thus matching summat ```javascript -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); -var Float64Array = require( '@stdlib/array/float64' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var compile = require( '@stdlib/math/base/tools/evalpoly-compile' ); -var coef; -var sign; -var str; -var i; - -// Create an array of random coefficients... -coef = new Float64Array( 10 ); -for ( i = 0; i < coef.length; i++ ) { - if ( randu() < 0.5 ) { - sign = -1.0; - } else { - sign = 1.0; - } - coef[ i ] = sign * round( randu()*100.0 ); -} +// Create an array of random coefficients: +var coef = discreteUniform( 10, -100, 100 ); // Compile a module for evaluating a polynomial: -str = compile( coef ); +var str = compile( coef ); console.log( str ); ``` diff --git a/base/tools/evalpoly-compile/docs/types/index.d.ts b/base/tools/evalpoly-compile/docs/types/index.d.ts index 68b278d25..93e260005 100644 --- a/base/tools/evalpoly-compile/docs/types/index.d.ts +++ b/base/tools/evalpoly-compile/docs/types/index.d.ts @@ -18,17 +18,28 @@ // TypeScript Version: 4.1 +/** +* Interface describing function options. +*/ +interface Options { + /** + * Input value floating-point data type (e.g., `float64` or `float32`). Default: `'float64'`. + */ + dtype?: 'float64' | 'float32'; +} + /** * Compiles a module string which exports a function for evaluating a polynomial. * * @param c - polynomial coefficients sorted in ascending degree +* @param options - function options * @returns module string exporting a function for evaluating a polynomial * * @example * var str = compile( [ 3.0, 2.0, 1.0 ] ); * // returns */ -declare function compile( c: Array ): string; +declare function compile( c: Array, options?: Options ): string; // EXPORTS // diff --git a/base/tools/evalpoly-compile/docs/types/test.ts b/base/tools/evalpoly-compile/docs/types/test.ts index 74506548e..93d3b80cb 100644 --- a/base/tools/evalpoly-compile/docs/types/test.ts +++ b/base/tools/evalpoly-compile/docs/types/test.ts @@ -24,6 +24,7 @@ import compile = require( './index' ); // The function returns a string... { compile( [ 3.0, 2.0, 1.0 ] ); // $ExpectType string + compile( [ 3.0, 2.0, 1.0 ], { 'dtype': 'float32' } ); // $ExpectType string } // The compiler throws an error if the function is provided a first argument which is not an array of numbers... @@ -34,6 +35,31 @@ import compile = require( './index' ); compile( 123 ); // $ExpectError compile( {} ); // $ExpectError compile( ( x: number ): number => x ); // $ExpectError + + compile( true, {} ); // $ExpectError + compile( false, {} ); // $ExpectError + compile( 'abc', {} ); // $ExpectError + compile( 123, {} ); // $ExpectError + compile( {}, {} ); // $ExpectError + compile( ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an object... +{ + compile( [ 3.0, 2.0, 1.0 ], true ); // $ExpectError + compile( [ 3.0, 2.0, 1.0 ], false ); // $ExpectError + compile( [ 3.0, 2.0, 1.0 ], 'abc' ); // $ExpectError + compile( [ 3.0, 2.0, 1.0 ], 123 ); // $ExpectError + compile( [ 3.0, 2.0, 1.0 ], ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dtype` option... +{ + compile( [ 3.0, 2.0, 1.0 ], { 'dtype': true } ); // $ExpectError + compile( [ 3.0, 2.0, 1.0 ], { 'dtype': false } ); // $ExpectError + compile( [ 3.0, 2.0, 1.0 ], { 'dtype': [] } ); // $ExpectError + compile( [ 3.0, 2.0, 1.0 ], { 'dtype': 123 } ); // $ExpectError + compile( [ 3.0, 2.0, 1.0 ], { 'dtype': ( x: number ): number => x } ); // $ExpectError } // The compiler throws an error if the function is provided an insufficient number of arguments... diff --git a/base/tools/evalpoly-compile/examples/index.js b/base/tools/evalpoly-compile/examples/index.js index 8e2960eba..74c2c078c 100644 --- a/base/tools/evalpoly-compile/examples/index.js +++ b/base/tools/evalpoly-compile/examples/index.js @@ -19,9 +19,7 @@ 'use strict'; var resolve = require( 'path' ).resolve; -var randu = require( '@stdlib/random/base/randu' ); -var round = require( './../../../../base/special/round' ); -var Float64Array = require( '@stdlib/array/float64' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var tryRequire = require( '@stdlib/utils/try-require' ); var compile = tryRequire( resolve( __dirname, '..', 'lib' ) ); @@ -32,23 +30,10 @@ if ( compile instanceof Error ) { } function main() { - var coef; - var sign; - var str; - var i; - - // Create an array of random coefficients... - coef = new Float64Array( 10 ); - for ( i = 0; i < coef.length; i++ ) { - if ( randu() < 0.5 ) { - sign = -1.0; - } else { - sign = 1.0; - } - coef[ i ] = sign * round( randu()*100.0 ); - } + // Create an array of random coefficients: + var coef = discreteUniform( 10, -100, 100 ); // Compile a module for evaluating a polynomial: - str = compile( coef ); + var str = compile( coef ); console.log( str ); } diff --git a/base/tools/evalpoly-compile/lib/main.js b/base/tools/evalpoly-compile/lib/main.js index e610cd9b5..5e4fe4829 100644 --- a/base/tools/evalpoly-compile/lib/main.js +++ b/base/tools/evalpoly-compile/lib/main.js @@ -24,6 +24,7 @@ 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 // @@ -35,9 +36,15 @@ 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) @@ -47,25 +54,39 @@ var MAX_CHARS = 68; // max-len (80) - chars already in line ('tab': 4, 'return ' * 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 */ -function compile( c ) { +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(); @@ -88,17 +109,34 @@ function compile( c ) { str += ',\n'; } } - return replace( LOOP_TEMPLATE, '{{coefficients}}', str ); + 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... - horner = c[ 0 ].toString(); + if ( opts.dtype === 'float32' ) { + horner = 'float64ToFloat32('; + } else { + horner = ''; + } + horner += c[ 0 ].toString(); if ( isInteger( c[ 0 ] ) ) { horner += '.0'; } for ( i = 1; i < n; i++ ) { - horner += ' + (x * '; - if ( i < m ) { - horner += '('; + 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 ] ) ) { @@ -109,11 +147,19 @@ function compile( c ) { 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'; } - str = replace( EVALPOLY_TEMPLATE, '{{coefficient}}', str ); + 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' : '' ); } diff --git a/base/tools/evalpoly-compile/lib/templates/evalpoly.float32.js.txt b/base/tools/evalpoly-compile/lib/templates/evalpoly.float32.js.txt new file mode 100644 index 000000000..c3964fd59 --- /dev/null +++ b/base/tools/evalpoly-compile/lib/templates/evalpoly.float32.js.txt @@ -0,0 +1,33 @@ +'use strict'; + +// MODULES // + +var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); + + +// MAIN // + +/** +* Evaluates a polynomial. +* +* ## Notes +* +* - The implementation uses [Horner's rule][horners-method] for efficient computation. +* +* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method +* +* @private +* @param {number} x - value at which to evaluate the polynomial +* @returns {number} evaluated polynomial +*/ +function evalpoly( x ) { + if ( x === 0.0 ) { + return {{coefficient}}; + } + return {{horner}};{{eslint}} +} + + +// EXPORTS // + +module.exports = evalpoly; diff --git a/base/tools/evalpoly-compile/lib/templates/loop.float32.js.txt b/base/tools/evalpoly-compile/lib/templates/loop.float32.js.txt new file mode 100644 index 000000000..848985d05 --- /dev/null +++ b/base/tools/evalpoly-compile/lib/templates/loop.float32.js.txt @@ -0,0 +1,51 @@ +'use strict'; + +// MODULES // + +var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); + + +// VARIABLES // + +var C = [ +{{coefficients}} +]; +var END = C.length - 1; + + +// MAIN // + +/** +* Evaluates a polynomial. +* +* ## Notes +* +* - The implementation uses [Horner's rule][horners-method] for efficient computation. +* +* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method +* +* @private +* @param {number} x - value at which to evaluate the polynomial +* @returns {number} evaluated polynomial +*/ +function evalpoly( x ) { + var p; + var i; + + if ( x === 0.0 ) { + return C[ 0 ]; + } + i = END; + p = float64ToFloat32( float64ToFloat32( C[ i ] * x ) + C[ i-1 ] ); + i -= 2; + while ( i >= 0 ) { + p = float64ToFloat32( float64ToFloat32( p * x ) + C[ i ] ); + i -= 1; + } + return p; +} + + +// EXPORTS // + +module.exports = evalpoly; diff --git a/base/tools/evalpoly-compile/test/fixtures/evalpoly1.float32.js.txt b/base/tools/evalpoly-compile/test/fixtures/evalpoly1.float32.js.txt new file mode 100644 index 000000000..09a91437d --- /dev/null +++ b/base/tools/evalpoly-compile/test/fixtures/evalpoly1.float32.js.txt @@ -0,0 +1,33 @@ +'use strict'; + +// MODULES // + +var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); + + +// MAIN // + +/** +* Evaluates a polynomial. +* +* ## Notes +* +* - The implementation uses [Horner's rule][horners-method] for efficient computation. +* +* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method +* +* @private +* @param {number} x - value at which to evaluate the polynomial +* @returns {number} evaluated polynomial +*/ +function evalpoly( x ) { + if ( x === 0.0 ) { + return 1.0; + } + return float64ToFloat32(1.0 + float64ToFloat32(x * float64ToFloat32(2.5 + float64ToFloat32(x * float64ToFloat32(3.140000104904175 + float64ToFloat32(x * -1.0)))))); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = evalpoly; diff --git a/base/tools/evalpoly-compile/test/fixtures/evalpoly2.float32.js.txt b/base/tools/evalpoly-compile/test/fixtures/evalpoly2.float32.js.txt new file mode 100644 index 000000000..1e6b3d228 --- /dev/null +++ b/base/tools/evalpoly-compile/test/fixtures/evalpoly2.float32.js.txt @@ -0,0 +1,33 @@ +'use strict'; + +// MODULES // + +var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); + + +// MAIN // + +/** +* Evaluates a polynomial. +* +* ## Notes +* +* - The implementation uses [Horner's rule][horners-method] for efficient computation. +* +* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method +* +* @private +* @param {number} x - value at which to evaluate the polynomial +* @returns {number} evaluated polynomial +*/ +function evalpoly( x ) { + if ( x === 0.0 ) { + return -3.140000104904175; + } + return float64ToFloat32(-3.140000104904175 + float64ToFloat32(x * 0.0)); +} + + +// EXPORTS // + +module.exports = evalpoly; diff --git a/base/tools/evalpoly-compile/test/fixtures/evalpoly3.float32.js.txt b/base/tools/evalpoly-compile/test/fixtures/evalpoly3.float32.js.txt new file mode 100644 index 000000000..55b118490 --- /dev/null +++ b/base/tools/evalpoly-compile/test/fixtures/evalpoly3.float32.js.txt @@ -0,0 +1,33 @@ +'use strict'; + +// MODULES // + +var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); + + +// MAIN // + +/** +* Evaluates a polynomial. +* +* ## Notes +* +* - The implementation uses [Horner's rule][horners-method] for efficient computation. +* +* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method +* +* @private +* @param {number} x - value at which to evaluate the polynomial +* @returns {number} evaluated polynomial +*/ +function evalpoly( x ) { + if ( x === 0.0 ) { + return 1.0; + } + return float64ToFloat32(1.0 + float64ToFloat32(x * float64ToFloat32(2.5 + float64ToFloat32(x * float64ToFloat32(3.140000104904175 + float64ToFloat32(x * float64ToFloat32(-1.0 + float64ToFloat32(x * float64ToFloat32(5.0 + float64ToFloat32(x * float64ToFloat32(2.0 + float64ToFloat32(x * float64ToFloat32(3.5 + float64ToFloat32(x * float64ToFloat32(8.0 + float64ToFloat32(x * float64ToFloat32(4.199999809265137 + float64ToFloat32(x * 1.0)))))))))))))))))); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = evalpoly; diff --git a/base/tools/evalpoly-compile/test/fixtures/loop1.float32.js.txt b/base/tools/evalpoly-compile/test/fixtures/loop1.float32.js.txt new file mode 100644 index 000000000..aca23c389 --- /dev/null +++ b/base/tools/evalpoly-compile/test/fixtures/loop1.float32.js.txt @@ -0,0 +1,551 @@ +'use strict'; + +// MODULES // + +var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); + + +// VARIABLES // + +var C = [ + 0.0, + 1.0, + 2.0, + 3.0, + 4.0, + 5.0, + 6.0, + 7.0, + 8.0, + 9.0, + 10.0, + 11.0, + 12.0, + 13.0, + 14.0, + 15.0, + 16.0, + 17.0, + 18.0, + 19.0, + 20.0, + 21.0, + 22.0, + 23.0, + 24.0, + 25.0, + 26.0, + 27.0, + 28.0, + 29.0, + 30.0, + 31.0, + 32.0, + 33.0, + 34.0, + 35.0, + 36.0, + 37.0, + 38.0, + 39.0, + 40.0, + 41.0, + 42.0, + 43.0, + 44.0, + 45.0, + 46.0, + 47.0, + 48.0, + 49.0, + 50.0, + 51.0, + 52.0, + 53.0, + 54.0, + 55.0, + 56.0, + 57.0, + 58.0, + 59.0, + 60.0, + 61.0, + 62.0, + 63.0, + 64.0, + 65.0, + 66.0, + 67.0, + 68.0, + 69.0, + 70.0, + 71.0, + 72.0, + 73.0, + 74.0, + 75.0, + 76.0, + 77.0, + 78.0, + 79.0, + 80.0, + 81.0, + 82.0, + 83.0, + 84.0, + 85.0, + 86.0, + 87.0, + 88.0, + 89.0, + 90.0, + 91.0, + 92.0, + 93.0, + 94.0, + 95.0, + 96.0, + 97.0, + 98.0, + 99.0, + 100.0, + 101.0, + 102.0, + 103.0, + 104.0, + 105.0, + 106.0, + 107.0, + 108.0, + 109.0, + 110.0, + 111.0, + 112.0, + 113.0, + 114.0, + 115.0, + 116.0, + 117.0, + 118.0, + 119.0, + 120.0, + 121.0, + 122.0, + 123.0, + 124.0, + 125.0, + 126.0, + 127.0, + 128.0, + 129.0, + 130.0, + 131.0, + 132.0, + 133.0, + 134.0, + 135.0, + 136.0, + 137.0, + 138.0, + 139.0, + 140.0, + 141.0, + 142.0, + 143.0, + 144.0, + 145.0, + 146.0, + 147.0, + 148.0, + 149.0, + 150.0, + 151.0, + 152.0, + 153.0, + 154.0, + 155.0, + 156.0, + 157.0, + 158.0, + 159.0, + 160.0, + 161.0, + 162.0, + 163.0, + 164.0, + 165.0, + 166.0, + 167.0, + 168.0, + 169.0, + 170.0, + 171.0, + 172.0, + 173.0, + 174.0, + 175.0, + 176.0, + 177.0, + 178.0, + 179.0, + 180.0, + 181.0, + 182.0, + 183.0, + 184.0, + 185.0, + 186.0, + 187.0, + 188.0, + 189.0, + 190.0, + 191.0, + 192.0, + 193.0, + 194.0, + 195.0, + 196.0, + 197.0, + 198.0, + 199.0, + 200.0, + 201.0, + 202.0, + 203.0, + 204.0, + 205.0, + 206.0, + 207.0, + 208.0, + 209.0, + 210.0, + 211.0, + 212.0, + 213.0, + 214.0, + 215.0, + 216.0, + 217.0, + 218.0, + 219.0, + 220.0, + 221.0, + 222.0, + 223.0, + 224.0, + 225.0, + 226.0, + 227.0, + 228.0, + 229.0, + 230.0, + 231.0, + 232.0, + 233.0, + 234.0, + 235.0, + 236.0, + 237.0, + 238.0, + 239.0, + 240.0, + 241.0, + 242.0, + 243.0, + 244.0, + 245.0, + 246.0, + 247.0, + 248.0, + 249.0, + 250.0, + 251.0, + 252.0, + 253.0, + 254.0, + 255.0, + 256.0, + 257.0, + 258.0, + 259.0, + 260.0, + 261.0, + 262.0, + 263.0, + 264.0, + 265.0, + 266.0, + 267.0, + 268.0, + 269.0, + 270.0, + 271.0, + 272.0, + 273.0, + 274.0, + 275.0, + 276.0, + 277.0, + 278.0, + 279.0, + 280.0, + 281.0, + 282.0, + 283.0, + 284.0, + 285.0, + 286.0, + 287.0, + 288.0, + 289.0, + 290.0, + 291.0, + 292.0, + 293.0, + 294.0, + 295.0, + 296.0, + 297.0, + 298.0, + 299.0, + 300.0, + 301.0, + 302.0, + 303.0, + 304.0, + 305.0, + 306.0, + 307.0, + 308.0, + 309.0, + 310.0, + 311.0, + 312.0, + 313.0, + 314.0, + 315.0, + 316.0, + 317.0, + 318.0, + 319.0, + 320.0, + 321.0, + 322.0, + 323.0, + 324.0, + 325.0, + 326.0, + 327.0, + 328.0, + 329.0, + 330.0, + 331.0, + 332.0, + 333.0, + 334.0, + 335.0, + 336.0, + 337.0, + 338.0, + 339.0, + 340.0, + 341.0, + 342.0, + 343.0, + 344.0, + 345.0, + 346.0, + 347.0, + 348.0, + 349.0, + 350.0, + 351.0, + 352.0, + 353.0, + 354.0, + 355.0, + 356.0, + 357.0, + 358.0, + 359.0, + 360.0, + 361.0, + 362.0, + 363.0, + 364.0, + 365.0, + 366.0, + 367.0, + 368.0, + 369.0, + 370.0, + 371.0, + 372.0, + 373.0, + 374.0, + 375.0, + 376.0, + 377.0, + 378.0, + 379.0, + 380.0, + 381.0, + 382.0, + 383.0, + 384.0, + 385.0, + 386.0, + 387.0, + 388.0, + 389.0, + 390.0, + 391.0, + 392.0, + 393.0, + 394.0, + 395.0, + 396.0, + 397.0, + 398.0, + 399.0, + 400.0, + 401.0, + 402.0, + 403.0, + 404.0, + 405.0, + 406.0, + 407.0, + 408.0, + 409.0, + 410.0, + 411.0, + 412.0, + 413.0, + 414.0, + 415.0, + 416.0, + 417.0, + 418.0, + 419.0, + 420.0, + 421.0, + 422.0, + 423.0, + 424.0, + 425.0, + 426.0, + 427.0, + 428.0, + 429.0, + 430.0, + 431.0, + 432.0, + 433.0, + 434.0, + 435.0, + 436.0, + 437.0, + 438.0, + 439.0, + 440.0, + 441.0, + 442.0, + 443.0, + 444.0, + 445.0, + 446.0, + 447.0, + 448.0, + 449.0, + 450.0, + 451.0, + 452.0, + 453.0, + 454.0, + 455.0, + 456.0, + 457.0, + 458.0, + 459.0, + 460.0, + 461.0, + 462.0, + 463.0, + 464.0, + 465.0, + 466.0, + 467.0, + 468.0, + 469.0, + 470.0, + 471.0, + 472.0, + 473.0, + 474.0, + 475.0, + 476.0, + 477.0, + 478.0, + 479.0, + 480.0, + 481.0, + 482.0, + 483.0, + 484.0, + 485.0, + 486.0, + 487.0, + 488.0, + 489.0, + 490.0, + 491.0, + 492.0, + 493.0, + 494.0, + 495.0, + 496.0, + 497.0, + 498.0, + 499.0, + 500.0 +]; +var END = C.length - 1; + + +// MAIN // + +/** +* Evaluates a polynomial. +* +* ## Notes +* +* - The implementation uses [Horner's rule][horners-method] for efficient computation. +* +* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method +* +* @private +* @param {number} x - value at which to evaluate the polynomial +* @returns {number} evaluated polynomial +*/ +function evalpoly( x ) { + var p; + var i; + + if ( x === 0.0 ) { + return C[ 0 ]; + } + i = END; + p = float64ToFloat32( float64ToFloat32( C[ i ] * x ) + C[ i-1 ] ); + i -= 2; + while ( i >= 0 ) { + p = float64ToFloat32( float64ToFloat32( p * x ) + C[ i ] ); + i -= 1; + } + return p; +} + + +// EXPORTS // + +module.exports = evalpoly; diff --git a/base/tools/evalpoly-compile/test/fixtures/loop1.js.txt b/base/tools/evalpoly-compile/test/fixtures/loop1.js.txt index 716b46141..c3d4535ca 100644 --- a/base/tools/evalpoly-compile/test/fixtures/loop1.js.txt +++ b/base/tools/evalpoly-compile/test/fixtures/loop1.js.txt @@ -519,7 +519,6 @@ var END = C.length - 1; * * [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method * -* * @private * @param {number} x - value at which to evaluate the polynomial * @returns {number} evaluated polynomial diff --git a/base/tools/evalpoly-compile/test/fixtures/loop2.float32.js.txt b/base/tools/evalpoly-compile/test/fixtures/loop2.float32.js.txt new file mode 100644 index 000000000..e4d0f5875 --- /dev/null +++ b/base/tools/evalpoly-compile/test/fixtures/loop2.float32.js.txt @@ -0,0 +1,551 @@ +'use strict'; + +// MODULES // + +var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); + + +// VARIABLES // + +var C = [ + 0.5, + 1.5, + 2.5, + 3.5, + 4.5, + 5.5, + 6.5, + 7.5, + 8.5, + 9.5, + 10.5, + 11.5, + 12.5, + 13.5, + 14.5, + 15.5, + 16.5, + 17.5, + 18.5, + 19.5, + 20.5, + 21.5, + 22.5, + 23.5, + 24.5, + 25.5, + 26.5, + 27.5, + 28.5, + 29.5, + 30.5, + 31.5, + 32.5, + 33.5, + 34.5, + 35.5, + 36.5, + 37.5, + 38.5, + 39.5, + 40.5, + 41.5, + 42.5, + 43.5, + 44.5, + 45.5, + 46.5, + 47.5, + 48.5, + 49.5, + 50.5, + 51.5, + 52.5, + 53.5, + 54.5, + 55.5, + 56.5, + 57.5, + 58.5, + 59.5, + 60.5, + 61.5, + 62.5, + 63.5, + 64.5, + 65.5, + 66.5, + 67.5, + 68.5, + 69.5, + 70.5, + 71.5, + 72.5, + 73.5, + 74.5, + 75.5, + 76.5, + 77.5, + 78.5, + 79.5, + 80.5, + 81.5, + 82.5, + 83.5, + 84.5, + 85.5, + 86.5, + 87.5, + 88.5, + 89.5, + 90.5, + 91.5, + 92.5, + 93.5, + 94.5, + 95.5, + 96.5, + 97.5, + 98.5, + 99.5, + 100.5, + 101.5, + 102.5, + 103.5, + 104.5, + 105.5, + 106.5, + 107.5, + 108.5, + 109.5, + 110.5, + 111.5, + 112.5, + 113.5, + 114.5, + 115.5, + 116.5, + 117.5, + 118.5, + 119.5, + 120.5, + 121.5, + 122.5, + 123.5, + 124.5, + 125.5, + 126.5, + 127.5, + 128.5, + 129.5, + 130.5, + 131.5, + 132.5, + 133.5, + 134.5, + 135.5, + 136.5, + 137.5, + 138.5, + 139.5, + 140.5, + 141.5, + 142.5, + 143.5, + 144.5, + 145.5, + 146.5, + 147.5, + 148.5, + 149.5, + 150.5, + 151.5, + 152.5, + 153.5, + 154.5, + 155.5, + 156.5, + 157.5, + 158.5, + 159.5, + 160.5, + 161.5, + 162.5, + 163.5, + 164.5, + 165.5, + 166.5, + 167.5, + 168.5, + 169.5, + 170.5, + 171.5, + 172.5, + 173.5, + 174.5, + 175.5, + 176.5, + 177.5, + 178.5, + 179.5, + 180.5, + 181.5, + 182.5, + 183.5, + 184.5, + 185.5, + 186.5, + 187.5, + 188.5, + 189.5, + 190.5, + 191.5, + 192.5, + 193.5, + 194.5, + 195.5, + 196.5, + 197.5, + 198.5, + 199.5, + 200.5, + 201.5, + 202.5, + 203.5, + 204.5, + 205.5, + 206.5, + 207.5, + 208.5, + 209.5, + 210.5, + 211.5, + 212.5, + 213.5, + 214.5, + 215.5, + 216.5, + 217.5, + 218.5, + 219.5, + 220.5, + 221.5, + 222.5, + 223.5, + 224.5, + 225.5, + 226.5, + 227.5, + 228.5, + 229.5, + 230.5, + 231.5, + 232.5, + 233.5, + 234.5, + 235.5, + 236.5, + 237.5, + 238.5, + 239.5, + 240.5, + 241.5, + 242.5, + 243.5, + 244.5, + 245.5, + 246.5, + 247.5, + 248.5, + 249.5, + 250.5, + 251.5, + 252.5, + 253.5, + 254.5, + 255.5, + 256.5, + 257.5, + 258.5, + 259.5, + 260.5, + 261.5, + 262.5, + 263.5, + 264.5, + 265.5, + 266.5, + 267.5, + 268.5, + 269.5, + 270.5, + 271.5, + 272.5, + 273.5, + 274.5, + 275.5, + 276.5, + 277.5, + 278.5, + 279.5, + 280.5, + 281.5, + 282.5, + 283.5, + 284.5, + 285.5, + 286.5, + 287.5, + 288.5, + 289.5, + 290.5, + 291.5, + 292.5, + 293.5, + 294.5, + 295.5, + 296.5, + 297.5, + 298.5, + 299.5, + 300.5, + 301.5, + 302.5, + 303.5, + 304.5, + 305.5, + 306.5, + 307.5, + 308.5, + 309.5, + 310.5, + 311.5, + 312.5, + 313.5, + 314.5, + 315.5, + 316.5, + 317.5, + 318.5, + 319.5, + 320.5, + 321.5, + 322.5, + 323.5, + 324.5, + 325.5, + 326.5, + 327.5, + 328.5, + 329.5, + 330.5, + 331.5, + 332.5, + 333.5, + 334.5, + 335.5, + 336.5, + 337.5, + 338.5, + 339.5, + 340.5, + 341.5, + 342.5, + 343.5, + 344.5, + 345.5, + 346.5, + 347.5, + 348.5, + 349.5, + 350.5, + 351.5, + 352.5, + 353.5, + 354.5, + 355.5, + 356.5, + 357.5, + 358.5, + 359.5, + 360.5, + 361.5, + 362.5, + 363.5, + 364.5, + 365.5, + 366.5, + 367.5, + 368.5, + 369.5, + 370.5, + 371.5, + 372.5, + 373.5, + 374.5, + 375.5, + 376.5, + 377.5, + 378.5, + 379.5, + 380.5, + 381.5, + 382.5, + 383.5, + 384.5, + 385.5, + 386.5, + 387.5, + 388.5, + 389.5, + 390.5, + 391.5, + 392.5, + 393.5, + 394.5, + 395.5, + 396.5, + 397.5, + 398.5, + 399.5, + 400.5, + 401.5, + 402.5, + 403.5, + 404.5, + 405.5, + 406.5, + 407.5, + 408.5, + 409.5, + 410.5, + 411.5, + 412.5, + 413.5, + 414.5, + 415.5, + 416.5, + 417.5, + 418.5, + 419.5, + 420.5, + 421.5, + 422.5, + 423.5, + 424.5, + 425.5, + 426.5, + 427.5, + 428.5, + 429.5, + 430.5, + 431.5, + 432.5, + 433.5, + 434.5, + 435.5, + 436.5, + 437.5, + 438.5, + 439.5, + 440.5, + 441.5, + 442.5, + 443.5, + 444.5, + 445.5, + 446.5, + 447.5, + 448.5, + 449.5, + 450.5, + 451.5, + 452.5, + 453.5, + 454.5, + 455.5, + 456.5, + 457.5, + 458.5, + 459.5, + 460.5, + 461.5, + 462.5, + 463.5, + 464.5, + 465.5, + 466.5, + 467.5, + 468.5, + 469.5, + 470.5, + 471.5, + 472.5, + 473.5, + 474.5, + 475.5, + 476.5, + 477.5, + 478.5, + 479.5, + 480.5, + 481.5, + 482.5, + 483.5, + 484.5, + 485.5, + 486.5, + 487.5, + 488.5, + 489.5, + 490.5, + 491.5, + 492.5, + 493.5, + 494.5, + 495.5, + 496.5, + 497.5, + 498.5, + 499.5, + 500.5 +]; +var END = C.length - 1; + + +// MAIN // + +/** +* Evaluates a polynomial. +* +* ## Notes +* +* - The implementation uses [Horner's rule][horners-method] for efficient computation. +* +* [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method +* +* @private +* @param {number} x - value at which to evaluate the polynomial +* @returns {number} evaluated polynomial +*/ +function evalpoly( x ) { + var p; + var i; + + if ( x === 0.0 ) { + return C[ 0 ]; + } + i = END; + p = float64ToFloat32( float64ToFloat32( C[ i ] * x ) + C[ i-1 ] ); + i -= 2; + while ( i >= 0 ) { + p = float64ToFloat32( float64ToFloat32( p * x ) + C[ i ] ); + i -= 1; + } + return p; +} + + +// EXPORTS // + +module.exports = evalpoly; diff --git a/base/tools/evalpoly-compile/test/fixtures/loop2.js.txt b/base/tools/evalpoly-compile/test/fixtures/loop2.js.txt index 353ce527d..6b80d33bb 100644 --- a/base/tools/evalpoly-compile/test/fixtures/loop2.js.txt +++ b/base/tools/evalpoly-compile/test/fixtures/loop2.js.txt @@ -519,7 +519,6 @@ var END = C.length - 1; * * [horners-method]: https://en.wikipedia.org/wiki/Horner%27s_method * -* * @private * @param {number} x - value at which to evaluate the polynomial * @returns {number} evaluated polynomial diff --git a/base/tools/evalpoly-compile/test/fixtures/single_coefficient.float32.js.txt b/base/tools/evalpoly-compile/test/fixtures/single_coefficient.float32.js.txt new file mode 100644 index 000000000..bc649465a --- /dev/null +++ b/base/tools/evalpoly-compile/test/fixtures/single_coefficient.float32.js.txt @@ -0,0 +1,19 @@ +'use strict'; + +// MAIN // + +/** +* Evaluates a polynomial. +* +* @private +* @param {number} x - value at which to evaluate the polynomial +* @returns {number} evaluated polynomial +*/ +function evalpoly() { + return 3.140000104904175; +} + + +// EXPORTS // + +module.exports = evalpoly; diff --git a/base/tools/evalpoly-compile/test/test.js b/base/tools/evalpoly-compile/test/test.js index 4b38d50d6..8077c407b 100644 --- a/base/tools/evalpoly-compile/test/test.js +++ b/base/tools/evalpoly-compile/test/test.js @@ -26,6 +26,7 @@ var tape = require( 'tape' ); var IS_BROWSER = require( '@stdlib/assert/is-browser' ); var readFile = require( '@stdlib/fs/read-file' ).sync; var Float64Array = require( '@stdlib/array/float64' ); +var Float32Array = require( '@stdlib/array/float32' ); var tryRequire = require( '@stdlib/utils/try-require' ); @@ -63,6 +64,52 @@ tape( 'the function returns a string', opts, function test( t ) { t.end(); }); +tape( 'the function returns a string (dtype=float64)', opts, function test( t ) { + var options; + var str; + + options = { + 'dtype': 'float64' + }; + + str = compile( [], options ); + t.equal( typeof str, 'string', 'returns a string' ); + + str = compile( [ 1.0 ], options ); + t.equal( typeof str, 'string', 'returns a string' ); + + str = compile( [ 1.0, 2.0, 3.0 ], options ); + t.equal( typeof str, 'string', 'returns a string' ); + + str = compile( new Float64Array( 501 ) ); + t.equal( typeof str, 'string', 'returns a string' ); + + t.end(); +}); + +tape( 'the function returns a string (dtype=float32)', opts, function test( t ) { + var options; + var str; + + options = { + 'dtype': 'float32' + }; + + str = compile( [], options ); + t.equal( typeof str, 'string', 'returns a string' ); + + str = compile( [ 1.0 ], options ); + t.equal( typeof str, 'string', 'returns a string' ); + + str = compile( [ 1.0, 2.0, 3.0 ], options ); + t.equal( typeof str, 'string', 'returns a string' ); + + str = compile( new Float64Array( 501 ) ); + t.equal( typeof str, 'string', 'returns a string' ); + + t.end(); +}); + tape( 'if provided an empty coefficient array, the function returns a module string containing an exported function which always returns `0`', opts, function test( t ) { var expected; var actual; @@ -81,6 +128,46 @@ tape( 'if provided an empty coefficient array, the function returns a module str t.end(); }); +tape( 'if provided an empty coefficient array, the function returns a module string containing an exported function which always returns `0` (dtype=float64)', opts, function test( t ) { + var expected; + var actual; + var fpath; + var fopts; + + fpath = join( __dirname, 'fixtures', 'empty.js.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); + + actual = compile( [], { + 'dtype': 'float64' + }); + t.equal( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an empty coefficient array, the function returns a module string containing an exported function which always returns `0` (dtype=float32)', opts, function test( t ) { + var expected; + var actual; + var fpath; + var fopts; + + fpath = join( __dirname, 'fixtures', 'empty.js.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); + + actual = compile( [], { + 'dtype': 'float32' + }); + t.equal( actual, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided only 1 coefficient, the function returns a module string containing an exported function which always returns that coefficient', opts, function test( t ) { var expected; var actual; @@ -99,6 +186,46 @@ tape( 'if provided only 1 coefficient, the function returns a module string cont t.end(); }); +tape( 'if provided only 1 coefficient, the function returns a module string containing an exported function which always returns that coefficient (dtype=float64)', opts, function test( t ) { + var expected; + var actual; + var fpath; + var fopts; + + fpath = join( __dirname, 'fixtures', 'single_coefficient.js.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); + + actual = compile( [ 3.14 ], { + 'dtype': 'float64' + }); + t.equal( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided only 1 coefficient, the function returns a module string containing an exported function which always returns that coefficient (dtype=float32)', opts, function test( t ) { + var expected; + var actual; + var fpath; + var fopts; + + fpath = join( __dirname, 'fixtures', 'single_coefficient.float32.js.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); + + actual = compile( new Float32Array( [ 3.14 ] ), { + 'dtype': 'float32' + }); + t.equal( actual, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'if provided only 1 coefficient, the function returns a module string containing an exported function which always returns that coefficient (integer value)', opts, function test( t ) { var expected; var actual; @@ -117,6 +244,46 @@ tape( 'if provided only 1 coefficient, the function returns a module string cont t.end(); }); +tape( 'if provided only 1 coefficient, the function returns a module string containing an exported function which always returns that coefficient (integer value; dtype=float64)', opts, function test( t ) { + var expected; + var actual; + var fpath; + var fopts; + + fpath = join( __dirname, 'fixtures', 'single_coefficient_integer.js.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); + + actual = compile( [ -3.0 ], { + 'dtype': 'float64' + }); + t.equal( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided only 1 coefficient, the function returns a module string containing an exported function which always returns that coefficient (integer value; dtype=float32)', opts, function test( t ) { + var expected; + var actual; + var fpath; + var fopts; + + fpath = join( __dirname, 'fixtures', 'single_coefficient_integer.js.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); + + actual = compile( new Float32Array( [ -3.0 ] ), { + 'dtype': 'float32' + }); + t.equal( actual, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function returns a module string containing an exported function which evaluates a polynomial', opts, function test( t ) { var expected; var actual; @@ -135,6 +302,46 @@ tape( 'the function returns a module string containing an exported function whic t.end(); }); +tape( 'the function returns a module string containing an exported function which evaluates a polynomial (dtype=float64)', opts, function test( t ) { + var expected; + var actual; + var fpath; + var fopts; + + fpath = join( __dirname, 'fixtures', 'evalpoly1.js.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); + + actual = compile( [ 1.0, 2.5, 3.14, -1.0 ], { + 'dtype': 'float64' + }); + t.equal( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a module string containing an exported function which evaluates a polynomial (dtype=float32)', opts, function test( t ) { + var expected; + var actual; + var fpath; + var fopts; + + fpath = join( __dirname, 'fixtures', 'evalpoly1.float32.js.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); + + actual = compile( new Float32Array( [ 1.0, 2.5, 3.14, -1.0 ] ), { + 'dtype': 'float32' + }); + t.equal( actual, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function returns a module string containing an exported function which evaluates a polynomial', opts, function test( t ) { var expected; var actual; @@ -153,6 +360,46 @@ tape( 'the function returns a module string containing an exported function whic t.end(); }); +tape( 'the function returns a module string containing an exported function which evaluates a polynomial (dtype=float64)', opts, function test( t ) { + var expected; + var actual; + var fpath; + var fopts; + + fpath = join( __dirname, 'fixtures', 'evalpoly2.js.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); + + actual = compile( [ -3.14, 0.0 ], { + 'dtype': 'float64' + }); + t.equal( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a module string containing an exported function which evaluates a polynomial (dtype=float32)', opts, function test( t ) { + var expected; + var actual; + var fpath; + var fopts; + + fpath = join( __dirname, 'fixtures', 'evalpoly2.float32.js.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); + + actual = compile( new Float32Array( [ -3.14, 0.0 ] ), { + 'dtype': 'float32' + }); + t.equal( actual, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function returns a module string containing an exported function which evaluates a polynomial', opts, function test( t ) { var expected; var actual; @@ -171,6 +418,46 @@ tape( 'the function returns a module string containing an exported function whic t.end(); }); +tape( 'the function returns a module string containing an exported function which evaluates a polynomial (dtype=float64)', opts, function test( t ) { + var expected; + var actual; + var fpath; + var fopts; + + fpath = join( __dirname, 'fixtures', 'evalpoly3.js.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); + + actual = compile( [ 1.0, 2.5, 3.14, -1.0, 5.0, 2.0, 3.5, 8.0, 4.2, 1.0 ], { + 'dtype': 'float64' + }); + t.equal( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a module string containing an exported function which evaluates a polynomial (dtype=float32)', opts, function test( t ) { + var expected; + var actual; + var fpath; + var fopts; + + fpath = join( __dirname, 'fixtures', 'evalpoly3.float32.js.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); + + actual = compile( new Float32Array( [ 1.0, 2.5, 3.14, -1.0, 5.0, 2.0, 3.5, 8.0, 4.2, 1.0 ] ), { // eslint-disable-line max-len + 'dtype': 'float32' + }); + t.equal( actual, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function returns a module string containing an exported function which evaluates a polynomial (large number of coefficients; integers)', opts, function test( t ) { var expected; var actual; @@ -185,9 +472,9 @@ tape( 'the function returns a module string containing an exported function whic }; expected = readFile( fpath, fopts ); - c = new Array( 501 ); - for ( i = 0; i < c.length; i++ ) { - c[ i ] = i; + c = []; + for ( i = 0; i < 501; i++ ) { + c.push( i ); } actual = compile( c ); t.equal( actual, expected, 'returns expected value' ); @@ -195,6 +482,58 @@ tape( 'the function returns a module string containing an exported function whic t.end(); }); +tape( 'the function returns a module string containing an exported function which evaluates a polynomial (large number of coefficients; integers; dtype=float64)', opts, function test( t ) { + var expected; + var actual; + var fpath; + var fopts; + var c; + var i; + + fpath = join( __dirname, 'fixtures', 'loop1.js.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); + + c = []; + for ( i = 0; i < 501; i++ ) { + c.push( i ); + } + actual = compile( c, { + 'dtype': 'float64' + }); + t.equal( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a module string containing an exported function which evaluates a polynomial (large number of coefficients; integers; dtype=float32)', opts, function test( t ) { + var expected; + var actual; + var fpath; + var fopts; + var c; + var i; + + fpath = join( __dirname, 'fixtures', 'loop1.float32.js.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); + + c = []; + for ( i = 0; i < 501; i++ ) { + c.push( i ); + } + actual = compile( new Float32Array( c ), { + 'dtype': 'float32' + }); + t.equal( actual, expected, 'returns expected value' ); + + t.end(); +}); + tape( 'the function returns a module string containing an exported function which evaluates a polynomial (large number of coefficients; decimals)', opts, function test( t ) { var expected; var actual; @@ -209,12 +548,64 @@ tape( 'the function returns a module string containing an exported function whic }; expected = readFile( fpath, fopts ); - c = new Array( 501 ); - for ( i = 0; i < c.length; i++ ) { - c[ i ] = i + 0.1; + c = []; + for ( i = 0; i < 501; i++ ) { + c.push( i + 0.1 ); } actual = compile( c ); t.equal( actual, expected, 'returns expected value' ); t.end(); }); + +tape( 'the function returns a module string containing an exported function which evaluates a polynomial (large number of coefficients; decimals; dtype=float64)', opts, function test( t ) { + var expected; + var actual; + var fpath; + var fopts; + var c; + var i; + + fpath = join( __dirname, 'fixtures', 'loop2.js.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); + + c = []; + for ( i = 0; i < 501; i++ ) { + c.push( i + 0.1 ); + } + actual = compile( c, { + 'dtype': 'float64' + }); + t.equal( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a module string containing an exported function which evaluates a polynomial (large number of coefficients; decimals; dtype=float32)', opts, function test( t ) { + var expected; + var actual; + var fpath; + var fopts; + var c; + var i; + + fpath = join( __dirname, 'fixtures', 'loop2.float32.js.txt' ); + fopts = { + 'encoding': 'utf8' + }; + expected = readFile( fpath, fopts ); + + c = []; + for ( i = 0; i < 501; i++ ) { + c.push( i + 0.5 ); + } + actual = compile( new Float32Array( c ), { + 'dtype': 'float32' + }); + t.equal( actual, expected, 'returns expected value' ); + + t.end(); +});