bsparse.sparse.diag#

sparse.diag(values, offset=0, shape=None, dtype=None, format='coo')#

Create a sparse diagonal matrix of specified values and offset.

Parameters:
valuesArrayLike

The values of the diagonal.

offsetint, optional

The offset of the diagonal, by default 0.

shapetuple[int, int], optional

The shape of the matrix, by default None.

dtypenp.dtype, optional

The data type of the matrix elements, by default None.

formatstr, optional

The sparse format of the matrix, by default “coo”.

Returns:
Sparse

A sparse diagonal matrix of specified values and offset.

Examples

>>> diag([1, 2, 3])
COO(shape=(3, 3), nnz=3, dtype=int64)
>>> diag([1, 2, 3], format="csr").toarray()
array([[1, 0, 0],
       [0, 2, 0],
       [0, 0, 3]])
>>> diag([1, 2, 3], offset=1).toarray()
array([[0, 1, 0, 0],
       [0, 0, 2, 0],
       [0, 0, 0, 3],
       [0, 0, 0, 0]])