In [1]:
#sparse

from scipy import sparse as sp
from numpy import array,r_
In [3]:
I = array([0,0,1,3,1,0,0])
J = array([0,2,1,3,1,0,0])
V = array([1,1,1,1,1,1,2])
# zde se opakovane indexy poscitaji
B = sp.coo_matrix((V,(I,J)),shape=(4,4)).tocsr()
B.todense()
Out[3]:
matrix([[4, 0, 1, 0],
        [0, 2, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 1]], dtype=int64)
In [5]:
#zde se prepisi
A = sp.coo_matrix((V,(I,J)),shape=(4,4)).todia()
(A-B).todense()
Out[5]:
matrix([[-2,  0,  0,  0],
        [ 0, -1,  0,  0],
        [ 0,  0,  0,  0],
        [ 0,  0,  0,  0]], dtype=int64)
In [10]:
from scipy import io
io.mmwrite("resmat",B,precision=5)
!head resmat.mtx
%%MatrixMarket matrix coordinate integer general
%
4 4 4
1 1 4
1 3 1
2 2 2
4 4 1
In [9]:
C=sp.coo_matrix((V*(1.2+.2j),(I,J)),shape=(4,4)).tocsr()
io.mmwrite("resmat2",C,precision=5)
!head resmat2.mtx
%%MatrixMarket matrix coordinate complex general
%
4 4 4
1 1 4.8 0.8
1 3 1.2 0.2
2 2 2.4 0.4
4 4 1.2 0.2

Back to main site