scipy 1.8.0 Pypi GitHub Homepage
Other Docs
NotesParametersBackRef
diags(diagonals, offsets=0, shape=None, format=None, dtype=None)

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

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.

Construct a sparse matrix from diagonals.

See Also

spdiags

construct matrix from diagonals

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

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

scipy.sparse._construct.bmat scipy.sparse._construct.diags scipy.sparse._construct.block_diag scipy.sparse._construct.spdiags scipy.sparse.linalg._eigen._svds.svds

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /scipy/sparse/_construct.py#66
type: <class 'function'>
Commit: