0
$\begingroup$

I am trying to show that two matrices are the same.

One matrix has random elements drawn from a distribution of ${(-0.75,-0.25,0,0.25,0.75,0.95)}$, while the other has values with imaginary parts too (but they are close to the entries ${(-0.75,-0.25,0,0.25,0.75,0.95)}$). In fact, they need to be the same when I approximate the matrix with imaginary elements.

I have tried both N and Round but, when using Round, the elements of the matrix with complex entries approximate to 1, -1.

Hence, what command shall I use so that the two matrices are the same?

$\endgroup$
8
  • $\begingroup$ You may want to use Re to only get the real parts of the second matrix? $\endgroup$ – Carl Lange Feb 19 '19 at 22:37
  • $\begingroup$ Yes. That works. I used Chop as well to approximate the very small values to 0. However, when I try to test it, it does not give me the answer "True" or "False". I am using MatrixForm[A]==MatrixForm[B]. Does the command not work for matrices? $\endgroup$ – user62722 Feb 19 '19 at 22:54
  • 1
    $\begingroup$ MatrixForm should be used only for display: mathematica.stackexchange.com/questions/3098/… and mathematica.stackexchange.com/questions/18393/… detail that. A == B is what you're looking for. $\endgroup$ – Carl Lange Feb 19 '19 at 22:56
  • $\begingroup$ What I did precisely is: $"Re[B]==A"$. When not using MatrixForm, it says it is false. I double-checked and both A and B are defined as a list. I checked it visually and the two are the same. $\endgroup$ – user62722 Feb 19 '19 at 23:02
  • $\begingroup$ Please update your question with copy-pasteable code so that we can see exactly what we're working with :) $\endgroup$ – Carl Lange Feb 19 '19 at 23:09
1
$\begingroup$

Make a matrix:

m = {{0., -1.}, {1., 0.}}

Put in some small imaginary parts:

mapp = m + I 10^-10

Check for equality:

m == mapp
(* False *)

Mathematica has heuristics for approximate equality. If they aren't working for your specific problem, you may have to come up with a better measure, for example:

Norm[m - mapp] < 10^-9
(* True *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy