Skip to content

Commit ca1d577

Browse files
committed
Add method to compile an evalpoly module
1 parent 4f64815 commit ca1d577

File tree

10 files changed

+1395
-3
lines changed

10 files changed

+1395
-3
lines changed

lib/node_modules/@stdlib/math/base/tools/evalpoly/lib/compile.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,28 @@ function compile( c ) {
7373
}
7474
// If more than one coefficient, apply Horner's method...
7575
horner = c[ 0 ].toString();
76+
if ( isInteger( c[ 0 ] ) ) {
77+
horner += '.0';
78+
}
7679
for ( i = 1; i < n; i++ ) {
77-
horner += '+x*';
80+
horner += ' + (x * ';
7881
if ( i < m ) {
7982
horner += '(';
8083
}
8184
horner += c[ i ].toString();
85+
if ( isInteger( c[ i ] ) ) {
86+
horner += '.0';
87+
}
8288
}
8389
// Close all the parentheses...
84-
for ( i = 0; i < m-1; i++ ) {
90+
for ( i = 0; i < (2*(n-1))-1; i++ ) {
8591
horner += ')';
8692
}
87-
str = replace( EVALPOLY_TEMPLATE, '{{coefficient}}', c[ 0 ].toString() );
93+
str = c[ 0 ].toString();
94+
if ( isInteger( c[ 0 ] ) ) {
95+
str += '.0';
96+
}
97+
str = replace( EVALPOLY_TEMPLATE, '{{coefficient}}', str );
8898
return replace( str, '{{horner}}', horner );
8999
} // end FUNCTION compile()
90100

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
// MAIN //
4+
5+
/**
6+
* Evaluates a polynomial.
7+
*
8+
* @private
9+
* @param {number} x - value at which to evaluate the polynomial
10+
* @returns {number} evaluated polynomial
11+
*/
12+
function evalpoly() {
13+
return 0.0;
14+
} // end FUNCTION evalpoly()
15+
16+
17+
// EXPORTS //
18+
19+
module.exports = evalpoly;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
// MAIN //
4+
5+
/**
6+
* Evaluates a polynomial.
7+
*
8+
* ## Notes
9+
*
10+
* - The implementation uses [Horner's rule]{@link http://en.wikipedia.org/wiki/Horner's_method} for efficient computation.
11+
*
12+
*
13+
* @private
14+
* @param {number} x - value at which to evaluate the polynomial
15+
* @returns {number} evaluated polynomial
16+
*/
17+
function evalpoly( x ) {
18+
if ( x === 0.0 ) {
19+
return 1.0;
20+
}
21+
return 1.0 + (x * (2.5 + (x * (3.14 + (x * -1.0))))); // eslint-disable-line max-len
22+
} // end FUNCTION evalpoly()
23+
24+
25+
// EXPORTS //
26+
27+
module.exports = evalpoly;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
// MAIN //
4+
5+
/**
6+
* Evaluates a polynomial.
7+
*
8+
* ## Notes
9+
*
10+
* - The implementation uses [Horner's rule]{@link http://en.wikipedia.org/wiki/Horner's_method} for efficient computation.
11+
*
12+
*
13+
* @private
14+
* @param {number} x - value at which to evaluate the polynomial
15+
* @returns {number} evaluated polynomial
16+
*/
17+
function evalpoly( x ) {
18+
if ( x === 0.0 ) {
19+
return -3.14;
20+
}
21+
return -3.14 + (x * 0.0); // eslint-disable-line max-len
22+
} // end FUNCTION evalpoly()
23+
24+
25+
// EXPORTS //
26+
27+
module.exports = evalpoly;

0 commit comments

Comments
 (0)