# An efficient way to compare two matrices

I have two matrices mat1 and mat2, both are results from different algorithms that are supposed to calculate the same result. The nmax is around 300, the q varies from 0.001 to 1.

expr1 = Exp[-q^2/2] Sum[q^(i + j - 2 k) (-1)^(-k + j) (Sqrt[i!] Sqrt[j!])/((i - k)! (j - k)! k!),
{k, 0, Min[i, j]}];

expr2 = Exp[-q^2/2] Sqrt[j!/i!] q^(i - j) LaguerreL[j, i - j, Abs[q]^2];

mat1 = Table[expr1, {i, 0, nmax}, {j, 0, nmax}];

mat2 = Table[expr2, {i, 0, nmax}, {j, 0, nmax}];


What would be the best method of comparing them depending on q to see if they are close enough to be considered the same result? Thanks in advance for the advices.

• can't you take just the norm of the difference of the 2 matrices? There are number of norms defined for matrices. reference.wolfram.com/language/ref/Norm.html may be pick the appropriate norm for your needs, as in Norm[mat1-mat2, "Frobenius"] ? This will work for sparse matrices as well. Oct 26, 2014 at 20:46

expr1 = Exp[-q^2/2] Sum[
q^(i + j - 2 k) (-1)^(-k + j) (Sqrt[i!] Sqrt[j!])/((i - k)! (j - k)! k!), {k, 0,Min[i, j]}];
expr2 = Exp[-q^2/2] Sqrt[j!/i!] q^(i - j) LaguerreL[j, i - j, Abs[q]^2];
mat1 := Table[expr1, {i, 0, nmax}, {j, 0, nmax}];
mat2 := Table[expr2, {i, 0, nmax}, {j, 0, nmax}];

nmax = 30; mat1 // Dimensions


{31, 31}

matb1 = mat1 /. Abs[a_]^b_?EvenQ -> a^b // Simplify;
matb2 = mat2 /. Abs[a_]^b_?EvenQ -> a^b // Simplify;

stlist = Select[matb1 - matb2 // Flatten // Union, (# =!= 0 &)];
stlist // FullSimplify


{0, 0, 0, 0, 0}

• Does this true for any q > 0?
– Suro
Oct 26, 2014 at 22:18
• @Suren Sure, that is symbolically same. Oct 26, 2014 at 22:32