Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 5 down vote accepted

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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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