# ContourPlot taking too long

This code creates an 12x12 matrix with entries being complex functions of (kx,ky). Then asks to sort the eigenvalues and make Plot3D or ContourPlot of the lowest eigenvalue. The code was producing reasonable results, but I made some very minor changes (which I don't recall) and it now takes forever (> 4-5 mins) to run, sometimes complaining about "Unable to find all roots of the characteristic polynomial" (which is weird). Can anyone help with identifying what might be going wrong. {I'm running Mathematica 11.0 on a Mac OS X 10.11.6, and it was working just fine.}

Hfull is the 12x12 matrix whose entries are straightforward complex functions of (kx,ky). The matrix is Hermitian. However, the code piece below is taking forever.

ContourPlot[Sort[Eigenvalues[Hfull]][], {kx, -Pi,Pi}, {ky, -Pi, Pi}]


As a quick check, I tried

p1 = Plot[Sort[Eigenvalues[Hfull /. ky -> 0.]][], {kx, -Pi, Pi}] // AbsoluteTiming


This shows the plot super quickly, and the time ~0.2

Full Code Here:

htrig = (\[Delta]/3) {{0, 1, 1}, {1, 0, 1}, {1, 1, 0}};
lz = {{0, -I,0}, {I, 0, 0}, {0, 0, 0}};
lx = {{0, 0, 0}, {0, 0, -I}, {0, I,0}};
ly = {{0, 0, I}, {0, 0, 0}, {-I, 0, 0}};
sz = {{1,0},{0,-1}};sx={{0, 1}, {1, 0}}; sy = {{0, -I}, {I, 0}};
id3 =DiagonalMatrix[{1, 1, 1}]; id2 = DiagonalMatrix[{1, 1}];
Htrig = KroneckerProduct[id2, KroneckerProduct[id2, htrig]];
Hsoc = - (\[Lambda]/2) KroneckerProduct[
id2, (KroneckerProduct[sx, lx] + KroneckerProduct[sy, ly] +
KroneckerProduct[sz, lz])];
k1 = kx Sqrt/2 + ky/2;
k2 = -kx Sqrt/2 + ky/2;
k3 = -ky;
\[Gamma]12 = Exp[I k1] + Exp[I k2];
\[Gamma]13 = Exp[I k1] + Exp[I k3];
\[Gamma]23 = Exp[I k2] + Exp[I k3];
\[Gamma]12s = Exp[-I k1] + Exp[-I k2];
\[Gamma]13s = Exp[-I k1] + Exp[-I k3];
\[Gamma]23s = Exp[-I k2] + Exp[-I k3];
Re\[Gamma] = DiagonalMatrix[{-t (\[Gamma]12 + \[Gamma]12s)/
2, -t (\[Gamma]13 + \[Gamma]13s)/
2, -t (\[Gamma]23 + \[Gamma]23s)/2}];
Im\[Gamma] = DiagonalMatrix[{-t (\[Gamma]12 - \[Gamma]12s)/(2 I), -t (\[Gamma]13 - \[Gamma]13s)/(2 I), -t (\[Gamma]23 - \[Gamma]23s)/(2 I)}];
Hhop = KroneckerProduct[sx, KroneckerProduct[id2, Re\[Gamma]]] -
KroneckerProduct[sy, KroneckerProduct[id2, Im\[Gamma]]];
kpi = Pi*1.0;
Hfull = (Hhop /. t -> 1.) + (Hsoc /. \[Lambda] -> 0.6) + (Htrig /. \[Delta] -> 0.3);
ContourPlot[
Sort[Eigenvalues[Hfull]][], {kx, -kpi, kpi}, {ky, -kpi, kpi},
Contours -> 10]

• We need the complete code that generates Hfull. How could we possibly guess what’s going wrong just from that one-liner? The only thing that comes to mind is that you may want to define an external function that calculates the eigenvalues numerically, only after values have been plugged in to your matrix (look up NumericQ). Jun 1 '20 at 5:21
• Sorry, full code posted now. Jun 1 '20 at 5:27

This is a comment-with-images rather than an answer.

Something is odd here. I ran your code exactly as posted (only removing a spurious \ that was probably leftover from copy-pasting), and it returns a contour plot very quickly (in roughly one second or so):

ContourPlot[
Sort[Eigenvalues[Hfull]][],
{kx, -kpi, kpi}, {ky, -kpi, kpi},
Contours -> 10
] // AbsoluteTiming

(* Out: {1.02846, <plot>} *) • Can you please add the full code you ran to your answer. I see a lot of errors after fixing the spurious  on "12.1.0 for Mac OS X x86 (64-bit) (March 14, 2020)". Jun 1 '20 at 18:14
• @Rohit It is literally just the code under "Full code here" in the OP. I only removed an extra \ in the definition of Im\[Gamma], then evaluated it. Jun 1 '20 at 18:18
• Thanks Marco! Looks like some problem with my installation, or something is broken (weird though since it was working a day ago). I'm just going to re-install Mathematica and hope this problem disappears. Jun 1 '20 at 20:52