evalrational
Evaluates a rational function, i.e. the ratio of two polynomials.
A rational function f(x)
is defined as
where both P(x)
and Q(x)
are polynomials in x
.
Installation
$ npm install math-evalrational
Usage
var 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 05 ;var v = ;// 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 4var P = -6 -5 4 2 ;// 0.5x^1 + 3x^0 => degree 2var Q = 3 05 0 0 ; // zero-paddedvar v = ;// 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
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;var v = ;// 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 = ;// 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 = ;var evalrational = ;var rational;var sign;var len;var P;var Q;var v;var i;// Create two arrays of random coefficients...len = 10;P = len ;Q = len ;for i = 0; i < len; i++if Math < 05sign = -1;elsesign = 1;P i = sign * ;Q i = sign * ;// Evaluate the rational function at random values...for i = 0; i < 100; i++v = Math * 100;console;// Generate an `evalrational` function...rational = evalrational;for i = 0; i < 100; i++v = Math*100 - 50;console;
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
Copyright
Copyright © 2016. The Compute.io Authors..