>>> import scipy
>>> scipy.__version__
'0.19.1'
>>> from scipy.sparse import csr_matrix
>>> A1 = csr_matrix([[1,2],[2,3],[3,4]])
>>> A2 = csr_matrix([[1,2],[2,3],[3,4]])
>>> B = csr_matrix([[1,2],[3,4],[5,6]])
Überprüfen Sie, ob die Anzahl der Nicht-Null-Elemente in A1 --A2
0 ist.
# equal
>>> A1 - A2
<3x2 sparse matrix of type '<class 'numpy.int64'>'
with 0 stored elements in Compressed Sparse Row format>
>>> (A1 - A2).nnz == 0
True
# not equal
>>> A1 - B
<3x2 sparse matrix of type '<class 'numpy.int64'>'
with 4 stored elements in Compressed Sparse Row format>
>>> (A1 - B).nnz == 0
False
Dies kann auch bestätigt werden, indem geprüft wird, ob die Anzahl der Nicht-Null-Elemente von "A1! = A2" 0 wird.
# equal
>>> A1 != A2
<3x2 sparse matrix of type '<class 'numpy.bool_'>'
with 0 stored elements in Compressed Sparse Row format>
>>> (A1 != A2).nnz == 0
True
# not equal
>>> A1 != B
<3x2 sparse matrix of type '<class 'numpy.bool_'>'
with 4 stored elements in Compressed Sparse Row format>
>>> (A1 != B).nnz == 0
False
Eine ähnliche Vergleichsmethode kann mit scipy.sparse.coo_matrix
und scipy.sparse.csc_matix
verwendet werden.