code: def group_columns(A, order=0):
      """Group columns of a 2-D matrix for sparse finite differencing [1]_.
  
      Two columns are in the same group if in each row at least one of them
      has zero. A greedy sequential algorithm is used to construct groups.
  
      Parameters
      ----------
      A : array_like or sparse matrix, shape (m, n)
          Matrix of which to group columns.
      order : int, iterable of int with shape (n,) or None
          Permutation array which defines the order of columns enumeration.
          If int or None, a random permutation is used with `order` used as
          a random seed. Default is 0, that is use a random permutation but
          guarantee repeatability.
  
      Returns
      -------
      groups : ndarray of int, shape (n,)
          Contains values from 0 to n_groups-1, where n_groups is the number
          of found groups. Each value ``groups[i]`` is an index of a group to
          which ith column assigned. The procedure was helpful only if
          n_groups is significantly less than n.
  
      References
      ----------
      .. [1] A. Curtis, M. J. D. Powell, and J. Reid, "On the estimation of
             sparse Jacobian matrices", Journal of the Institute of Mathematics
             and its Applications, 13 (1974), pp. 117-120.
      """
      if issparse(A):
          A = csc_matrix(A)
      else:
          A = np.atleast_2d(A)
          A = (A != 0).astype(np.int32)
  
      if A.ndim != 2:
          raise ValueError("`A` must be 2-dimensional.")
  
      m, n = A.shape
  
      if order is None or np.isscalar(order):
          rng = np.random.RandomState(order)
          order = rng.permutation(n)
      else:
          order = np.asarray(order)
          if order.shape != (n,):
              raise ValueError("`order` has incorrect shape.")
  
      A = A[:, order]
  
      if issparse(A):
          groups = group_sparse(m, n, A.indices, A.indptr)
      else:
          groups = group_dense(m, n, A)
  
      groups[order] = groups.copy()
  
      return groups
  
file_path: scipy/scipy/optimize/_numdiff.py
imports: import functools
  import numpy
  from numpy.linalg import norm
  from scipy.sparse.linalg import LinearOperator
  from ..sparse import issparse, csc_matrix, csr_matrix, coo_matrix, find
  from ._group_columns import group_dense, group_sparse
  from scipy._lib._array_api import atleast_nd, array_namespace
label_desc: Use image node_id 5 for calling a global function with example usage: group_columns(A, order) and returns: groups