Bug introduced in 12.0 and fixed in 12.1
The following code calculates the eigenvalues of a certain complex matrix, which come in pairs of opposite complex numbers. Therefore one can check whether the sum of all eigenvalues is equal to the trace of the matrix, which is zero.
This is indeed the case in Version 10.1 & 11.3 as far as I tested. However, Version 12.0 (Windows, Mac, Linux) gives something seriously wrong.
NN = 374; R = 0.05;
t1 = -1 + Cos[x] - I Sin[x] + I R; t1p = -1 + Cos[x] + I Sin[x] +
I R;
mat[x_] =
DiagonalMatrix[Table[If[EvenQ[n], t1, -1], {n, 0, 2 NN - 1 - 1}],
1] + DiagonalMatrix[
Table[If[EvenQ[n], t1p, -1], {n, 0, 2 NN - 1 - 1}], -1] +
DiagonalMatrix[Table[If[EvenQ[n], -1, 0], {n, 0, 2 NN - 1 - 3}],
3] + DiagonalMatrix[
Table[If[EvenQ[n], -1, 0], {n, 0, 2 NN - 1 - 3}], -3];
mat0 = mat[-0.2 \[Pi]];
Tr@mat0 (* 0. *)
Total@Eigenvalues@mat0 (* 0.394003 - 0.566499 I *)
I would rather switch back to 11.3 for a while. This looks really dangerous...
Original post of a more complex matrix with the same issue:
The code plots the real part of adding each pair. So the correct plot should be just zeros everywhere. This is the case in Version 10.1 & 11.3 as far as I tested (scattered numbers around $10^{-14}$ or so). However, Version 12.0 (Windows, Mac, Linux) gives something different as shown below.
NN = 200; R = 0.05;
xlist = Table[x, {x, -0.2 \[Pi], 0.2 \[Pi], 0.01}];
modl[n_] := 2*^-3 (Quotient[n, 2] - NN/2);
t1 = -1 + Cos[x] - I Sin[x] + I R; t1p = -1 + Cos[x] + I Sin[x] + I R;
t2a[n_] := -1 - modl[n]; t2b[n_] := -1 + modl[n];
mat[x_] =
DiagonalMatrix[
Table[If[EvenQ[n], t1, t2a[n]], {n, 0, 2 NN - 1 - 1}], 1] +
DiagonalMatrix[
Table[If[EvenQ[n], t1p, t2a[n]], {n, 0, 2 NN - 1 - 1}], -1] +
DiagonalMatrix[
Table[If[EvenQ[n], t2b[n], 0], {n, 0, 2 NN - 1 - 3}], 3] +
DiagonalMatrix[
Table[If[EvenQ[n], t2b[n], 0], {n, 0, 2 NN - 1 - 3}], -3];
list0 = Sort@Re@Eigenvalues[mat[xlist[[3]]]];
list0p = Table[list0[[i]] + list0[[2 NN - i + 1]], {i, NN}];
ListPlot[Tooltip@list0p, PlotRange -> All]
Tr[matrix]
withTotal[Eigenvalues[matrix]]
which shows a large discrepancy. I think this is worth reporting to support. It happens on OSX as well. $\endgroup$Tr[matrix]
gives 0. andTotal[Eigenvalues[matrix]]
gives-2.23821*10^-13 - 2.66454*10^-15 I
. $\endgroup$