Cyforanumera
npm

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.

NameSummary
np.linalg.detDeterminant of a square matrix, or of each matrix in a batch.
np.linalg.invMatrix inverse.
np.linalg.solveSolves a @ x = b for x.
np.linalg.eigEigen-decomposition of a general square matrix.
np.linalg.eighEigen-decomposition of a symmetric/Hermitian matrix (real or complex), using the lower triangle.
np.linalg.svdSingular value decomposition a = U @ diag(S) @ Vh.
np.linalg.qrQR factorization a = Q @ R.
np.linalg.lstsqLeast-squares solution of a @ x ≈ b, computed with SVD.
np.linalg.normVector or matrix norm.
np.linalg.backendName of the active native LAPACK backend: "accelerate" (macOS) or "fallback".
np.linalg.choleskyCholesky factor of a Hermitian positive-definite matrix (stack): lower L with a = L Lᴴ, or upper U with a = Uᴴ U.
np.linalg.slogdetSign and natural log of the absolute determinant, robust against overflow.
np.linalg.svdvalsSingular values in descending order (same as svd(x, { computeUV: false }).S).
np.linalg.matrixPowerRaises a square matrix (stack) to the integer power n by repeated squaring.
np.linalg.pinvMoore–Penrose pseudo-inverse of a matrix (stack) via SVD, or via eigh when hermitian.
np.linalg.matrixRankRank of a matrix (stack): the number of singular values above the threshold.
np.linalg.condCondition number of a matrix (stack).
np.linalg.vectorNormVector norm over one axis, several axes (treated as one flattened vector), or the whole array (axis: null, the default).
np.linalg.matrixNormMatrix norm over the last two axes of x (stack).
np.linalg.matrixTransposeSwaps the last two axes (a view).
np.linalg.diagonalDiagonals of the trailing matrices (np.diagonal with axis1 = -2, axis2 = -1).
np.linalg.traceTraces of the trailing matrices (np.trace with axis1 = -2, axis2 = -1).
np.linalg.outerOuter product of two 1-D arrays.
np.linalg.tensorinvInverse of an N-d array with respect to tensordot(·, ·, ind).
np.linalg.tensorsolveSolves tensordot(a, x, x.ndim) = b for x, where x.shape = a.shape[b.ndim:].
np.vdotDot product of a and b flattened to 1-D, conjugating a first (complex input).
np.kronKronecker product: a block array whose blocks are a[i, j, …] * b.
np.crossCross product of (broadcast arrays of) 3-element vectors.
np.tensordotSums 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.multiDotChained dot of two or more arrays in the cheapest order (matrix-chain dynamic programming).
np.vecdotVector dot product along axis, conjugating x1 (complex): sum(conj(x1) * x2, axis).
np.matvecMatrix-vector product over the last axes of x1 (..., M, N) and x2 (..., N), broadcasting the batch axes.
np.vecmatVector-matrix product of x1 (..., N) (conjugated if complex) and x2 (..., N, M), broadcasting the batch axes.
np.einsumEinstein summation.
np.einsumPathCheapest 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

TypeScript
np.linalg.det([[3, 1], [1, 2]]).item(); // => 5
TypeScript declaration
np.linalg.det(a: ArrayLike): NDArray

np.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

TypeScript
np.linalg.inv([[4, 7], [2, 6]]); // => [[0.6, -0.7], [-0.2, 0.4]]
TypeScript declaration
np.linalg.inv(a: ArrayLike): NDArray

np.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

TypeScript
np.linalg.solve([[3, 1], [1, 2]], [9, 8]); // => [2, 3]
TypeScript declaration
np.linalg.solve(a: ArrayLike, b: ArrayLike): NDArray

np.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

TypeScript
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): NDArray

np.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

TypeScript
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): NDArray

np.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. Default true.
[options.computeUV]boolean
If false, U and Vh are null. Default true.

Returns

{ U: NDArray | null, S: NDArray, Vh: NDArray | null }

Example

TypeScript
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): SvdResult

np.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

TypeScript
np.linalg.qr([[1, 2], [3, 4], [5, 6]]).R.shape; // => [2, 2]
np.linalg.qr([[1, 2], [3, 4]], "r").Q;          // => null
TypeScript declaration
np.linalg.qr(a: ArrayLike, mode?: QrMode | undefined): QrResult

np.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

TypeScript
// 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; // => 2
TypeScript declaration
np.linalg.lstsq(a: ArrayLike, b: ArrayLike, rcond?: number | null | undefined): LstsqResult

np.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

TypeScript
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): NDArray

np.linalg.backend

#
np.linalg.backend()

Name of the active native LAPACK backend: "accelerate" (macOS) or "fallback".

Returns

string

Example

TypeScript
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

TypeScript
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): NDArray

np.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

TypeScript
np.linalg.slogdet([[1, 2], [3, 4]]).sign.item();     // => -1
np.linalg.slogdet([[2, 0], [0, 4]]).logabsdet.item(); // => 2.0794415416798357
TypeScript declaration
np.linalg.slogdet(a: ArrayLike): SlogdetResult

np.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

TypeScript
np.linalg.svdvals([[3, 0], [0, 4]]); // => [4, 3]
TypeScript declaration
np.linalg.svdvals(x: ArrayLike): NDArray

np.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

TypeScript
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): NDArray

np.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; null means max(M, N) * eps. Cannot be combined with rcond.
[options.hermitian]boolean
Treat a as Hermitian. Default false.

Returns

NDArray

Example

TypeScript
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): NDArray

np.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. Default false.

Returns

NDArray

Example

TypeScript
np.linalg.matrixRank([[1, 2], [2, 4]]).item(); // => 1
np.linalg.matrixRank(np.eye(3)).item();        // => 3
TypeScript declaration
np.linalg.matrixRank(A: ArrayLike, opts?: MatrixRankOptions | undefined): NDArray

np.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

TypeScript
np.linalg.cond([[1, 0], [0, 2]]).item();        // => 2
np.linalg.cond([[4, 0], [0, 2]], 1).item();     // => 2
TypeScript declaration
np.linalg.cond(x: ArrayLike, p?: NormOrder | undefined): NDArray

np.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 NDArray or 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

TypeScript
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): NDArray

np.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

TypeScript
np.linalg.matrixNorm([[3, 0], [0, 4]]).item();            // => 5
np.linalg.matrixNorm([[1, 2], [3, 4]], { ord: 1 }).item(); // => 6
TypeScript declaration
np.linalg.matrixNorm(x: ArrayLike, opts?: MatrixNormOptions | undefined): NDArray

np.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

TypeScript
np.linalg.matrixTranspose([[1, 2, 3]]); // => [[1], [2], [3]]
TypeScript declaration
np.linalg.matrixTranspose(x: ArrayLike): NDArray

np.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

TypeScript
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): NDArray

np.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

TypeScript
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): NDArray

np.linalg.outer

#
np.linalg.outer(x1, x2)

Outer product of two 1-D arrays. Unlike np.outer, other dimensions raise ValueError.

Parameters

x1ArrayLike
An NDArray or nested JS array.
x2ArrayLike
An NDArray or nested JS array.

Returns

NDArray

Example

TypeScript
np.linalg.outer([1, 2], [3, 4]); // => [[3, 4], [6, 8]]
TypeScript declaration
np.linalg.outer(x1: ArrayLike, x2: ArrayLike): NDArray

np.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 NDArray or nested JS array.
[options.ind]number
Number of leading indices. Default 2.

Returns

NDArray

Example

TypeScript
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): NDArray

np.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 NDArray or nested JS array.
bArrayLike
An NDArray or nested JS array.
[options.axes]number[]
Axes of a moved to the end before solving.

Returns

NDArray

Example

TypeScript
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): NDArray

np.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 NDArray or nested JS array.
bArrayLike
An NDArray or nested JS array.

Returns

NDArray (0-d)

Example

TypeScript
np.vdot([[1, 2], [3, 4]], [[1, 1], [1, 1]]).item(); // => 10
TypeScript declaration
np.vdot(a: ArrayLike, b: ArrayLike): NDArray

np.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 NDArray or nested JS array.
bArrayLike
An NDArray or nested JS array.

Returns

NDArray

Example

TypeScript
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): NDArray

np.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 NDArray or nested JS array.
bArrayLike
An NDArray or 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

TypeScript
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): NDArray

np.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 NDArray or nested JS array.
bArrayLike
An NDArray or nested JS array.
[options.axes]number | [number | number[], number | number[]]
Axes to contract. Default 2.

Returns

NDArray

Example

TypeScript
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): NDArray

np.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

TypeScript
np.linalg.multiDot([[1, 2], [[1, 0], [0, 1]], [3, 4]]).item(); // => 11
TypeScript declaration
np.linalg.multiDot(arrays: readonly ArrayLike[]): NDArray

np.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 NDArray or nested JS array.
x2ArrayLike
An NDArray or nested JS array.
[options.axis]number
Vector axis. Default -1.

Returns

NDArray

Example

TypeScript
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): NDArray

np.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 NDArray or nested JS array.

Returns

NDArray

Example

TypeScript
np.matvec([[1, 2], [3, 4]], [1, 1]); // => [3, 7]
TypeScript declaration
np.matvec(x1: ArrayLike, x2: ArrayLike): NDArray

np.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 NDArray or nested JS array.
x2ArrayLike
Matrix or stack of matrices (..., M, N).

Returns

NDArray

Example

TypeScript
np.vecmat([1, 1], [[1, 2], [3, 4]]); // => [4, 6]
TypeScript declaration
np.vecmat(x1: ArrayLike, x2: ArrayLike): NDArray

np.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

TypeScript
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)[]): NDArray

np.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

TypeScript
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]