Table of Contents

Class LinalgEngine

Namespace
NumSharp
Assembly
NumSharp.dll

The contract of numpy.linalg: one member per function that computes (29 of NumPy's 31; the views diagonal and matrix_transpose are the array object's), plus a second C# form where NumPy's argument is an int or a tuple (Tensordot(NDArray, NDArray, int[], int[]), VectorNorm(NDArray, int, bool, object)). An engine reaches it through Linalg, and every public np.linalg function calls one member (woven: a numpy.matrix or numpy.char.chararray operand arrives as its plain view, and the declared mechanism answers for the class).

public abstract class LinalgEngine
Inheritance
LinalgEngine
Derived
Inherited Members

Remarks

The default is DefaultLinalgEngine: NumPy's statements in NumPy's order (the argument checks, _commonType's dtype rule, the gufuncs' text refusals and each function's tail) over ten kernels that stand for the _umath_linalg gufuncs and ask Blas first, so the bundled OpenBLAS of NumSharp.Interop.OpenBLAS needs no subclass.

Providing one. Derive from DefaultLinalgEngine and override its kernels to compute the factorisations another way while NumPy's statements stay; derive from this class to replace whole functions. Assign the instance to Linalg of the cached engine: it reaches every array bound to that engine, existing ones included.

Operands. A member receives plain arrays: the np.linalg functions decide the numpy.matrix and numpy.char.chararray classes before they call it (wrapping its result where NumPy returns the class), so an override never sees those classes from them. A member called directly computes with whatever it is given.

Results are NumPy's objects: dtype (float64 for bool / integer operands, float32 kept, complex stays complex128; NumSharp has no complex64), shape, memory order and ownership, and NumPy's 0-d scalar stand-ins (read-only 0-d arrays) where NumPy returns a scalar.

Methods

Cholesky(NDArray, bool)

NumPy's np.linalg.cholesky(a, upper=False): the Cholesky factor of a Hermitian positive-definite matrix, or of each matrix in a stack.

public abstract NDArray Cholesky(NDArray a, bool upper = false)

Parameters

a NDArray

The matrix or stack (the last two axes, square). Only the lower triangle is read, so a non-Hermitian operand is factorised as its own reflection rather than rejected.

upper bool

False (NumPy's default) for the lower factor L with a = L L*; true for the upper U with a = U* U.

Returns

NDArray

The triangular factor, a fresh array of the loop dtype (float64 for bool / integer operands).

Exceptions

LinAlgError

Fewer than two axes, a non-square operand, or a matrix that is not positive definite (Matrix is not positive definite).

TypeError

A float16, decimal or char operand (array type float16 is unsupported in linalg).

TypeError

A text operand (NumPy's gufunc input-cast refusal, a UFuncTypeError).

OpenBlasMissingBackendException

A non-empty operand that no matrix backend serves (an empty operand or a zero-size batch needs none).

Cond(NDArray, object)

NumPy's np.linalg.cond(x, p=None): the condition number of a matrix in the given norm, or of each matrix in a stack.

public abstract NDArray Cond(NDArray x, object p = null)

Parameters

x NDArray

The matrix or stack.

p object

The order: null (NumPy's default) and ±2 from the singular values; ±1, ±inf and "fro" / "nuc" as norm(x, p) * norm(inv(x), p).

Returns

NDArray

The condition number, real: a 0-d array for one matrix (NumPy's scalar), one per matrix of a stack; inf for a singular matrix, NaN only where the matrix itself holds a NaN.

Remarks

The inverse runs in float64 (complex128 for a complex operand) whatever the result dtype, and a singular matrix's inverse is NaN rather than an error, as NumPy computes it under errstate(all='ignore').

Exceptions

LinAlgError

An empty operand (cond is not defined on empty arrays), a non-square one for an inverse-based order, or the nuclear norm of a singular matrix (the SVD of its NaN inverse: SVD did not converge).

TypeError

An array or list order (NumPy's p in {2, -2} hashes it: unhashable type: 'numpy.ndarray' / 'list'); a float16, decimal or char operand.

ValueError

An order the norm refuses (Invalid norm order for matrices.).

TypeError

A text operand (NumPy's gufunc input-cast refusal).

OpenBlasMissingBackendException

An SVD-based order (null, ±2, "nuc") over a non-empty operand that no matrix backend serves. The inverse-based orders need none: the managed LU inverts.

Cross(NDArray, NDArray, int)

NumPy's np.linalg.cross(x1, x2, axis=-1), the Array-API cross product: stricter than cross(NDArray, NDArray, int, int, int, int?), it takes 3-vectors only, over one axis shared by both operands and the result.

public abstract NDArray Cross(NDArray x1, NDArray x2, int axis = -1)

Parameters

x1 NDArray

The first operand.

x2 NDArray

The second operand.

axis int

The axis holding the three components, in both operands and in the result (negative counts from the end).

Returns

NDArray

np.cross's result: promote_types(x1.dtype, x2.dtype) (a strong promotion), a view of a fresh C-ordered array whose component axis is moved to axis.

Exceptions

IndexError

axis is out of range for either operand (tuple index out of range: NumPy indexes x1.shape[axis] first).

ValueError

The component axis of either operand does not have length 3, or the other axes do not broadcast.

TypeError

Either operand holds text (the multiply has no string loop).

Det(NDArray)

NumPy's np.linalg.det(a): the determinant of a matrix, or of each matrix in a stack, from an LU factorisation (getrf), so a large or ill-conditioned matrix can overflow where Slogdet(NDArray) stays finite.

public abstract NDArray Det(NDArray a)

Parameters

a NDArray

The square matrix or stack.

Returns

NDArray

The determinant in the loop dtype: NumPy's scalar (a read-only 0-d array) for one matrix, one value per matrix of a stack.

Remarks

Needs no backend: without one the managed LU computes it, allclose to NumPy's blocked LAPACK (a matrix whose zero pivot appears only after several eliminations can give a tiny value where LAPACK gives 0).

Exceptions

LinAlgError

Fewer than two axes, or a non-square operand.

TypeError

A float16, decimal or char operand.

TypeError

A text operand (NumPy's gufunc input-cast refusal).

Eig(NDArray)

NumPy's np.linalg.eig(a): the eigenvalues and right eigenvectors of a general (not necessarily symmetric) matrix, or of each matrix in a stack.

public abstract (NDArray eigenvalues, NDArray eigenvectors) Eig(NDArray a)

Parameters

a NDArray

The square matrix or stack; it must hold no inf or NaN.

Returns

(NDArray Lhs, NDArray Rhs)

(eigenvalues, eigenvectors), the columns of the second being the vectors. Always complex128 (NumPy 2.5: a real matrix with real eigenvalues gives zero imaginary parts, where NumPy 2.4 returned a real dtype); a float32 operand's result carries NumPy's complex64 values.

Exceptions

LinAlgError

Fewer than two axes, a non-square operand, an operand holding inf / NaN (Array must not contain infs or NaNs), or no convergence.

TypeError

A text operand (isfinite has no string loop), or a float16, decimal or char operand.

OpenBlasMissingBackendException

A non-empty operand that no matrix backend serves.

Eigh(NDArray, char)

NumPy's np.linalg.eigh(a, UPLO='L'): the eigenvalues and eigenvectors of a real symmetric or complex Hermitian matrix, or of each matrix in a stack. Faster than Eig(NDArray) and always real.

public abstract (NDArray eigenvalues, NDArray eigenvectors) Eigh(NDArray a, char UPLO = 'L')

Parameters

a NDArray

The symmetric / Hermitian matrix or stack.

UPLO char

The triangle that holds the data, 'L' (NumPy's default) or 'U', either case. The other triangle is NOT read, so a non-symmetric operand is interpreted as its own reflection.

Returns

(NDArray Lhs, NDArray Rhs)

(eigenvalues, eigenvectors): the eigenvalues ascending and real (the real counterpart dtype for a complex operand).

Exceptions

ValueError

UPLO is neither 'L' nor 'U' (UPLO argument must be 'L' or 'U', checked before the operand).

LinAlgError

Fewer than two axes, a non-square operand, or no convergence.

TypeError

A float16, decimal or char operand.

TypeError

A text operand (NumPy's gufunc input-cast refusal).

OpenBlasMissingBackendException

A non-empty operand that no matrix backend serves.

Eigvals(NDArray)

NumPy's np.linalg.eigvals(a): Eig(NDArray)'s eigenvalues without the eigenvectors.

public abstract NDArray Eigvals(NDArray a)

Parameters

a NDArray

The square matrix or stack; it must hold no inf or NaN.

Returns

NDArray

The eigenvalues, always complex128 (NumPy 2.5), as Eig(NDArray)'s.

Exceptions

LinAlgError

Fewer than two axes, a non-square operand, an operand holding inf / NaN, or no convergence.

TypeError

A text operand, or a float16, decimal or char operand.

OpenBlasMissingBackendException

A non-empty operand that no matrix backend serves.

Eigvalsh(NDArray, char)

NumPy's np.linalg.eigvalsh(a, UPLO='L'): Eigh(NDArray, char)'s eigenvalues without the eigenvectors. LAPACK computes them another way when it computes no vectors, so they can differ from Eigh's in the last bits, in NumPy as here.

public abstract NDArray Eigvalsh(NDArray a, char UPLO = 'L')

Parameters

a NDArray

The symmetric / Hermitian matrix or stack.

UPLO char

The triangle that holds the data, 'L' or 'U', either case.

Returns

NDArray

The eigenvalues, ascending and real.

Exceptions

ValueError

UPLO is neither 'L' nor 'U'.

LinAlgError

Fewer than two axes, a non-square operand, or no convergence.

TypeError

A float16, decimal or char operand.

TypeError

A text operand (NumPy's gufunc input-cast refusal).

OpenBlasMissingBackendException

A non-empty operand that no matrix backend serves.

Inv(NDArray)

NumPy's np.linalg.inv(a): the multiplicative inverse of a matrix, or of each matrix in a stack, by solving a x = I (gesv), so a singular operand raises from the factorisation, not from a determinant test.

public abstract NDArray Inv(NDArray a)

Parameters

a NDArray

The square matrix or stack.

Returns

NDArray

The inverse, a fresh array of the loop dtype (float64 for bool / integer operands).

Remarks

Needs no backend: without one the managed LU computes it.

Exceptions

LinAlgError

Fewer than two axes, a non-square operand, or a singular matrix (Singular matrix).

TypeError

A float16, decimal or char operand.

TypeError

A text operand (NumPy's gufunc input-cast refusal).

Lstsq(NDArray, NDArray, double?)

NumPy's np.linalg.lstsq(a, b, rcond=None): the least-squares solution to a x = b. It takes a single matrix, not a stack.

public abstract (NDArray Solution, NDArray Residuals, NDArray Rank, NDArray SingularValues) Lstsq(NDArray a, NDArray b, double? rcond = null)

Parameters

a NDArray

The coefficient matrix (M, N), exactly two-dimensional.

b NDArray

The ordinate values, (M,) or (M, K).

rcond double?

Singular values below rcond times the largest are treated as zero; null for NumPy's eps * max(M, N) (float64's eps, the compute type's).

Returns

(NDArray, NDArray, NDArray, NDArray)

NumPy's four-tuple (solution, residuals, rank, singularValues): the residuals EMPTY unless the system is overdetermined and of full rank; the rank NumPy's scalar.

Exceptions

LinAlgError

An operand that is not two-dimensional (after a 1-D b became a column), b's rows differ from a's (Incompatible dimensions), or no convergence.

TypeError

A float16, decimal or char operand.

TypeError

A text operand (NumPy's gufunc input-cast refusal).

OpenBlasMissingBackendException

A non-empty operand that no matrix backend serves.

Matmul(NDArray, NDArray)

NumPy's np.linalg.matmul(x1, x2), the Array-API spelling of matmul(NDArray, NDArray, NDArray, int[][], int?, bool?, DType, string, char): the same operation with every keyword at its default.

public abstract NDArray Matmul(NDArray x1, NDArray x2)

Parameters

x1 NDArray

The left operand (at least one-dimensional).

x2 NDArray

The right operand (at least one-dimensional).

Returns

NDArray

np.matmul's product: a fresh array that owns its data, NumPy's 0-d scalar stand-in for two vectors.

Exceptions

ValueError

A 0-d operand, or core dimensions that do not match, or loop axes that do not broadcast (NumPy's gufunc texts).

TypeError

Text operands (matmul has no string loop).

NullReferenceException

An operand is null.

MatrixNorm(NDArray, bool, object)

NumPy's np.linalg.matrix_norm(x, keepdims=False, ord='fro'), the Array-API matrix norm: always over the LAST TWO axes.

public abstract NDArray MatrixNorm(NDArray x, bool keepdims = false, object ord = null)

Parameters

x NDArray

The matrix or stack.

keepdims bool

Leave the two reduced axes in the result with length 1.

ord object

The matrix order; null stands for NumPy's default "fro" (C# cannot spell a non-null default for an object parameter).

Returns

NDArray

The matrix norm over the last two axes.

Exceptions

ValueError

An invalid matrix order, or a min order of an empty matrix.

AxisError

An operand of fewer than two axes.

TypeError

An order that is neither null, a number nor a string.

OpenBlasMissingBackendException

An order 2, -2 or "nuc" over a non-empty matrix with no matrix backend installed.

MatrixPower(NDArray, int)

NumPy's np.linalg.matrix_power(a, n): a square matrix (or each of a stack) raised to an integer power by binary exponentiation; a negative power inverts first.

public abstract NDArray MatrixPower(NDArray a, int n)

Parameters

a NDArray

The square matrix or stack (the last two axes).

n int

Any integer: 0 gives the identity in a's dtype and memory order.

Returns

NDArray

The power. Footgun (NumPy's own): n == 1 returns a ITSELF, so writing to the result writes to a; n == -1 returns the fresh inverse.

Exceptions

LinAlgError

Fewer than two axes, a non-square operand, or a negative power of a singular matrix.

TypeError

A text matrix with n of 2 or more (matmul has no string loop), or with a negative n (inv's input cast).

MatrixRank(NDArray, double?, bool, double?)

NumPy's np.linalg.matrix_rank(A, tol=None, hermitian=False, rtol=None): the number of singular values above the tolerance, per matrix.

public abstract NDArray MatrixRank(NDArray A, double? tol = null, bool hermitian = false, double? rtol = null)

Parameters

A NDArray

The matrix or stack. Below two axes NumPy's answer is a PREDICATE, int(not all(A == 0)): matrix_rank([1, 2, 3]) is 1, an empty operand 0.

tol double?

The absolute threshold; null for S.max() * max(M, N) * eps (eps of the singular values' dtype).

hermitian bool

Take the singular values from the Hermitian (real symmetric) matrix whose lower triangle A holds (eigvalsh's square check applies).

rtol double?

The relative threshold (the Array-API spelling).

Returns

NDArray

The rank: an int64 0-d array for one matrix (or below two axes), one per matrix of a stack.

Exceptions

ValueError

Both tol and rtol are set.

LinAlgError

A non-square operand with hermitian.

TypeError

A float16, decimal or char operand.

TypeError

A text operand of two or more axes (NumPy's gufunc input-cast refusal).

OpenBlasMissingBackendException

A non-empty operand of two or more axes that no matrix backend serves.

MultiDot(NDArray[], NDArray)

NumPy's np.linalg.multi_dot(arrays, out=None): a chained matrix product in the cheapest association order (the matrix-chain dynamic program; a direct comparison for three).

public abstract NDArray MultiDot(NDArray[] arrays, NDArray @out = null)

Parameters

arrays NDArray[]

Two or more arrays. With three or more, every one but the ends must be 2-D; a 1-D first operand is a row and a 1-D last one a column, the added axis removed again afterwards. Two arrays are a plain np.dot.

out NDArray

The final product's out array (np.dot's strict out: the product's exact dtype and rank, C-contiguous, the TWO-dimensional product's shape), or null.

Returns

NDArray

The product: out itself when given (a two-array chain, or no vector end); a view of it raveled when one end was a vector; NumPy's scalar stand-in when both were; otherwise a fresh array.

Exceptions

ValueError

Fewer than two arrays (Expecting at least two arrays.), or an out array np.dot refuses.

IncorrectShapeException

Misaligned operands (np.dot's shapes (…) and (…) not aligned).

LinAlgError

With three or more arrays, an operand other than a vector end that is not two-dimensional.

Norm(NDArray, object, int[], bool)

NumPy's np.linalg.norm(x, ord=None, axis=None, keepdims=False): a matrix or vector norm. The number of reduced axes decides the vocabulary of orders: one axis is a vector norm, two a matrix norm.

public abstract NDArray Norm(NDArray x, object ord = null, int[] axis = null, bool keepdims = false)

Parameters

x NDArray

The operand: integer, bool and text (parsed by astype(float)) operands compute in float64.

ord object

The order: null (Frobenius for a matrix, 2 for a vector), a number (an infinity included), or "fro" / "f" / "nuc".

axis int[]

The reduced axes (one or two), or null for every axis (NumPy's int axis is a one-element array here).

keepdims bool

Leave the reduced axes in the result with length 1.

Returns

NDArray

The norm: a 0-d array for a whole-array norm, else the reduced array. An empty operand's inf-, 1- and 2-norms are 0.0 (NumPy 2.x reduces those with initial=0).

Exceptions

ValueError

An order that is invalid for the number of reduced axes, duplicate axes, more than two reduced axes (Improper number of dimensions to norm.), a min order of an empty operand, or a text element that does not parse as a float.

AxisError

An axis out of range.

TypeError

An order that is neither null, a number nor a string.

OpenBlasMissingBackendException

A matrix order 2, -2 or "nuc" over a non-empty matrix with no matrix backend installed; every other order is a reduction and needs none.

Outer(NDArray, NDArray)

NumPy's np.linalg.outer(x1, x2): the outer product of two VECTORS. Not outer(NDArray, NDArray, NDArray), which flattens whatever it is given; this one refuses anything but 1-D.

public abstract NDArray Outer(NDArray x1, NDArray x2)

Parameters

x1 NDArray

The first vector.

x2 NDArray

The second vector.

Returns

NDArray

The (len(x1), len(x2)) outer product, a fresh array.

Exceptions

ValueError

Either operand is not one-dimensional (Input arrays must be one-dimensional, but they are x1.ndim={n} and x2.ndim={m}.).

Pinv(NDArray, double?, bool, double?)

NumPy's np.linalg.pinv(a, rcond=None, hermitian=False, rtol=None): the Moore-Penrose pseudo-inverse, from the singular value decomposition.

public abstract NDArray Pinv(NDArray a, double? rcond = null, bool hermitian = false, double? rtol = null)

Parameters

a NDArray

The matrix or stack.

rcond double?

Singular values at or below rcond times the largest are treated as zero; with rtol null too, NumPy's 1e-15.

hermitian bool

Decompose the Hermitian (real symmetric) matrix whose lower triangle the conjugated operand holds, through the symmetric eigensolver.

rtol double?

The Array-API spelling of rcond; setting both is an error.

Returns

NDArray

The pseudo-inverse, shape (..., N, M): float64 / float32 / complex128 by the loop dtype, the operand's own dtype for an empty operand.

Remarks

C# cannot tell NumPy's rtol=_NoValue (1e-15) from rtol=None (the Array-API default max(M, N) * eps): with neither tolerance set, NumPy's 1e-15 applies.

Exceptions

ValueError

Both rcond and rtol are set, or an empty operand of fewer than two axes.

TypeError

A text operand (cannot conjugate non-numeric dtype), or a float16, decimal or char operand.

LinAlgError

Fewer than two axes, or a non-square operand with hermitian.

OpenBlasMissingBackendException

A non-empty operand that no matrix backend serves.

Qr(NDArray, string)

NumPy's np.linalg.qr(a, mode='reduced'): a = Q R with Q orthonormal and R upper triangular, per matrix of a stack.

public abstract (NDArray Q, NDArray R) Qr(NDArray a, string mode = "reduced")

Parameters

a NDArray

The matrix or stack.

mode string

"reduced" (Q (..., M, K), R (..., K, N), K = min(M, N)), "complete" (a square Q), "r" (R alone), "raw" (LAPACK's packed (h, tau)), or the legacy aliases NumPy still honours ("f" / "full" for reduced, "e" / "economic").

Returns

(NDArray Lhs, NDArray Rhs)

(Q, R): for "r" the Q slot is null, for "raw" the pair is (h, tau), for "economic" the packed matrix and null.

Exceptions

ValueError

An unknown or null mode (Unrecognized mode '...', checked before the operand).

LinAlgError

Fewer than two axes.

TypeError

A float16, decimal or char operand.

OpenBlasMissingBackendException

A non-empty operand that no matrix backend serves.

Slogdet(NDArray)

NumPy's np.linalg.slogdet(a): the sign and the natural log of the absolute determinant, the overflow-safe Det(NDArray).

public abstract (NDArray sign, NDArray logabsdet) Slogdet(NDArray a)

Parameters

a NDArray

The square matrix or stack.

Returns

(NDArray Lhs, NDArray Rhs)

(sign, logabsdet), such that sign * exp(logabsdet) is the determinant: NumPy's scalars for one matrix, arrays for a stack. A singular matrix gives (0, -inf); a complex operand a unit-modulus complex sign.

Remarks

Needs no backend: without one the managed LU computes it.

Exceptions

LinAlgError

Fewer than two axes, or a non-square operand.

TypeError

A float16, decimal or char operand.

TypeError

A text operand (NumPy's gufunc input-cast refusal).

Solve(NDArray, NDArray)

NumPy's np.linalg.solve(a, b): the solution of a x = b. b is a stack of vectors only when it is exactly 1-D (NumPy 2.0's rule); any other rank is a stack of matrices, so the two gufuncs are chosen by rank alone and report their own core-dimension errors.

public abstract NDArray Solve(NDArray a, NDArray b)

Parameters

a NDArray

The coefficient matrix or stack (..., M, M).

b NDArray

The ordinate values, (..., M) or (..., M, K); the stacked axes broadcast.

Returns

NDArray

The solution, a fresh array of the loop dtype (float64 for bool / integer operands).

Remarks

Needs no backend: without one the managed LU computes it.

Exceptions

LinAlgError

Fewer than two axes, a non-square a, or a singular matrix.

TypeError

A float16, decimal or char operand.

TypeError

A text operand (NumPy's gufunc input-cast refusal).

ValueError

A core dimension of b that does not match, a b of too few axes for the matrix gufunc, or stacked axes that do not broadcast (NumPy's gufunc texts).

Svd(NDArray, bool, bool, bool)

NumPy's np.linalg.svd(a, full_matrices=True, compute_uv=True, hermitian=False): the singular value decomposition a = U diag(S) Vh, per matrix of a stack; the routine behind pinv, matrix_rank, cond and the spectral / nuclear norms.

public abstract (NDArray U, NDArray S, NDArray Vh) Svd(NDArray a, bool full_matrices = true, bool compute_uv = true, bool hermitian = false)

Parameters

a NDArray

The matrix or stack.

full_matrices bool

True (NumPy's default) for square U and Vh, false for K = min(M, N) columns / rows; ignored with hermitian.

compute_uv bool

False for the singular values alone (U and Vh null).

hermitian bool

Decompose the Hermitian (real symmetric) matrix whose lower triangle a holds, through eigh / eigvalsh as NumPy does: the upper triangle is not read, so a non-Hermitian operand gives another matrix's factors without an error.

Returns

(NDArray, NDArray, NDArray)

(U, S, Vh), the singular values descending and real.

Exceptions

LinAlgError

Fewer than two axes, a non-square operand with hermitian, or no convergence.

TypeError

A float16, decimal or char operand.

TypeError

A text operand (NumPy's gufunc input-cast refusal).

OpenBlasMissingBackendException

A non-empty operand that no matrix backend serves.

Svdvals(NDArray)

NumPy's np.linalg.svdvals(x), the Array-API spelling of svd(x, compute_uv=False): the singular values alone.

public abstract NDArray Svdvals(NDArray x)

Parameters

x NDArray

The matrix or stack.

Returns

NDArray

The singular values, descending and real.

Exceptions

LinAlgError

Fewer than two axes, or no convergence.

TypeError

A float16, decimal or char operand.

TypeError

A text operand (NumPy's gufunc input-cast refusal).

OpenBlasMissingBackendException

A non-empty operand that no matrix backend serves.

Tensordot(NDArray, NDArray, int)

NumPy's np.linalg.tensordot(x1, x2, axes=2) with an integer axes, the Array-API spelling of tensordot(NDArray, NDArray, int): a sum product over the last axes axes of x1 and the first ones of x2.

public abstract NDArray Tensordot(NDArray x1, NDArray x2, int axes = 2)

Parameters

x1 NDArray

The first tensor.

x2 NDArray

The second tensor.

axes int

How many axes to contract: 0 is the outer product, 1 np.dot; a negative count contracts nothing.

Returns

NDArray

The contraction, shape x1.shape[:-axes] + x2.shape[axes:].

Exceptions

ValueError

The contracted extents differ (shape-mismatch for sum).

IndexError

The count exceeds an operand's rank (tuple index out of range).

Tensordot(NDArray, NDArray, int[], int[])

NumPy's np.linalg.tensordot(x1, x2, axes=(axesA, axesB)), the Array-API spelling of tensordot(NDArray, NDArray, (int[] AxesA, int[] AxesB)): axesA of x1 contracted element by element with axesB of x2.

public abstract NDArray Tensordot(NDArray x1, NDArray x2, int[] axesA, int[] axesB)

Parameters

x1 NDArray

The first tensor.

x2 NDArray

The second tensor.

axesA int[]

The contracted axes of x1.

axesB int[]

The contracted axes of x2, paired in order.

Returns

NDArray

The contraction: x1's free axes, then x2's.

Exceptions

ValueError

A repeated axis (duplicate axes are not allowed in tensordot, checked first), or lists of unequal length or mismatched extents (shape-mismatch for sum).

IndexError

A contraction axis out of range (tuple index out of range).

Tensorinv(NDArray, int)

NumPy's np.linalg.tensorinv(a, ind=2): the inverse of an N-dimensional array with respect to a tensordot contraction over its last a.ndim - ind axes.

public abstract NDArray Tensorinv(NDArray a, int ind = 2)

Parameters

a NDArray

The tensor to invert.

ind int

How many LEADING axes form the "row" side; must be positive.

Returns

NDArray

The inverse, shape a.shape[ind:] + a.shape[:ind]: a view of the inverse of the square matrix (NumPy's ia.reshape(*invshape)).

Exceptions

ValueError

ind is not positive (Invalid ind argument.), or the reshape to a square matrix is impossible.

LinAlgError

The reshaped matrix is not square, or is singular.

TypeError

A float16, decimal or char operand, or a 0-d operand whose 1x1 inverse succeeded (NumPy's reshape() takes exactly 1 argument (0 given)).

TypeError

A text operand (inv's gufunc input-cast refusal).

Tensorsolve(NDArray, NDArray, int[])

NumPy's np.linalg.tensorsolve(a, b, axes=None): the solution x of the tensor equation tensordot(a, x, x.ndim) = b, a reshape of Solve(NDArray, NDArray).

public abstract NDArray Tensorsolve(NDArray a, NDArray b, int[] axes = null)

Parameters

a NDArray

The coefficient tensor; its leading b.ndim axes (after axes moved some to the end) index the equations.

b NDArray

The right-hand side.

axes int[]

Axes of a to move to the right first, as LIST VALUES (NumPy never normalizes them: only 0 .. a.ndim - 1 are found), or null.

Returns

NDArray

The solution, of NumPy's oldshape = a.shape[-(a.ndim - b.ndim):] (after the axis move); it owns its data (NumPy's res.shape = oldshape).

Exceptions

ValueError

An axis that is not one of 0 .. a.ndim - 1 (list.remove(x): x not in list), or a right-hand side that does not match the square system (solve's gufunc text).

LinAlgError

a.size is not prod(oldshape) ** 2, or the system is singular.

TypeError

A float16, decimal or char operand.

TypeError

A text operand (solve's gufunc input-cast refusal).

Trace(NDArray, int, DType)

NumPy's np.linalg.trace(x, offset=0, dtype=None), the Array-API trace: the sum along the diagonals of the LAST two axes, where trace(NDArray, int, int, int, DType, NDArray) defaults to the first two (a (2, 3, 3) stack gives (2,) here and (3,) there).

public abstract NDArray Trace(NDArray x, int offset = 0, DType dtype = null)

Parameters

x NDArray

The operand, at least two-dimensional.

offset int

The diagonal's offset from the main one (positive above).

dtype DType

The accumulator and result dtype, or null for np.trace's rule (NumPy's sum rule).

Returns

NDArray

The traces, one per matrix (a 0-d array for one matrix).

Exceptions

AxisError

An operand of fewer than two axes.

ArgumentNullException

x is null.

TypeError

A text operand with a non-text dtype, or a string dtype.

Vecdot(NDArray, NDArray, int)

NumPy's np.linalg.vecdot(x1, x2, axis=-1), the Array-API spelling of vecdot(NDArray, NDArray, NDArray, int[][], int?, bool, DType, string, char): the dot product of vectors along axis, conjugating x1.

public abstract NDArray Vecdot(NDArray x1, NDArray x2, int axis = -1)

Parameters

x1 NDArray

The first operand (conjugated when complex).

x2 NDArray

The second operand.

axis int

The axis of the vectors in both operands (negative counts from the end).

Returns

NDArray

The dot products: the operands' other axes broadcast, in the loop dtype (an int32 pair stays int32).

Exceptions

ValueError

A 0-d operand, vectors of different lengths, or other axes that do not broadcast (NumPy's gufunc texts).

AxisError

axis is out of range.

TypeError

Text operands (no vecdot loop).

NullReferenceException

An operand is null.

VectorNorm(NDArray, int, bool, object)

NumPy's np.linalg.vector_norm(x, axis=axis, keepdims=False, ord=2) with an integer axis: NumPy's norm(x, axis=axis, ord=ord), then for keepdims a reshape that puts the axis back with length 1.

public abstract NDArray VectorNorm(NDArray x, int axis, bool keepdims = false, object ord = null)

Parameters

x NDArray

The operand: integer and bool operands compute in float64.

axis int

The reduced axis (negative counts from the end).

keepdims bool

Leave the reduced axis in the result with length 1.

ord object

The vector order; null stands for NumPy's default of 2.

Returns

NDArray

The norm along axis; with keepdims a view of it (NumPy's reshape, which does not own its data).

Exceptions

AxisError

axis is out of range (a 0-d operand has no axis).

ValueError

A text order, or -inf of an empty axis.

TypeError

An order that is neither null, a number nor a string.

VectorNorm(NDArray, int[], bool, object)

NumPy's np.linalg.vector_norm(x, axis=None, keepdims=False, ord=2) with a tuple of axes (or none): the reduced axes moved to the front and merged into one, then a VECTOR norm over it, whatever their number.

public abstract NDArray VectorNorm(NDArray x, int[] axis = null, bool keepdims = false, object ord = null)

Parameters

x NDArray

The operand: integer and bool operands compute in float64.

axis int[]

The reduced axes, any number of them (negative counts from the end), or null for the flattened array. An empty array reduces nothing (the elementwise magnitude).

keepdims bool

Leave the reduced axes in the result with length 1.

ord object

The vector order; null stands for NumPy's default of 2.

Returns

NDArray

The vector norm over the axes.

Remarks

NumPy's int axis and its one-element tuple take different statements (the tuple is moved and merged first), which reduce in different layouts: the int is VectorNorm(NDArray, int, bool, object).

Exceptions

AxisError

An axis is out of range.

ValueError

An axis appears twice (repeated axis), a text order, or -inf of an empty operand.

TypeError

An order that is neither null, a number nor a string.