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

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -