Skip to contents

Chain_matmul

Usage

torch_chain_matmul(matrices)

Arguments

matrices

(Tensors...) a sequence of 2 or more 2-D tensors whose product is to be determined.

TEST

Returns the matrix product of the \(N\) 2-D tensors. This product is efficiently computed using the matrix chain order algorithm which selects the order in which incurs the lowest cost in terms of arithmetic operations ([CLRS]_). Note that since this is a function to compute the product, \(N\) needs to be greater than or equal to 2; if equal to 2 then a trivial matrix-matrix product is returned. If \(N\) is 1, then this is a no-op - the original matrix is returned as is.

Examples

if (torch_is_installed()) {

a = torch_randn(c(3, 4))
b = torch_randn(c(4, 5))
c = torch_randn(c(5, 6))
d = torch_randn(c(6, 7))
torch_chain_matmul(list(a, b, c, d))
}
#> torch_tensor
#>   5.2753   9.0749 -10.0314  16.1983  -8.9291 -27.9575  15.6468
#>   3.6463  -2.0789   3.7154  -5.4625   5.3446   8.7573  -4.3893
#>  -6.6320   0.8368  -3.4749   4.0188  -6.2127  -6.9446   2.7121
#> [ CPUFloatType{3,7} ]