2

Suppose I have a numpy array of matricies

X = np.array([[[1.1, 2.1],
               [2.1, 1.1]],

              [[1.2, 2.2],
               [2.2, 1.2]],

              [[1.3, 2.3],
               [2.3, 1.3]]])

How do I make a matrix multiplication of all of them?

The problem comes from the function X = foo(a) that gives a matrix output. Once I calculate it on vector of length N, I have N matricies. Mathematically, I want to find this

ans = X[0] @ X[1] ... @ X[N-1]

For extremely large N loop approach is going to be slow :( So I want to vectorize it somehow.

Any help is appriciated!

  • This doesn't 'vectorize' because there isn't compiled code that does sequential `dot`. `matmul` does parellel batched `dots` – hpaulj May 18 '22 at 07:21

1 Answers1

3

numpy.linalg.multi_dot helps to multiply a sequence of matrices at once, and also internally optimizes the call order in order to reduce the number of computations required.

I also observe that there's a mathematical pattern in the sequence of matrices provided in question. If you could reduce it to a formula, then perhaps you could reduce the number of matrices needed to multiply.

Aravind G.
  • 401
  • 5
  • 13
  • 3
    But these arrays are all the same shape. Order does not matter. I don't think multidot will help. – hpaulj May 18 '22 at 07:13
  • @hpaulj Ah I see, yes you are right. I just found out that multidot doesn't run any C code, it just calls np.dot internally after optimizing the order. Still, it should not be slower than a manual loop I think? – Aravind G. May 18 '22 at 07:17
  • 1
    I looked at it in 2017, see Ofek's link. – hpaulj May 18 '22 at 07:18
  • Thanks! Indeed, just typing np.linalg.multi_dot(X) works as expected. – Ivan Toftul May 19 '22 at 03:21