JSPM

math-evalrational

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 400
  • Score
    100M100P100Q90072F
  • License MIT

Evaluates a rational function.

Package Exports

  • math-evalrational

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (math-evalrational) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

evalrational

NPM version Build Status Coverage Status Dependencies

Evaluates a rational function, i.e. the ratio of two polynomials.

A rational function f(x) is defined as

Rational function definition.

where both P(x) and Q(x) are polynomials in x.

Installation

$ npm install math-evalrational

Usage

var evalrational = require( 'math-evalrational' );

evalrational( P, Q, x )

Evaluates a rational function at a value x. The coefficients P and Q are expected to be arrays of the same length.

var P = [ -6, -5 ];
var Q = [ 3, 0.5 ];

var v = evalrational( P, Q, 6 );
// returns -6 => ( -6*6^0 - 5*6^1 ) / ( 3*6^0 + 0.5*6^1 ) = (-6-30)/(3+3)

For polynomials of different degree, the coefficient array for the lower degree polynomial should be padded with zeros.

// 2x^3 + 4x^2 - 5x^1 - 6x^0 => degree 4
var P = [ -6, -5, 4, 2 ];

// 0.5x^1 + 3x^0 => degree 2
var Q = [ 3, 0.5, 0, 0 ]; // zero-padded

var v = evalrational( P, Q, 6 );
// returns 90 => ( -6*6^0 - 5*6^1 + 4*6^2 + 2*6^3 ) / ( 3*6^0 + 0.5*6^1 + 0*6^2 + 0*6^3 ) = (-6-30+144+432)/(3+3)

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.

evalrational.factory( P, Q )

Uses code generation to in-line coefficients and return a reusable function for evaluating a rational function.

var P = [ 20, 8, 3 ];
var Q = [ 10, 9, 1 ];

var rational = evalrational.factory( P, Q );

var v = rational( 10 );
// returns 2 => (20*10^0 + 8*10^1 + 3*10^2) / (10*10^0 + 9*10^1 + 1*10^2) = (20+80+300)/(10+90+100)

v = rational( 2 );
// returns 1.5 => (20*2^0 + 8*2^1 + 3*2^2) / (10*2^0 + 9*2^1 + 1*2^2) = (20+16+12)/(10+18+4)

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 evalrational = require( 'math-evalrational' );

var rational;
var sign;
var len;
var P;
var Q;
var v;
var i;

// Create two arrays of random coefficients...
len = 10;
P = new Float64Array( len );
Q = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
    if ( Math.random() < 0.5 ) {
        sign = -1;
    } else {
        sign = 1;
    }
    P[ i ] = sign * round( Math.random()*100 );
    Q[ i ] = sign * round( Math.random()*100 );
}

// Evaluate the rational function at random values...
for ( i = 0; i < 100; i++ ) {
    v = Math.random() * 100;
    console.log( 'f(%d) = %d', v, evalrational( P, Q, v ) );
}

// Generate an `evalrational` function...
rational = evalrational.factory( P, Q );
for ( i = 0; i < 100; i++ ) {
    v = Math.random()*100 - 50;
    console.log( 'f(%d) = %d', v, rational( 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 © 2016. The Compute.io Authors..