code: def diagonalize_real_symmetric_and_sorted_diagonal_matrices(
          symmetric_matrix: np.ndarray,
          diagonal_matrix: np.ndarray,
          *,
          rtol: float = 1e-5,
          atol: float = 1e-8,
          check_preconditions: bool = True) -> np.ndarray:
      if check_preconditions:
          if (np.any(np.imag(symmetric_matrix)) or
                  not predicates.is_hermitian(symmetric_matrix,
                                              rtol=rtol,
                                              atol=atol)):
              raise ValueError('symmetric_matrix must be real symmetric.')
          if (not predicates.is_diagonal(diagonal_matrix, atol=atol) or
                  np.any(np.imag(diagonal_matrix)) or
                  np.any(diagonal_matrix[:-1, :-1] < diagonal_matrix[1:, 1:])):
              raise ValueError(
                  'diagonal_matrix must be real diagonal descending.')
          if not predicates.commutes(diagonal_matrix,
                                     symmetric_matrix,
                                     rtol=rtol,
                                     atol=atol):
              raise ValueError('Given matrices must commute.')
      def similar_singular(i, j):
          return np.allclose(diagonal_matrix[i, i],
                              diagonal_matrix[j, j], rtol=rtol)
      ranges = _contiguous_groups(diagonal_matrix.shape[0], similar_singular)
      p = np.zeros(symmetric_matrix.shape, dtype=np.float64)
      for start, end in ranges:
          block = symmetric_matrix[start:end, start:end]
          p[start:end, start:end] = diagonalize_real_symmetric_matrix(
              block, rtol=rtol, atol=atol)
      return p