code: def fill_diagonal(
      a: Union[ivy.Array, ivy.NativeArray],
      v: Union[int, float, ivy.Array, ivy.NativeArray],
      /,
      *,
      wrap: bool = False,
  ) -> Union[ivy.Array, ivy.NativeArray]:
      """Fill the main diagonal of the given array of any dimensionality..
  
      Parameters
      ----------
      a
          Array at least 2D.
      v
          Value(s) to write on the diagonal. If val is scalar, the
          value is written along the diagonal. If array-like, the
          flattened val is written along the diagonal, repeating if
          necessary to fill all diagonal entries.
  
      wrap
          The diagonal 'wrapped' after N columns for tall matrices.
  
      Returns
      -------
      ret
          Array with the diagonal filled.
      """
      shape = a.shape
      max_end = ivy.prod(ivy.array(shape))
      end = max_end
      if len(shape) == 2:
          step = shape[1] + 1
          if not wrap:
              end = shape[1] * shape[1]
      else:
          step = int(
              1 + (ivy.cumprod(ivy.array(shape[:-1]), axis=0)).sum()
          )
      end = int(min(end, max_end))
      a = ivy.reshape(a, (-1,))
      steps = ivy.arange(0, end, step)
      if isinstance(v, (ivy.Array, ivy.NativeArray)):
          v = ivy.reshape(v, (-1,)).astype(a.dtype)
          v = ivy.tile(v, int(ivy.ceil(len(steps) / v.shape[0])))[
              : len(steps)
          ]
      else:
          v = ivy.repeat(v, len(steps))
      ivy.scatter_flat(
          steps, v, size=a.shape[0], reduction="replace", out=a
      )
      a = ivy.reshape(a, shape)
      return a
  
imports: from typing import Optional, Union, Tuple, Iterable, Sequence, Callable, Any, Literal, List
  from numbers import Number
  from functools import partial
  import math
  import ivy
  from ivy.func_wrapper import handle_out_argument, handle_partial_mixed_function, to_native_arrays_and_back, inputs_to_native_shapes, handle_nestable, handle_array_like_without_promotion, handle_view, inputs_to_ivy_arrays, handle_array_function, handle_device, handle_backend_invalid
  from ivy.functional.ivy.general import _numel
  from ivy.utils.backend import current_backend
  from ivy.utils.exceptions import handle_exceptions
label_desc: Use image node_id 47 for calling a global function with example usage: fill_diagonal() and returns: a