0

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?

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
  • You are looking for some sort of continuous array hashing function? – John Coleman Dec 18 '20 at 15:05
  • Relevant: [Hashing 2D, 3D and nD vectors](https://stackoverflow.com/q/5928725/4996248) and [Good way to hash a float vector?](https://stackoverflow.com/q/650175/4996248). You are going from a higher-dimensional space to 1-dimensional space. Some information loss is inevitable. If the goal is to *test* a numerical algorithm, is introducing another source of error really a good idea? – John Coleman Dec 18 '20 at 15:21

0 Answers0