Returns a matrix product of two arrays.


You can use operator symbol ‘@’ instead of matmul() in Python 3.5 or later version.

>>> from pyqubo import Array
>>> array_a = Array.create('a', shape=(2, 4), vartype='BINARY')
>>> array_b = Array.create('b', shape=(4, 3), vartype='BINARY')
>>> array_a @ array_b == array_a.matmul(array_b)
Parameters:other (Array/numpy.ndarray/list) –


Matrix product of two arrays falls into 3 patterns.

1. If either of the arguments is 1-D array, it is treated as a matrix where one is added to its dimension.

>>> from pyqubo import Array, Binary
>>> array_a = Array([[Binary('a'), Binary('b')], [Binary('c'), Binary('d')]])
>>> array_b = Array([Binary('e'), Binary('f')])
>>> array_a.matmul(array_b)
Array([((Binary(a)*Binary(e))+(Binary(b)*Binary(f))),                 ((Binary(c)*Binary(e))+(Binary(d)*Binary(f)))])
  1. If both arguments are 2-D array, conventional matrix product is calculated.
>>> array_a = Array([[Binary('a'), Binary('b')], [Binary('c'), Binary('d')]])
>>> array_b = Array([[Binary('e'), Binary('f')], [Binary('g'), Binary('h')]])
>>> array_a.matmul(array_b)
Array([[((Binary(a)*Binary(e))+(Binary(b)*Binary(g))),                 ((Binary(a)*Binary(f))+(Binary(b)*Binary(h)))],
       [((Binary(c)*Binary(e))+(Binary(d)*Binary(g))),                 ((Binary(c)*Binary(f))+(Binary(d)*Binary(h)))]])

3. If either argument is N-D (where N > 2), it is treated as an array whose element is a 2-D matrix of last two indices. In this example, array_a is treated as if it is a vector whose elements are two matrices of shape (2, 3).

>>> array_a = Array.create('a', shape=(2, 2, 3), vartype='BINARY')
>>> array_b = Array.create('b', shape=(3, 2), vartype='BINARY')
>>> (array_a @ array_b)[0] == array_a[0].matmul(array_b)