I have two pieces of code that produce a bunch of real numbers, say $A$ and $B$ respectively. (It is not relevant to the question, but $A$ consists of eigenvalues of the Hamiltonian of some physical system, so these are all energies, and $B$ are some energies computed based on an ansatz.) In theory, it should be the case that $B \subset A$.
Because these sets seem to grow quite fast, and because the code is prone to bugs (like $+1$ instead of $-1$, or need for assumptions that some parameter does not become $0$), I would very much like to have an automated way of checking if the inclusion $B \subset A$ really holds for the produced sets.
If $A$ and $B$ were just sets of, say, integers, it would suffice to check that:
Complement[B,A]=={}
evaluates to True
. However, we are dealing with reals in finite precision arithmetic, so A
and B
consist merely of approximately equal numbers. I tried a workaround like first projecting them to lower precision, and then doing the check: Complement[N[B,4],N[A,4]]=={}
(the number of digits that agree is always much greater than $4$ so far, and if there is a difference because of a bug, it has always been within the first couple of digits). Unfortunately, this does not work reliably (I suspect because $A$ and $B$ might be rounded differently if close to some boundary value). Also, this approach isn't too elegant. Could someone suggest a better way?
SameTest
option ofComplement[]
? That should allow you to accommodate fuzzier comparisons. $\endgroup$