• Stars
    star
    121
  • Rank 283,604 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A JavaScript library to work with polynomials

Polynomial.js

NPM Package Build Status MIT license

Polynomials are defined as the sum of variables with increasing integer power and their coefficients in a certain field. For example the following might be still known from school:

P(x) = x^2 + 4x + 3

Examples

Adding two polynomials

var p = new Polynomial("3x^2").add("-x^2"); // 2x^2

Second derivative of polynomial

var p = new Polynomial("5+3x^3+6x^5").derive(2); // 120x^3+18x

Parser

Any function (see below) as well as the constructor of the Polynomial class parses its input like this.

You can pass either Objects, Doubles or Strings. Make sure strings don't contain any white-spaces or brackets. The parser doesn't analyse the string recursively.

Objects

new Polynomial({'3': 4, '5': '9'}); // 9x^5+4x^3
new Polynomial([1,2,3]); //3x^2+2x+1

Doubles

new Polynomial(55); // 55x^0

Strings

new Polynomial("98x^2+4+23x^4");

The string parser passes every coefficient directly to the field parser, which allows to pass complex and rational coefficients as well:

// Example with complex numbers
Polynomial.setField("C");
new Polynomial("98x^2+i+23ix^4");

// Example with rational numbers
Polynomial.setField("Q");
new Polynomial("5/3x^3+4/3x");

Fields

Polynomial.js is held general in order to operate on various fields. Fraction.js and Complex.js build the perfect base to extend polynomials to rational and complex numbers.

  • β„š: Rational numbers supported by Fraction.js
  • β„‚: Complex numbers supported by Complex.js
  • ℍ: Quaternions supported by Quaternion.js
  • β„€p: Field of integers mod p, with p prime
  • ℝ: Field of real numbers

Examples

Polynomial.setField("Q");
Polynomial("3/2x^2-4x").mod("5x"); // 0

Polynomial.setField("Z11");
Polynomial("3x^2+x+7").gcd("3x^2+x+7"); // x^2+4x+6

Polynomial.setField("Z7");
Polynomial("9x^2+4").pow(3); // x^6+6x^4+5x^2+1

Polynomial.setField("R");
Polynomial("3x^3-1").mul(4); // 12x^3-4

// Derivative of polynomial
Polynomial.setField("Q");
Polynomial("5+3x^3+6x^5").derive(); // 30x^4+9x^2

// Integrated polynomial
Polynomial.setField("Q");
Polynomial("3x^2").integrate(); // x^3

Functions

Polynomial add(n)

Returns the sum of the actual polynomial and the parameter n

Polynomial sub(n)

Returns the difference of the actual polynomial and the parameter n

Polynomial mul(n)

Returns the product of the actual polynomial and the parameter n

Polynomial addmul(x, y)

Adds the product of x and y to the actual number

Polynomial div(n)

Returns the quotient of the actual polynomial and the parameter n

There is a global variable to enable division tracing like this, if you want to output details:

Polynomial.trace = true;
new Polynomial("x^4+3x^3+2x^2+6x")
        .div("x+3");
console.log(Polynomial.trace.map(x => x.toString())); // ["x^4+3x^3", "2x^2+6x", "0"]

Polynomial neg(n)

Returns the negated polynomial

Polynomial reciprocal(n)

Returns the reciprocal polynomial

Polynomial lc()

Gets the leading coefficient

Polynomial lm()

Gets the leading monomial

Polynomial monic()

Divide all coefficients of the polynomial by lc()

Polynomial derive(n)

Returns the n-th derivative of the polynomial

Polynomial integrate(n)

Returns the n-th integration of the polynomial

mixed eval(x)

Evaluate the polynomial at point x, using Horner's method. Type for x must be a valid value for the given field.

mixed result(x)

(Deprecated) Alias for eval.

Polynomial pow(exp)

Returns the power of the actual polynomial, raised to an integer exponent.

Polynomial mod(n)

Returns the modulus (rest of the division) of the actual polynomial and n (this % n).

Polynomial gcd(n)

Returns the greatest common divisor of two polynomials

Number degree()

Returns the degree of the polynomial

String toString()

Generates a string representation of the actual polynomial. This makes use of the toString() function of the field.

String toLatex()

Generates a LaTeX representation of the actual polynomial.

String toHorner()

Formats the actual polynomial to a Horner Scheme

Polynomial clone()

Creates a copy of the actual Polynomial object

Polynomial Polynomial::fromRoots(roots)

Creates a new (monic) Polynomial whose roots lie at the values provided in the array roots

Polynomial::setField(x)

Sets the field globally. Choose one of the following strings for x:

  • "R": real numbers
  • "Q": rational numbers
  • "H": quaternions
  • "C": complex numbers
  • "Zp": with p a prime number, like "Z7"
  • or an object with the field operators. See examples folders for bigint

Exceptions

If a really hard error occurs (parsing error, division by zero), polynomial.js throws exceptions! Please make sure you handle them correctly.

Installation

Installing polynomial.js is as easy as cloning this repo or use one of the following commands:

bower install polynomial.js

or

npm install polynomial

Using Polynomial.js with the browser

<script src="fraction.js"></script> <!-- Needed for field/ring Q -->
<script src="complex.js"></script> <!-- Needed for field C -->
<script src="polynomial.js"></script>
<script>
Polynomial.setField("C")
console.log(Polynomial("4x+3i"));
</script>

Using Polynomial.js with require.js

<script src="require.js"></script>
<script>
requirejs(['polynomial.js'],
function(Polynomial) {
console.log(Polynomial("4x+3i"));
});
</script>

Coding Style

As every library I publish, polynomial.js is also built to be as small as possible after compressing it with Google Closure Compiler in advanced mode. Thus the coding style orientates a little on maxing-out the compression rate. Please make sure you keep this style if you plan to extend the library.

Testing

If you plan to enhance the library, make sure you add test cases and all the previous tests are passing. You can test the library with

npm test

Copyright and licensing

Copyright (c) 2015, Robert Eisele Dual licensed under the MIT or GPL Version 2 licenses.

More Repositories

1

jQuery-webcam

A webcam wrapper plugin for jQuery
ActionScript
515
star
2

Fraction.js

Fraction is a rational numbers library written in JavaScript
JavaScript
422
star
3

node-dhcp

A DHCP server and client written in pure JavaScript
JavaScript
292
star
4

PHP-Facedetect

A simple OpenCV wrapper for PHP to detect faces on images
C++
263
star
5

GPS.js

A NMEA parser and GPS utility library
JavaScript
243
star
6

jQuery-Paging

Probably the most advanced jQuery pagination plugin, no really!
HTML
233
star
7

Complex.js

A complex numbers library
JavaScript
224
star
8

BitSet.js

An arbitrary size Bit-Vector implementation in JavaScript
JavaScript
217
star
9

jQuery-xcolor

An easy to use color manipulation plugin for jQuery
JavaScript
157
star
10

PHP

A PHP improvement
C
137
star
11

udf_infusion

A MySQL functionality enhancement UDF
Shell
136
star
12

Quaternion.js

A JavaScript Quaternion library
JavaScript
115
star
13

Fritzing

My fritzing sketches
C++
63
star
14

node-gamecontroller

A node.js driver for several gamecontrollers
JavaScript
51
star
15

HTML5-Tetris

A HTML5 Tetris Implementation
JavaScript
45
star
16

HTML5-Experiments

My HTML5 Experiments
HTML
34
star
17

Trackball.js

A library to add a virtual Trackball to your DOM
JavaScript
34
star
18

Kalman.js

A JavaScript Kalman filter library
JavaScript
30
star
19

PHP-Daemon

PHP
30
star
20

Stewart

Inverse kinematics for Stewart Platforms written in JavaScript
JavaScript
25
star
21

Rectangles.js

Rectangles.js is a collection of functions to work with rectangles
JavaScript
20
star
22

Angles.js

Angles.js is a collection of functions to work with angles
JavaScript
17
star
23

PHP-Classes

A collection of PHP classes
PHP
15
star
24

PHP-Defcon

A global constant management library for PHP
C
14
star
25

JavaScript-Files

A collection of JavaScript files
JavaScript
11
star
26

Circle.js

A function collection for working with circles
JavaScript
11
star
27

node-dsh

A full featured JavaScript shell
JavaScript
9
star
28

PHP-IDNA

A libidn wrapper for PHP
C
5
star
29

PHP-Infusion

A PHP functionality enhancement library
C
5
star
30

PHP-Sysload

A simple sysload monitoring extension for PHP
C
3
star
31

mod_mysql_accesslog

A lighttpd module to log directly into MySQL
C
3
star
32

UnitFormat.js

A human readable unit formatting tool in JavaScript
JavaScript
2
star
33

jQuery-borderstyle

A handy jQuery plugin to manipulate the border stlye CSS attributes
JavaScript
1
star