evalpoly
Evaluates a polynomial.
Installation
$ npm install math-evalpoly
Usage
var evalpoly = require( 'math-evalpoly' );
evalpoly( c, x )
Evaluates a polynomial having coefficients c
and degree n
at a value x
, where n = c.length-1
.
var v = evalpoly( [3,2,1], 10 );
The coefficients should be ordered in ascending degree. For example, for a polynomial
the coefficients would be
[c_0, c_1, ..., c_(n-1), c_n]
matching the summation notation.
evalpoly.factory( c )
Uses code generation to in-line coefficients and return a reusable function
for evaluating a polynomial.
var poly = evalpoly.factory( [3,2,1] );
var v = poly( 10 );
v = poly( 5 );
Note: For hot code paths in which coefficients are invariant, the generated function
will be more performant than the main export.
Examples
var round = require( 'math-round' );
var evalpoly = require( 'math-evalpoly' );
var coef;
var sign;
var poly;
var v;
var i;
coef = new Float64Array( 10 );
for ( i = 0; i < coef.length; i++ ) {
if ( Math.random() < 0.5 ) {
sign = -1;
} else {
sign = 1;
}
coef[ i ] = sign * round( Math.random()*100 );
}
for ( i = 0; i < 100; i++ ) {
v = Math.random() * 100;
console.log( 'f(%d) = %d', v, evalpoly( coef, v ) );
}
poly = evalpoly.factory( coef );
for ( i = 0; i < 100; i++ ) {
v = Math.random()*100 - 50;
console.log( 'f(%d) = %d', v, poly( v ) );
}
To run the example code from the top-level application directory,
$ node ./examples/index.js
Tests
Unit
This repository uses tape for unit tests. To run the tests, execute the following command in the top-level application directory:
$ make test
All new feature development should have corresponding unit tests to validate correct functionality.
Test Coverage
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-cov
Istanbul creates a ./reports/coverage
directory. To access an HTML version of the report,
$ make view-cov
Browser Support
This repository uses Testling for browser testing. To run the tests in a (headless) local web browser, execute the following command in the top-level application directory:
$ make test-browsers
To view the tests in a local web browser,
$ make view-browser-tests
License
MIT license.
Copyright
Copyright © 2016. The Compute.io Authors..