code: def diags(diagonals, offsets=0, shape=None, format=None, dtype=None):
      """
      Construct a sparse matrix from diagonals.
  
      .. warning::
  
          This function returns a sparse matrix -- not a sparse array.
          You are encouraged to use ``diags_array`` to take advantage
          of the sparse array functionality.
  
      Parameters
      ----------
      diagonals : sequence of array_like
          Sequence of arrays containing the matrix diagonals,
          corresponding to `offsets`.
      offsets : sequence of int or an int, optional
          Diagonals to set:
            - k = 0  the main diagonal (default)
            - k > 0  the kth upper diagonal
            - k < 0  the kth lower diagonal
      shape : tuple of int, optional
          Shape of the result. If omitted, a square matrix large enough
          to contain the diagonals is returned.
      format : {"dia", "csr", "csc", "lil", ...}, optional
          Matrix format of the result. By default (format=None) an
          appropriate sparse matrix format is returned. This choice is
          subject to change.
      dtype : dtype, optional
          Data type of the matrix.
  
      See Also
      --------
      spdiags : construct matrix from diagonals
      diags_array : construct sparse array instead of sparse matrix
  
      Notes
      -----
      This function differs from `spdiags` in the way it handles
      off-diagonals.
  
      The result from `diags` is the sparse equivalent of::
  
          np.diag(diagonals[0], offsets[0])
          + ...
          + np.diag(diagonals[k], offsets[k])
  
      Repeated diagonal offsets are disallowed.
  
      .. versionadded:: 0.11
  
      Examples
      --------
      >>> from scipy.sparse import diags
      >>> diagonals = [[1, 2, 3, 4], [1, 2, 3], [1, 2]]
      >>> diags(diagonals, [0, -1, 2]).toarray()
      array([[1, 0, 1, 0],
             [1, 2, 0, 2],
             [0, 2, 3, 0],
             [0, 0, 3, 4]])
  
      Broadcasting of scalars is supported (but shape needs to be
      specified):
  
      >>> diags([1, -2, 1], [-1, 0, 1], shape=(4, 4)).toarray()
      array([[-2.,  1.,  0.,  0.],
             [ 1., -2.,  1.,  0.],
             [ 0.,  1., -2.,  1.],
             [ 0.,  0.,  1., -2.]])
  
  
      If only one diagonal is wanted (as in `numpy.diag`), the following
      works as well:
  
      >>> diags([1, 2, 3], 1).toarray()
      array([[ 0.,  1.,  0.,  0.],
             [ 0.,  0.,  2.,  0.],
             [ 0.,  0.,  0.,  3.],
             [ 0.,  0.,  0.,  0.]])
      """
      A = diags_array(
          diagonals, offsets=offsets, shape=shape, dtype=dtype
      )
      return dia_matrix(A).asformat(format)
  
imports: import numbers
  import math
  import numpy
  from scipy._lib._util import check_random_state, rng_integers
  from ._sputils import upcast, get_index_dtype, isscalarlike
  from ._sparsetools import csr_hstack
  from ._bsr import bsr_matrix, bsr_array
  from ._coo import coo_matrix, coo_array
  from ._csc import csc_matrix, csc_array
  from ._csr import csr_matrix, csr_array
  from ._dia import dia_matrix, dia_array
  from ._base import issparse, sparray
label_desc: Use image node_id 3 for calling a global function with example usage: diags(diagonals, offsets, shape, format, dtype) and returns: dia_matrix