bundles / scipy 1.18.0 / scipy / sparse / _construct / spdiags
function
scipy.sparse._construct:spdiags
source: /scipy/sparse/_construct.py:255
Signature
def spdiags(data, diags, m = None, n = None, format = None)Summary
Return a sparse matrix from diagonals.
Extended Summary
Parameters
data: array_likeMatrix diagonals stored row-wise
diags: sequence of int or an intDiagonals to set:
k = 0 the main diagonal
k > 0 the kth upper diagonal
k < 0 the kth lower diagonal
m, n: int, tuple, optionalShape of the result. If
nis None andmis a given tuple, the shape is this tuple. If omitted, the matrix is square and its shape islen(data[0]).format: str, optionalFormat of the result. By default (format=None) an appropriate sparse matrix format is returned. This choice is subject to change.
Returns
new_matrix: sparse matrixdia_matrix format with values in
dataon diagonals fromdiags.
Notes
This function can be replaced by an equivalent call to dia_matrix as
dia_matrix((data, diags), shape=(m, n)).asformat(format)Examples
import numpy as np from scipy.sparse import spdiags data = np.array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]) diags = np.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]])
See also
- dia_matrix
the sparse DIAgonal format.
- diags
matrix version of diags_array.
- diags_array
more convenient form of this function.
Aliases
scipy.sparse.spdiags