2

I have a matrix which should be equal to a null matrix. However due to the numerical precision, a brutal equality test with a matrix initialized with zeros does not work.

How should I perform the numerical equality test (with a given threshold for the precision) ?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Cedric H.
  • 696
  • 1
  • 4
  • 15

1 Answers1

5

A simpler way than to adjust the threshold for Equal is to use Chop which

replaces approximate real numbers in expr that are close to zero by the exact integer 0.

Adding the suggestions from the comments you have the following possibilities:

  • Use Chop as it is. Here, you may only chop the Norm at the end by Chop[Norm[mat, 1]] == 0.
  • Look at the second argument to Chop when you want to adjust the default tolerance. Ideally, it should correspond to the "sizes" of the matrices from which the putative null was constructed. For instance, if those matrices involve numbers in the millions, then any matrix whose coordinates are all less than about 0.0001 would likely need to be considered null. Typically, the second argument will be somewhere between the smallest and largest singular values of those matrices. (These singular values can reliably be found with SingularValueDecomposition; in many applications, they are already available from previous computations.)
  • Look at the Internal`$EqualTolerance (which is probably not the best idea in your case).
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
halirutan
  • 112,764
  • 7
  • 263
  • 474
  • 1
    Note, also, that one can control the tolerance used by Chop[] through its second argument. See the docs for details. – J. M.'s missing motivation Oct 04 '12 at 10:03
  • 1
    Another possible route to check if the matrix mat is a null matrix: Chop[Norm[mat, 1]] == 0; only a null matrix has zero norm. – J. M.'s missing motivation Oct 04 '12 at 10:14
  • 1
    It might be good to not compare against zero but test if the norm is smaller than an epsilon. –  Oct 04 '12 at 11:44
  • Perfect, thanks for the answer and the comments. – Cedric H. Oct 04 '12 at 12:05
  • 1
    Be careful. There does not exist any absolute universal test like this. The comparison of a matrix to zero needs to account for the matrices used to create it. As an example, emulate the SingularValueDecomposition help by creating a random matrix m with entries on the order of, say, $10^{12}$, reconstruct m via its SVD, and subtract the reconstruction from m to see whether the two are equal. They're not--due to imprecision--but chop won't help. How much imprecision should one expect? Use the sizes of the eigenvalues of m (the diagonal of the SVD) to estimate the tolerance. – whuber Oct 04 '12 at 14:44
  • 1
    @whuber I gave you comment +1 and I would be happy if you insert your concerns into the answer. – halirutan Oct 04 '12 at 15:01
  • @whuber: "Use the sizes of the eigenvalues of m (the diagonal of the SVD) to estimate the tolerance." - I suppose, since Norm[mat] == Max[SingularValueList[mat]]... you and ruebenko are right, maybe machine epsilon times the norm of some convenient matrix might be a better comparison... – J. M.'s missing motivation Oct 04 '12 at 15:17
  • @J.M. Right. But I was purposely vague, because in some situations you might want to use the smallest absolute eigenvalue as the tolerance threshold. The main point is that the comparison to zero cannot possibly be absolute; it has to be relative to the characteristic size of the problem. This is true even in the $1$ by $1$ case--that is, for numbers. – whuber Oct 04 '12 at 15:45