Dec-18-2017, 03:49 PM
Im asked to make a class SparseMatrix that takes in a numpy array and represent it in a specific format(in my case its CSR format). Then im asked to add a method to the class that changes the representation from CSR to another format(in my case to CSC, but in casd you wish to help you can choose any simple format you wish). So basicly i would like to be able to create an instance "a=SparseMatrix(array)" and the have a method "toCCS" so that "b=a.toCCS()" creates a new instance b having the same array but represented a different format. The problem is that i get alot of attribute errors and other whatever i try... any tips?
import numpy as np class SparseMatrix: def __init__(self,matrix): self.intern_represent = 'CSR' self.matrix=np.asarray(matrix) @property def numofnonzero(self): return len(self.values) def __repr__(self): return (' A= '+ str(self.values) + '\nIA= ' + str(self.IA) + '\nJA= '+ str(self.JA)) def change(self,i,j,new): self.matrix[i][j]=new @property def values(self): return self.matrix[np.nonzero(self.matrix)] @property def IA (self): IAlist =[] IAlist.append (0) for i in range (1,np.shape(self.matrix)[0]+1): num=IAlist [i-1] + np.count_nonzero(self.matrix [i-1]) IAlist.append(num) IAlist=np.asarray (IAlist) return IAlist @property def JA(self): return np.nonzero(self.matrix)[1] def toCSC(self): #Here i want to be able to create a new instance with the same array but changing #the properties values ,IA and JA so the __repr__ prints it out in the desired way a=SparseMatrix([[0,1,0],[1,2,3]]) print(a)