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?

  • $\begingroup$ 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. $\endgroup$ – Roman May 23 at 12:00
  • $\begingroup$ you are right. I corrected it $\endgroup$ – 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].

If you think I'm not addressing your question, please read about the XY problem.


Check this:

 matrix = {{1, 0}, {2, 0}};
 If[MemberQ[Eigenvalues[matrix], 0], , Print["Ok"]]

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.