# Condition on all eigenvalues of a matrix

I am going to help Mathematica to satisfy the condition:

If all elements of a matrix's eigenvalues are different from zero, then print "ok"

For matrix={{1, 1}, {2, 2}}:

If[Eigenvalues[matrix] != 0, Print@"OK"]


must print OK.

and for matrix={{1, 0}, {2, 0}} its result is nothing.

How can show All elements of a list in a condition?

• Your given matrix={{1, 0}, {2, 0}} has eigenvalues {1, 0} and so this command should print nothing; your statement that "it must print OK" disagrees with your requirement of printing "OK" only when all eigenvalues are nonzero. – Roman May 23 at 12:00
• you are right. I corrected it – Inzo Babaria May 23 at 12:44

If[Det[matrix] != 0, Print["ok"]]


The reason why this works is that Det[matrix] is the product of all of the eigenvalues of matrix. If and only if any of the eigenvalues is zero, then the determinant is zero. There is no need to calculate the eigenvalues here.

A paranoid version would be

If[! PossibleZeroQ[Det[matrix]], Print["ok"]]


which is a bit more careful in checking whether the determinant is really zero.

For large matrices you may need to calculate the determinant numerically if speed is an issue: Det[N[matrix]] instead of Det[matrix].

 matrix = {{1, 0}, {2, 0}};