RiptideRiptide
Download Riptide SDKInstall Riptorch
Menu
Riptorch docs · Reference

torch.linalg

Linear algebra: decompositions, solvers, inverses, norms, plus exact extras for quadrance and spread. Every name stock PyTorch has is here; the reference marks what is the same, what is different and what Riptorch adds. PyTorch’s own page for torch.linalg.

import riptorch as torch
torch.linalg
38 same as PyTorch6 different11 Riptorch extras

Functions

linalg.choleskyfunctionSame as PyTorch
cholesky(A, *, upper=False, out=None)

linalg.cholesky(A, *, upper=False): A = L L^T (only A's lower triangle is read).

linalg.cholesky_exfunctionSame as PyTorch
cholesky_ex(A, *, upper=False, check_errors=False, out=None)

linalg.cholesky_ex: (L, info), info an int32 per matrix (0, or k > 0 when the leading k x k minor is not positive definite); raises only with check_errors=True.

linalg.condfunctionSame as PyTorch
cond(A, p=None, *, out=None)

linalg.cond(A, p=None): s_max / s_min for p None / 2 (-2: the reciprocal); else norm(A, p) norm(A^-1, p).

linalg.crossfunctionSame as PyTorch
cross(input, other, dim=-1)

The 3-d cross product along dim (size 3), broadcasting.

linalg.detfunctionDifferent from PyTorch
det(A, *, out=None, maths=None, exact=False)

Different from PyTorch: exact integer determinants (Bareiss); PyTorch refuses integer input, and for [[1e9, 1e9+1], [1e9+1, 1e9+2]] PyTorch float64 gives -84.7 and MLX float64 gives 0.0 where the answer is -1

PyTorch
integer input raises (Expected a floating point or complex tensor); integer matrices as float lose the answer (det [[1e9,1e9+1],[1e9+1,1e9+2]] = -84.7 in float64, true -1)
Riptorch
integer input: exact determinant (Bareiss), int64; raises when it does not fit (torch.det is the same function)
Measured
unimodular 8x8 entries <= 100: 200/200 wrong (PyTorch float64), 0
Stock behaviour
torch.set_compat("pytorch")
linalg.diagonalfunctionSame as PyTorch
diagonal(A, *, offset=0, dim1=-2, dim2=-1)

torch.linalg.diagonal: the diagonal of the last two dims (offset, dim1=-2, dim2=-1).

linalg.eigfunctionSame as PyTorch
eig(A, *, out=None)

Hessenberg + Francis QR (EISPACK hqr2); each vector unit length with its largest component real (LAPACK geev). No backward yet.

linalg.eighfunctionSame as PyTorch
eigh(A, UPLO='L', *, out=None)

fixed (largest-magnitude component positive); LAPACK's is arbitrary, so compare V up to column signs.

linalg.eigvalsfunctionSame as PyTorch
eigvals(A, *, out=None)

linalg.eigvals: the (complex) eigenvalues of square A.

linalg.eigvalshfunctionSame as PyTorch
eigvalsh(A, UPLO='L', *, out=None)

linalg.eigvalsh: the eigenvalues of symmetric A (read from the UPLO triangle), ascending; differentiable.

linalg.householder_productfunctionSame as PyTorch
householder_product(A, tau, *, out=None)

linalg.householder_product(A, tau): the first n columns of H_1... H_k (LAPACK orgqr).

linalg.invfunctionDifferent from PyTorch
inv(A, *, out=None, maths=None, exact=False)

Different from PyTorch: exact inverses of integer matrices (a FractionTensor); PyTorch and MLX refuse integer input, and PyTorch's float64 inverse of the 12x12 Hilbert matrix is off by 3.4e-2

PyTorch
integer input raises
Riptorch
integer input: exact inverse as a FractionTensor
Measured
Hilbert 12x12 (float64): PyTorch inv max rel error 3.4e-2; Riptorch exact
Stock behaviour
torch.set_compat("pytorch")
linalg.inv_exfunctionSame as PyTorch
inv_ex(A, *, check_errors=False, out=None)

linalg.inv_ex: (inverse, info), info an int32 per matrix (0, or k > 0 when the matrix is singular); raises only with check_errors=True.

linalg.ldl_factorfunctionSame as PyTorch
ldl_factor(A, *, hermitian=False, out=None)

linalg.ldl_factor: (LD, pivots) of symmetric A, as ldl_factor_ex without info.

linalg.ldl_factor_exfunctionDifferent from PyTorch
ldl_factor_ex(A, *, hermitian=False, check_errors=False, out=None)

linalg.ldl_factor_ex: LAPACK sytrf's compact form (L strictly below the diagonal, D on it) for matrices where Bunch-Kaufman takes only 1x1 pivots without interchanges (|d_k| >= alpha * column max, alpha = (1 + 17^(1/2)) / 8); those factors equal LAPACK's. Others raise NotImplementedError (no 2x2 pivots yet).

Different from PyTorch: Others raise NotImplementedError (no 2x2 pivots yet).

linalg.ldl_solvefunctionSame as PyTorch
ldl_solve(LD, pivots, B, *, hermitian=False, out=None)

linalg.ldl_solve for the factors ldl_factor gives (no interchanges): L D L^T X = B.

linalg.lstsqfunctionSame as PyTorch
lstsq(A, B, rcond=None, *, driver=None, out=None)

least-squares solution from the SVD (all drivers; gelsy / gels / gelsd / gelss agree for full-rank A), with each driver's PyTorch outputs: residuals for gels / gelsd / gelss when m > n and A has full rank, rank for all but gels, singular values for gelsd / gelss.

linalg.lufunctionSame as PyTorch
lu(A, *, pivot=True, out=None)

Same as PyTorch’s torch.linalg.lu.

linalg.lu_factorfunctionSame as PyTorch
lu_factor(A, *, pivot=True, out=None)

(as in PyTorch: the zero pivot stays in U).

linalg.lu_factor_exfunctionDifferent from PyTorch
lu_factor_ex(A, *, pivot=True, check_errors=False, out=None)

linalg.lu_factor_ex: (LU, pivots, info) with partial pivoting (pivot=False raises NotImplementedError); info is 0, or k > 0 when U[k-1, k-1] is zero.

Different from PyTorch: linalg.lu_factor_ex: (LU, pivots, info) with partial pivoting (pivot=False raises NotImplementedError); info is 0, or k > 0 when U[k-1, k-1] is zero.

linalg.lu_solvefunctionSame as PyTorch
lu_solve(LU, pivots, B, *, left=True, adjoint=False, out=None)

linalg.lu_solve(LU, pivots, B, *, left=True, adjoint=False).

linalg.matmulfunctionSame as PyTorch
matmul(input, other)

Same as PyTorch’s torch.linalg.matmul.

linalg.matrix_expfunctionSame as PyTorch
matrix_exp(A)

linalg.matrix_exp(A): Pade-13 scaling and squaring (Higham 2005). Gradient: the exponential of the block matrix [[A^T, G], [0, A^T]] (its top-right block).

linalg.matrix_normfunctionSame as PyTorch
matrix_norm(A, ord='fro', dim=(-2, -1), keepdim=False, *, dtype=None, out=None, maths=None)

linalg.matrix_norm(A, ord='fro'): 'fro', 'nuc', inf, -inf, 1, -1, 2, -2. maths="rational" with 'fro': the quadrance (the sum of squares).

linalg.matrix_powerfunctionSame as PyTorch
matrix_power(A, n, *, out=None)

linalg.matrix_power(A, n): repeated squaring (n < 0: of the inverse); n = 0 gives the identity.

linalg.matrix_rankfunctionDifferent from PyTorch
matrix_rank(A, *, atol=None, rtol=None, hermitian=False, tol=None, out=None, maths=None)

Different from PyTorch: exact rank of integer matrices; MLX has no matrix_rank; PyTorch refuses integer input, and in float64 gives rank 12 for the full-rank 16x16 integer Hilbert matrix

PyTorch
integer input raises; float rank of integer Hilbert matrices is wrong from n = 12 (11 for 12, 12 for 16)
Riptorch
integer input: exact rank (fraction-free elimination)
Measured
integer Hilbert n = 16: PyTorch float64 rank 12, Riptorch 16 (true 16)
Stock behaviour
torch.set_compat("pytorch")
linalg.matrix_sqrthfunctionSame as PyTorch
matrix_sqrth(A)

linalg.matrix_sqrth(A): V diag(w^(1/2)) V^T from eigh (lower triangle), differentiable through eigh.

linalg.multi_dotfunctionSame as PyTorch
multi_dot(tensors, *, out=None)

linalg.multi_dot([A, B,...]): the chain product (first 1-d as a row, last 1-d as a column), multiplied in the order that needs the fewest scalar products (dynamic programming over the chain).

linalg.normfunctionSame as PyTorch
norm(A, ord=None, dim=None, keepdim=False, *, out=None, dtype=None, maths=None)

linalg.norm: a matrix norm for a str ord or a 2-tuple dim (or 2-d A with ord given and dim None), else a vector norm.

linalg.pinvfunctionSame as PyTorch
pinv(A, *, atol=None, rtol=None, hermitian=False, out=None)

linalg.pinv(A): V diag(1/s, s > tol) U^T from the SVD (eigh when hermitian), differentiable through it.

linalg.polarfunctionSame as PyTorch
polar(A, *, out=None)

Same as PyTorch’s torch.linalg.polar.

linalg.qrfunctionSame as PyTorch
qr(A, mode='reduced', *, out=None)

Same as PyTorch’s torch.linalg.qr.

linalg.slogdetfunctionSame as PyTorch
slogdet(A, *, out=None, maths=None)

Same as PyTorch’s torch.linalg.slogdet.

linalg.solvefunctionDifferent from PyTorch
solve(A, B, *, left=True, out=None, maths=None, exact=False)

Different from PyTorch: exact solutions for integer systems (a FractionTensor); PyTorch and MLX refuse integer input, and PyTorch's float64 solve on the 12x12 Hilbert matrix is off by 5.8e-2

PyTorch
integer input raises
Riptorch
integer input: exact solution as a FractionTensor
Measured
Hilbert 12x12 solve(H, ones): PyTorch max rel error 5.8e-2; Riptorch exact
Stock behaviour
torch.set_compat("pytorch")
linalg.solve_exfunctionSame as PyTorch
solve_ex(A, B, *, left=True, check_errors=False, out=None)

linalg.solve_ex: (result, info) as linalg.solve, info an int32 per matrix (0, or k > 0 when A is singular); raises only with check_errors=True.

linalg.solve_triangularfunctionSame as PyTorch
solve_triangular(A, B, *, upper, left=True, unitriangular=False, out=None)

linalg.solve_triangular(A, B, *, upper, left=True, unitriangular=False).

linalg.svdfunctionSame as PyTorch
svd(A, full_matrices=True, *, driver=None, out=None)

sign (per pair), as with LAPACK.

linalg.svdvalsfunctionSame as PyTorch
svdvals(A, *, driver=None, out=None)

linalg.svdvals: the singular values of A, descending; differentiable.

linalg.tensorinvfunctionSame as PyTorch
tensorinv(A, ind=2, *, out=None)

linalg.tensorinv(A, ind=2): the inverse of A viewed as a prod(shape[:ind]) x prod(shape[ind:]) matrix.

linalg.tensorsolvefunctionSame as PyTorch
tensorsolve(A, B, dims=None, *, out=None)

linalg.tensorsolve(A, B, dims=None): X with tensordot(A, X, X.ndim) == B.

linalg.vanderfunctionSame as PyTorch
vander(x, *, N=None)

linalg.vander(x, N=None): columns x^0, x^1,..., x^(N-1) (increasing powers).

linalg.vecdotfunctionSame as PyTorch
vecdot(x, y, *, dim=-1, out=None)

linalg.vecdot: the dot product of x and y along dim (broadcast); real inputs.

linalg.vector_normfunctionSame as PyTorch
vector_norm(x, ord=2, dim=None, keepdim=False, *, dtype=None, maths=None)

ord 2 (default), 1, inf, -inf, 0 or any p; dims (None: all). torch.norm / linalg.norm with a vector ord or "fro" are this row; linalg.matrix_norm supports "fro".

Classes

linalg.LinAlgErrorclassSame as PyTorch
LinAlgError

torch.linalg.LinAlgError: a factorisation or solve failed on its input (singular, not positive-definite).

Riptorch extras

linalg.cayleyfunctionRiptorch extra
cayley(S, *, exact=False)

The Cayley transform Q = (I - S)^-1 (I + S) of a skew-symmetric S: an orthogonal matrix with no sin / cos and (rational S gives a rational orthogonal Q exactly; 2x2 with S = [[0, -t], [t, 0]] is the rotation ((1 - t^2), 2t) / (1 + t^2)). Differentiable. exact=True (or integer S): a FractionTensor.

Riptorch extra: not in stock PyTorch.

linalg.cayley_inversefunctionRiptorch extra
cayley_inverse(Q, *, exact=False)

S = (Q - I)(Q + I)^-1: the skew-symmetric matrix whose Cayley transform is Q (Q orthogonal without eigenvalue -1).

Riptorch extra: not in stock PyTorch.

linalg.FractionTensorclassRiptorch extra
FractionTensor(values, shape)

Exact results: a tensor of reduced fractions (Python ints of any size, no rounding).

.numerator /.denominator -> int64 tensors (OverflowError when an entry does not fit);.fractions /.tolist -> nested lists of fractions.Fraction;.to(dtype) /.double /.float -> each entry correctly rounded once;.shape,.dim,.numel, [i] (a FractionTensor or a Fraction), ==, @ (exact matrix product). Build one from a tensor or nested lists of ints / Fractions with FractionTensor.of(x).

Riptorch extra: not in stock PyTorch.

12 methods and properties defined on FractionTensor
  • denominatorThe (positive) denominators as an int64 tensor (OverflowError when one does not fit).
  • dim(self)The number of dims.
  • double(self)to(float64): each entry correctly rounded once.
  • float(self)to(float32): each entry correctly rounded once.
  • fractions(self)Nested lists of fractions.Fraction (tolist is the same).
  • item(self)The single entry as a fractions.Fraction (RuntimeError for more than one entry).
  • numel(self)The number of entries.
  • numeratorThe numerators as an int64 tensor (OverflowError when one does not fit).
  • of(x)A FractionTensor from a tensor, a FractionTensor, a number, or nested lists of ints / Fractions.
  • size(self, d=None)The shape, or its size along d.
  • to(self, dtype=None)A float tensor of dtype (float32 by default), each entry correctly rounded once from its exact value.
  • tolist(self)Nested lists of fractions.Fraction (tolist is the same).
linalg.givens_triangularizefunctionRiptorch extra
givens_triangularize(A, *, exact=False)

Exact triangularisation by scaled rotations (Givens with rational parameters): G A = R, R upper triangular, G's rows orthogonal with quadrances sigma (G G^T = diag(sigma)). Each step zeroes one entry with the rotation [[a s_i, b s_c], [-b, a]] of two rows (a, b their entries, s their quadrance scales): no sin or cos. exact=True (or integer A): FractionTensors.

Riptorch extra: not in stock PyTorch.

linalg.ldlfunctionRiptorch extra
ldl(A, *, exact=False)

A = L D L^T (L unit lower triangular, D the diagonal as a vector): Cholesky without its root (L_chol = L D^(1/2)). No pivoting: raises when a leading minor is zero. exact=True (or integer A): FractionTensors.

Riptorch extra: not in stock PyTorch.

linalg.principal_spreadsfunctionRiptorch extra
principal_spreads(A, B)

The spreads (ascending; classically sin^2 of the principal angles) between the column spaces of A (m x k) and B (m x l), full column rank: 1 - the eigenvalues of (A^T A)^-1 A^T B (B^T B)^-1 B^T A (the crosses; of their own, the eigensolver is classic).

Riptorch extra: not in stock PyTorch.

linalg.qr_quadrancefunctionRiptorch extra
qr_quadrance(A, *, exact=False)

Exact QR: A = Q R with Q's columns orthogonal but not normalised, R unit upper triangular, and quadrances[j] = Q(q_j) = q_j. q_j (modified Gram-Schmidt). The classic factors are Q / quadrances^(1/2) and quadrances^(1/2) R (up to the signs LAPACK picks). exact=True (or integer A): FractionTensors.

Riptorch extra: not in stock PyTorch.

linalg.quadrancefunctionRiptorch extra
quadrance(x, dim=-1, keepdim=False)

Q(x) = x. x along dim (the squared length).

Riptorch extra: not in stock PyTorch.

linalg.rotationfunctionRiptorch extra
rotation(t, n=2, i=0, j=1, *, dtype=None)

The rational rotation by Cayley parameter t (= tan(angle / 2)) in the (i, j) plane of n dimensions: cos = (1 - t^2) / (1 + t^2), sin = 2t / (1 + t^2) placed as a Givens matrix. t a number or a tensor (batched, with gradients).

Riptorch extra: not in stock PyTorch.

linalg.spreadfunctionRiptorch extra
spread(u, v, dim=-1, keepdim=False)

The spread between vectors u and v: 1 - (u. v)^2 / (Q(u) Q(v)) (rational).

Riptorch extra: not in stock PyTorch.

linalg.subspace_crossfunctionRiptorch extra
subspace_cross(A, B, *, exact=False)

The product of the crosses (classically cos^2 of the principal angles) between the column spaces of A and B (both m x k, full column rank): det(A^T B)^2 / (det(A^T A) det(B^T B)). Rational; exact for integer inputs or exact=True.

Riptorch extra: not in stock PyTorch.