python - Add values to a Scipy sparse matrix with indexes and values -
i'm working in program power system analysis , need work sparse matrices.
there routine fill sparse matrix following call:
self.a = bsr_matrix((val, (row,col)), shape=(nele, nbus), dtype=complex)
as matrix won't change on time. yet matrix change on time , need update it. there way having, example:
co = [ 2, 3, 6] row = [ 5, 5, 5] val = [ 0.1 + 0.1j, 0.1 - 0.2j, 0.1 - 0.4j]
i can add initialized sparse matrix? how more pythonic way it?
thank you
you should use coo_matrix
instead, can change attributes col
, row
, data
of created sparse matrix:
from scipy.sparse import coo_matrix nele=30 nbus=40 col = [ 2, 3, 6] row = [ 5, 5, 5] val = [ 0.1 + 0.1j, 0.1 - 0.2j, 0.1 - 0.4j] test = coo_matrix((val, (row,col)), shape=(nele, nbus), dtype=complex) print test.col #[2 3 6] print test.row #[5 5 5] print test.data #[ 0.1+0.1j 0.1-0.2j 0.1-0.4j]
Comments
Post a Comment