Thanks for the pointer on how to code this. However, the answer from @K. Nielson has a small error, so I'm posting this here for other people. Consider this MWE:
import numpy as np
from scipy import sparse
a = sparse.eye(3).tocsr()
b = b.copy().tolil()
b[1, 0] = 1e-16
b = b.tocsr()
print(f"{np.allclose(a.todense(), b.todense())=}")
# np.allclose(a.todense(), b.todense())=True
print(f"{csr_allclose(a, b)=}")
# csr_allclose(a, b)=np.False_
Here, the proposed answer gives False
. Looking more closely at the NumPy docs, there is an np.abs
too much. This works:
def csr_allclose2(a, b, rtol=1e-5, atol = 1e-8):
c = np.abs(a - b) - rtol * np.abs(b)
return c.max() <= atol
print(f"{csr_allclose2(a, b)=}")
# csr_allclose2(a, b)=np.True_