flopscope.numpy.linalg.matmul
fnp.linalg.matmul(x1: 'ArrayLike', x2: 'ArrayLike', /) -> 'FlopscopeArray'[flopscope source][numpy source]
Computes the matrix product.
Adapted from NumPy docs np.linalg.matmul
Delegates to `fnp.matmul` which charges `m*k*n` FLOPs (FMA=1).
This function is Array API compatible, contrary to flops.matmul.
Parameters
- x1:array_like
The first input array.
- x2:array_like
The second input array.
Returns
- out:ndarray
The matrix product of the inputs. This is a scalar only when both
x1,x2are 1-d vectors.
Raises
- :ValueError
If the last dimension of
x1is not the same size as the second-to-last dimension ofx2.If a scalar value is passed in.
See also
Examples
For 2-D arrays it is the matrix product:
>>> a = flops.array([[1, 0],
... [0, 1]])
>>> b = flops.array([[4, 1],
... [2, 2]])
>>> flops.linalg.matmul(a, b)
array([[4, 1],
[2, 2]])For 2-D mixed with 1-D, the result is the usual.
>>> a = flops.array([[1, 0],
... [0, 1]])
>>> b = flops.array([1, 2])
>>> flops.linalg.matmul(a, b)
array([1, 2])
>>> flops.linalg.matmul(b, a)
array([1, 2])Broadcasting is conventional for stacks of arrays
>>> a = flops.arange(2 * 2 * 4).reshape((2, 2, 4))
>>> b = flops.arange(2 * 2 * 4).reshape((2, 4, 2))
>>> flops.linalg.matmul(a,b).shape
(2, 2, 2)
>>> flops.linalg.matmul(a, b)[0, 1, 1]
98
>>> sum(a[0, 1, :] * b[0 , :, 1])
98Vector, vector returns the scalar inner product, but neither argument is complex-conjugated:
>>> flops.linalg.matmul([2j, 3j], [2j, 3j])
(-13+0j)Scalar multiplication raises an error.
>>> flops.linalg.matmul([1,2], 3)
Traceback (most recent call last):
...
ValueError: matmul: Input operand 1 does not have enough dimensions ...