API reference
np.linalg
Dense linear algebra on LAPACK: Apple Accelerate on macOS, and a portable built-in backend elsewhere. The functions take the last two axes as the matrix, and inputs with extra leading axes are processed as batches, as in NumPy. Singular or non-convergent inputs throw LinAlgError. Float results are shown rounded; they are exact up to rounding.
| Name | Summary |
|---|---|
np.linalg.det | Determinant of a square matrix, or of each matrix in a batch. |
np.linalg.inv | Matrix inverse. |
np.linalg.solve | Solves a @ x = b for x. |
np.linalg.eig | Eigen-decomposition of a general square matrix. |
np.linalg.eigh | Eigen-decomposition of a symmetric/Hermitian matrix (real or complex), using the lower triangle. |
np.linalg.svd | Singular value decomposition a = U @ diag(S) @ Vh. |
np.linalg.qr | QR factorization a = Q @ R. |
np.linalg.lstsq | Least-squares solution of a @ x ≈ b, computed with SVD. |
np.linalg.norm | Vector or matrix norm. |
np.linalg.backend | Name of the active native LAPACK backend: "accelerate" (macOS) or "fallback". |
np.linalg.cholesky | Cholesky factor of a Hermitian positive-definite matrix (stack): lower L with a = L Lᴴ, or upper U with a = Uᴴ U. |
np.linalg.slogdet | Sign and natural log of the absolute determinant, robust against overflow. |
np.linalg.svdvals | Singular values in descending order (same as svd(x, { computeUV: false }).S). |
np.linalg.matrixPower | Raises a square matrix (stack) to the integer power n by repeated squaring. |
np.linalg.pinv | Moore–Penrose pseudo-inverse of a matrix (stack) via SVD, or via eigh when hermitian. |
np.linalg.matrixRank | Rank of a matrix (stack): the number of singular values above the threshold. |
np.linalg.cond | Condition number of a matrix (stack). |
np.linalg.vectorNorm | Vector norm over one axis, several axes (treated as one flattened vector), or the whole array (axis: null, the default). |
np.linalg.matrixNorm | Matrix norm over the last two axes of x (stack). |
np.linalg.matrixTranspose | Swaps the last two axes (a view). |
np.linalg.diagonal | Diagonals of the trailing matrices (np.diagonal with axis1 = -2, axis2 = -1). |
np.linalg.trace | Traces of the trailing matrices (np.trace with axis1 = -2, axis2 = -1). |
np.linalg.outer | Outer product of two 1-D arrays. |
np.linalg.tensorinv | Inverse of an N-d array with respect to tensordot(·, ·, ind). |
np.linalg.tensorsolve | Solves tensordot(a, x, x.ndim) = b for x, where x.shape = a.shape[b.ndim:]. |
np.vdot | Dot product of a and b flattened to 1-D, conjugating a first (complex input). |
np.kron | Kronecker product: a block array whose blocks are a[i, j, …] * b. |
np.cross | Cross product of (broadcast arrays of) 3-element vectors. |
np.tensordot | Sums products over the chosen axes: axes: N pairs the last N axes of a with the first N of b; axes: [axesA, axesB] lists them explicitly. |
np.linalg.multiDot | Chained dot of two or more arrays in the cheapest order (matrix-chain dynamic programming). |
np.vecdot | Vector dot product along axis, conjugating x1 (complex): sum(conj(x1) * x2, axis). |
np.matvec | Matrix-vector product over the last axes of x1 (..., M, N) and x2 (..., N), broadcasting the batch axes. |
np.vecmat | Vector-matrix product of x1 (..., N) (conjugated if complex) and x2 (..., N, M), broadcasting the batch axes. |
np.einsum | Einstein summation. |
np.einsumPath | Cheapest contraction order for an einsum expression, as [path, report]: path is ["einsum_path", [i, j], ...] (pass it as optimize) and report is NumPy's text summary. |
np.linalg.det
#np.linalg.det(a)
Determinant of a square matrix, or of each matrix in a batch.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar.
Returns
NDArray
Example
np.linalg.det([[3, 1], [1, 2]]).item(); // => 5TypeScript declaration
np.linalg.det(a: ArrayLike): NDArraynp.linalg.inv
#np.linalg.inv(a)
Matrix inverse. Throws LinAlgError for singular matrices.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar.
Returns
NDArray
Example
np.linalg.inv([[4, 7], [2, 6]]); // => [[0.6, -0.7], [-0.2, 0.4]]TypeScript declaration
np.linalg.inv(a: ArrayLike): NDArraynp.linalg.solve
#np.linalg.solve(a, b)
Solves a @ x = b for x.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. bArrayLike- An
NDArray, nested JS array or scalar.
Returns
NDArray
Example
np.linalg.solve([[3, 1], [1, 2]], [9, 8]); // => [2, 3]TypeScript declaration
np.linalg.solve(a: ArrayLike, b: ArrayLike): NDArraynp.linalg.eig
#np.linalg.eig(a) · np.linalg.eigvals(a)
Eigen-decomposition of a general square matrix. Results are always complex: complex64 for float32 or complex64 input, otherwise complex128. eigvals returns only the eigenvalues, computed without eigenvectors as in NumPy.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar.
Returns
{ eigenvalues: NDArray, eigenvectors: NDArray }
Example
const { eigenvalues } = np.linalg.eig([[2, 0], [0, 3]]);
eigenvalues.dtype.name; // => "complex128"TypeScript declaration
np.linalg.eig(a: ArrayLike): EigResult
np.linalg.eigvals(a: ArrayLike): NDArraynp.linalg.eigh
#np.linalg.eigh(a) · np.linalg.eigvalsh(a)
Eigen-decomposition of a symmetric/Hermitian matrix (real or complex), using the lower triangle. Eigenvalues are real and sorted in ascending order. eigvalsh computes eigenvalues only.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar.
Returns
{ eigenvalues: NDArray, eigenvectors: NDArray }
Example
np.linalg.eigh([[2, 1], [1, 2]]).eigenvalues; // => [1, 3]
np.linalg.eigvalsh([[2, 1], [1, 2]]); // => [1, 3]TypeScript declaration
np.linalg.eigh(a: ArrayLike): EigResult
np.linalg.eigvalsh(a: ArrayLike): NDArraynp.linalg.svd
#np.linalg.svd(a, [options])
Singular value decomposition a = U @ diag(S) @ Vh. S is in descending order.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. [options.fullMatrices]boolean- Return square
U/Vh. Defaulttrue. [options.computeUV]boolean- If
false,UandVharenull. Defaulttrue.
Returns
{ U: NDArray | null, S: NDArray, Vh: NDArray | null }
Example
np.linalg.svd([[3, 0], [0, 4]]).S; // => [4, 3]
np.linalg.svd([[1, 2], [3, 4], [5, 6]], { fullMatrices: false }).U.shape; // => [3, 2]TypeScript declaration
np.linalg.svd(a: ArrayLike, opts?: SvdOptions | undefined): SvdResultnp.linalg.qr
#np.linalg.qr(a, [mode="reduced"])
QR factorization a = Q @ R. mode is "reduced", "complete" or "r"; with "r", Q is null.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. [mode]"reduced" | "complete" | "r"- Output shapes. Default
"reduced".
Returns
{ Q: NDArray | null, R: NDArray }
Example
np.linalg.qr([[1, 2], [3, 4], [5, 6]]).R.shape; // => [2, 2]
np.linalg.qr([[1, 2], [3, 4]], "r").Q; // => nullTypeScript declaration
np.linalg.qr(a: ArrayLike, mode?: QrMode | undefined): QrResultnp.linalg.lstsq
#np.linalg.lstsq(a, b, [rcond])
Least-squares solution of a @ x ≈ b, computed with SVD.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. bArrayLike- An
NDArray, nested JS array or scalar. [rcond]number | null- Cut-off for small singular values. Default
eps * max(M, N).
Returns
{ x: NDArray, residuals: NDArray, rank: number, s: NDArray }
Example
// fit y = m*x + c
const r = np.linalg.lstsq([[0, 1], [1, 1], [2, 1], [3, 1]], [-1, 0.2, 0.9, 2.1]);
r.x; // => [1, -0.95]
r.rank; // => 2TypeScript declaration
np.linalg.lstsq(a: ArrayLike, b: ArrayLike, rcond?: number | null | undefined): LstsqResultnp.linalg.norm
#np.linalg.norm(a, [options])
Vector or matrix norm. The default is the 2-norm of the flattened input.
Parameters
aArrayLike- An
NDArray, nested JS array or scalar. [options.ord]number | "fro" | "nuc" | null- Norm order (
Infinity,1,2,"fro", ...). [options.axis]number | [number, number] | null- Vector axis or matrix axes.
[options.keepdims]boolean- Keep reduced axes.
Returns
NDArray
Example
np.linalg.norm([3, 4]).item(); // => 5
np.linalg.norm([[1, -2], [3, 4]], { ord: 1 }).item(); // => 6
np.linalg.norm([[3, 4], [6, 8]], { axis: 1 }); // => [5, 10]TypeScript declaration
np.linalg.norm(a: ArrayLike, opts?: NormOptions | undefined): NDArraynp.linalg.backend
#np.linalg.backend()
Name of the active native LAPACK backend: "accelerate" (macOS) or "fallback".
Returns
string
Example
typeof np.linalg.backend(); // => "string"np.linalg.cholesky
#np.linalg.cholesky(a, [options])
Cholesky factor of a Hermitian positive-definite matrix (stack): lower L with a = L Lᴴ, or upper U with a = Uᴴ U. Only that triangle of a is read. Raises LinAlgError if a is not positive definite.
Parameters
aArrayLike- Matrix or stack of matrices
(..., M, N). [options.upper]boolean- Return the upper factor. Default
false.
Returns
NDArray
Example
np.linalg.cholesky([[4, 2], [2, 5]]); // => [[2, 0], [1, 2]]
np.linalg.cholesky([[4, 2], [2, 5]], { upper: true }); // => [[2, 1], [0, 2]]TypeScript declaration
np.linalg.cholesky(a: ArrayLike, opts?: CholeskyOptions | undefined): NDArraynp.linalg.slogdet
#np.linalg.slogdet(a)
Sign and natural log of the absolute determinant, robust against overflow. A singular matrix gives sign = 0, logabsdet = -Infinity. For complex input the sign is a complex number of modulus 1.
Parameters
aArrayLike- Matrix or stack of matrices
(..., M, N).
Returns
{ sign: NDArray, logabsdet: NDArray }
Example
np.linalg.slogdet([[1, 2], [3, 4]]).sign.item(); // => -1
np.linalg.slogdet([[2, 0], [0, 4]]).logabsdet.item(); // => 2.0794415416798357TypeScript declaration
np.linalg.slogdet(a: ArrayLike): SlogdetResultnp.linalg.svdvals
#np.linalg.svdvals(x)
Singular values in descending order (same as svd(x, { computeUV: false }).S).
Parameters
xArrayLike- Matrix or stack of matrices
(..., M, N).
Returns
NDArray
Example
np.linalg.svdvals([[3, 0], [0, 4]]); // => [4, 3]TypeScript declaration
np.linalg.svdvals(x: ArrayLike): NDArraynp.linalg.matrixPower
#np.linalg.matrixPower(a, n)
Raises a square matrix (stack) to the integer power n by repeated squaring. n = 0 gives the identity in a's dtype; n < 0 inverts first (float result).
Parameters
aArrayLike- Matrix or stack of matrices
(..., M, N). nnumber- Integer exponent.
Returns
NDArray
Example
np.linalg.matrixPower([[1, 1], [0, 1]], 3); // => [[1, 3], [0, 1]]
np.linalg.matrixPower([[1, 1], [0, 1]], -1); // => [[1, -1], [0, 1]]TypeScript declaration
np.linalg.matrixPower(a: ArrayLike, n: number): NDArraynp.linalg.pinv
#np.linalg.pinv(a, [options])
Moore–Penrose pseudo-inverse of a matrix (stack) via SVD, or via eigh when hermitian. Singular values at or below rcond * max(s) are treated as zero. Shape (..., M, N) gives (..., N, M).
Parameters
aArrayLike- Matrix or stack of matrices
(..., M, N). [options.rcond]ArrayLike | number- Relative cutoff, broadcast over the batch. Default
1e-15. [options.rtol]ArrayLike | number | null- Array-API alias of
rcond;nullmeansmax(M, N) * eps. Cannot be combined withrcond. [options.hermitian]boolean- Treat
aas Hermitian. Defaultfalse.
Returns
NDArray
Example
np.linalg.pinv([[1, 0], [0, 2]]); // => [[1, 0], [0, 0.5]]
np.linalg.pinv([[2, 0, 0], [0, 4, 0]]); // => [[0.5, 0], [0, 0.25], [0, 0]]TypeScript declaration
np.linalg.pinv(a: ArrayLike, opts?: PinvOptions | undefined): NDArraynp.linalg.matrixRank
#np.linalg.matrixRank(A, [options])
Rank of a matrix (stack): the number of singular values above the threshold. By default the threshold is max(s) * max(M, N) * eps. For 0-d and 1-d input the result is 1 if any element is nonzero, else 0. Returns int64.
Parameters
AArrayLike- Matrix or stack of matrices
(..., M, N). [options.tol]ArrayLike | number- Absolute threshold.
[options.rtol]ArrayLike | number- Relative threshold (times the largest singular value). Cannot be combined with
tol. [options.hermitian]boolean- Use eigenvalue magnitudes of a Hermitian
A. Defaultfalse.
Returns
NDArray
Example
np.linalg.matrixRank([[1, 2], [2, 4]]).item(); // => 1
np.linalg.matrixRank(np.eye(3)).item(); // => 3TypeScript declaration
np.linalg.matrixRank(A: ArrayLike, opts?: MatrixRankOptions | undefined): NDArraynp.linalg.cond
#np.linalg.cond(x, [p])
Condition number of a matrix (stack). null, 2 and -2 use singular values (any shape); 1, -1, Infinity, -Infinity, "fro" and "nuc" compute norm(x) * norm(inv(x)) and need square input. A singular matrix gives Infinity.
Parameters
xArrayLike- Matrix or stack of matrices
(..., M, N). [p]number | "fro" | "nuc" | null- Norm order. Default
null(2-norm).
Returns
NDArray
Example
np.linalg.cond([[1, 0], [0, 2]]).item(); // => 2
np.linalg.cond([[4, 0], [0, 2]], 1).item(); // => 2TypeScript declaration
np.linalg.cond(x: ArrayLike, p?: NormOrder | undefined): NDArraynp.linalg.vectorNorm
#np.linalg.vectorNorm(x, [options])
Vector norm over one axis, several axes (treated as one flattened vector), or the whole array (axis: null, the default). Supports any real order, Infinity and -Infinity.
Parameters
xArrayLike- An
NDArrayor nested JS array. [options.axis]number | number[] | null- Axes to reduce. Default
null(all). [options.keepdims]boolean- Keep reduced axes with length 1.
[options.ord]number- Norm order. Default
2.
Returns
NDArray
Example
np.linalg.vectorNorm([3, 4]).item(); // => 5
np.linalg.vectorNorm([[1, -2], [3, 4]], { axis: 1, ord: 1 }); // => [3, 7]TypeScript declaration
np.linalg.vectorNorm(x: ArrayLike, opts?: VectorNormOptions | undefined): NDArraynp.linalg.matrixNorm
#np.linalg.matrixNorm(x, [options])
Matrix norm over the last two axes of x (stack).
Parameters
xArrayLike- Matrix or stack of matrices
(..., M, N). [options.keepdims]boolean- Keep the two reduced axes with length 1.
[options.ord]number | "fro" | "nuc"- Norm order:
"fro"(default),"nuc", 1, -1, 2, -2,Infinity,-Infinity.
Returns
NDArray
Example
np.linalg.matrixNorm([[3, 0], [0, 4]]).item(); // => 5
np.linalg.matrixNorm([[1, 2], [3, 4]], { ord: 1 }).item(); // => 6TypeScript declaration
np.linalg.matrixNorm(x: ArrayLike, opts?: MatrixNormOptions | undefined): NDArraynp.linalg.matrixTranspose
#np.linalg.matrixTranspose(x)
Swaps the last two axes (a view). Needs at least 2 dimensions.
Parameters
xArrayLike- Matrix or stack of matrices
(..., M, N).
Returns
NDArray
Example
np.linalg.matrixTranspose([[1, 2, 3]]); // => [[1], [2], [3]]TypeScript declaration
np.linalg.matrixTranspose(x: ArrayLike): NDArraynp.linalg.diagonal
#np.linalg.diagonal(x, [options])
Diagonals of the trailing matrices (np.diagonal with axis1 = -2, axis2 = -1).
Parameters
xArrayLike- Matrix or stack of matrices
(..., M, N). [options.offset]number- Diagonal offset. Default
0.
Returns
NDArray
Example
np.linalg.diagonal([[1, 2], [3, 4]]); // => [1, 4]
np.linalg.diagonal([[1, 2], [3, 4]], { offset: 1 }); // => [2]TypeScript declaration
np.linalg.diagonal(x: ArrayLike, opts?: { offset?: number | undefined; } | undefined): NDArraynp.linalg.trace
#np.linalg.trace(x, [options])
Traces of the trailing matrices (np.trace with axis1 = -2, axis2 = -1).
Parameters
xArrayLike- Matrix or stack of matrices
(..., M, N). [options.offset]number- Diagonal offset. Default
0. [options.dtype]DTypeLike- Accumulator/result dtype.
Returns
NDArray
Example
np.linalg.trace([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]); // => [5, 13]TypeScript declaration
np.linalg.trace(x: ArrayLike, opts?: { offset?: number | undefined; dtype?: DTypeLike | null | undefined; } | undefined): NDArraynp.linalg.outer
#np.linalg.outer(x1, x2)
Outer product of two 1-D arrays. Unlike np.outer, other dimensions raise ValueError.
Parameters
x1ArrayLike- An
NDArrayor nested JS array. x2ArrayLike- An
NDArrayor nested JS array.
Returns
NDArray
Example
np.linalg.outer([1, 2], [3, 4]); // => [[3, 4], [6, 8]]TypeScript declaration
np.linalg.outer(x1: ArrayLike, x2: ArrayLike): NDArraynp.linalg.tensorinv
#np.linalg.tensorinv(a, [options])
Inverse of an N-d array with respect to tensordot(·, ·, ind). prod(a.shape[:ind]) must equal prod(a.shape[ind:]); the result has shape a.shape[ind:] + a.shape[:ind].
Parameters
aArrayLike- An
NDArrayor nested JS array. [options.ind]number- Number of leading indices. Default
2.
Returns
NDArray
Example
np.linalg.tensorinv(np.eye(4).reshape([4, 2, 2]), { ind: 1 }).shape; // => [2, 2, 4]TypeScript declaration
np.linalg.tensorinv(a: ArrayLike, opts?: TensorinvOptions | undefined): NDArraynp.linalg.tensorsolve
#np.linalg.tensorsolve(a, b, [options])
Solves tensordot(a, x, x.ndim) = b for x, where x.shape = a.shape[b.ndim:].
Parameters
aArrayLike- An
NDArrayor nested JS array. bArrayLike- An
NDArrayor nested JS array. [options.axes]number[]- Axes of
amoved to the end before solving.
Returns
NDArray
Example
np.linalg.tensorsolve(np.eye(4).reshape([4, 2, 2]), [1, 2, 3, 4]); // => [[1, 2], [3, 4]]TypeScript declaration
np.linalg.tensorsolve(a: ArrayLike, b: ArrayLike, opts?: TensorsolveOptions | undefined): NDArraynp.vdot
#np.vdot(a, b)
Dot product of a and b flattened to 1-D, conjugating a first (complex input). Both must have the same number of elements.
Parameters
aArrayLike- An
NDArrayor nested JS array. bArrayLike- An
NDArrayor nested JS array.
Returns
NDArray (0-d)
Example
np.vdot([[1, 2], [3, 4]], [[1, 1], [1, 1]]).item(); // => 10TypeScript declaration
np.vdot(a: ArrayLike, b: ArrayLike): NDArraynp.kron
#np.kron(a, b)
Kronecker product: a block array whose blocks are a[i, j, …] * b. Shapes are left-padded with 1s to the same length; the result shape is their elementwise product.
Parameters
aArrayLike- An
NDArrayor nested JS array. bArrayLike- An
NDArrayor nested JS array.
Returns
NDArray
Example
np.kron([1, 10], [1, 2, 3]); // => [1, 2, 3, 10, 20, 30]
np.kron([[1, 0], [0, 1]], [[1, 2]]); // => [[1, 2, 0, 0], [0, 0, 1, 2]]TypeScript declaration
np.kron(a: ArrayLike, b: ArrayLike): NDArraynp.cross
#np.cross(a, b, [options]) / np.linalg.cross(x1, x2, [options])
Cross product of (broadcast arrays of) 3-element vectors. As in NumPy 2, 2-element vectors raise ValueError. np.linalg.cross only takes axis.
Parameters
aArrayLike- An
NDArrayor nested JS array. bArrayLike- An
NDArrayor nested JS array. [options.axisa]number- Vector axis of
a. Default-1. [options.axisb]number- Vector axis of
b. Default-1. [options.axisc]number- Vector axis of the result. Default
-1. [options.axis]number- Sets all three axes at once.
Returns
NDArray
Example
np.cross([1, 2, 3], [4, 5, 6]); // => [-3, 6, -3]
np.linalg.cross([1, 0, 0], [0, 1, 0]); // => [0, 0, 1]TypeScript declaration
np.cross(a: ArrayLike, b: ArrayLike, opts?: CrossOptions | undefined): NDArray
np.linalg.cross(x1: ArrayLike, x2: ArrayLike, opts?: { axis?: number | undefined; } | undefined): NDArraynp.tensordot
#np.tensordot(a, b, [options]) / np.linalg.tensordot(a, b, [options])
Sums products over the chosen axes: axes: N pairs the last N axes of a with the first N of b; axes: [axesA, axesB] lists them explicitly. Free axes of a come first in the result, then those of b.
Parameters
aArrayLike- An
NDArrayor nested JS array. bArrayLike- An
NDArrayor nested JS array. [options.axes]number | [number | number[], number | number[]]- Axes to contract. Default
2.
Returns
NDArray
Example
np.tensordot([[1, 2], [3, 4]], [[1, 2], [3, 4]]).item(); // => 30
np.tensordot([1, 2], [3, 4], { axes: 0 }); // => [[3, 4], [6, 8]]TypeScript declaration
np.tensordot(a: ArrayLike, b: ArrayLike, opts?: TensordotOptions | undefined): NDArray
np.linalg.tensordot(a: ArrayLike, b: ArrayLike, opts?: TensordotOptions | undefined): NDArraynp.linalg.multiDot
#np.linalg.multiDot(arrays)
Chained dot of two or more arrays in the cheapest order (matrix-chain dynamic programming). The first and last may be 1-D; the others must be 2-D.
Parameters
arraysArrayLike[]- At least two arrays.
Returns
NDArray
Example
np.linalg.multiDot([[1, 2], [[1, 0], [0, 1]], [3, 4]]).item(); // => 11TypeScript declaration
np.linalg.multiDot(arrays: readonly ArrayLike[]): NDArraynp.vecdot
#np.vecdot(x1, x2, [options]) / np.linalg.vecdot(x1, x2, [options])
Vector dot product along axis, conjugating x1 (complex): sum(conj(x1) * x2, axis). The other axes broadcast.
Parameters
x1ArrayLike- An
NDArrayor nested JS array. x2ArrayLike- An
NDArrayor nested JS array. [options.axis]number- Vector axis. Default
-1.
Returns
NDArray
Example
np.vecdot([[1, 2], [3, 4]], [1, 1]); // => [3, 7]TypeScript declaration
np.vecdot(x1: ArrayLike, x2: ArrayLike, opts?: VecdotOptions | undefined): NDArray
np.linalg.vecdot(x1: ArrayLike, x2: ArrayLike, opts?: VecdotOptions | undefined): NDArraynp.matvec
#np.matvec(x1, x2)
Matrix-vector product over the last axes of x1 (..., M, N) and x2 (..., N), broadcasting the batch axes. Result (..., M).
Parameters
x1ArrayLike- Matrix or stack of matrices
(..., M, N). x2ArrayLike- An
NDArrayor nested JS array.
Returns
NDArray
Example
np.matvec([[1, 2], [3, 4]], [1, 1]); // => [3, 7]TypeScript declaration
np.matvec(x1: ArrayLike, x2: ArrayLike): NDArraynp.vecmat
#np.vecmat(x1, x2)
Vector-matrix product of x1 (..., N) (conjugated if complex) and x2 (..., N, M), broadcasting the batch axes. Result (..., M).
Parameters
x1ArrayLike- An
NDArrayor nested JS array. x2ArrayLike- Matrix or stack of matrices
(..., M, N).
Returns
NDArray
Example
np.vecmat([1, 1], [[1, 2], [3, 4]]); // => [4, 6]TypeScript declaration
np.vecmat(x1: ArrayLike, x2: ArrayLike): NDArraynp.einsum
#np.einsum(subscripts, ...operands, [options]) / np.einsum(op0, sub0, op1, sub1, ..., [outSub], [options])
Einstein summation. Supports explicit (->) and implicit output (labels seen once, sorted), ... broadcasting, repeated labels (diagonals) and the sublist form (integers 0–51 and np.ellipsis). optimize picks the pairwise contraction order (false, true/"greedy", "optimal", an einsumPath path, or [name, memoryLimit]). Integer results wrap as in NumPy. The result is always a new array.
Parameters
subscriptsstring- Labels, e.g.
"ij,jk->ik". ...operandsArrayLike[]- Input arrays.
[options.optimize]boolean | string | Array- Contraction order. Default
false.
Returns
NDArray
Example
np.einsum("ij,jk->ik", [[1, 2], [3, 4]], [[1, 0], [0, 1]]); // => [[1, 2], [3, 4]]
np.einsum("ii", [[1, 2], [3, 4]]).item(); // => 5
np.einsum([[1, 2], [3, 4]], [0, 1], [1, 0]); // => [[1, 3], [2, 4]]TypeScript declaration
np.einsum(subscripts: string, ...operands: (ArrayLike | EinsumOptions)[]): NDArray
np.einsum(...args: (ArrayLike | EinsumOptions | Sublist)[]): NDArraynp.einsumPath
#np.einsumPath(subscripts, ...operands, [options])
Cheapest contraction order for an einsum expression, as [path, report]: path is ["einsum_path", [i, j], ...] (pass it as optimize) and report is NumPy's text summary. optimize defaults to "greedy".
Parameters
subscriptsstring- Labels.
...operandsArrayLike[]- Input arrays (only shapes are used).
[options.optimize]boolean | string | Array- Path algorithm. Default
"greedy".
Returns
[EinsumPath, string]
Example
np.einsumPath("ij,jk,kl->il", np.ones([2, 2]), np.ones([2, 5]), np.ones([5, 2]))[0]; // => ["einsum_path", [1, 2], [0, 1]]TypeScript declaration
np.einsumPath(subscripts: string, ...operands: (ArrayLike | EinsumOptions)[]): [EinsumPath, string]
np.einsumPath(...args: (ArrayLike | EinsumOptions | Sublist)[]): [EinsumPath, string]