cupyx.scipy.sparse.diags(diagonals, offsets=0, shape=None, format=None, dtype=None)[source]#

Construct a sparse matrix from diagonals.

  • diagonals (sequence of array_like) – Sequence of arrays containing the matrix diagonals, corresponding to offsets.

  • offsets (sequence of int or an int) –

    Diagonals to set:
    • k = 0 the main diagonal (default)

    • k > 0 the k-th upper diagonal

    • k < 0 the k-th lower diagonal

  • shape (tuple of int) – Shape of the result. If omitted, a square matrix large enough to contain the diagonals is returned.

  • format ({"dia", "csr", "csc", "lil", ...}) – Matrix format of the result. By default (format=None) an appropriate sparse matrix format is returned. This choice is subject to change.

  • dtype (dtype) – Data type of the matrix.


Generated matrix.

Return type:



This function differs from spdiags in the way it handles off-diagonals.

The result from diags is the sparse equivalent of:

cupy.diag(diagonals[0], offsets[0])
+ ...
+ cupy.diag(diagonals[k], offsets[k])

Repeated diagonal offsets are disallowed.