Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Mar 24, 2024
1 parent 9a10e5e commit 96080fb
Show file tree
Hide file tree
Showing 11 changed files with 396 additions and 13 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,6 @@ For more information on the project, filing bug reports and feature requests, an

---

## License

See [LICENSE][stdlib-license].


## Copyright

Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].
Expand Down Expand Up @@ -261,8 +256,6 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].

[stdlib-authors]: https://github.com/stdlib-js/stdlib/graphs/contributors

[stdlib-license]: https://raw.githubusercontent.com/stdlib-js/math-base-tools-evalpoly-compile/main/LICENSE

[@stdlib/math/base/tools/evalpoly]: https://github.com/stdlib-js/math-base-tools-evalpoly

</section>
Expand Down
27 changes: 23 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
"use strict";var L=function(e,l){return function(){return l||e((l={exports:{}}).exports,l),l.exports}};var y=L(function(q,u){
var f=require("path").join,s=require('@stdlib/fs-read-file/dist').sync,T=require('@stdlib/string-replace/dist'),E=require('@stdlib/assert-is-integer/dist').isPrimitive,A=require('@stdlib/array-float32/dist'),v={encoding:"utf8"},p=f(__dirname,"templates"),_=s(f(p,"single_coefficient.js.txt"),v),g=s(f(p,"evalpoly.js.txt"),v),P=s(f(p,"evalpoly.float32.js.txt"),v),x=s(f(p,"empty.js.txt"),v),m=s(f(p,"loop.js.txt"),v),F=s(f(p,"loop.float32.js.txt"),v),O=68;function j(e,l){var i,a,n,r,o,d,t;if(a={dtype:"float64"},arguments.length>1&&(a.dtype=l.dtype||a.dtype),o=e.length,o===0)return x;if(a.dtype==="float32"&&(e=new A(e)),o===1)return r=e[0].toString(),E(e[0])&&(r+=".0"),T(_,"{{coefficient}}",r);if(d=o-1,o>500){for(r="",t=0;t<o;t++)r+=" "+e[t].toString(),E(e[t])&&(r+=".0"),t<d&&(r+=",\n");return a.dtype==="float32"?n=F:n=m,T(n,"{{coefficients}}",r)}for(a.dtype==="float32"?i="float64ToFloat32(":i="",i+=e[0].toString(),E(e[0])&&(i+=".0"),t=1;t<o;t++)a.dtype==="float32"?(i+=" + float64ToFloat32(x * ",t<d&&(i+="float64ToFloat32(")):(i+=" + (x * ",t<d&&(i+="(")),i+=e[t].toString(),E(e[t])&&(i+=".0");for(t=0;t<2*(o-1)-1;t++)i+=")";return a.dtype==="float32"&&(i+=")"),r=e[0].toString(),E(e[0])&&(r+=".0"),a.dtype==="float32"?n=P:n=g,r=T(n,"{{coefficient}}",r),r=T(r,"{{horner}}",i),T(r,"{{eslint}}",i.length>O?" // eslint-disable-line max-len":"")}u.exports=j
});var M=y();module.exports=M;
/** @license Apache-2.0 */
//# sourceMappingURL=index.js.map

'use strict';

/**
* Compile a module for evaluating a polynomial.
*
* @module @stdlib/math-base-tools-evalpoly-compile
*
* @example
* var compile = require( '@stdlib/math-base-tools-evalpoly-compile' );
*
* var str = compile( [3.0,2.0,1.0] ); // 3*10^0 + 2*10^1 + 1*10^2
* // returns <string>
*/

// MODULES //

var main = require( './main.js' );


// EXPORTS //

module.exports = main;
170 changes: 170 additions & 0 deletions dist/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/**
* @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;
19 changes: 19 additions & 0 deletions dist/templates/empty.js.txt
Original file line number Diff line number Diff line change
@@ -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 0.0;
}


// EXPORTS //

module.exports = evalpoly;
33 changes: 33 additions & 0 deletions dist/templates/evalpoly.float32.js.txt
Original file line number Diff line number Diff line change
@@ -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;
28 changes: 28 additions & 0 deletions dist/templates/evalpoly.js.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

// 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;
51 changes: 51 additions & 0 deletions dist/templates/loop.float32.js.txt
Original file line number Diff line number Diff line change
@@ -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;
Loading

0 comments on commit 96080fb

Please sign in to comment.