code: def csr_polynomial_expansion(X, interaction_only, degree):
      """Apply polynomial expansion on CSR matrix
  
      Parameters
      ----------
      X : sparse CSR matrix
          Input array
  
      Returns
      -------
      New expansed matrix
      """
      assert degree in (2, 3)
  
      interaction_only = 1 if interaction_only else 0
  
      d = X.shape[1]
      if degree == 2:
          expanded_dimensionality = int(
              (d**2 + d) / 2 - interaction_only * d
          )
      else:
          expanded_dimensionality = int(
              (d**3 + 3 * d**2 + 2 * d) / 6
              - interaction_only * d**2
          )
      if expanded_dimensionality == 0:
          return None
      assert expanded_dimensionality > 0
  
      nnz = cp.diff(X.indptr)
      if degree == 2:
          total_nnz = (nnz**2 + nnz) / 2 - interaction_only * nnz
      else:
          total_nnz = (
              nnz**3 + 3 * nnz**2 + 2 * nnz
          ) / 6 - interaction_only * nnz**2
      del nnz
      nnz_cumsum = total_nnz.cumsum(dtype=cp.int64)
      total_nnz_max = int(total_nnz.max())
      total_nnz = int(total_nnz.sum())
  
      num_rows = X.indptr.shape[0] - 1
  
      expanded_data = cp.empty(shape=total_nnz, dtype=X.data.dtype)
      expanded_indices = cp.empty(
          shape=total_nnz, dtype=X.indices.dtype
      )
      expanded_indptr = cp.empty(
          shape=num_rows + 1, dtype=X.indptr.dtype
      )
      expanded_indptr[0] = X.indptr[0]
      expanded_indptr[1:] = nnz_cumsum
  
      tpb = (32, 32)
      bpg_x = ceil(X.indptr.shape[0] / tpb[0])
      bpg_y = ceil(total_nnz_max / tpb[1])
      bpg = (bpg_x, bpg_y)
      perform_expansion[bpg, tpb](
          X.indptr,
          X.indices,
          X.data,
          expanded_data,
          expanded_indices,
          d,
          interaction_only,
          degree,
          expanded_indptr,
      )
  
      return cpx.scipy.sparse.csr_matrix(
          (expanded_data, expanded_indices, expanded_indptr),
          shape=(num_rows, expanded_dimensionality),
      )
  
file_path: cuml/python/cuml/thirdparty_adapters/sparsefuncs_fast.py
imports: from math import ceil
  from cuml.internals.safe_imports import gpu_only_import_from
  from cuml.internals.safe_imports import gpu_only_import
label_desc: Use image node_id 12 for calling a global function with example usage: csr_polynomial_expansion(X, interaction_only, degree) and returns: cpx, None