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) ?


1 Answer 1


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).
  • 1
    $\begingroup$ Note, also, that one can control the tolerance used by Chop[] through its second argument. See the docs for details. $\endgroup$ Oct 4, 2012 at 10:03
  • 1
    $\begingroup$ 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. $\endgroup$ Oct 4, 2012 at 10:14
  • 1
    $\begingroup$ It might be good to not compare against zero but test if the norm is smaller than an epsilon. $\endgroup$
    – user21
    Oct 4, 2012 at 11:44
  • 1
    $\begingroup$ 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. $\endgroup$
    – whuber
    Oct 4, 2012 at 14:44
  • 1
    $\begingroup$ @whuber I gave you comment +1 and I would be happy if you insert your concerns into the answer. $\endgroup$
    – halirutan
    Oct 4, 2012 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.