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
Adding two polynomials
var p = "3x^2"; // 2x^2
Second derivative of polynomial
var p = "5+3x^3+6x^5"; // 120x^3+18x
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.
'3': 4 '5': '9'; // 9x^5+4x^3123; //3x^2+2x+1
55; // 55x^0
The string parser passes every coefficient directly to the field parser, which allows to pass complex and rational coefficients as well:
// Example with complex numbersPolynomial;"98x^2+i+23ix^4";// Example with rational numbersPolynomial;"5/3x^3+4/3x";
- ℚ: 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
Polynomial;; // 0Polynomial;; // x^2+4x+6Polynomial;; // x^6+6x^4+5x^2+1Polynomial;; // 12x^3-4// Derivative of polynomialPolynomial;; // 30x^4+9x^2// Integrated polynomialPolynomial;; // x^3
Returns the sum of the actual polynomial and the parameter n
Returns the difference of the actual polynomial and the parameter 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
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:
Polynomialtrace = true;"x^4+3x^3+2x^2+6x";console; // ["x^4+3x^3", "2x^2+6x", "0"]
Returns the negated polynomial
Returns the reciprocal polynomial
Gets the leading coefficient
Gets the leading monomial
Divide all coefficients of the polynomial by lc()
Returns the n-th derivative of the polynomial
Returns the n-th integration of the polynomial
Evaluate the polynomial at point x, using Horner's method. Type for x must be a valid value for the given field.
(Deprecated) Alias for
Returns the power of the actual polynomial, raised to an integer exponent.
Returns the modulus (rest of the division) of the actual polynomial and n (this % n).
Returns the greatest common divisor of two polynomials
Returns the degree of the polynomial
Generates a string representation of the actual polynomial. This makes use of the
toString() function of the field.
Generates a LaTeX representation of the actual polynomial.
Formats the actual polynomial to a Horner Scheme
Creates a copy of the actual Polynomial object
Creates a new (monic) Polynomial whose roots lie at the values provided in the array
Sets the field globally. Choose one of the following strings for
- "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
If a really hard error occurs (parsing error, division by zero), polynomial.js throws exceptions! Please make sure you handle them correctly.
Installing polynomial.js is as easy as cloning this repo or use one of the following commands:
bower install polynomial.js
npm install polynomial
Using Polynomial.js with the browser
<!-- Needed for field/ring Q --><!-- Needed for field C -->
Using Polynomial.js with require.js
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.
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
Copyright and licensing
Copyright (c) 2015, Robert Eisele Dual licensed under the MIT or GPL Version 2 licenses.