bsparse.diag#

diag(values, offset=0, overlap=0, bshape=None, dtype=None, format='bcoo')#

Create a sparse diagonal matrix of specified values and offset.

Parameters:
valueslist

The values of the diagonal.

offsetint, optional

The offset of the diagonal, by default 0.

overlapint, optional

The overlap of the diagonal blocks, by default 0. If overlap > 0, the diagonal blocks overlap additively by the specified amount. The l

bshapetuple[int, int], optional

The bshape 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:
BSparse

A sparse diagonal matrix of specified values and offset.

Examples

>>> import numpy as np
>>> values = [i * np.ones((2, 2)) for i in range(1, 4)]
>>> bcoo = diag(values)
>>> bcoo
BCOO(bshape=(3, 3), bnnz=3 | shape=(6, 6), nnz=12)
>>> bcoo.toarray()
array([[1., 1., 0., 0., 0., 0.],
       [1., 1., 0., 0., 0., 0.],
       [0., 0., 2., 2., 0., 0.],
       [0., 0., 2., 2., 0., 0.],
       [0., 0., 0., 0., 3., 3.],
       [0., 0., 0., 0., 3., 3.]])

With overlap:

>>> bcoo = diag(values, overlap=1)
>>> bcoo
BCOO(bshape=(4, 4), bnnz=10 | shape=(4, 4), nnz=10)
>>> bcoo.toarray()
array([[1., 1., 0., 0.],
       [1., 3., 2., 0.],
       [0., 2., 5., 3.],
       [0., 0., 3., 3.]])