Nominally Patriotic Meathead
    Wondering what’s next for npm?Check out our public roadmap! »

    math-evalpoly

    1.1.0 • Public • Published

    evalpoly

    NPM version Build Status Coverage Status Dependencies

    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 );
    // returns 123 => 3*10^0 + 2*10^1 + 1*10^2

    The coefficients should be ordered in ascending degree. For example, for a polynomial

    Polynomial expression.

    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 );
    // returns 123 => 3*10^0 + 2*10^1 + 1*10^2
     
    = poly( 5 );
    // returns 38 => 3*5^0 + 2*5^1 + 1*5^2

    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;
     
    // Create an array of random coefficients...
    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 );
    }
     
    // Evaluate the polynomial at random values...
    for ( i = 0; i < 100; i++ ) {
        v = Math.random() * 100;
        console.log( 'f(%d) = %d', v, evalpoly( coef, v ) );
    }
     
    // Generate an `evalpoly` function...
    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..

    Install

    npm i math-evalpoly

    DownloadsWeekly Downloads

    421

    Version

    1.1.0

    License

    MIT

    Last publish

    Collaborators

    • avatar
    • avatar