linalg. live under the linalg namespace (e.g., np.linalg.norm(...)). Functions without the prefix are top-level (e.g., np.trace(...)).
linalg.norm
Compute the norm of a matrix or vector. The behavior depends on theord parameter and the dimensions of the input.
| Name | Type | Default | Description |
|---|---|---|---|
x | ArrayLike | — | Input array. |
ord | number | 'fro' | 'nuc' | undefined | Order of the norm. For vectors: any numeric value, Infinity, or -Infinity. For matrices: 1, -1, 2, -2, Infinity, -Infinity, 'fro' (Frobenius), or 'nuc' (nuclear). Default is Frobenius for matrices, 2-norm for vectors. |
axis | number | [number, number] | undefined | Axis or axes along which to compute the norm. If undefined, the norm of the flattened array (vector) or the matrix norm is computed. |
keepdims | boolean | false | If true, the axes over which the norm is taken are left as size-1 dimensions. |
number | NDArray — Scalar when computing the norm of the entire array; NDArray when computing along axes.
linalg.matrix_norm
Compute a matrix norm. This is a convenience function equivalent tolinalg.norm with a 2-D input.
| Name | Type | Default | Description |
|---|---|---|---|
x | ArrayLike | — | Input matrix (2-D). |
ord | number | 'fro' | 'nuc' | 'fro' | Matrix norm order. Supported values: 1, -1, 2, -2, Infinity, -Infinity, 'fro', 'nuc'. |
keepdims | boolean | false | If true, the result has shape [1, 1] instead of being a scalar. |
number | NDArray — The matrix norm.
linalg.vector_norm
Compute a vector norm. This is a convenience function for computing norms along specific axes.| Name | Type | Default | Description |
|---|---|---|---|
x | ArrayLike | — | Input array. |
ord | number | 2 | Order of the norm (e.g., 1, 2, Infinity, -Infinity, 0). |
axis | number | number[] | undefined | Axis or axes along which to compute. If undefined, uses the flattened array. |
keepdims | boolean | false | If true, reduced axes are kept as size-1 dimensions. |
number | NDArray — The vector norm.
linalg.cond
Compute the condition number of a matrix. A large condition number indicates the matrix is close to singular.| Name | Type | Default | Description |
|---|---|---|---|
a | ArrayLike | — | Input matrix. |
p | number | 'fro' | '-fro' | 'inf' | '-inf' | undefined | Norm type used in the condition number computation. Default uses the 2-norm (ratio of largest to smallest singular value). |
number — The condition number.
linalg.det
Compute the determinant of a square matrix.| Name | Type | Default | Description |
|---|---|---|---|
a | ArrayLike | — | Input square matrix. |
number — The determinant.
linalg.slogdet
Compute the sign and natural logarithm of the determinant. This is more numerically stable than computingdet directly for matrices with very large or very small determinants.
| Name | Type | Default | Description |
|---|---|---|---|
a | ArrayLike | — | Input square matrix. |
{ sign, logabsdet } where sign is -1, 0, or 1 and logabsdet is the natural log of the absolute value of the determinant. The determinant is sign * exp(logabsdet).
linalg.matrix_rank
Compute the rank of a matrix using SVD. Singular values below a threshold are treated as zero.| Name | Type | Default | Description |
|---|---|---|---|
a | ArrayLike | — | Input matrix. |
tol | number | undefined | Threshold below which singular values are considered zero. Default is max(M, N) * eps * max(sv). |
number — The effective rank.
trace
Return the sum along diagonals of the array. For a 2-D array, this returns the sum of the diagonal elements. For higher-dimensional arrays, the axes along which the diagonal is taken can be specified. Also available asnp.linalg.trace(...).
| Name | Type | Default | Description |
|---|---|---|---|
a | ArrayLike | — | Input array. |
offset | number | 0 | Offset of the diagonal from the main diagonal. Positive means above, negative means below. |
axis1 | number | 0 | First axis of the 2-D sub-arrays from which the diagonals are taken. |
axis2 | number | 1 | Second axis of the 2-D sub-arrays from which the diagonals are taken. |
number | NDArray — Scalar for 2-D input, NDArray for higher-dimensional input.
diagonal
Return the specified diagonal of an array. For a 2-D array, returns the diagonal elements. For higher-dimensional arrays, the diagonal is taken over the specified axes. Also available asnp.linalg.diagonal(...).
| Name | Type | Default | Description |
|---|---|---|---|
a | ArrayLike | — | Input array (must be at least 2-D). |
offset | number | 0 | Offset of the diagonal from the main diagonal. |
axis1 | number | 0 | First axis of the 2-D sub-array from which the diagonal is taken. |
axis2 | number | 1 | Second axis of the 2-D sub-array from which the diagonal is taken. |
NDArray — The extracted diagonal.
cross
Compute the cross product of two vectors. For 3-D vectors, this returns a 3-D vector. For 2-D vectors, this returns the scalarz-component.
Also available as np.linalg.cross(...).
| Name | Type | Default | Description |
|---|---|---|---|
a | ArrayLike | — | First input array. Components along the cross-product axis must have length 2 or 3. |
b | ArrayLike | — | Second input array. Components along the cross-product axis must have length 2 or 3. |
axisa | number | -1 | Axis of a that defines the vector(s). |
axisb | number | -1 | Axis of b that defines the vector(s). |
axisc | number | -1 | Axis of the result containing the cross product vector(s). |
axis | number | undefined | If set, overrides axisa, axisb, and axisc. |
NDArray — The cross product. For 3-D inputs: a 3-D vector. For 2-D inputs: a scalar (the z-component).