Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

math-evalpoly

Package Overview
Dependencies
Maintainers
2
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

math-evalpoly

Evaluates a polynomial.

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

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

v = 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 © 2016. The Compute.io Authors..

Keywords

FAQs

Package last updated on 04 Feb 2016

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc