79632293

Date: 2025-05-21 15:23:50
Score: 0.5
Natty:
Report link

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_
Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: PBB