new Polynomial()

Class representing a polynomial.

Description

A polynomial is stored as an Array of coefficients [an, ..., a1, a0] which represents the polynomial an x^n + ... + a1 x + a0 in the variable x. Polynomials can be added and scaled like Vectors, but can also be multiplied and evaluated at a number.

Do not use the constructor directly; instead use the convenience routine Polynomial.create.

Examples

			Polynomial.create(1, 2, 1).toString(1); // "x^2 + 2.0 x + 1.0"
Details

Array

Members


deg :integer

The degree of the polynomial.

Description

This is one less than the number of coefficients, or -Infinity for the zero polynomial.

Examples

			Polynomial.create(1, 0, 0).deg;  // 2
			Polynomia.lcreate().deg;         // -Infinity
Details
integer

Methods


<static> create( ...coeffs ) → {Polynomial}

Create a Polynomial with the given coefficients.

Description

This is like Polynomial.of, except it strips all leading zeros from the coefficients.

Parameters
Name Type Attributes Description
coeffs number <repeatable>

The coefficients of the resulting Polynomial.

Returns

The polynomial with the given coefficients.

Examples

			Polynomial.create(1, 2, 1).toString(1); // "x^2 + 2.0 x + 1.0"
			Polynomial.create(1).toString(1);       // "1.0"
			Polynomial.create(0, 1, 2).toString(1); // "x + 2.0"
Details

<static> fromRoots( ...roots ) → {Polynomial}

Create a monic Polynomial with the given roots.

Description

Given roots λ1, λ2, ..., λn, this returns the polynomial that factors as (x-λ1)(x-λ2)...(x-λn). Roots are assumed to be provided in complex-conjugate pairs; the complex part of the product is dropped.

Parameters
Name Type Attributes Description
roots Root <repeatable>

The roots of the resulting polynomial.

Returns

The monic polynomial with the given roots.

Examples

			Polynomial.fromRoots(1, -1).toString(1);    // x^2 - 1.0
			Polynomial.fromRoots([1, 3]).toString(1);   // x^3 - 3.0 x^2 + 3.0 x - 1.0
			Polynomial.fromRoots(1, Complex.i, Complex.i.conj()).toString(1);
			   // x^3 - 1.0 x^2 + 1.0 x - 1.0
Details

<static> legendre( n ) → {Polynomial}

Compute the Legendre polynomial of degree n.

Description

See the Wikipedia article for the definition and many wonderful properties of Legendre polynomials.

Parameters
Name Type Description
n integer

Compute the Legendre polynomial of this degree.

Returns

The nth Legendre polynomial.

Examples

			Polynomial.legendre(5).toString(); // 7.8750 x^5 - 8.7500 x^3 + 1.8750 x
Details

clone() → {Polynomial}

Create a new Polynomial with the same coefficients.

Returns

The new polynomial.

Details

toString( [ precision [, variable ] ] ) → {string}

Return a string representation of the polynomial.

Parameters
Name Type Attributes Default Description
precision integer <optional>
4

The number of decimal places to include.

variable string <optional>
'x'

The dummy variable.

Returns

A string representation of the polynomial.

Examples

			Polynomial.create(1, 2, 1).toString(1);       // "x^2 + 2.0 x + 1.0"
			Polynomial.create(1, 0, -2).toString(1, 'z'); // "z^2 - 2.0"
Details

isZero() → {boolean}

Test if a polynomial is zero.

Description

This means that the degree is -Infinity, i.e. that the coefficient list is empty.

Returns

True if the polynomial is zero.

Examples

			Polynomial.create(1, 0, 0).isZero();  // false
			Polynomial.create(0, 0, 0).isZero();  // true
Details

equals( P [, ε ] ) → {boolean}

Test if this Polynomial is equal to P.

Description

Two polynomials are equal if they have the same degree, and all coefficients are equal.

Parameters
Name Type Attributes Default Description
P Polynomial

The polynomial to compare.

ε number <optional>
0

Coefficients will test as equal if they are within ε of each other. This is provided in order to account for rounding errors.

Returns

True if the polynomials are equal.

Examples

			let p = Polynomial.create(1, 0.01, -0.01, 0);
			let q = Polynomial.create(1, 0, 0, 0);
			p.equals(q);                          // false
			p.equals(q, 0.05);                    // true
			q.equals(Polynomial.create(1, 0, 0)); // false
Details

add( P [, factor ] ) → {Polynomial}

Add a polynomial.

Description

This creates a new Polynomial equal to the sum of this and P. Leading zero coefficients are stripped.

Parameters
Name Type Attributes Default Description
P Polynomial

The polynomial to add.

factor number <optional>
1

Add factor times P instead of just adding P.

Returns

A new polynomial equal to the sum.

Examples

			let p = Polynomial.create(1, 2), q = Polynomial.create(1, 3, 4);
			p.add(q).toString(1);      // "x^2 + 4.0 x + 6.0"
			p.add(q, 2).toString(1);   // "2.0 x^2 + 7.0 x + 10.0"
			let q1 = Polynomial.create(-1, 0, 0);
			q.add(q1).toString(1);     // "3.0 x + 4.0"
Details

sub( P ) → {Polynomial}

Subtract a polynomial.

Description

This creates a new Polynomial equal to the difference of this and P. This is an alias for this.add(P, -1).

Parameters
Name Type Description
P Polynomial

The polynomial to subtract.

Returns

A new polynomial equal to the difference.

Examples

			let p = Polynomial.create(1, 2), q = Polynomial.create(1, 3, 4);
			p.sub(q).toString(1);   // "-x^2 - 2.0 x - 2.0"
			let q1 = Polynomial.create(1, 0, 0);
			q.sub(q1).toString(1);  // "3.0 x + 4.0"
Details

mult( P ) → {Polynomial}

Multiply by another polynomial.

Description

This creates a new Polynomial equal to the product of this and P. The degree of the resulting polynomial is the sum of the degrees.

Parameters
Name Type Description
P Polynomial

The polynomial to multiply.

Returns

A new polynomial equal to the product.

Examples

			let p = Polynomial.create(1, 2), q = Polynomial.create(1, 3, 4);
			p.mult(q).toString(1);   // "x^3 + 5.0 x^2 + 10.0 x + 8.0"
Details

div( P [, ε ] ) → {Array.<Polynomial>}

Synthetic division by another polynomial.

Description

This returns the quotient and remainder obtained by dividing this by P. The quotient has degree equal to this.deg - P.deg, and the remainder has degree less than P.deg.

Parameters
Name Type Attributes Default Description
P Polynomial

The polynomial to divide.

ε number <optional>
0

Leading coefficients of the remainder are considered to be zero if they are smaller than this. This is provided in order to account for rounding errors.

Returns

An array [Q, R], where Q is the quotient of this by P and R is the remainder.

Examples

			let p = Polynomial.create(6, 5, 0, -7);
			let q = Polynomial.create(3, -2, -1);
			let [Q, R] = p.div(q);
			Q.toString(1);  // 2.0 x + 3.0
			R.toString(1);  // 8.0 x - 4.0
Throws

Will throw an error if P has larger degree or is the zero polynomial.

Details

scale( c ) → {Polynomial}

Multiply in-place by a scalar.

Description

This multiplies all coefficients of this by the number c;

Parameters
Name Type Description
c number

The scalar to multiply.

Returns

this

Examples

			let p = Polynomial.create(1, 2, 3);
			p.mult(2);
			p.toString(1);   // "2.0 x^2 + 4.0 x + 6.0"
Details

monic() → {Polynomial}

Scale in-place to be monic.

Description

This scales by the reciprocal of the highest-order coefficient.

Returns

this

Examples

			let p = Polynomial.create(2, 4, 6);
			p.monic();
			p.toString(1);   // "x^2 + 2.0 x + 3.0"
Details

pow( n ) → {Polynomial}

Raise to an integer power.

Description

This returns a new polynomial eaqual to the nth power of this.

Parameters
Name Type Description
n integer

The power to raise.

Returns

A new Polynomial equal to the nth power of this.

Examples

			Polynomial.create(1, 1).pow(4).toString(1);
			   // x^4 + 4.0 x^3 + 6.0 x^2 + 4.0 x + 1.0
Details

eval( z ) → {number|Complex}

Evaluate the polynomial at a number.

Description

This substitutes the dummy variable for the number z, which may be real or Complex, and returns the result.

Parameters
Name Type Description
z number | Complex

The number to evaluate.

Returns

The result of evaluating this at z.

Examples

			let p = Polynomial.create(1, 1, 1, 1);
			p.evaluate(2);                              // 2**3 + 2**2 + 2 + 1
			p.evaluate(new Complex(1, 1)).toString(1);  // 0.0 + 5.0 i
Details

compose( P ) → {Polynomial}

Compose with another polynomial.

Description

This substitutes the dummy variable for the polynomial P, and returns a new Polynomial. The degree of the resulting polynomial is the product of the degrees.

Parameters
Name Type Description
P Polynomial

The polynomial to substitute.

Returns

A new Polynomial equal to the result of composing this with P.

Examples

			let p = Polynomial.create(1, 1, 1, 1);
			let q = Polynomial.create(1, 1, 1);
			p.compose(q).toString(0);  // x^6 + 3 x^5 + 7 x^4 + 9 x^3 + 10 x^2 + 6 x + 4
			q.compose(p).toString(0);  // x^6 + 2 x^5 + 3 x^4 + 5 x^3 + 4 x^2 + 3 x + 3
Details

derivative( [ n ] ) → {Polynomial}

Compute the nth formal derivative of this.

Description

The first derivative is the polynomial of degree one less, where the coefficient of x^i is multiplied by i. Higher derivatives are computed recursively.

Parameters
Name Type Attributes Default Description
n integer <optional>
1

The number of derivatives to take.

Returns

A new Polynomial obtained by taking n derivatives of this.

Examples

			let p = Polynomial.create(1, 1, 1, 1);
			p.derivative().toString(1);   // "3.0 x^2 + 2.0 x + 1.0"
			p.derivative(2).toString(1);  // "6.0 x + 2.0"
Details

factor( [ ε ] ) → {Array.<Root>}

Find all (real and complex) roots of the polynomial.

Description

This uses the quadratic formula in degree 2, Cardano's formula in degree 3, and Descarte's method in degree 4. It is not implemented for higher degrees.

Closed-form solutions like the quadratic formula do not exist in degrees 5 and above: see the Abel–Ruffini theorem. Of course, there are numerical methods for finding roots of polynomials—the best of which use linear algebraic methods to directly find the eigenvalues of a matrix with a given characteristic polynomial—but they are beyond the scope of this library.

The return value is ordered as follows.

  • Roots with smaller real part come first.
  • Real roots come before complex roots with the same real part.
  • Complex roots come in adjacent conjugate pairs; the one with positive imaginary part comes first.
  • Pairs of complex roots with smaller imaginary part (in absolute value) come first.
Parameters
Name Type Attributes Default Description
ε number <optional>
1e-10

If certain discriminant quantities are smaller than this, then multiple roots have been found.

Returns

The roots found.

Examples

			Polynomial.fromRoots([-2, 2], 3, 5).factor();  // [[-2, 2], [3, 1], [5, 1]]
Throws

Will throw an error if the degree is not 1, 2, 3, or 4.

Details