Im asked to write a clss SparseMatrix with an __init__ method which uses a numpy.array as matrix input and which generates an internal representation of the matrix in CSR form. Furthermore it should set an attribute intern_represent to the value 'CSR'. After that i need to give the class a method changing the internal representation from CSR to CSC. For that ill need to change the attribute intern_represent.
I know about the CSR and CSC format of a matrix, but i dont understand what im asked to do with setting an attribute to the value CSR. Is it to be able to do print (object.csr) and print(object.csc) and get the different formats printed? In that case how is that done?
Here is my code:
I know about the CSR and CSC format of a matrix, but i dont understand what im asked to do with setting an attribute to the value CSR. Is it to be able to do print (object.csr) and print(object.csc) and get the different formats printed? In that case how is that done?
Here is my code:
import numpy as np class SparseMatrix: def __init__(self,matrix): self.matrix = np.asarray(matrix) def getnonzeroind (self): return np.nonzero (self.matrix) def __getitem__(self,index): return self.matrix[index] @property def shape (self): return np.shape (self.matrix) @property def IA (self): IA =[] IA.append (0) for i in range (1,self.shape[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): coords=self.getnonzeroind() return self.matrix [coords] @property def JA (self): index=self.getnonzeroind () return index [1] def __repr__(self): return (' A= '+ str(self.values) + '\nIA= ' + str(self.IA) + '\nJA= '+ str(self.JA))