When writing tests for numerical code, a typical thing I have to do is to assert that an output float array is what I expect. Since I do not want to store the entire reference array, I usually just compare the sum of the array entries against a reference number. To be sure, I take the sum of some powers, e.g.,
import foobar
import numpy
def test_array():
x = foobar.generate_values()
assert isinstance(x, numpy.ndarray)
assert x.dtype == numpy.float64
ref_sum1 = 1.4311
assert numpy.abs(numpy.sum(x) - ref_sum1) < 1.0e-13
ref_sum2 = 8.2343
assert numpy.abs(numpy.sum(x ** 2) - ref_sum2) < 1.0e-13
ref_sum3 = -0.7534
assert numpy.abs(numpy.sum(x ** 3) - ref_sum3) < 1.0e-13
But wait! This doesn't catch permutations of x
, and certainly there are other caveats too.
Is there a more systematic way of testing for (near) equality in float arrays?
Aucun commentaire:
Enregistrer un commentaire