# Numerical comparisons of matrices

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

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 absolute values of the eigenvalues of those matrices. (These eigenvalues 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
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

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.♦ Oct 4 '12 at 10:14

1
It might be good to not compare against zero but test if the norm is smaller than an epsilon. – ruebenko Oct 4 '12 at 11:44

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 4 '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 4 '12 at 15:01
show 3 more comments
lang-mma
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.3, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.9100081920623779, "perplexity_flag": "middle"}