code: def to_coo(x, ldnz=None, requires_grad=False):
      """Returns a single or a batch of matrices in COO format.
  
      Args:
          x (:ref:`ndarray`): Input dense matrix. The ndim of
              ``x`` must be two or three. If ndim is two, it is treated as
              a single matrix. If three, it is treated as batched matrices.
          ldnz (int): Size of arrays for data, row index and column index to be
              created. The Actual size becomes max(nnz, ldnz) where nnz is number
              of non-zero elements in a input dense matrix.
          requires_grad (bool): If ``True``, gradient of sparse matrix will be
              computed in back-propagation.
  
      Returns:
          ~chainer.utils.CooMatrix: A sparse matrix or batched sparse matrices
          in COO format of a given dense matrix or batched dense matrices.
  
      .. admonition:: Example
  
          Create a :class:`~chainer.utils.CooMatrix` from an array with 2
          non-zero elements and 4 zeros and access its attributes. No batch
          dimension is involved.
  
          .. doctest::
  
              >>> data = np.array([[0, 2, 0], [-1, 0, 0]], np.float32)
              >>> x = chainer.utils.to_coo(data)
              >>> x.data
              variable([ 2., -1.])
              >>> x.row
              array([0, 1], dtype=int32)
              >>> x.col
              array([1, 0], dtype=int32)
              >>> x.shape
              (2, 3)
      """
      xp = backend.get_array_module(x)
      if x.ndim == 2:
          _row, _col = xp.where(x != 0)
          nnz = len(_row)
          if ldnz is None or ldnz < nnz:
              ldnz = nnz
          data = xp.zeros((ldnz), dtype=x.dtype)
          row = xp.full((ldnz), -1, dtype=xp.int32)
          col = xp.full((ldnz), -1, dtype=xp.int32)
          data[:nnz] = x[_row, _col]
          row[:nnz] = xp.array(_row).astype(xp.int32)
          col[:nnz] = xp.array(_col).astype(xp.int32)
          shape = x.shape
          return CooMatrix(
              data, row, col, shape, requires_grad=requires_grad
          )
      elif x.ndim == 3:
          # first axis is batch axis
          nb = x.shape[0]
          if ldnz is None:
              ldnz = 0
          for i in range(nb):
              ldnz = max(ldnz, len(xp.where(x[i] != 0)[0]))
          data = xp.empty((nb, ldnz), dtype=x.dtype)
          row = xp.empty((nb, ldnz), dtype=xp.int32)
          col = xp.empty((nb, ldnz), dtype=xp.int32)
          for i in range(nb):
              coo = to_coo(x[i], ldnz)
              data[i] = coo.data.data
              row[i] = coo.row
              col[i] = coo.col
          shape = x.shape[1:]
          return CooMatrix(
              data, row, col, shape, requires_grad=requires_grad
          )
      else:
          raise ValueError("ndim of x must be 2 or 3.")
  
file_path: chainer/chainer/utils/sparse.py
imports: import numpy
  import chainer
  from chainer import backend
  from chainer import cuda
label_desc: Use image node_id 2 for calling a global function with example usage: to_coo(x, ldnz, requires_grad) and returns: CooMatrix, CooMatrix