Efficiently multiplies two or more matrices by reordering the multiplications so that the fewest arithmetic operations are performed.
Arguments
- tensors
(
Sequence[Tensor]
): two or more tensors to multiply. The first and last tensors may be 1D or 2D. Every other tensor must be 2D.
Details
Supports inputs of float
, double
, cfloat
and cdouble
dtypes.
This function does not support batched inputs.
Every tensor in tensors
must be 2D, except for the first and last which
may be 1D. If the first tensor is a 1D vector of shape (n,)
it is treated as a row vector
of shape (1, n)
, similarly if the last tensor is a 1D vector of shape (n,)
it is treated
as a column vector of shape (n, 1)
.
If the first and last tensors are matrices, the output will be a matrix. However, if either is a 1D vector, then the output will be a 1D vector.
Note
This function is implemented by chaining torch_mm()
calls after
computing the optimal matrix multiplication order.
The cost of multiplying two matrices with shapes (a, b)
and (b, c)
is
a * b * c
. Given matrices A
, B
, C
with shapes (10, 100)
,
(100, 5)
, (5, 50)
respectively, we can calculate the cost of different
multiplication orders as follows:
In this case, multiplying A
and B
first followed by C
is 10 times faster.
See also
Other linalg:
linalg_cholesky_ex()
,
linalg_cholesky()
,
linalg_det()
,
linalg_eigh()
,
linalg_eigvalsh()
,
linalg_eigvals()
,
linalg_eig()
,
linalg_householder_product()
,
linalg_inv_ex()
,
linalg_inv()
,
linalg_lstsq()
,
linalg_matrix_norm()
,
linalg_matrix_power()
,
linalg_matrix_rank()
,
linalg_norm()
,
linalg_pinv()
,
linalg_qr()
,
linalg_slogdet()
,
linalg_solve_triangular()
,
linalg_solve()
,
linalg_svdvals()
,
linalg_svd()
,
linalg_tensorinv()
,
linalg_tensorsolve()
,
linalg_vector_norm()
Examples
if (torch_is_installed()) {
linalg_multi_dot(list(torch_tensor(c(1, 2)), torch_tensor(c(2, 3))))
}
#> torch_tensor
#> 8
#> [ CPUFloatType{} ]