Members
-
deg :integer
The degree of the polynomial.
-
Description
This is one less than the number of coefficients, or
-Infinityfor the zero polynomial.Examples
Polynomial.create(1, 0, 0).deg; // 2 Polynomia.lcreate().deg; // -InfinityDetails
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 coeffsnumber <repeatable> The coefficients of the resulting Polynomial.
Returns
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 rootsRoot <repeatable> The roots of the resulting polynomial.
Returns
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.0Details
-
<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 ninteger Compute the Legendre polynomial of this degree.
Returns
Examples
Polynomial.legendre(5).toString(); // 7.8750 x^5 - 8.7500 x^3 + 1.8750 xDetails
-
clone() → {Polynomial}
Create a new Polynomial with the same coefficients.
-
Returns
Details
-
toString( [ precision [, variable ] ] ) → {string}
Return a string representation of the polynomial.
-
Parameters
Name Type Attributes Default Description precisioninteger <optional> 4 The number of decimal places to include.
variablestring <optional> 'x' The dummy variable.
Returns
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
Examples
Polynomial.create(1, 0, 0).isZero(); // false Polynomial.create(0, 0, 0).isZero(); // trueDetails
-
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 PPolynomial 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
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)); // falseDetails
-
add( P [, factor ] ) → {Polynomial}
Add a polynomial.
-
Description
This creates a new Polynomial equal to the sum of
thisandP. Leading zero coefficients are stripped.Parameters
Name Type Attributes Default Description PPolynomial The polynomial to add.
factornumber <optional> 1 Add
factortimesPinstead of just addingP.Returns
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
thisandP. This is an alias forthis.add(P, -1).Parameters
Name Type Description PPolynomial The polynomial to subtract.
Returns
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
thisandP. The degree of the resulting polynomial is the sum of the degrees.Parameters
Name Type Description PPolynomial The polynomial to multiply.
Returns
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
thisbyP. The quotient has degree equal tothis.deg - P.deg, and the remainder has degree less thanP.deg.Parameters
Name Type Attributes Default Description PPolynomial 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
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.0Throws
Details
-
scale( c ) → {Polynomial}
Multiply in-place by a scalar.
-
Description
This multiplies all coefficients of
thisby the numberc;Parameters
Name Type Description cnumber The scalar to multiply.
Returns
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
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 ofthis.Parameters
Name Type Description ninteger The power to raise.
Returns
Examples
Polynomial.create(1, 1).pow(4).toString(1); // x^4 + 4.0 x^3 + 6.0 x^2 + 4.0 x + 1.0Details
-
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 znumber | Complex The number to evaluate.
Returns
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 iDetails
-
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 PPolynomial The polynomial to substitute.
Returns
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 + 3Details
-
derivative( [ n ] ) → {Polynomial}
Compute the
nth formal derivative ofthis. -
Description
The first derivative is the polynomial of degree one less, where the coefficient of
x^iis multiplied byi. Higher derivatives are computed recursively.Parameters
Name Type Attributes Default Description ninteger <optional> 1 The number of derivatives to take.
Returns
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
Examples
Polynomial.fromRoots([-2, 2], 3, 5).factor(); // [[-2, 2], [3, 1], [5, 1]]Throws
Details