
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
math-evalrational
Advanced tools
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
.
$ npm install math-evalrational
var evalrational = require( 'math-evalrational' );
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
the coefficients would be
[c_0, c_1, ..., c_(n-1), c_n]
matching the summation notation.
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.
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
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.
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
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
Copyright © 2016. The Compute.io Authors..
FAQs
Evaluates a rational function.
We found that math-evalrational demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.