I need to compare two matrices, built using two different functions.
a=csr_matrix(M) (scipy function)
b=SparseMatrix(M) (My own function)
If I print 'a' and 'b' I get:
[[10. 20. 0. 0. 0. 0.]
[ 0. 30. 99. 40. 0. 0.]
[ 0. 0. 50. 60. 70. 0.]
[ 0. 0. 0. 0. 0. 80.]]
[[10. 20. 0. 0. 0. 0.]
[ 0. 30. 99. 40. 0. 0.]
[ 0. 0. 50. 60. 70. 0.]
[ 0. 0. 0. 0. 0. 80.]]
ie. the identical matrix.
BUT!: If I do
print(a==b)
it returns 'false'
Now,
print(a.dtype) = float64
print(b.dtype) = 'SparseMatrix' object has no attribute 'dtype'.
I'm guessing this difference in regard to the type is the reason the boolean returns false?
Is there a smooth way to overcome this problem? - Preferably without changing 'SparseMatrix'.
Edit: Here is 'SparseMatrix':
class SparseMatrix:
def __init__(self, A, tol = 1e-8):
"""
Parameters
----------
A : Numpy.Array
Input matrix.
tol : float, optional
Tolerance for what is to be considered equal to 0. The default is 1e-8.
Returns
-------
None.
"""
self.n_rows = A.shape[0]
self.n_cols = A.shape[1]
self.intern_represent = 'CSR'
self.tol = tol
self.V = []
self.col_index = []
self.row_index = zeros(self.n_rows + 1, dtype = int)
self.number_of_nonzero = 0
for row in range(self.n_rows):
self.row_index[row] = self.number_of_nonzero
for column in range(self.n_cols):
if abs(A[row, column]) >= tol:
self.number_of_nonzero += 1
self.V.append(A[row, column])
self.col_index.append(column)
self.row_index[-1] = self.number_of_nonzero