API reference
Polynomials
NumPy's legacy polynomial API (np.poly1d and the np.poly* functions). Coefficients are listed highest power first.
| Name | Summary |
|---|---|
np.poly | Coefficients of the monic polynomial with the given roots, or the characteristic polynomial of a square matrix. |
np.roots | Roots of a polynomial, computed as the eigenvalues of its companion matrix. |
np.polyval | Evaluates a polynomial at x with Horner's scheme. |
np.polyadd | Sum of two polynomials. |
np.polysub | Difference a1 - a2 of two polynomials. |
np.polymul | Product of two polynomials (a full convolution, computed natively). |
np.polydiv | Polynomial long division. |
np.polyder | The m-th derivative (default 1). |
np.polyint | The m-th antiderivative (default 1). |
np.polyfit | Least-squares polynomial fit of degree deg, coefficients highest power first. |
np.poly1d | A 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
np.poly([1, 2]); // => [1, -3, 2]
np.poly([]); // => 1TypeScript declaration
np.poly(seqOfZeros: ArrayLike): number | NDArraynp.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
np.roots([1, -3, 2]); // => [2, 1]
np.roots([1, 0, 0]); // => [0, 0]TypeScript declaration
np.roots(p: PolyLike): NDArraynp.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
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): NDArraynp.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
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): NDArraynp.polysub
#np.polysub(a1, a2)
Difference a1 - a2 of two polynomials.
Parameters
a1ArrayLike | poly1d- Minuend.
a2ArrayLike | poly1d- Subtrahend.
Returns
NDArray | poly1d
Example
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): NDArraynp.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
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): NDArraynp.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
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
np.polyder([1, 2, 3]); // => [2, 2]TypeScript declaration
np.polyder(p: poly1d, m?: number | undefined): poly1d
np.polyder(p: ArrayLike, m?: number | undefined): NDArraynp.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
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): NDArraynp.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
np.allclose(np.polyfit([0, 1, 2], [1, 3, 5], 1), [2, 1]); // => trueTypeScript 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
cas roots. [options.variable]string- Variable name for printing (default
"x").
Returns
poly1d
Example
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)