code: def smart_array_index(array, indices):
      """
      Index array along its first dimension, smartly handling empty and broadcasted arrays to avoid
      allocating.
      """
      if len(array) > 0 and (array.size == 0 or array.strides[0] == 0):
          # this handles two cases when NumPy is suboptimal for StellarGraph:
          #
          # - 'array' containing no data, because one of its dimensions is zero length: fancy indexing
          #   allocates unnecessarily (FIXME https://github.com/numpy/numpy/issues/16410)
          #
          # - 'array' being a broadcasted array, so every element along the first dimension is the
          #   same; in some places (edge weights, in particular), it's fine to preserve this
          #   broadcasting when indexing, rather than allocating a whole new array.
  
          largest = indices.max(initial=0)
          smallest = indices.min(initial=0)
          valid = -len(array) <= smallest and largest < len(array)
  
          if valid:
              # if the indexing would work in this case, the elements are indistinguishable, so we can
              # rebroadcast (to ensure the length matches `indices`). If the indices are invalid, the
              # error is handled by the fallback below.
              final_shape = (*indices.shape, *array.shape[1:])
              return np.broadcast_to(array[:1, ...], final_shape)
  
      # fallback to normal indexing
      return array[indices]
  
file_path: stellargraph/stellargraph/core/utils.py
imports: import collections
  import scipy.sparse
  from scipy.sparse.linalg import ArpackNoConvergence, eigsh
  import numpy
label_desc: Use image node_id 3 for calling a global function with example usage: smart_array_index(array, indices) and returns: array, np