def diags(diagonals, offsets, shape=None, format=None, dtype=None):
<EOL>try:<EOL><INDENT>iter(offsets)<EOL><DEDENT>except TypeError:<EOL><INDENT>try:<EOL><INDENT>iter(diagonals[<NUM_LIT:0>])<EOL><DEDENT>except TypeError:<EOL><INDENT>diagonals = [np.atleast_1d(diagonals)]<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>else:<EOL><INDENT>diagonals = list(map(np.atleast_1d, diagonals))<EOL><DEDENT>offsets = np.atleast_1d(offsets)<EOL>if len(diagonals) != len(offsets):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if shape is None:<EOL><INDENT>m = len(diagonals[<NUM_LIT:0>]) + abs(int(offsets[<NUM_LIT:0>]))<EOL>shape = (m, m)<EOL><DEDENT>if dtype is None:<EOL><INDENT>dtype = np.common_type(*diagonals)<EOL><DEDENT>m, n = shape<EOL>M = max([min(m + offset, n - offset) + max(<NUM_LIT:0>, offset)<EOL>for offset in offsets])<EOL>M = max(<NUM_LIT:0>, M)<EOL>data_arr = np.zeros((len(offsets), M), dtype=dtype)<EOL>for j, diagonal in enumerate(diagonals):<EOL><INDENT>offset = offsets[j]<EOL>k = max(<NUM_LIT:0>, offset)<EOL>length = min(m + offset, n - offset)<EOL>if length <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>" % (offset, j))<EOL><DEDENT>try:<EOL><INDENT>data_arr[j, k:k+length] = diagonal<EOL><DEDENT>except ValueError:<EOL><INDENT>if len(diagonal) != length and len(diagonal) != <NUM_LIT:1>:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % (<EOL>j, len(diagonal), offset, m, n))<EOL><DEDENT>raise<EOL><DEDENT><DEDENT>return dia_matrix((data_arr, offsets), shape=(m, n)).asformat(format)<EOL>
Construct a sparse matrix from diagonals.

Parameters
----------
diagonals : sequence of array_like
    Sequence of arrays containing the matrix diagonals,
    corresponding to `offsets`.
offsets  : sequence of int
    Diagonals to set:
      - k = 0  the main diagonal
      - k > 0  the k-th upper diagonal
      - k < 0  the k-th 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

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
--------
>>> 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.]])