# How to run 1000 examples of matrices

I have two functions of matrices, let say $$f(A,\,B)$$ and $$g(A,\,B)$$, where $$A,\,B$$ are positive semidefinite matrices. I want to check if $$f(A, B)$$ is greater than $$g(A,\,B)$$ for any $$A,\,B > 0$$. I want to run 1000 examples of random $$A,\,B$$ and check and stop the loop when

$$\qquad f(A,B) - g(A,B) < 0$$,

i.e, when a counter example appears. And I need to print out the counter example, too. Would you please help me with that?

• What you are really trying to do is prove it doesn't hold true, by looking for a counter-example. It is not true that not finding a counter-example means it is true. Post your efforts so far, so we can help. May 20, 2020 at 18:44
• Yes, that is what I want to check. Actually, I want to compare x=det(log((A+B)/2)) - 1/2 log(det(AB)) and y=det(A+B)^(1/n) - det(B)^(1/n) -det(A)^(1/n), where n is the size of matrices, A and B are positive definite. I want to run 1000 of examples of random A and B and compare x and y. Thanks. May 20, 2020 at 19:19
• @HoaDinh: What is the logarithm of a matrix? May 20, 2020 at 21:58
• You can use polar decomposition A =U*diag(A)U. If all eigenvalues of A are positive then we may apply log to all eigenvalues in diag(A) to get log(A). In analysis it is the spectral theorem, or, sometime we call functional calculus. May 21, 2020 at 15:32

Supposing that you have defined your function f(a,b) and g(a,b), and that a and b are to be 2 by 2 matrices:

Select[Table[a = RandomReal[{-1, 1}, {2, 2}];
b = RandomReal[{-1, 1}, {2, 2}];
If[f[a, b] - g[a, b] < 0, {a, b}], {i, 1000}], #=!=Null &]


This lists all the {a,b} for which the condition holds. You can save a bit (as Henrik points out):

Table[a = RandomReal[{-1, 1}, {2, 2}];
b = RandomReal[{-1, 1}, {2, 2}];
If[f[a, b] - g[a, b] < 0, {a, b}, Nothing], {i, 1000}]

• It might be more useful to just use Sow[]+Reap[] with Do[], as opposed to outputting a list full of Nulls. May 20, 2020 at 19:44
• That's what Nothing is good for... It saves you the Select step (but I doubt that it is efficient). May 20, 2020 at 20:03
• ParallelTable may help wiith the speed May 20, 2020 at 20:27

You can use Catch and Throw:

SeedRandom[0];
Block[{a, b, f, g},
exception = Catch[
Do[
a = RandomReal[{-1, 1}, {2, 2}];
a = a\[Transpose].a;
b = RandomReal[{-1, 1}, {2, 2}];
b = b\[Transpose].b;
f[x_, y_] :=
Det@MatrixFunction[Log, (x + y)/2] - Log[Det[x.y]]/2;
g[x_, y_] := Det[x + y]^(1/2) - Det[x]^(1/2) - Det[y]^(1/2);
If[f[a, b] - g[a, b] < 0,
Throw[i -> {a, b}, "exc"]], (* or leave out "i ->" *)
{i, 1000}],
"exc"
]]
(*
92 -> {{{0.923165, 0.870033}, {0.870033, 1.09025}},
{{1.68746, -0.281795}, {-0.281795, 1.43311}}}
*)


Or Return:

Block[{a, b, f, g},
exception = Do[
a = RandomReal[{-1, 1}, {2, 2}];
a = a\[Transpose].a;
b = RandomReal[{-1, 1}, {2, 2}];
b = b\[Transpose].b;
f[x_, y_] := Det@MatrixFunction[Log, (x + y)/2] - Log[Det[x.y]]/2;
g[x_, y_] := Det[x + y]^(1/2) - Det[x]^(1/2) - Det[y]^(1/2);
If[f[a, b] - g[a, b] < 0,
Return[i -> {a, b}, Do]],
{i, 1000}]
]

• Thank you so much! May 21, 2020 at 15:30