import itertools


def nonzero_entries(array, eps=1e-7):
  """Extracts sparse [(value, *indices), ...] array representation.

  Args:
    array: The numpy array to obtain a sparse representation for,
    eps: Threshold magnitude. Entries <= `eps` are skipped.
  Returns:
    List of (coefficient, index0, ..., indexN) tuples representing
    the non-zero entries of the array.
  """
  entries = []
  for indices in itertools.product(*[range(n) for n in array.shape]):
    v = array[indices]
    if abs(v) <= eps:
      continue
    entries.append((v,) + indices)
  return entries