compute-polynomial
Advanced tools
Comparing version 1.0.0 to 1.1.0
169
lib/index.js
@@ -0,86 +1,109 @@ | ||
'use strict'; | ||
// MODULES // | ||
var isNumber = require( 'validate.io-number-primitive' ), | ||
isArray = require( 'validate.io-array' ), | ||
isNumberArray = require( 'validate.io-number-primitive-array' ), | ||
isObject = require( 'validate.io-object' ), | ||
isBoolean = require( 'validate.io-boolean-primitive' ), | ||
isFunction = require( 'validate.io-function' ); | ||
// POLYVAL // | ||
/** | ||
* FUNCTION: polyval( coef, x ) | ||
* Evaluates a polynomial. | ||
* | ||
* COMPUTE: polynomial | ||
* | ||
* | ||
* DESCRIPTION: | ||
* - Evaluates a polynomial. | ||
* | ||
* | ||
* NOTES: | ||
* [1] | ||
* | ||
* | ||
* TODO: | ||
* [1] | ||
* | ||
* | ||
* LICENSE: | ||
* MIT | ||
* | ||
* Copyright (c) 2014. Athan Reines. | ||
* | ||
* | ||
* AUTHOR: | ||
* Athan Reines. kgryte@gmail.com. 2014. | ||
* | ||
* @private | ||
* @param {Number[]} coef - array of coefficients sorted in descending degree | ||
* @param {Number} x - value at which to evaluate the polynomial | ||
* @return {Number} evaluated polynomial | ||
*/ | ||
function polyval( c, x ) { | ||
var len = c.length, | ||
p = 0, | ||
i = 0; | ||
for ( ; i < len; i++ ) { | ||
p = p*x + c[ i ]; | ||
} | ||
return p; | ||
} // end FUNCTION polyval() | ||
(function() { | ||
'use strict'; | ||
/** | ||
* FUNCTION: polyval( coef, x ) | ||
* Evaluates a polynomial. | ||
* | ||
* @private | ||
* @param {Array} coef - array of coefficients sorted in descending degree | ||
* @param {Number} x - value at which to evaluate the polynomial | ||
* @return {Number} evaluated polynomial | ||
*/ | ||
function polyval( c, x ) { | ||
var len = c.length, | ||
p = 0; | ||
// EVALUATE // | ||
// NaN check: | ||
if ( x !== x ) { | ||
throw new TypeError( 'polynomial()::invalid input argument. Value must be numeric.' ); | ||
/** | ||
* FUNCTION: evaluate( coef, x[, options] ) | ||
* Evaluates a polynomial. | ||
* | ||
* @param {Number[]} coef - array of coefficients sorted in descending degree | ||
* @param {Array|Number[]|Number} x - value(s) at which to evaluate the polynomial | ||
* @param {Object} [options] - function options | ||
* @param {Boolean} [options.copy=true] - boolean indicating whether to return a new array | ||
* @param {Function} [options.accessor] - accessor function for accessing array values | ||
* @returns {Number|Number[]} evaluated polynomial | ||
*/ | ||
function evaluate( c, x, opts ) { | ||
var copy = true, | ||
clbk, | ||
len, | ||
arr, | ||
v, i; | ||
if ( !isNumberArray( c ) ) { | ||
throw new TypeError( 'polynomial()::invalid input argument. Coefficients must be provided as an array of number primitives. Value: `' + c + '`.' ); | ||
} | ||
if ( isNumber( x ) ) { | ||
return polyval( c, x ); | ||
} | ||
if ( !isArray( x ) ) { | ||
throw new TypeError( 'polynomial()::invalid input argument. Second argument must be either a single number primitive or an array of values. Value: `' + x + '`.' ); | ||
} | ||
if ( arguments.length > 2 ) { | ||
if ( !isObject( opts ) ) { | ||
throw new TypeError( 'polynomial()::invalid input argument. Options argument must be an object. Value: `' + opts + '`.' ); | ||
} | ||
for ( var i = 0; i < len; i++ ) { | ||
p = p*x + c[ i ]; | ||
if ( opts.hasOwnProperty( 'copy' ) ) { | ||
copy = opts.copy; | ||
if ( !isBoolean( copy ) ) { | ||
throw new TypeError( 'polynomial()::invalid option. Copy option must be a boolean primitive. Option: `' + copy + '`.' ); | ||
} | ||
} | ||
return p; | ||
} // end FUNCTION polyval() | ||
// EXPORTS // | ||
/** | ||
* FUNCTION: polyval( coef, x ) | ||
* Evaluates a polynomial. | ||
* | ||
* @param {Array} coef - array of coefficients sorted in descending degree | ||
* @param {Array|Number} x - value(s) at which to evaluate the polynomial | ||
* @returns {Number|Array} evaluated polynomial | ||
*/ | ||
module.exports = function( c, x ) { | ||
if ( !Array.isArray( c ) ) { | ||
throw new TypeError( 'polynomial()::invalid input argument. Coefficients must be an array.' ); | ||
if ( opts.hasOwnProperty( 'accessor' ) ) { | ||
clbk = opts.accessor; | ||
if ( !isFunction( clbk ) ) { | ||
throw new TypeError( 'polynomial()::invalid option. Accessor must be a function. Option: `' + clbk + '`.' ); | ||
} | ||
} | ||
if ( typeof x === 'number' ) { | ||
return polyval( c, x ); | ||
} | ||
len = x.length; | ||
if ( copy ) { | ||
arr = new Array( len ); | ||
} else { | ||
arr = x; | ||
} | ||
if ( clbk ) { | ||
for ( i = 0; i < len; i++ ) { | ||
v = clbk( x[ i ], i ); | ||
if ( !isNumber( v ) ) { | ||
throw new TypeError( 'polynomial()::invalid input argument. Accessed array values must be number primitives. Value: `' + v + '`.' ); | ||
} | ||
arr[ i ] = polyval( c, v ); | ||
} | ||
if ( !Array.isArray( x ) ) { | ||
throw new TypeError( 'polynomial()::invalid input argument. Second argument must be either an array of numeric values or a single numeric value.' ); | ||
} else { | ||
for ( i = 0; i < len; i++ ) { | ||
v = x[ i ]; | ||
if ( !isNumber( v ) ) { | ||
throw new TypeError( 'polynomial()::invalid input argument. Array values must be number primitives. Value: `' + v + '`.' ); | ||
} | ||
arr[ i ] = polyval( c, v ); | ||
} | ||
var len = x.length, | ||
arr = new Array( len ); | ||
} | ||
return arr; | ||
} // end FUNCTION evaluate() | ||
for ( var i = 0; i < len; i++ ) { | ||
arr[ i ] = polyval( c, x[ i ] ); | ||
} | ||
return arr; | ||
}; | ||
})(); | ||
// EXPORTS // | ||
module.exports = evaluate; |
{ | ||
"name": "compute-polynomial", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Evaluates a polynomial.", | ||
@@ -32,4 +32,6 @@ "author": { | ||
"horner", | ||
"horner's rule", | ||
"polyval", | ||
"math" | ||
"math", | ||
"mathematics" | ||
], | ||
@@ -39,8 +41,17 @@ "bugs": { | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"validate.io-array": "^1.0.3", | ||
"validate.io-boolean-primitive": "^1.0.0", | ||
"validate.io-function": "^1.0.2", | ||
"validate.io-number-primitive": "^1.0.0", | ||
"validate.io-number-primitive-array": "^1.0.0", | ||
"validate.io-object": "^1.0.3" | ||
}, | ||
"devDependencies": { | ||
"chai": "1.x.x", | ||
"mocha": "1.x.x", | ||
"chai": "2.x.x", | ||
"coveralls": "^2.11.1", | ||
"istanbul": "^0.3.0" | ||
"istanbul": "^0.3.0", | ||
"jshint": "2.x.x", | ||
"jshint-stylish": "^1.0.0", | ||
"mocha": "2.x.x" | ||
}, | ||
@@ -47,0 +58,0 @@ "licenses": [ |
@@ -19,4 +19,2 @@ polynomial | ||
To use the module, | ||
``` javascript | ||
@@ -26,4 +24,7 @@ var polyval = require( 'compute-polynomial' ); | ||
The method requires two input arguments: an `array` of coefficients and either a single `numeric` value or an `array` of values at which to evaluate the polynomial. | ||
#### polyval( coef, x[, options] ) | ||
Evaluates a polynomial whose coefficients are defined by `coef`. `x` may be either a single `numeric` value or an `array` of values at which to evaluate to the polynomial. | ||
The coefficients should be ordered in __descending__ degree. For example, for a polynomial | ||
@@ -44,3 +45,3 @@ | ||
``` javascript | ||
polyval( [ 4, 2, 6, -17 ], 10 ); | ||
var val = polyval( [ 4, 2, 6, -17 ], 10 ); | ||
// returns 4243 | ||
@@ -52,10 +53,53 @@ ``` | ||
``` javascript | ||
polyval( [ 4, 2, 6, -17 ], [ 10, -3 ] ); | ||
var vals = polyval( [ 4, 2, 6, -17 ], [ 10, -3 ] ); | ||
// returns [ 4243, -125 ] | ||
``` | ||
When provided an input `array`, the function accepts the following `options`: | ||
* __copy__: `boolean` indicating whether to return a new `array`. Default: `true`. | ||
* __accessor__: accessor `function` for accessing numeric values in object `arrays`. | ||
To mutate the input `array` (e.g., when input values can be discarded or when optimizing memory usage), set the `copy` option to `false`. | ||
``` javascript | ||
var coefs = [ 4, 2, 6, -17 ], | ||
x = [ 10, -3 ]; | ||
var vals = polyval( coefs, x, { | ||
'copy': false | ||
}); | ||
// returns [ 4243, -125 ] | ||
console.log( x === vals ); | ||
// returns true | ||
``` | ||
For object `arrays`, provide an accessor `function` for accessing `array` values. | ||
``` javascript | ||
var coefs = [ 4, 2, 6, -17 ]; | ||
var data = [ | ||
['beep', 10], | ||
['boop', -3] | ||
]; | ||
function getValue( d, i ) { | ||
return d[ 1 ]; | ||
} | ||
var vals = polyval( coefs, data, { | ||
'accessor': getValue | ||
}); | ||
// returns [ 4243, -125 ] | ||
``` | ||
## Examples | ||
``` javascript | ||
var polyval = require( 'compute-polynomial' ); | ||
var coef = new Array( 25 ), | ||
@@ -95,3 +139,3 @@ sign; | ||
Unit tests use the [Mocha](http://visionmedia.github.io/mocha) test framework with [Chai](http://chaijs.com) assertions. To run the tests, execute the following command in the top-level application directory: | ||
Unit tests use the [Mocha](http://mochajs.org) test framework with [Chai](http://chaijs.com) assertions. To run the tests, execute the following command in the top-level application directory: | ||
@@ -116,6 +160,7 @@ ``` bash | ||
``` bash | ||
$ open reports/coverage/lcov-report/index.html | ||
$ make view-cov | ||
``` | ||
--- | ||
## License | ||
@@ -126,6 +171,5 @@ | ||
--- | ||
## Copyright | ||
Copyright © 2014. Athan Reines. | ||
Copyright © 2014-2015. Athan Reines. | ||
@@ -149,2 +193,2 @@ | ||
[github-issues-image]: http://img.shields.io/github/issues/compute-io/polynomial.svg | ||
[github-issues-url]: https://github.com/compute-io/polynomial/issues | ||
[github-issues-url]: https://github.com/compute-io/polynomial/issues |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10423
98
187
6
6
+ Addedvalidate.io-array@^1.0.3
+ Addedvalidate.io-function@^1.0.2
+ Addedvalidate.io-object@^1.0.3
+ Addedvalidate.io-array@1.0.6(transitive)
+ Addedvalidate.io-boolean-primitive@1.0.0(transitive)
+ Addedvalidate.io-function@1.0.2(transitive)
+ Addedvalidate.io-number-primitive@1.0.0(transitive)
+ Addedvalidate.io-number-primitive-array@1.0.0(transitive)
+ Addedvalidate.io-object@1.0.4(transitive)