Socket
Socket
Sign inDemoInstall

polynomium

Package Overview
Dependencies
32
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    polynomium

Library for symbolically representing and working with polynomials.


Version published
Maintainers
1
Install size
2.55 MB
Created

Readme

Source

polynomium

Library for symbolically representing and working with polynomials.

npm version

Package Installation and Usage

The package is available on npm:

npm install polynomium

The library can be imported in the usual ways:

var polynomium = require('polynomium');

The library also supports standalone usage in browsers:

<script src="https://lapets.github.io/polynomium/lib/polynomium.js"></script>

Examples

The library supports the creation of objects that represent polynomials of zero or more variables:

var x = polynomium.v('x');
var y = polynomium.v('y');
var a = polynomium.c(2);
var b = polynomium.c(3);
var c = polynomium.c(5);
var p = (y.mul(x.add(b)));
var q = (y.mul(x.add(b))).mul(a);
var r = (y.mul(x.add(b))).mul((y.mul(x.add(b))));

Given the polynomials above, it is possible to display them in a human-readable way:

> x.toString()
'x'
> b.toString()
'3'
> (y.add(x.add(b))).toString()
'y + x + 3'
> p.toString()
'x*y + 3y'
> q.toString()
'2x*y + 6y'
> q.maxCoefficients(p).toString()
'2x*y + 6y'
> r.toString()
'x^2*y^2 + 6x*y^2 + 9y^2'

When possible, the operator functions will convert arguments that are numeric constants and valid variable names into polynomium objects:

> (y.add(x.add(5))).toString()
'y + x + 5'
> (y.add('x')).toString()
'y + x'

By default, the terms in the outer sum are in order of descending significance (where a term's significance is the sum of its exponents across its factors). The individual variables within factors are in ascending alphabetical order.

It is also possible to evaluate a polynomial by supplying an object that binds explicit values to each variable:

> r.evaluate({"x":2, "y":5})
625
> r({"x":2, "y":5})
625

In some cases, function objects cannot be used within data structures (such as when converting to JSON). A method is included for such scenarios:

> var o = r.toObject()
> o
{ polynomium: true,
  terms: 
   { 'y': { '2': 9 }, 
     'x,y': { '1,2': 6, '2,2': 1 } } }
> polynomium.add(o, o).toObject()
{ polynomium: true,
  terms: 
   { 'y': { '2': 18 }, 
     'x,y': { '1,2': 12, '2,2': 2 } } }

Testing

Unit tests are included in test/test.js. They can be run using Mocha:

npm test

Keywords

FAQs

Last updated on 26 Apr 2018

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc