Reputation: 1
I am new to python and was wondering if something like numpy.matmul exists for N square matrices? Can this not be done without a loop?
I don't know how to do this without looping through the matrices.
Upvotes: 0
Views: 49
Reputation: 5559
If you mean you want to multiply N matrices, yes you can use np.linalg.multi_dot
like this:
np.linalg.multi_dot([A, B, C])
which is equivalent to A @ B @ C
or np.matmul(np.matmul(A, B), C)
or np.dot(np.dot(A, B), C)
or A.dot(B).dot(C)
.
And if you want to multiply many matrices, you can use reduce
from the functools
module.
matrices = [A, B, C, D, E, F]
result = reduce(np.dot, matrices)
Upvotes: 1