avatar Bite 31. Matrix multiplication / @ operator

Since 3.5 Python has a binary operator to be used for matrix multiplication: @, see PEP 465 -- A dedicated infix operator for matrix multiplication.

The @ sign can now be used on types implementing the __matmul__ special/magic/dunder method.

It is important to note that whilst this feature shipped in 3.5, none of the standard library builtin types have matrix multiplication implementations. So let's try to implement it on a custom type for this Bite.

Implement a simple class called Matrix that takes a list of lists in its constructor.

Implement the __matmul__, __rmatmul__ (reversed) and __imatmul__ (in place) dunder methods.

Yes, using numpy a np.dot(self, other) would suffice, but the point is to get you thinking about implementing @ yourself!

Here is a how matrix multiplication works:

A = [[1, 2],  [3, 4]]
B = [[11, 12], [13, 14]]

Doing A @ B would do the following multiplications:

[[1 * 11 + 2 * 13,   1 * 12 + 2 * 14],
[3 * 11 + 4 * 13,   3 * 12 + 4 * 14]]

See the tests for more info. Good luck!

Special thanks to Anthony Shaw for providing the idea and collaborating with us on this Bite!
Login and get coding
go back Advanced level
Bitecoin 4X

190 out of 196 users completed this Bite.
Will you be the 191st person to crack this Bite?
Resolution time: ~78 min. (avg. submissions of 5-240 min.)
Our community rates this Bite 7.67 on a 1-10 difficulty scale.
» Up for a challenge? 💪

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help