# Remove +0Is from Root function

I'm trying to find the zeros of the eigenvalues (functions of $$k_x$$) of a self-adjoint $$4\times4$$ matrix H:

H = {{kx^2 + a kx + B, 0, I a kx,
del}, {0, -kx^2 + a kx - B, -del, -I a kx}, {-I a kx, -del,
kx^2 - a kx - B, 0}, {del, I a kx, 0, -kx^2 - a kx + B}};
B = 1;
a = 0.5;
del = 0.02;
evals = Eigenvalues[H]
solns = Table[NSolve[evals[[ii]] == 0, kx, Reals], {ii, 4}]


My eigenvalues are all real, but when mathematica calculates them, because of the complex entries in the off-diagonals, it produces a +0I inside the root on some of the coefficients. This causes a problem when I try to solve restricting the domain to the reals:

Solve::nddc: The system contains a nonreal constant 0.128451 +0. I. With the domain specified, all constants should be real.


I have tried using Re which doesn't work, and Chop which completely breaks the output when applied to the smaller numbers in the full code (I've tried multiplying by a constant and dividing afterwards). ToRadicals leads to an extremely large expression which NSolve struggles to evaluate (FullSimplify on this gives similar problems). How do I remove the +0I efficiently?

• Does expr /. I -> 0 work? Sep 4, 2019 at 12:43
• Provide definition of H so that we can reproduce the problem Sep 4, 2019 at 13:24
• @MariusLadegårdMeyer Nope Sep 4, 2019 at 13:28
• @BobHanlon updated Sep 4, 2019 at 13:36
• Please provide the definition in Mathematica code along with the values of the parameters that are causing the problem. Sep 4, 2019 at 13:37

Clear[H, B, a, del]

H = {{kx^2 + a kx + B, 0, I a kx,
del}, {0, -kx^2 + a kx - B, -del, -I a kx}, {-I a kx, -del,
kx^2 - a kx - B, 0}, {del, I a kx, 0, -kx^2 - a kx + B}};
B = 1;
a = 0.5;
del = 0.02;


Use Chop in the definition of evals to eliminate the complex artifacts (0. I).

evals = Eigenvalues[H] // Chop;

solns = NSolve[# == 0, kx, Reals] & /@ evals

(* {{}, {{kx -> -1.34174}, {kx -> -0.845824}}, {{kx -> 1.34174}, {kx ->
0.845824}}, {}} *)


Alternatively, use exact values for the parameters

Clear[H, B, a, del]

H = {{kx^2 + a kx + B, 0, I a kx,
del}, {0, -kx^2 + a kx - B, -del, -I a kx}, {-I a kx, -del,
kx^2 - a kx - B, 0}, {del, I a kx, 0, -kx^2 - a kx + B}};
B = 1;
a = 1/2;
del = 1/50;

evals = Eigenvalues[H];

solns2 = NSolve[# == 0, kx, Reals] & /@ evals

(* {{}, {{kx -> -1.34174}, {kx -> -0.845824}}, {{kx -> 1.34174}, {kx ->
0.845824}}, {}} *)

solns == solns2

(* True *)