cunumeric.matmul#

cunumeric.matmul(a: ndarray, b: ndarray, /, out: ndarray | None = None, *, casting: CastingKind = 'same_kind', dtype: np.dtype[Any] | None = None) ndarray#

Matrix product of two arrays.

Parameters:
  • a (array_like) – Input arrays, scalars not allowed.

  • b (array_like) – Input arrays, scalars not allowed.

  • out (ndarray, optional) – A location into which the result is stored. If provided, it must have a shape that matches the signature (n,k),(k,m)->(n,m).

  • casting ({'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional) –

    Controls what kind of data casting may occur.

    • ’no’ means the data types should not be cast at all.

    • ’equiv’ means only byte-order changes are allowed.

    • ’safe’ means only casts which can preserve values are allowed.

    • ’same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed.

    • ’unsafe’ means any data conversions may be done.

    Default is ‘same_kind’.

  • dtype (data-type, optional) – If provided, forces the calculation to use the data type specified. Note that you may have to also give a more liberal casting parameter to allow the conversions. Default is None.

Returns:

output – The matrix product of the inputs. This is a scalar only when both a, b are 1-d vectors. If out is given, then it is returned.

Return type:

ndarray

Notes

The behavior depends on the arguments in the following way.

  • If both arguments are 2-D they are multiplied like conventional matrices.

  • If either argument is N-D, N > 2, it is treated as a stack of matrices residing in the last two indexes and broadcast accordingly.

  • If the first argument is 1-D, it is promoted to a matrix by prepending a 1 to its dimensions. After matrix multiplication the prepended 1 is removed.

  • If the second argument is 1-D, it is promoted to a matrix by appending a 1 to its dimensions. After matrix multiplication the appended 1 is removed.

matmul differs from dot in two important ways:

  • Multiplication by scalars is not allowed, use * instead.

  • Stacks of matrices are broadcast together as if the matrices were elements, respecting the signature (n,k),(k,m)->(n,m):

    >>> a = ones([9, 5, 7, 4])
    >>> c = ones([9, 5, 4, 3])
    >>> dot(a: ndarray, c).shape
    (9, 5, 7, 9, 5, 3)
    >>> matmul(a: ndarray, c).shape
    (9, 5, 7, 3)
    >>> # n is 7, k is 4, m is 3
    

The cuNumeric implementation is a little more liberal than NumPy in terms of allowed broadcasting, e.g. matmul(ones((3,1)), ones((4,5))) is allowed.

Only floating-point types are supported.

See also

numpy.matmul

Availability:

Multiple GPUs, Multiple CPUs