Search code examples
pythonnumpymatrix-multiplication

How to multiply many matrices in one go in python?


Suppose I have four matrices, a, b, c, and d.

In Python (with numpy), I need to do result = np.matmul(np.matmul(np.matmul(a,b),c),d) to multiply them.

In MATLAB/GNU Octave, I can multiply them in a much simpler manner, result = a*b*c*d.

Is there any way to multiply matrices in Python, so that I would not have to repeatedly write np.matmul avoid nested brackets?


Solution

  • Use the @ operator. result = a@b@c@d.