I have written a class that takes in a matrix as argument and changes it's internal representation to CSR. Now I'm trying to write a class method that changes the internal representation from CSR to another format(in my case it's to CSC, but choose any simple format you wish). The problem is that I don't know how to change the attributes/methods when making such a method, I just get errors. Any TIPS? Here is my code:
I would like to write a method called to_csc that returns a new object represented in other formats.
I would like to write a method called to_csc that returns a new object represented in other formats.
import numpy as np
class SparseMatrix:
def __init__(self,matrix):
self.matrix=np.asarray(matrix)
self.intern_represent = 'CSR'
self.number_of_nonzero
self.IA
self.values
self.JA
def change (self,row,column,new):
self.matrix [row][column]=new
def __repr__(self):
return (' A= '+ str(self.values) +
'\nIA= ' + str(self.IA) +
'\nJA= '+ str(self.JA))
@property
def IA (self):
IA =[]
IA.append (0)
for i in range (1,np.shape(self.matrix)[0]+1):
num=IA [i-1] + np.count_nonzero(self.matrix [i-1])
IA.append(num)
IA=np.asarray (IA)
return IA
@property
def values (self):
return self.matrix [np.nonzero(self.matrix)]
@property
def JA (self):
index=np.nonzero(self.matrix)
return index [1]
@property
def number_of_nonzero(self):
return len (self.values)
