SciPy

scipy.sparse.spdiags

scipy.sparse.spdiags(data, diags, m, n, format=None)[source]

Return a sparse matrix from diagonals.

Parameters:

data : array_like

matrix diagonals stored row-wise

diags : diagonals to set

  • k = 0 the main diagonal
  • k > 0 the k-th upper diagonal
  • k < 0 the k-th lower diagonal

m, n : int

shape of the result

format : format of the result (e.g. “csr”)

By default (format=None) an appropriate sparse matrix format is returned. This choice is subject to change.

See also

diags
more convenient form of this function
dia_matrix
the sparse DIAgonal format.

Examples

>>> data = array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]])
>>> diags = array([0, -1, 2])
>>> spdiags(data, diags, 4, 4).toarray()
array([[1, 0, 3, 0],
       [1, 2, 0, 4],
       [0, 2, 3, 0],
       [0, 0, 3, 4]])

Previous topic

scipy.sparse.diags

Next topic

scipy.sparse.block_diag