Cyforanumera
npm

API reference

Polynomials

NumPy's legacy polynomial API (np.poly1d and the np.poly* functions). Coefficients are listed highest power first.

NameSummary
np.polyCoefficients of the monic polynomial with the given roots, or the characteristic polynomial of a square matrix.
np.rootsRoots of a polynomial, computed as the eigenvalues of its companion matrix.
np.polyvalEvaluates a polynomial at x with Horner's scheme.
np.polyaddSum of two polynomials.
np.polysubDifference a1 - a2 of two polynomials.
np.polymulProduct of two polynomials (a full convolution, computed natively).
np.polydivPolynomial long division.
np.polyderThe m-th derivative (default 1).
np.polyintThe m-th antiderivative (default 1).
np.polyfitLeast-squares polynomial fit of degree deg, coefficients highest power first.
np.poly1dA polynomial object.

np.poly

#
np.poly(seqOfZeros)

Coefficients of the monic polynomial with the given roots, or the characteristic polynomial of a square matrix. Returns 1 when there are no roots. Conjugate-pair roots give real coefficients.

Parameters

seqOfZerosArrayLike
Roots (1-D) or a square matrix.

Returns

NDArray | number

Example

TypeScript
np.poly([1, 2]); // => [1, -3, 2]
np.poly([]); // => 1
TypeScript declaration
np.poly(seqOfZeros: ArrayLike): number | NDArray

np.roots

#
np.roots(p)

Roots of a polynomial, computed as the eigenvalues of its companion matrix. Real when every root is real, otherwise complex.

Parameters

pArrayLike | poly1d
Coefficients, highest power first.

Returns

NDArray

Example

TypeScript
np.roots([1, -3, 2]); // => [2, 1]
np.roots([1, 0, 0]); // => [0, 0]
TypeScript declaration
np.roots(p: PolyLike): NDArray

np.polyval

#
np.polyval(p, x)

Evaluates a polynomial at x with Horner's scheme. With a poly1d as x, the result is the composed polynomial.

Parameters

pArrayLike | poly1d
Coefficients, highest power first.
xArrayLike | poly1d
Points to evaluate at.

Returns

NDArray | poly1d

Example

TypeScript
np.polyval([1, 2, 3], [0, 1, 2]); // => [3, 6, 11]
TypeScript declaration
np.polyval(p: PolyLike, x: poly1d): poly1d
np.polyval(p: PolyLike, x: ArrayLike): NDArray

np.polyadd

#
np.polyadd(a1, a2)

Sum of two polynomials. The result is a poly1d if either input is one.

Parameters

a1ArrayLike | poly1d
First polynomial.
a2ArrayLike | poly1d
Second polynomial.

Returns

NDArray | poly1d

Example

TypeScript
np.polyadd([1, 2], [3, 4, 5]); // => [3, 5, 7]
TypeScript declaration
np.polyadd(a1: poly1d, a2: PolyLike): poly1d
np.polyadd(a1: PolyLike, a2: poly1d): poly1d
np.polyadd(a1: ArrayLike, a2: ArrayLike): NDArray

np.polysub

#
np.polysub(a1, a2)

Difference a1 - a2 of two polynomials.

Parameters

a1ArrayLike | poly1d
Minuend.
a2ArrayLike | poly1d
Subtrahend.

Returns

NDArray | poly1d

Example

TypeScript
np.polysub([2, 2], [1]); // => [2, 1]
TypeScript declaration
np.polysub(a1: poly1d, a2: PolyLike): poly1d
np.polysub(a1: PolyLike, a2: poly1d): poly1d
np.polysub(a1: ArrayLike, a2: ArrayLike): NDArray

np.polymul

#
np.polymul(a1, a2)

Product of two polynomials (a full convolution, computed natively).

Parameters

a1ArrayLike | poly1d
First factor.
a2ArrayLike | poly1d
Second factor.

Returns

NDArray | poly1d

Example

TypeScript
np.polymul([1, 2], [3, 4]); // => [3, 10, 8]
TypeScript declaration
np.polymul(a1: poly1d, a2: PolyLike): poly1d
np.polymul(a1: PolyLike, a2: poly1d): poly1d
np.polymul(a1: ArrayLike, a2: ArrayLike): NDArray

np.polydiv

#
np.polydiv(u, v)

Polynomial long division. Returns [quotient, remainder] in floating point; leading near-zero remainder terms are dropped.

Parameters

uArrayLike | poly1d
Dividend.
vArrayLike | poly1d
Divisor.

Returns

[NDArray, NDArray] | [poly1d, poly1d]

Example

TypeScript
const [q, r] = np.polydiv([1, -3, 2], [1, -1]);
q; // => [1, -2]
r; // => [0]
TypeScript declaration
np.polydiv(u: poly1d, v: PolyLike): [poly1d, poly1d]
np.polydiv(u: PolyLike, v: poly1d): [poly1d, poly1d]
np.polydiv(u: ArrayLike, v: ArrayLike): [NDArray, NDArray]

np.polyder

#
np.polyder(p, [m])

The m-th derivative (default 1).

Parameters

pArrayLike | poly1d
Coefficients, highest power first.
[m]number
Order of the derivative.

Returns

NDArray | poly1d

Example

TypeScript
np.polyder([1, 2, 3]); // => [2, 2]
TypeScript declaration
np.polyder(p: poly1d, m?: number | undefined): poly1d
np.polyder(p: ArrayLike, m?: number | undefined): NDArray

np.polyint

#
np.polyint(p, [m], [k])

The m-th antiderivative (default 1). k gives the integration constants: one value for all, or one per integration.

Parameters

pArrayLike | poly1d
Coefficients, highest power first.
[m]number
Order of the integral.
[k]number | number[]
Integration constants (default 0).

Returns

NDArray | poly1d

Example

TypeScript
np.polyint([3, 2], 1, 5); // => [1.5, 2, 5]
TypeScript declaration
np.polyint(p: poly1d, m?: number | undefined, k?: ArrayLike | null | undefined): poly1d
np.polyint(p: ArrayLike, m?: number | undefined, k?: ArrayLike | null | undefined): NDArray

np.polyfit

#
np.polyfit(x, y, deg, [options])

Least-squares polynomial fit of degree deg, coefficients highest power first. y may be 2-D (one fit per column). With full, returns [c, residuals, rank, singularValues, rcond]; with cov, returns [c, covariance]. A rank-deficient fit emits a RankWarning.

Parameters

xArrayLike
Sample points (1-D).
yArrayLike
Values, 1-D or 2-D.
degnumber
Degree of the fit.
[options.rcond]number
Singular value cutoff (default len(x) * eps).
[options.full]boolean
Return diagnostic values too.
[options.w]ArrayLike
Weights.
[options.cov]boolean | "unscaled"
Return the covariance matrix too.

Returns

NDArray | Array

Example

TypeScript
np.allclose(np.polyfit([0, 1, 2], [1, 3, 5], 1), [2, 1]); // => true
TypeScript declaration
np.polyfit(x: ArrayLike, y: ArrayLike, deg: number, options?: PolyfitOptions | undefined): NDArray | (number | NDArray)[]

np.poly1d

#
new np.poly1d(c, [options])

A polynomial object. Leading zeros are trimmed. Python operators are methods: call(x), add, sub, mul, div (a scalar, or a polynomial giving [q, r]), pow, neg, equals. get(k) and set(k, v) read and write the coefficient of x**k. Also available: coeffs, order, length, roots, variable, integ(m, k), deriv(m), iteration over the coefficients, and toString(), which matches NumPy's str(p).

Parameters

cArrayLike | poly1d
Coefficients, highest power first (or roots with r: true).
[options.r]boolean
Treat c as roots.
[options.variable]string
Variable name for printing (default "x").

Returns

poly1d

Example

TypeScript
const p = new np.poly1d([1, -2, 3]);
p.call(2); // => 3
p.mul(p).coeffs; // => [1, -4, 10, -12, 9]
p.deriv().coeffs; // => [2, -2]
String(p); // => "   2\n1 x - 2 x + 3"
TypeScript declaration
new np.poly1d(cOrR: PolyLike, options?: boolean | Poly1dOptions | undefined)