cupyx.scipy.sparse.safely_cast_index_arrays#

cupyx.scipy.sparse.safely_cast_index_arrays(A, idx_dtype=<class 'numpy.int32'>, msg='')[source]#

Safely cast sparse array indices to idx_dtype.

Check the shape of A to determine if it is safe to cast its index arrays to dtype idx_dtype. If any dimension in shape is larger than fits in the dtype, casting is unsafe so raise ValueError. If safe, cast the index arrays to idx_dtype and return the result without changing the input A. The caller can assign the results to A’s attributes if desired or use the recast index arrays directly.

Unless downcasting is needed, the original index arrays are returned. You can test e.g. A.indptr is new_indptr to see if downcasting occurred.

Parameters:
  • A (cupyx.scipy.sparse) – The array for which index arrays should be (potentially) downcast.

  • idx_dtype (dtype) – Desired index dtype. Defaults to numpy.int32.

  • msg (str, optional) – String appended to the ValueError message when A.shape is too big to fit in idx_dtype.

Returns:

For CSR/CSC, (indices, indptr). For COO, (row, col) – CuPy exposes the two coordinate arrays rather than SciPy’s n-D coords tuple, and a 1-D COO array carries an all-zero row. For DIA, offsets.

Return type:

ndarray or tuple of ndarrays

Raises:

ValueError – When the dtype cannot represent A’s shape or existing index values.