Skip to content

dot

Perform the equivalent of numpy.dot on two arrays.

Parameters:

Name Type Description Default
a Union[SparseArray, ndarray, spmatrix]

The arrays to perform the dot operation on.

required
b Union[SparseArray, ndarray, spmatrix]

The arrays to perform the dot operation on.

required

Returns:

Type Description
Union[SparseArray, ndarray]

The result of the operation.

Raises:

Type Description
ValueError

If all arguments don't have zero fill-values.

See Also
  • numpy.dot : NumPy equivalent function.
  • COO.dot : Equivalent function for COO objects.
Source code in sparse/numba_backend/_common.py
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
def dot(a, b):
    """
    Perform the equivalent of [numpy.dot][] on two arrays.

    Parameters
    ----------
    a, b : Union[SparseArray, np.ndarray, scipy.sparse.spmatrix]
        The arrays to perform the `dot` operation on.

    Returns
    -------
    Union[SparseArray, numpy.ndarray]
        The result of the operation.

    Raises
    ------
    ValueError
        If all arguments don't have zero fill-values.

    See Also
    --------
    - [numpy.dot][] : NumPy equivalent function.
    - [COO.dot][sparse.COO.dot] : Equivalent function for COO objects.
    """
    check_zero_fill_value(a, b)
    if not hasattr(a, "ndim") or not hasattr(b, "ndim"):
        raise TypeError(f"Cannot perform dot product on types {type(a)}, {type(b)}")

    if a.ndim == 1 and b.ndim == 1:
        if isinstance(a, SparseArray):
            a = asCOO(a)
        if isinstance(b, SparseArray):
            b = asCOO(b)
        return (a * b).sum()

    a_axis = -1
    b_axis = -2

    if b.ndim == 1:
        b_axis = -1
    return tensordot(a, b, axes=(a_axis, b_axis))