code: def diag(v, k=0):
      """Returns a diagonal or a diagonal array.
  
      Args:
          v (array-like): Array or array-like object.
          k (int): Index of diagonals. Zero indicates the main diagonal, a
              positive value an upper diagonal, and a negative value a lower
              diagonal.
  
      Returns:
          cupy.ndarray: If ``v`` indicates a 1-D array, then it returns a 2-D
          array with the specified diagonal filled by ``v``. If ``v`` indicates a
          2-D array, then it returns the specified diagonal of ``v``. In latter
          case, if ``v`` is a :class:`cupy.ndarray` object, then its view is
          returned.
  
      .. seealso:: :func:`numpy.diag`
  
      """
      if isinstance(v, cupy.ndarray):
          ndim = v.ndim
      else:
          ndim = numpy.ndim(v)
          if ndim == 1:
              v = cupy.array(v)
          if ndim == 2:
              # to save bandwidth, don't copy non-diag elements to GPU
              v = numpy.array(v)
  
      if ndim == 1:
          size = v.size + abs(k)
          ret = cupy.zeros((size, size), dtype=v.dtype)
          ret.diagonal(k)[:] = v
          return ret
      elif ndim == 2:
          return cupy.array(v.diagonal(k))
      else:
          raise ValueError("Input must be 1- or 2-d.")
  
file_path: cupy/cupy/_creation/matrix.py
imports: import numpy
  import cupy
  from cupy import _core
label_desc: Use image node_id 1 for calling a global function with example usage: diag(v, k) and returns: ret, cupy