Shortcuts

Linear Algebra

PyTorch routines in the older section for BLAS & LAPACK and the newer linalg namespace are documented here.

Matrix properties

det

torch.det is implemented with det().

det(matrix) determinant
Parameters:

matrix (array,tensor) – square input matrix or tensor pointer to a matrix or batch of matrices

Returns:

If k array given as input returns determinant as k scalar else tensor scalar.

q)seed 123
q)x:tensor(`randn;3 3)
q)y:det x
q)tensor y
0.3735779e

q)x:return x
q)det(x;x)
0.3735779 0.3735779e

logdet

torch.logdet is implemented with logdet().

logdet(matrix) log determinant
Parameters:

matrix (array,tensor) – square input matrix or tensor pointer to a matrix or batch of matrices

Returns:

If k array given as input returns log determinant as k scalar else tensor scalar.

q)seed 123
q)x:tensor(`randn;3 3)
q)y:logdet x
q)tensor y
-0.9846289e

q)x:return x
q)logdet(x;x)
-0.9846289 -0.9846289e

slogdet

torch.slogdet is implemented with slogdet(), which computes the sign and natural logarithm of the absolute value of the determinant of a square matrix or a set of matrices.

slogdet(matrix) log determinant
Parameters:

matrix (array,tensor) – square input matrix or tensor pointer to a matrix or batch of matrices

Returns:

If k array given as input returns sign and log determinant as k list(s) else tensor vector.

q)seed 101
q)x:tensor(`randn;3 3)
q)y:logdet x
q)tensor y
0Ne

q)v:slogdet x
q)vector v
-1 0.5532773e

q)x:return x
q)slogdet(x;x;x;x)
-1        -1        -1        -1
0.5532773 0.5532773 0.5532773 0.5532773

mrank

torch.linalg.matrix_rank is implemented as mrank().

mrank(matrix;atol;rtol;hermitian) rank
mrank(matrix;atol;rtol;hermitian;output) null
Allowable argument combinations:
  • mrank(matrix)

  • mrank(matrix;atol)

  • mrank(matrix;atol;rtol)

  • mrank(matrix;atol;rtol;hermitian)

  • mrank(matrix;hermitian)

  • any of the above combinations followed by a trailing output tensor

Parameters:
  • matrix (array,tensor) – input matrix or tensor pointer to a matrix or batch of matrices

  • atol (double) – absolute tolerance, default=0 if left unspecified.

  • rtol (double) –

    relative tolerance, default is derived from input, see torch.linalg.matrix_rank

  • hermitian (bool) – optional flag, set false by default, set true to indicate Hermitian if complex input else symmetric for real input.

  • output (tensor) – an optional complex <complex> tensor to use for function output

Returns:

Returns the numerical rank of the input matrix or matrices, as an array if input is an array, else as tensor. If output tensor supplied, writes output to tensor and returns null.

q)x:return tensor(`eye;10)
q)mrank x
10

q)mrank .[x;0 0;:;0e]
9

q)x:tensor(`randn; 2 4 3 3; `cdouble)
q)y:mrank x
q)tensor y
3 3 3 3
3 3 3 3

q)use[y]mrank(x;1b) /hermitian=true
q)tensor y
3 3 3 3
3 3 3 3

q)use[y]mrank(x;1.0;0.0;1b) /atol=1, rtol=0, Hermitian=true
q)tensor y
2 2 1 2
2 2 3 2

q)use[y]mrank(x;1.0;0.0;0b) /atol=1, rtol=0, Hermitian=false
q)tensor y
1 1 2 2
2 2 2 2

Decompositions

chol

torch.linalg.cholesky is implemented as function chol(), which returns the Cholesky factorization \(L\) for each matrix input.

\[A = LL^{\text{H}}\mathrlap{\qquad L \in \mathbb{K}^{n \times n}}\]

where \(L\) is a lower triangular matrix and \(L^{\text{H}}\) is the conjugate transpose when \(L\) is complex, and the transpose when \(L\) is real-valued.

chol(x) Cholesky decomposition
chol(x;upper) Cholesky decomposition
chol(x;upper;output) null
Parameters:
  • x (array,tensor) – a k array or tensor pointer of shape \((*, n, n)\) where * is zero or more batch dimensions consisting of symmetric or Hermitian positive-definite matrices

  • upper (bool) – default=``false`` to return lower triangular output, set true for upper triangular output

  • output (tensor) – an optional tensor to use for function output

Returns:

Returns lower/upper triangular Cholesky factors for each of the input matrices, as a tensor if tensor input, else k array. If an output tensor is given, the factors are written to this tensor and null is returned.

q)seed 123
q)a:tensor(`randn;3 3;`double)
q)t:transpose a
q)use[a]mm(a;t)

q)L:chol a
q){x mmu flip x}tensor L
0.1635  -0.1946 -0.1022
-0.1946 1.534   1.205
-0.1022 1.205   1.62

q)tensor a
0.1635  -0.1946 -0.1022
-0.1946 1.534   1.205
-0.1022 1.205   1.62

cholx

torch.linalg.cholesky_ex is implemented as function cholx(), which computes the same Cholesky decomposition as chol(), but with additional error codes and options.

cholx(x;upper;check) Cholesky decomposition and error codes
cholx(x;upper;check;output) null
Allowable argument combinations:
  • cholx(x)

  • cholx(x;upper)

  • cholx(x;upper;check)

  • any of the above combinations followed by a trailing output vector

Parameters:
  • x (array,tensor) – a k array or tensor pointer of shape \((*, n, n)\) where * is zero or more batch dimensions consisting of symmetric or Hermitian positive-definite matrices

  • upper (bool) – default = false to return lower triangular output, set true for upper triangular output

  • check (bool) – default = false for no checking, to check error codes before returning, set true

  • output (vector) – an optional vector to use for function output of decomposition and errors

Returns:

Returns lower/upper triangular Cholesky factors for each of the input matrices along with error code(s). If tensor input, returns vector of tensors, else k array. If an output vector is given, the factors and errors are written to this vector and null is returned.

q)a:tensor(`randn;3 3;`double)
q)t:transpose a
q)use[a]mm(a;t)

q)v:cholx a
q){x mmu flip x}vector(v;0)
0.1635  -0.1946 -0.1022
-0.1946 1.534   1.205
-0.1022 1.205   1.62

q)tensor a
0.1635  -0.1946 -0.1022
-0.1946 1.534   1.205
-0.1022 1.205   1.62

q)vector(v;1)
0i

Supply incorrect input, i.e. not symmetric or positive-definite matrix:

q)cholx(t;1b;0b;v)  / upper triangular, no checks
q)vector(v;1)
1i

q)cholx(t;1b;1b;v)  / turn on error checking
'torch.linalg.cholesky_ex: The factorization could not be completed because the input is not positive-definite (the leading minor of order 1 is not positive-definite).
  [0]  cholx(t;1b;1b;v)  / turn on error checking
       ^

eig

pytorch.linalg.eig is implemented by function eig(), which calculates eigenvalues and eigenvectors of a square matrix or set of square matrices.

Letting \(\mathbb{K}\) be \(\mathbb{R}\) or \(\mathbb{C}\), the eigenvalue decomposition of a square matrix \(A \in \mathbb{K}^{n \times n}\) (if it exists) is defined as

\[A = V \operatorname{diag}(\Lambda) V^{-1}\mathrlap{\qquad V \in \mathbb{C}^{n \times n}, \Lambda \in \mathbb{C}^n}\]
eig(x) eigenvalues and eigenvectors
eig(x;output) null
Parameters:
  • x (array,tensor) – k array or tensor pointer to a square matrix or batches of square matrices

  • output (vector) – an optional vector to use for function output of eigenvalues and eigenvectors

Returns:

Returns a vector of tensors if x is a tensor, else a 2-element k list with eigenvalues and eigenvectors, corresponding to \(\Lambda\) and \(V\) above. The eigenvalues and vectors will be complex even when input x is real. If an output vector is supplied, function output is written to the vector and null returned.

Note

By default, complex tensors are converted to k arrays with the real and imaginary parts along the 1st dimension, see settings for more detail.

q)show x:3 3#2 -3 0.0, 2 -5 0.0, 0 0 3.0
2 -3 0
2 -5 0
0 0  3

q)v:first each eig x  / take real part only

q)mmu/[(v 1;diag v 0;inverse v 1)]
2 -3 0
2 -5 0
0 0  3

eigvals

pytorch.linalg.eigvals is implemented by function eigvals(), which calculates eigenvalues only (see eig for more detail on the full eigenvalue decomposition).

eigvals(x) eigenvalues
eigvals(x;output) null
Parameters:
  • x (array,tensor) – k array or tensor pointer to a square matrix or batches of square matrices

  • output (vector) – an optional tensor pointer to use for function output of eigenvalues

Returns:

Returns a complex valued tensor if x is a tensor, else a 2-element k list with the real and imaginary part of the eigenvalues. If an output tensor is supplied, function output is written to the tensor and null returned.

Note

By default, complex tensors are converted to k arrays with the real and imaginary parts along the 1st dimension, see settings for more detail.

q)show x:(7 1 1.0; 3 1 2.0; 1 3 2.0)
7 1 1
3 1 2
1 3 2

q)v:eigvals x

q)v 0          /real part
8 3 -1f

q)v 1          /imaginary part
0 0 0f

q)/check determinant zero for all eigenvalues:
q){det x-diag count[x]#y}[x]'[v 0]
8.349e-14 1.679e-14 3.07e-14

eigh

pytorch.linalg.eigh is implemented by function eigh(), which calculates eigenvalues and eigenvectors for a symmetric or complex Hermitian matrix or a set of matrices.

Letting \(\mathbb{K}\) be \(\mathbb{R}\) or \(\mathbb{C}\), the eigenvalue decomposition of a complex Hermitian or real symmetric matrix \(A \in \mathbb{K}^{n \times n}\) is defined as

\[A = Q \operatorname{diag}(\Lambda) Q^{\text{H}}\mathrlap{\qquad Q \in \mathbb{K}^{n \times n}, \Lambda \in \mathbb{R}^n}\]

where \(Q^{\text{H}}\) is the conjugate transpose when \(Q\) is complex, and the transpose when \(Q\) is real-valued. \(Q\) is orthogonal in the real case and unitary in the complex case.

eigh(x;upper) eigenvalues and eigenvectors
eigh(x;upper;output) null
Parameters:
  • x (array,tensor) – k array or tensor pointer to a square matrix or batches of square matrices

  • upper (bool) – an optional flag, set false by default to indicate only the lower tirangular part of the matrix is used, set true to use only the pper triangle

  • output (vector) – an optional vector to use for function output of eigenvalues and eigenvectors

Returns:

Returns a vector of tensors if x is a tensor, else a 2-element k list with eigenvalues and eigenvectors, corresponding to \(\Lambda\) and \(Q\) above. The eigenvalues and vectors will be complex even when input x is real. If an output vector is supplied, function output is written to the vector and null returned.

q)seed 123
q)x:return tensor(`randn;3 3;`double)
q)x:x mmu flip x  / make symmetric

q)x
0.1635  -0.1946 -0.1022
-0.1946 1.534   1.205
-0.1022 1.205   1.62

q)v:eigh x
q)v 0                     /eigen values
0.1276 0.3907 2.8

q)v 1
0.9594  -0.2709 -0.07897  /eigen vectors
0.248   0.6761  0.6938
-0.1346 -0.6852 0.7158

q)mmu/[(v 1;diag v 0;flip v 1)]
0.1635  -0.1946 -0.1022
-0.1946 1.534   1.205
-0.1022 1.205   1.62

q)allclose(x;mmu/[(v 1;diag v 0;flip v 1)])
1b

eigvalsh

pytorch.linalg.eigvalsh is implemented by function eigvalsh(), which returns only the eiganvalues from the decomposition of a symmetric or complex Hermitian matrix (see eigh for more detail).

eigvalsh(x) eigenvalues
eigvalsh(x;output) null
Parameters:
  • x (array,tensor) – k array or tensor pointer to a square symmetric or complex Hermitian matrix or batches of matrices

  • upper (bool) – an optional flag, set false by default to indicate only the lower tirangular part of the matrix is used, set true to use only the pper triangle

  • output (vector) – an optional tensor pointer to use for function output of eigenvalues

Returns:

Returns a tensor if x is a tensor, else a k list. If an output tensor is supplied, function output is written to the tensor and null returned.

q)seed 123
q)x:tensor(`randn;3 3;`cdouble)
q)t:transpose x
q)use[x]add(x;t)

q)real x
1.287  0.1874 -0.622
0.1874 -1.52  -1.13
-0.622 -1.13  1.006

q)imag x
0.2174  -0.3182 1.072
-0.3182 1.347   0.7224
1.072   0.7224  0.6038

q)return eigvalsh x
-2.124 0.181 2.716

qr

torch.linalg.qr is implemented as function qr(), which computes the QR decomposition of a matrix of batches of matrices.

Letting \(\mathbb{K}\) be \(\mathbb{R}\) or \(\mathbb{C}\), the full QR decomposition of a matrix \(A \in \mathbb{K}^{m \times n}\) is defined as

\[A = QR\mathrlap{\qquad Q \in \mathbb{K}^{m \times m}, R \in \mathbb{K}^{m \times n}}\]

where \(Q\) is orthogonal in the real case and unitary in the complex case, and \(R\) is upper triangular.

When m > n (tall matrix), as R is upper triangular, its last m - n rows are zero. In this case, we can drop the last m - n columns of Q to form the reduced QR decomposition:

\[A = QR\mathrlap{\qquad Q \in \mathbb{K}^{m \times n}, R \in \mathbb{K}^{n \times n}}\]

The reduced QR decomposition agrees with the full QR decomposition when n >= m (wide matrix).

qr(x;mode) QR decomposition
qr(x;mode;output) null
Parameters:
  • x (array,tensor) – k array or tensor of shape \((*, m, n)\) where \(*\) is zero or more batch dimensions

  • mode (symbol) – optional, if given, must be one of the following: - `reduced - default, return Q of shape \((*,m,k)\) and R of shape \((*,k,n)\) - `complete - return Q of shape \((*,m,m)\) and R of shape \((*,m,n)\) - `r - return empty Q and R of shape \((*,k,n)\)

  • output (vector) – an optional vector to use for function output of Q and R matrices

Returns:

Returns the \(Q\) and \(R\) matrices as a 2-element tensor vector if tensor input, else a k list. If an output vector given, the matrices are written to the vector supplied and null is returned.

q)show x:(12 -51 4.0; 6 167 -68.0; -4 24 -41.0)
12 -51 4
6  167 -68
-4 24  -41

q)y[0] mmu last y:qr x
12 -51 4
6  167 -68
-4 24  -41

lu

torch.linalg.lu_factor is implemented as function lu().

This function computes a compact representation of the LU decomposition given a matrix or set of matrices. If the matrix is square, this representation may be used in lusolve() to solve system of linear equations that use the same input matrix.

The returned decomposition has 2 parts: The LU matrix has the same shape as the input matrix or matrices. Its upper and lower triangular parts encode the non-constant elements of L and U of the LU decomposition.

The returned permutation matrix is represented by a 1-indexed vector. pivots[i] == j represents that in the i-th step of the algorithm, the i-th row was permuted with the j-1-th row.

On CUDA, pivot can be set false to function returns the LU decomposition without pivoting if the decomposition exists.

lu(x;pivot) compact factorization and pivots
lu(x;pivot;output) null
Parameters:
  • x (array,tensor) – k array or tensor of shape \((*, m, n)\) where \(*\) is zero or more batch dimensions

  • pivot (bool) – set true by default but can be set false with CUDA tensors to attempt the LU decomposition without pivoting.

  • output (vector) – an optional vector to use for function output of the LU compact decomposition and the pivots

Returns:

Returns a 2-element tensor vector if tensor input, else a 2-element k array, with the LU matrix and pivots. If an output vector supplied, these elements are written to the vector and null returned.

q)x:tensor(x;`cuda) /use CUDA to turn off pivoting
q)v:lu(x;0b)
q)show m:vector(v;0)
3  -7 -2 2
-1 -2 -1 2
2  -5 -1 1
-3 8  3  -1

q)vector(v;1)
1 2 3 4i

q)show U:triu m
3 -7 -2 2
0 -2 -1 2
0 0  -1 1
0 0  0  -1

q)show L:tril[(m;-1)]+diag count[m]#1.0
1  0  0 0
-1 1  0 0
2  -5 1 0
-3 8  3 1

q)L mmu U
3  -7 -2 2
-3 5  1  0
6  -4 0  -5
-9 5  -5 12

q)tensor[x]~L mmu U
1b

lux

torch.linalg.lu_factor_ex is implemented as function lux(), which computes the compact LU factorization of a matrix or a set of matrices, but includes additional flag for error checking and returns additional error codes. See lu <lu> for more information on the compact LU factorization.

lux(x;pivot;check) compact factorization with pivots and error codes
lux(x;pivot;check;output) null
Parameters:
  • x (array,tensor) – k array or tensor of shape \((*, m, n)\) where \(*\) is zero or more batch dimensions

  • pivot (bool) – set true by default but can be set false with CUDA tensors to attempt the LU decomposition without pivoting.

  • check (bool) – set false by default, set true for the function to signal an error if any LU decomposition fails

  • output (vector) – an optional vector to use for function output of the LU compact decomposition and the pivots

Returns:

Returns a 3-element tensor vector if tensor input, else a 3-element k array, with the LU matrix, pivots and error codes. If an output vector supplied, these elements are written to the vector and null returned.

q)show x:(3 -7 -2 2.0; -3 5 1 0.0; 6 -4 0 -5.0; -9 5 -5 12.0)
3  -7 -2 2
-3 5  1  0
6  -4 0  -5
-9 5  -5 12

q)v:lux x

q)v 0 / compact LU matrix
-9      5      -5      12
-0.3333 -5.333 -3.667  6
-0.6667 0.125  -2.875  2.25
0.3333  -0.625 -0.1304 0.04348

q)v 1 /pivots
4 4 3 4i

q)v 2 /error codes
0i

q)x-mmu/[luunpack 2#v]
0 0 0 0
0 0 0 -2.082e-17
0 0 0 0
0 0 0 0

lun

torch.lu_unpack is implemented by function lun(), which unpacks the data and pivots from the LU factorization, see lu. The result of lun() is a permutation matrix P and the lower triangular matrix L and upper triangular matrix M such that the original input matrix can be recreated by the matrix product of P x L x U.

lun(lu;dataflag;pivotflag) P, L, U matrix or matrices
lun(lu;dataflag;pivotflag;output) null
Parameters:
  • lu (array,vector) – output from lu(), either a 2-element k list or tensor vector of the compact LU matrix and pivots

  • dataflag (bool) – set true by default, optional flag that can be set false to skip unpack of L & U matrices

  • pivotflag (bool) – set true by default, optional flag that can be set false to skip processing of pivots

  • output (vector) – an optional vector to use for function output of unpacked pivot, lower triangular and upper triangular matrices.

Result:

Returns 3 matrices or sets of matrices, as a vector of tensors if any tensor input, else a k list with the unpacked pivot information and the lower and upper triangular matrices. If a trailing output vector supplied, output is written to the vector and null retuned.

An alternate form of the function takes two inputs: the compact LU matrix and the pivot information:

lun(matrix;pivot;dataflag;pivotflag) P, L, U matrix or matrices
lun(matrix;pivot;dataflag;pivotflag;output) null
Parameters:
  • matrix (array,tensor) – the compact LU matrix or set of matrices from a previous lu() call

  • pivot (array,tensor) –

    the pivot information from a previous lu() call

    Remaining parameters and results are the same as the prior lun() call which uses the vector/list output of lu() directly.

q)show x:(3 -7 -2 2.0; -3 5 1 0.0; 6 -4 0 -5.0; -9 5 -5 12.0)
3  -7 -2 2
-3 5  1  0
6  -4 0  -5
-9 5  -5 12

q)v:lu x   /compact LU factorization
q)u:lun v  /unpacked

q)u 0      /unpacked from pivots
0 1 0 0
0 0 0 1
0 0 1 0
1 0 0 0

q)u 1     /L - lower triangular matrix
1       0      0       0
-0.3333 1      0       0
-0.6667 0.125  1       0
0.3333  -0.625 -0.1304 1

q)u 2     /U - upper triangular matrix
-9 5      -5     12
0  -5.333 -3.667 6
0  0      -2.875 2.25
0  0      0      0.04348

q)mmu/[u]  /product of unpacked LU factorization
3  -7 -2 2
-3 5  1  2.082e-17
6  -4 0  -5
-9 5  -5 12

q)allclose(x; mmu/[u])
1b

Same as above example, but using tensors and tensor vectors instead of k arrays:

q)x:tensor x
q)v:lu x
q)u:lun v

q)size u
4 4
4 4
4 4

q)allclose(x;mmu/[vector u])
1b

q)m:tensor(v;0)  /extract compact LU matrix
q)p:tensor(v;1)  /exctact pivot information
q)use[u]lun(m;p) /inputs are individual tensors
q)allclose(x;mmu/[vector u])
1b

svd

torch.linalg.svd is implemented as function svd(), which computes the singular value decomposition (SVD) of a matrix or a set of matrices.

Letting \(\mathbb{K}\) be \(\mathbb{R}\) or \(\mathbb{C}\), the full SVD of a matrix \(A \in \mathbb{K}^{m \times n}\), if k = min(m,n), is defined as

\[A = U \operatorname{diag}(S) V^{\text{H}} \mathrlap{\qquad U \in \mathbb{K}^{m \times m}, S \in \mathbb{R}^k, V \in \mathbb{K}^{n \times n}}\]

where \(\operatorname{diag}(S) \in \mathbb{K}^{m \times n}\), \(V^{\text{H}}\) is the conjugate transpose when \(V\) is complex, and the transpose when \(V\) is real-valued. The matrices \(U\), \(V\) (and thus \(V^{\text{H}}\)) are orthogonal in the real case, and unitary in the complex case.

When m > n (resp. m < n) we can drop the last m - n (resp. n - m) columns of U (resp. V) to form the reduced SVD:

\[A = U \operatorname{diag}(S) V^{\text{H}} \mathrlap{\qquad U \in \mathbb{K}^{m \times k}, S \in \mathbb{R}^k, V \in \mathbb{K}^{k \times n}}\]

where \(\operatorname{diag}(S) \in \mathbb{K}^{k \times k}\). In this case, \(U\) and \(V\) also have orthonormal columns.

svd(x;full;driver) QR decomposition
svd(x;full;driver;output) null
Allowable argument combinations:
  • svd(x)

  • svd(x;full)

  • svd(x;driver)

  • svd(x;full;driver)

  • any of the above combinations followed by a trailing output vector

Parameters:
  • x (array,tensor) – k array or tensor of shape \((*, m, n)\) where \(*\) is zero or more batch dimensions

  • full (bool) – set true by default to compute the full SVD, set false to return the reduced SVD

  • driver (sym) – name of the cuSOLVER method, one of `gesvd, `gesvda or `gesvda, default is null

  • output (vector) – an optional vector to use for function output of \(U\), \(S\) and \(V^{\text{H}}\) matrices

Returns:

Returns \(U\), \(S\) and \(V^{\text{H}}\) matrices as a 3-element tensor vector if tensor input given, else a k list. If an output vector given, these matrices are written to the supplied vector and null is returned.

q)show x:(3 2 2.0; 2 3 -2.0)
3 2 2
2 3 -2

q){mmu/[(x;diag y;z)]} . svd(x;0b)
3 2 2
2 3 -2

svdvals

torch.linalg.svdvals is implemented as function svdvals(), which computes the singular values of a matrix or a set of matrices (see svd for more detail on the full SVD decomposition).

svdvals(x) singular values of QR decomposition
svdvals(x;driver) singular values of QR decomposition
svd(x;output) null
svd(x;driver;output) null
Parameters:
  • x (array,tensor) – k array or tensor of shape \((*, m, n)\) where \(*\) is zero or more batch dimensions

  • driver (sym) – name of the cuSOLVER method, one of `gesvd, `gesvda or `gesvda, default is null

  • output (tensor) – an optional tensor pointer to use for function output

Returns:

Returns the singular values as a tensor if tensor input, else a k list. If an output tensr given, these values are written to the supplied tensor and null is returned.

q)x:(3 2 2.0; 2 3 -2.0)

q)svdvals x
5 3f

q)sqrt first eigvals x mmu flip x
5 3f

Solvers

solve

torch.linalg.solve is implemented as solve(), which calculates the solution of a square system of linear equations, ax = b

solve(a;b) x
solve(a;b;left) x
solve(a;b;output) null
solve(a;b;left;output) null
Parameters:
  • a (array,tensor) – input array or tensor pointer of shape (*, n, n), where * is zero or more batch dimensions.

  • b (array,tensor) – input array or tensor pointer of shape right-hand side values of shape (*, n), (*, n, k), (n) or (n, k) according to the rules described here.

  • left (bool) – default is true to solve the system \(AX = B\), set flag false to solve \(XA = B\).

  • output (tensor) – an optional tensor to use for function output

Returns:

The solution x of a square system of linear equations, ax = b, as a tensor if any tensor input, else as an array. If an output tensor given, solution is written to the tensor with null return.

q)a:tensor(`randn; 3 3; `double)
q)b:tensor(`randn; 3 4; `double)
q)x:solve(a;b)  /solve ax=b

q)tensor[a] mmu tensor x
2.02  0.603 0.0223 -0.964
0.478 -1.32 0.386  0.42
0.32  0.311 0.0215 1.08

q)tensor b
2.02  0.603 0.0223 -0.964
0.478 -1.32 0.386  0.42
0.32  0.311 0.0215 1.08

trisolve

torch.linalg.solve_triangular is implemented as trisolve(), which calculates the solution of a triangular system of linear equations.

trisolve(a;b;upper;left;unitriangular) x
trisolve(a;b;upper;left;unitriangular;output) null
Parameters:
  • a (array,tensor) – input array or tensor pointer of shape (*, n, n) or (*, k, k) if left = true

  • b (array,tensor) – input array or tensor pointer of shape right-hand side values of shape (*, n, k)

  • upper (bool) – required flag, set true if a is an upper triangular matrix or matrices, false for lower triangular

  • left (bool) – default is `true to solve for x in ax = b, false to solve xa = b

  • unitriangular (bool) – default is false, set true if the diagonal elements of a are all equal to 1

  • output (tensor) – an optional tensor to use for function output (b may be passed as an output tensor with the result overwriting values in b)

Returns:

The solution x of a triangular system of linear equations, ax = b or xa = b as a tensor if any tensor input, else as an array. If an output tensor given, solution is written to the tensor with null return.

q)seed 123
q)a:tensor(`randn; 3 3; `double)
q)b:tensor(`randn; 3 4; `double)

q)triu(a;[])
q)tensor a
-0.111 0.12 -0.37
0      -1.2 0.209
0      0    0.324

q)x:trisolve(a;b;1b)
q)B:mm(a;x)

q)allclose(b;B)
1b

cholsolve

torch.cholesky_solve is implemented by function cholsolve(), which solves a linear system of equations with a positive semidefinite matrix to be inverted given its Cholesky factor matrix \(u\).

By default, \(u\) is lower triangular and the result returned is:

\[(u u^T)^{{-1}} b \]

If \(u\) is passed with the upper=true, the result becomes:

\[(u^T u)^{{-1}} b \]
cholsolve(b;u;upper) solution matrix or batch of matrices
cholsolve(b;u;upper;output) null
Parameters:
  • b (array,tensor) – input array or tensor pointer of size \((*, m, k)\), where \(*\) is zero or more batch dimensions

  • u (array,tensor) – input array or tensor pointer of Cholesky factors, size \((*, m, m)\), where \(*\) is zero or more batch dimensions

  • upper (bool) – optional flag, false by default, set true to indicate that u is upper triangular

  • output (tensor) – an optional tensor to use for function output

Returns:

solution matrix or set of matrices as tensor if any input supplied as tensor else k array. If output tensor supplied, results are written to the supplied tensor and null returned.

q)seed 123
q)a:tensor(`randn;3 3;`double)
q)t:transpose a
q)use[a]mm(a;t) /make symmetric positive definite

q)u:chol a
q)b:tensor(`randn;3 2;`double)
q)r:cholsolve(b;u)

q)i:inverse a
q)s:mm(i;b)      /compare alternate solution
q)allclose(r;s)
1b

q)tensor r
-1.712 1.685
-1.707 0.2702
1.572  0.123

q)inv[tensor a]mmu tensor b
-1.712 1.685
-1.707 0.2702
1.572  0.123

lstsq

torch.linalg.lstsq is implemented as lstsq(), which calculates a solution to the least squares problem of a system of linear equations.

lstsq(a;b;rcond;method) vector of x,residuals,rank,singular values
lstsq(a;b;rcond;method;output) null
Allowable argument combinations:
  • lstsq(a;b)

  • lstsq(a;b;rcond)

  • lstsq(a;b;rcond;method)

  • lstsq(a;b;method)

  • any of the above combinations followed by a trailing output vector

Parameters:
  • a (array,tensor) – input array or tensor pointer of shape (*, m, n) where * is zero or more batch dimensions

  • b (array,tensor) – input array or tensor pointer of shape (*, m, k) where * is zero or more batch dimensions

  • rcond (double) – optional effective rank of a, if not specified or set to 0n, the machine precision of the data type of a multiplied by the maximum value of dimensions (m, n) is used

  • method (symbol) – optional name of the LAPACK/MAGMA method, one of `gels, `gelsd, `gelss or `gelsy

  • output (vector) – a vector pointer <vectors> to contain function output

Returns:

The least squares solution x for ax = b, along with residuals, rank and any singular values. Returns a vector of tensors if either a or b is given as a tensor, else as a k list. If output vector supplied, writes function output to given vector and returns null.

q)a:1 3 3#10 2 3 3 10 5 5 6 12e
q)b:2 3 3#2 5 1 3 2 1 5 1 9 4 2 9 2 0 3 2 5 3e

q)`x`resid`rank`singular!lstsq(a;b)
x       | ((0.0793 0.535 -0.123e;0.113 0.146 -0.352e;0.327 -0.212 0.977e);(0...
resid   | `real$()
rank    | ,3
singular| `real$()

q)`x`resid`rank`singular!lstsq(a;b;`gelsd)  /singular value decomposition
x       | ((0.0793 0.535 -0.123e;0.113 0.146 -0.352e;0.327 -0.212 0.977e);(0...
resid   | `real$()
rank    | ,3
singular| ,19.1 7.75 5.29e

q)v:vector()
q)lstsq(a;b;`gelsd;v)
q)vector v
((0.0793 0.535 -0.123e;0.113 0.146 -0.352e;0.327 -0.212 0.977e);(0.394 0.102 ..
`real$()
,3
,19.1 7.75 5.29e

lusolve

torch.lu_solve is implemented by function lusolve().

Returns the LU solve of the linear system \(Ax = b\) using the partially pivoted LU factorization of A from lu().

lusolve(b;lu) solution x of Ax=b
lusolve(b;lu;output) null
Parameters:
  • b (array,tensor) – the right hand side of \(Ax = b\), a an array or tensor of size \((*, m, k)\) where \(*\) is zero or more batch dimensions

  • lu (array,vector) – output from lu(), either a 2-element k list or tensor vector of the compact LU matrix and pivots

  • output (tensor) – an optional tensor pointer to use for function output

Result:

Returns solution \(x\) of \(Ax = b\) as a matrix or set of matrices, result is a tensor if any inputs are tensors or a vector of tensors, else a k array. If optional trailing argument is an output tensor, solution is written to the supplied tensor and null returned.

An alternate form of the call:

lusolve(b;matrix;pivot) solution x of Ax=b
lusolve(b;matrix;pivot;output) null
Parameters:
  • b (array,tensor) – the right hand side of \(Ax = b\), a an array or tensor of size :math:`(*, m, k) where :math:`* is zero or more batch dimensions

  • matrix (array,tensor) – the compact LU matrix or set of matrices from a previous lu() call

  • pivot (array,tensor) –

    the pivot information from a previous lu() call

    Remaining parameters and results are the same as the prior lusolve() call which uses the vector/list output of lu() directly.

q)a:tensor(`randn;2 3 3)
q)b:tensor(`randn;2 3 1)
q)v:lu a  /LU factorization

q)x:lusolve(b;v)

q)x:lusolve(b;v)   / solve for x in Ax=b
q)r:bmm(a;x)       / recalc b from Ax
q)allclose(b;r)
1b

q)m:tensor(v;0)    / extract compact LU matrix from factorization
q)pv:tensor(v;1)   / extract tensor of pivot information

q)x1:lusolve(b;m;pv)  / alternate call with individual tensors
q)equal(x;x1)
1b

Inverses

inverse

torch.linalg.inv is implemented by inverse().

inverse(matrix) k array
inverse(matrix;output) null
Parameters:
  • matrix (array,tensor) – square input matrix or tensor pointer to a matrix or batch of matrices

  • output (tensor) – a pointer to a previously allocated tensor to be used for output

Returns:

The function returns a k array if given a k array as input and returns a tensor if a tensor is given as input. If an output tensor is supplied, this tensor is filled with the output values and null is returned.

q)x:tensor(`randn;3 3)
q)tensor x
0.336 1.12   0.17
0.214 -0.497 0.519
0.489 -0.23  -0.491

q)inverse tensor x
0.654 0.916  1.2
0.646 -0.447 -0.249
0.348 1.12   -0.73

q)y:inverse x
q)tensor y
0.654 0.916  1.2
0.646 -0.447 -0.249
0.348 1.12   -0.73

q)z:tensor 0#0e
q)inverse(x;z)
q)tensor z
0.654 0.916  1.2
0.646 -0.447 -0.249
0.348 1.12   -0.73

pinverse

torch.linalg.pinv is implemented by pinverse(), which calculates the pseudo-inverse with optional absolute and relative tolerance.

pinverse(input;atol;rtol;hermitian) rank
pinverse(input;atol;rtol;hermitian;output) null
Allowable argument combinations:
  • pinverse(input)

  • pinverse(input;atol)

  • pinverse(input;atol;rtol)

  • pinverse(input;atol;rtol;hermitian)

  • pinverse(input;hermitian)

  • any of the above combinations followed by a trailing output tensor

Parameters:
  • input (array,tensor) – input * x N x M array or tensor pointer where * can be other batch dimensions

  • atol (double) – absolute tolerance, default=0 if left unspecified.

  • rtol (double) –

    relative tolerance, default is derived from input, see torch.linalg.pinv

  • hermitian (bool) – optional flag, set false by default, set true to indicate Hermitian if complex input else symmetric for real input.

  • output (tensor) – an optional complex <complex> tensor to use for function output

Returns:

Returns the pseudo-inverse(s) of the input, as an array if input is an array, else as tensor. If output tensor supplied, writes output to tensor and returns null.

q)seed 123
q)show x:return tensor(`randn;2 3;`double)
-0.1115 0.1204 -0.3696
-0.2404 -1.197 0.2093

q)pinverse x
-1.022  -0.2864
-0.2266 -0.8089
-2.471  -0.177

q)x mmu pinverse x
1         -8.327e-17
5.551e-16 1

q)x:tensor((x;x;x);(x;x;x))
q)size x
2 3 2 3

q)y:pinverse x
q)size y
2 3 3 2

q)tensor[y][0][0]
-1.022  -0.2864
-0.2266 -0.8089
-2.471  -0.177

cholinverse

torch.cholesky_inverse is implemented as function cholinverse(), which computes the inverse of a symmetric positive-definite matrix using its Cholesky factorizations.

If upper is false, \(u\) is lower triangular such that the returned tensor is

\[(uu^{{T}})^{{-1}} \]

If upper is true, \(u\) is upper triangular such that the returned tensor is

\[(u^T u)^{{-1}} \]
cholinverse(x;upper) inverse
cholinverse(x;upper;output) null
Parameters:
  • x (array,tensor) – the input factorizations as k array or tensor pointer of size \((*, n,n)\), where \(*\) is zero or more batch dimensions

  • upper (bool) – default=``false`` for lower triangular input, set true for upper triangular input

  • output (tensor) – an optional output tensor for function output

Returns:

Returns the inverse(s) derived from the input factorizations, returned as a tensor if tensor input, else k array. If cout tensor supplied, output is written to the given tensor and null returned.

q)seed 123
q)a:tensor(`randn;3 3;`double)
q)t:transpose a
q)use[a]mm(a;t)

q)u:chol a
q)i:cholinverse u

q)tensor[a]mmu tensor i
1         -2.776e-17 2.776e-17
2.22e-16  1          2.22e-16
3.331e-16 0          1

Matrix functions

power

torch.linalg.matrix_power is implemented as power().

power(matrix;n) nth power of square matrix
power(matrix;n;output) null
Parameters:
  • matrix (matrix,tensor) – a k array or pointer to a tensor of a square matrix or batches of matrices.

  • n (long) – integer power

  • output (tensor) – a pointer to a previously allocated tensor to be used for output

Returns:

The function returns the input matrix/tensor raised to the given integer power. If an output tensor supplied, the raised matrix iw written to the supplied tensor and null is returned.

q)x:3 3#0 1 2 3 4 5 6 7 8.0
q)power(x;2)
15 18 21
42 54 66
69 90 111

q)power(x;3)
180 234  288
558 720  882
936 1206 1476

q)x$x$x
180 234  288
558 720  882
936 1206 1476

q)x:tensor(x;x;x)  /batches of matrices
q)y:power(x;2)
q)size y
3 3 3

q)tensor(y;2)
15 18 21
42 54 66
69 90 111

Matrix products

bmm

torch.bmm is implemented as bmm(), computing the batch matrix-matrix product.

Both inputs must be 3-dimensional arrays or tensors each containing the same number of matrices.

If x is a \((b \times n \times m)\) tensor, y is a \((b \times m \times p)\) tensor and the result will be a \((b \times n \times p)\) tensor.

\[\text{result}_i = \text{x}_i \mathbin{@} \text{y}_i \]
bmm(x;y) matrix products
bmm(x;y;output) null
Parameters:
  • x (array,tensor) – a k array or tensor pointer to batches of matrices

  • y (array,tensor) – a k array or tensor pointer to the same number of matrices as in x

  • output (tensor) – an optional pointer to a previously allocated tensor to be used for output

Returns:

Returns the matrix products as an array if both inputs given as k arrays, else tensor. If output tensor given, result is written to the given tensor and null return.

Note

This function does not support broadcasting. For broadcasting matrix products, see matmul().

q)x:tensor(`randn; 100 3 4; `double)
q)y:tensor(`randn; 100 4 5; `double)
q)z:bmm(x;y)

q)size z
100 3 5

q)allclose(z;tensor[x]mmu tensor y)
1b

cross

torch.linalg.cross is implemented as function Cross(), which computes the cross product of two vectors or batches of vectors.

Cross(x;y;dim) cross product
Cross(x;y;dim;output) null
Parameters:
  • x (array,tensor) – a k array or tensor pointer

  • y (array,tensor) – a k array or tensor pointer with the same number of elements as x

  • dim (long) – the dimension along which to calculate the cross product, default=-1, the last dimension

  • output (tensor) – an optional pointer to a previously allocated tensor to be used for output

Returns:

Returns a single cross product if given single vectors, else batches of cross products along given dim, with output having the same batch dimensions of the inputs broadcast to a common shape. If all inputs are k arrays, returns k array, else tensor. If output tensor given, results are written there and null return.

q)seed 123
q)x:return tensor(`randn; 3;`double)
q)y:return tensor(`randn; 3;`double)

q)show z:Cross(x;y)
-0.4172 0.1122 0.1624

q)(x[i]*y j) - x[j:2 0 1]*y i:1 2 0
-0.4172 0.1122 0.1624

q)x:return tensor(`randn;10 3;`double)
q)y:return tensor(`randn;10 3;`double)
q)z:Cross(x;y)

q)allclose(z; (x[;i]*y[;j]) - x[;j:2 0 1]*y[;i:1 2 0])
1b

dot

torch.dot is implemented as function dot(), which computes the dot product of two 1-dimensional inputs with the same number of elements.

dot(x;y) matrix products
dot(x;y;output) null
Parameters:
  • x (list,tensor) – a k list or 1-dimensional tensor pointer

  • y (list,tensor) – a k list or 1-dimensional tensor pointer with the same number of elements as x

  • output (tensor) – an optional pointer to a previously allocated tensor to be used for output

Returns:

Returns the dot products as a k scalar if both inputs given as k lists, else tensor. If output tensor given, result is written to the given tensor and null return.

q)dot(2 3;1 10)
32

q)x:tensor 2 3
q)z:dot(x;1 10)
q)tensor z
32

householder

torch.linalg.householder_product is implemented as function householder(), which computes the first n columns of a product of Householder matrices.

householder(x;y) Householder product
householder(x;y;output) null
Parameters:
  • x (array,tensor) – a k array or tensor pointer of shape \((*, m, n)\) where \(*\) is zero or more batch dimensions

  • y (array,tensor) – a k array or tensor pointer of shape \((*, k)\) where \(*\) is zero or more batch dimensions

  • output (tensor) – an optional pointer to a previously allocated tensor to be used for output

Returns:

Returns the Householder products as a k array if both inputs given as k arrays, else tensor. If output tensor given, result is written to the given tensor and null return.

q)h:tensor(`randn;3 2 2;`cdouble)
q)tau:tensor(`randn;3 1;`cdouble)
q)q:householder(h;tau)
q)size q
3 2 2

matmul

torch.matmul is implemented as function matmul() which calculates the matrix product of two inputs. The function behaves differently depending on the dimensions of the inputs, e.g. if both inputs are 1-dimensional, the dot product is returned. If both inputs are matrices, a matrix-matrix product is calculated. See the PyTorch documentation for all the possible cases.

matmul(x;y) matrix product
matmul(x;y;output) null
Parameters:
  • x (array,tensor) – a k array or tensor pointer

  • y (array,tensor) – a k array or tensor pointer

  • output (tensor) – an optional pointer to a previously allocated tensor to be used for output

Returns:

Returns the matrix products as an array if both inputs given as k arrays, else tensor. If output tensor given, result is written to the given tensor and null return.

q)matmul(1 2 3;4 5 6)
32

q)matmul(2 3#1 2 3;4 5 6)
32 32

q)matmul(5 2 3#1 2 3;4 5 6)
32 32
32 32
32 32
32 32
32 32

q)x:tensor(`randn; 100 3 4)
q)y:tensor(`randn; 100 4 5)

q)z:matmul(x;y)
q)size z
100 3 5

mm

torch.mm is implemented as function mm(), which calculates a matrix multiplication of inputs x and y.

If x is a \((n \times m)\) matrix and y is a \((m \times p)\) matrix, the result will be a \((n \times p)\) matrix.

mm(x;y) matrix product
mm(x;y;output) null
Parameters:
  • x (array,tensor) – a k matrix or tensor pointer

  • y (array,tensor) – a k matrix or tensor pointer

  • output (tensor) – an optional pointer to a previously allocated tensor to be used for output

Returns:

Returns the matrix product as a matrix if both inputs given as k arrays, else tensor. If output tensor given, result is written to the given tensor and null return.

Note

This function does not support broadcasting. For broadcasting matrix products, see matmul().

q)seed 123
q)x:tensor(`randn; 2 3; `double)
q)y:tensor(`randn; 3 1; `double)
q)z:mm(x;y)

q)tensor z
-0.102
1.21

q)tensor[x]mmu tensor y
-0.102
1.21

mmt

Function mmt() is a k api function that implements mm(), but transposing the second input, i.e. \(x y^T\). Syntax and parameters are the same as for mm().

mmt(x;y) matrix product
mmt(x;y;output) null
q)x:3 2#0 1 2 3 4 5.0
q)y:3 2#6 7 8 9 8 9.0

q)x mmu flip y
7  9  9
33 43 43
59 77 77

q)mmt(x;y)
7  9  9
33 43 43
59 77 77

mtm

Function mtm() is a k api function that implements mm(), but with the first input transposed, i.e. \(x^T y\). Syntax and parameters are the same as for mm().

mtm(x;y) matrix product
mtm(x;y;output) null
q)x:3 2#0 1 2 3 4 5.0
q)y:3 2#6 7 8 9 8 9.0

q)flip[x]mmu y
48 54
70 79

q)mtm(x;y)
48 54
70 79

multidot

torch.linalg.multi_dot is implemented as function multidot(), which efficiently multiplies multiple matrices by reordering the multiplications so that the fewest arithmetic operations are performed.

multidot(x) product
Param:

array,tensors,vector x: a list of k arrays and/or tensors or a single vector of tensors.

Returns:

Returns the product of the given matrices as a k array if all k arrays given, else tensor.

q)n:1000000
q)x:tensor(`randn; n,5; `double)
q)y:tensor(`randn; 5,n; `double)
q)z:tensor(`randn; n,1; `double)

q)r:mm(x;y)
'[enforce fail at alloc_cpu.cpp:73] . DefaultCPUAllocator: can't allocate memory: you tried to allocate 8000000000000 bytes. Error code 12 (Cannot allocate memory)
  [0]  r:mm(x;y)
         ^

q)r:multidot(x;y;z)
q)size r
1000000 1

mv

torch.mv is implemented as function mv(), which calculates a matrix-vector product.

If x is a \((n \times m)\) matrix/tensor, y is a 1-dimensional list/tensor of size \(m\) and result will be 1-dimensional list of size \(n\).

mv(x;y) matrix-vector product
mv(x;y;output) null
Parameters:
  • x (array,tensor) – a k matrix or 2-dimensional tensor pointer

  • y (array,tensor) – a k list or 1-dimensional tensor pointer

  • output (tensor) – an optional pointer to a previously allocated tensor to be used for output

Returns:

Returns the matrix-vector product as a matrix if both inputs given as k arrays, else tensor. If output tensor given, result is written to the given tensor and null return.

Note

This function does not support broadcasting.

q)x:3 2#0 1 2 3 4 5.0
q)y:10 100.0

q)x mmu y
100 320 540f

q)mv(x;y)
100 320 540f

outer

torch.outer is implemented as function outer() which calculates the outer product of two 1-dimensional inputs.

If x is a list of size \(n\) and y is a list of size \(m\), then the result is a matrix of size \((n \times m)\).

outer(x;y) outer product
outer(x;y;output) null
Parameters:
  • x (array,tensor) – a k list or 1-dimensional tensor pointer

  • y (array,tensor) – a k list or 1-dimensional tensor pointer

  • output (tensor) – an optional pointer to a previously allocated tensor to be used for output

Returns:

Returns the outer product as a matrix if both inputs given as k list, else tensor. If output tensor given, result is written to the given tensor and null return.

Note

This function does not support broadcasting.

q)outer(.1 1;2 3 4.0)
0.2 0.3 0.4
2   3   4

q)x:tensor .1 1
q)z:outer(x;2 3 4.0)
q)tensor z
0.2 0.3 0.4
2   3   4

q)outer(x;1 2 3.0;z)
q)tensor z
0.1 0.2 0.3
1   2   3

Matrix products with addition

PyTorch has a series of functions of the form: \(\beta\ \text{x} + \alpha\ \text{f}(\text{y}, \text{z})\), where optional multipliers, \(\beta\) and \(\alpha\), are set to 1 if not supplied. Required inputs \(\text{x}, \text{y}, \text{z}\) may be supplied as k arrays or tensors, with the result returned as an allocated tensor if any of the inputs was also a tensor.

If the last argument, aside from inputs and multipliers, is also a tensor, this is taken to be an output tensor: function results will be written to the tensor and null returned.

addbmm

torch.addbmm is implemented by function addbmm(), which adds input x to the sum of matrix-matrix products of the 3-d batches of matrices given in y and z.

If \(y\) is \((b \times n \times m)\) and \(z\) is \((b \times m \times p)\), \(x\) must be broadcastable with a \((n \times p)\) array/tensor and the result will be a \((n \times p)\) array or tensor.

\[\beta\ \text{x} + \alpha\ (\sum_{i=0}^{b-1} \text{y}_i \mathbin{@} \text{z}_i) \]
addbmm(x;y;z;beta;alpha) sum of batch of matrix-matrix multiplications
addbmm(x;y;z;beta;alpha;output) null
Allowable argument combinations:
  • addbmm(x;y;z)

  • addbmm(x;y;z;beta)

  • addbmm(x;y;z;beta;alpha)

  • any of the above combinations followed by a trailing output tensor

Parameters:
  • x (array,tensor) – a k array or tensor pointer

  • y (array,tensor) – a 3-dimensional array or tensor pointer with batches of matrices

  • z (array,tensor) – a 3-dimensional array or tensor pointer with the same number of matrices as y

  • beta (number) – a numeric scalar, must be integer type if inputs are integral, else double, used as multiplier for x, default=1

  • alpha (number) – a numeric scalar, must be integer type if inputs are integral, else double, used as multiple for sum of matrix products, default=1

  • output (tensor) – an optional pointer to a previously allocated tensor to be used for output

Returns:

Returns the sum of matrix products as a matrix if all inputs given as k arrays, else tensor. If output tensor given, result is written to the given tensor and null return.

q)y:"f"$5 2 3#til 30
q)z:"f"$5 3 4#til 60

q)show r:addbmm(10.0;y;z)
7670 7865 8060 8255
8930 9170 9410 9650

q)10+sum y mmu z
7670 7865 8060 8255
8930 9170 9410 9650

q)r~/:{addbmm(x;y;z)}[;y;z] each (1; 1 4; 2 4)#\:10.0
111b

addmm

torch.addmm is implemented by function addmm(), which adds input x to the matrix product of y and z.

If y is \((n \times m)\) and z` is \((m \times p)\), then x must be broadcastable with a \((n \times p)\) matrix and the result will be a \((n \times p)\) matrix.

\[\beta\ \text{x} + \alpha\ (\text{y}_i \mathbin{@} \text{y}_i) \]
addmm(x;y;z;beta;alpha) sum of matrix-matrix multiplication with additional input
addmm(x;y;z;beta;alpha;output) null
Allowable argument combinations:
  • addmm(x;y;z)

  • addmm(x;y;z;beta)

  • addmm(x;y;z;beta;alpha)

  • any of the above combinations followed by a trailing output tensor

Parameters:
  • x (array,tensor) – a k array or tensor pointer

  • y (array,tensor) – a k matrix or tensor pointer

  • z (array,tensor) – a k matrix or tensor pointer

  • beta (number) – a numeric scalar, must be integer type if inputs are integral, else double, used as multiplier for x, default=1

  • alpha (number) – a numeric scalar, must be integer type if inputs are integral, else double, used as multiple for matrix product of y and z, default=1

  • output (tensor) – an optional pointer to a previously allocated tensor to be used for output

Returns:

Returns matrix if all inputs given as k arrays, else tensor. If output tensor given, result is written to the given tensor and null return.

q)x:2 5#til 10
q)y:2 3#til 6
q)z:3 5#til 15

q)addmm(x;y;z)
25 29 33  37  41
75 88 101 114 127

q){x set"f"$get x}'[`x`y`z];
q)x+y mmu z
25 29 33  37  41
75 88 101 114 127

Using beta and alpha multipliers:

q)addmm(x;y;z;100)
25  128 231 334 437
570 682 794 906 1018

q)(100*x)+y mmu z
25  128 231 334 437
570 682 794 906 1018

q)addmm(x;y;z;100;.1)
2.5 102.8 203.1 303.4 403.7
507 608.2 709.4 810.6 911.8

q)(100*x)+.1*y mmu z
2.5 102.8 203.1 303.4 403.7
507 608.2 709.4 810.6 911.8

addmv

torch.addmv is implemented by function addmv(), which adds input x to the matrix-vector product of y and z.

\[\beta\ \text{x} + \alpha\ (\text{y} \mathbin{@} \text{z}) \]

If \(y\) is a matrix of size \(n \times m\) and \(z\) is a vector of size \(m\), then \(x\) must be broadcastable with a vector of size \(n\) and the result will be a 1-dimensional tensor or list of size \(n\).

addmv(x;y;z;beta;alpha) sum of matrix-vector multiplication with additional input
addmv(x;y;z;beta;alpha;output) null
Allowable argument combinations:
  • addmv(x;y;z)

  • addmv(x;y;z;beta)

  • addmv(x;y;z;beta;alpha)

  • any of the above combinations followed by a trailing output tensor

Parameters:
  • x (array,tensor) – a k list or 1-dimensional tensor pointer

  • y (array,tensor) – a k matrix or 2-dimensional tensor pointer

  • z (array,tensor) – a k list or 1-dimensional tensor pointer

  • beta (number) – a numeric scalar, must be integer type if inputs are integral, else double, used as multiplier for x, default=1

  • alpha (number) – a numeric scalar, must be integer type if inputs are integral, else double, used as multiple for product of y and z, default=1

  • output (tensor) – an optional pointer to a previously allocated tensor to be used for output

Returns:

Returns matrix if all inputs given as k arrays, else tensor. If output tensor given, result is written to the given tensor and null return.

q)x:1 2
q)y:2 3#0 1 2 3 4 5
q)z:10 100 1000

q)mv(y;z)
2100 5430
q)x+mv(y;z)
2101 5432

q)addmv(x;y;z)
2101 5432

q)x+("f"$y)mmu "f"$z
2101 5432f

addr

torch.addr is implemented by function addr(), which adds input x to the outer product of inputs y and z.

\[\text{out} = \beta\ \text{input} + \alpha\ (\text{vec1} \otimes \text{vec2}) \]

If \(y\) is a vector of size \(n\) and \(z\) is a vector of size \(m\), then \(x\) must be broadcastable with a \((n \times m)\) matrix and the result will be a matrix of size \((n \times m)\).

addr(x;y;z;beta;alpha) sum of matrix-matrix multiplication with additional input
addr(x;y;z;beta;alpha;output) null
Allowable argument combinations:
  • addr(x;y;z)

  • addr(x;y;z;beta)

  • addr(x;y;z;beta;alpha)

  • any of the above combinations followed by a trailing output tensor

Parameters:
  • x (array,tensor) – a k array or tensor pointer

  • y (array,tensor) – a k list or 1-dimensional tensor pointer

  • z (array,tensor) – a k list or 1-dimensional tensor pointer

  • beta (number) – a numeric scalar, must be integer type if inputs are integral, else double, used as multiplier for x, default=1

  • alpha (number) – a numeric scalar, must be integer type if inputs are integral, else double, used as multiple for outer product of y and z, default=1

  • output (tensor) – an optional pointer to a previously allocated tensor to be used for output

Returns:

Returns matrix if all inputs given as k arrays, else tensor. If output tensor given, result is written to the given tensor and null return.

q)x:2 3#til 6
q)y:1 2
q)z:3 4 5

q)outer(y;z)
3 4 5
6 8 10

q)x+outer(y;z)
3 5  7
9 12 15

q)addr(x;y;z)
3 5  7
9 12 15

q)x+y*\:z
3 5  7
9 12 15

q)addr(x;y;z;100)
3   104 205
306 408 510

baddbmm

torch.baddbmm is implemented by function baddbmm(), which calculates a set of matrix-matrix products between 3-dimensional inputs y and z and adds the result to input x.

\[\beta\ \text{x}_i + \alpha\ (\text{y}_i \mathbin{@} \text{z}_i) \]

If \(y\) is \((b \times n \times m)\) and \(z\) is \((b \times m \times p)\), then \(x\) must be broadcastable with a \((b \times n \times p)\) array/tensor and the result will be a \((b \times n \times p)\) array/tensor.

baddbmm(x;y;z;beta;alpha) sum of batch of matrix-matrix multiplications
baddbmm(x;y;z;beta;alpha;output) null
Allowable argument combinations:
  • baddbmm(x;y;z)

  • baddbmm(x;y;z;beta)

  • baddbmm(x;y;z;beta;alpha)

  • any of the above combinations followed by a trailing output tensor

Parameters:
  • x (array,tensor) – a k array or tensor pointer

  • y (array,tensor) – a 3-dimensional array or tensor pointer with batches of matrices

  • z (array,tensor) – a 3-dimensional array or tensor pointer with the same number of matrices as y

  • beta (number) – a numeric scalar, must be integer type if inputs are integral, else double, used as multiplier for x, default=1

  • alpha (number) – a numeric scalar, must be integer type if inputs are integral, else double, used as multiple for the matrix products, default=1

  • output (tensor) – an optional pointer to a previously allocated tensor to be used for output

Returns:

If all inputs given as k arrays, returns a 3-dimensional k array else a tensor. If output tensor given, result is written to the given tensor and null return.

q)seed 123
q)x:tensor(`randn; 7 3 5; `double)
q)y:tensor(`randn; 7 3 2; `double)
q)z:tensor(`randn; 7 2 5; `double)

q)r:baddbmm(x;y;z)
q)size r
7 3 5

q)allclose(r; tensor[x]+tensor[y] mmu tensor z)
1b

q)baddbmm(x;y;z;0;r) /beta=0, output to r
q)allclose(r; tensor[y]mmu tensor z)
1b

Docs

Access documentation for k api to PyTorch

View Docs

Examples

Examples using the k api to PyTorch

Examples

Github

C++ library source code and q/k examples

Github