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) ?
Answer
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
Chopas it is. Here, you may only chop theNormat the end byChop[Norm[mat, 1]] == 0. - Look at the second argument to
Chopwhen 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 withSingularValueDecomposition; 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).
Comments
Post a Comment