I am trying to find the eigensystem of a large sparse real symmetric matrix, and I only need the lowest 40 or so eigenstates. The relevant code is as follows:
TheModel[V0_, V1_, V2_, index_, period_] := (
Fn = Fibonacci[index]; Fn1 = Fibonacci[index - 1];
size = 2.0*period*Fn + 1;
RV0 = Table[(V0/4), size - Fn];
RV1 = Table[(V1/4), size - Fn1];
central =
Table[((2 \[Pi]/Fn)*(ind - Fn*period - 1)/\[Pi])^2 + (V0 + V1)/
2, {ind, 1, size}];
t1 = AbsoluteTime[];
HH = SparseArray[DiagonalMatrix[RV0, Fn] + DiagonalMatrix[RV1, Fn1] +
DiagonalMatrix[central, 0] + DiagonalMatrix[RV0, -Fn] +
DiagonalMatrix[RV1, -Fn1]];
t2 = AbsoluteTime[];
Print["Time is ", t2 - t1];
t1 = AbsoluteTime[];
{\[Epsilon], \[Phi]} = Eigensystem[HH, -Fn];
t2 = AbsoluteTime[];
Print["Time is ", t2 - t1];
Return[{\[Epsilon], \[Phi]}];
);
{\[Epsilon], \[Phi]} = TheModel[30.0, 2, 0, 9, 20]; // Timing
The matrix has a dimension of about 1200. I am surprised that without defining HH
as a sparse array, the diagonalization step alone takes 1.33 seconds; in contrast, if I keep the sparse array definition, the diagonalization only takes 0.054 seconds. In comparison, it takes Matlab 0.6 seconds to diagonalize the same matrix (no sparse array definition and returning all eigenstates).
I am very curious about why sparse array definition speeds up the diagonalization process so significantly. Can sometime give me some insights?
Also, in general why is it so slow in Mathematica to generate all eigenstates? I thought both Matlab and Mathematica use LAPACK to do the diagonalization. Why is there such a big difference?
AbsoluteTiming
instead ofTiming
? Please check this question first. $\endgroup$Method->"Arnoldi"
for both the sparse and non sparse case. $\endgroup$RV0
toRV0 = Table[(V0/4.), size - Fn];
and the same forRV1
. $\endgroup$