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
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
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
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
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
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
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(); // 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
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
andP
. Leading zero coefficients are stripped.Parameters
Name Type Attributes Default Description P
Polynomial The polynomial to add.
factor
number <optional> 1 Add
factor
timesP
instead 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
this
andP
. This is an alias forthis.add(P, -1)
.Parameters
Name Type Description P
Polynomial 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
this
andP
. The degree of the resulting polynomial is the sum of the degrees.Parameters
Name Type Description P
Polynomial 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
this
byP
. The quotient has degree equal tothis.deg - P.deg
, and the remainder has degree less thanP.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
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
Details
-
scale( c ) → {Polynomial}
Multiply in-place by a scalar.
-
Description
This multiplies all coefficients of
this
by the numberc
;Parameters
Name Type Description c
number 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
n
th power ofthis
.Parameters
Name Type Description n
integer 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.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
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
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
n
th formal derivative ofthis
. -
Description
The first derivative is the polynomial of degree one less, where the coefficient of
x^i
is multiplied byi
. Higher derivatives are computed recursively.Parameters
Name Type Attributes Default Description n
integer <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