Reputation:
Given a scipy.sparse.csr.csr_matrix
, is there a quick way to return the elements on the diagonal?
The reason I would like to do this is to compute inv(D) A
, where D
is a diagonal matrix whose diagonal entries agree with A
(A
is my sparse matrix, guaranteed to have nonzeros on the diagonal).
Upvotes: 2
Views: 1394
Reputation: 73366
Returns the main diagonal of the matrix
Example:
>>> import numpy as np
>>> from scipy.sparse import csr_matrix
>>> mymat = csr_matrix((4, 4), dtype=np.int8)
>>> mymat.diagonal()
array([0, 0, 0, 0], dtype=int8)
Upvotes: 2