# Speeding and optimizing the Graphics Plot

I am having a Hermitian Matrix(HCF[k]) depending on a parameter $k$.

My job is to plot the Eigenvalues of the matrix as the parameter $k$ is changed.

But I am facing some problems.
(i) My Mathematica file size, which is of around whopping 90Mb, when HCF[k] is around $284\times284$.
(ii) This part of code is very slow as the size of HCF[k] is increasing.

kdel = 0.00001; (* offset *)
kIn = -π - kdel;  (*Initial value of k *)
kFin = π + kdel;  (*Final value of k *)
kInc = 0.001; (*Increment of k*)

eigeng =
ParallelTable[Eigenvalues[HCF[k]], {k, kIn, kFin, kInc}];

kList = ParallelTable[k, {k, kIn, kFin, kInc}];

kFList = ParallelTable[kList, {i, Transpose@eigeng}];

dataToPlot = Flatten[{kFList\[Transpose], eigeng}\[Transpose], {{1, 3}, {2}}];

Graphics[{Point[{#1, #2}]} & @@@ dataToPlot, Frame -> True,
...(* for the aesthetic of plot, i.e. axis title, range and bla bla*)]

• Is there a way the size issue can be overcome?
• Is there a way the code can be sped up a little bit?(I checked my rest of the code, it is very fast, which I did by breaking and evaluating it into small cells).

• Form of HCF[k], as it is huge I will try to show you how it looks for small size say
\left( \begin{array}{cccccc} -20 \pi & -\frac{e^{i k}}{2} & 0 & -\frac{1}{2} & 0 & 0 \\ -\frac{1}{2} e^{-i k} & -20 \pi & -\frac{1}{2} & 0 & 0 & 0 \\ 0 & -\frac{1}{2} & 0 & -\frac{e^{i k}}{2} & 0 & -\frac{1}{2} \\ -\frac{1}{2} & 0 & -\frac{1}{2} e^{-i k} & 0 & -\frac{1}{2} & 0 \\ 0 & 0 & 0 & -\frac{1}{2} & 20 \pi & -\frac{e^{i k}}{2} \\ 0 & 0 & -\frac{1}{2} & 0 & -\frac{1}{2} e^{-i k} & 20 \pi \\ \end{array} \right)
• What does HCF look like? Feb 13, 2017 at 20:55
• @MarcoB Please see the edit, similar form may look like for 284X284.
– L.K.
Feb 14, 2017 at 9:28
• Please post code, not LaTeX, so one can copy/paste it. It would be best if you could either propose a smaller version of your matrix, or simply post the whole matrix somewhere (e.g. pastebin. Also, your kList and kFList do not need to be defined using ParallelTable; a simple Range would work, and probably be faster too. In general, have you tried without parallelization? Are you sure that parallel execution leads to a speedup? Feb 14, 2017 at 23:45
• @MarcoB Here you will find the Matrix(paste bin, thanks for this). I always thought Parallel does work fast(may be I was in some illusion). So, never thought of out of parallelization. I am sorry I can't give the code that generated the matrix as there is some work going on(some restrictions on me), I really hope you will understand it(take it good). I gave you the matrix and your help will definitely show some light on the problem.
– L.K.
Feb 15, 2017 at 10:00
• @MarcoB How kFList using Range, I failed in doing that. But able to do the kList
– L.K.
Feb 15, 2017 at 16:14

The following uses your definitions of the matrix from pastebin, and of starting points and increments.

Parallelization does help to speed up the calculation of the eigenvalues, but you have to distribute the definitions of your helper variables to the parallel kernels using DistributeDefinitions first:

AbsoluteTiming[Table[Eigenvalues[mat], {k, kIn, kFin, kInc}];]
(* Out: {142.851, Null} *)

DistributeDefinitions[kdel, kIn, kFin, kInc]
AbsoluteTiming[ParallelTable[Eigenvalues[mat], {k, kIn, kFin, kInc}];]
(* Out: {90.5666, Null} *)


This is on a two-core machine, so the benefits from parallelization may be even better on higher core-count machines.

Having said that, the definitions of kList and kFList can be simplified:

kList = Range[kIn, kFin, kInc];
kFList = ConstantArray[kList, Length@Transpose@eigeng];

• Better additions, like kList and kFList and DistributeDefinitions[kdel, kIn, kFin, kInc]. I think further simplifications are not possible. For Graphics also. As from my side, speed I got from previously is about 0.5s. Less but good
– L.K.
Feb 15, 2017 at 18:33
• @L.K. I think another important consideration, even for the preparation of the graphics objects, is whether you do need to try and plot all those points. There is no way that you can actually see that many points on a computer screen, or in most media printouts, so perhaps pruning the list to a more manageable size (plotting every ten points etc) would help. Feb 15, 2017 at 18:48
• @L.K. From a more fundamental standpoint, your matrix is very sparse, and has a relatively simple structure. I think it might be worth it to see if you can manipulate the math to determine whether you want (or need) to calculate all eigenvalues: there are numerical methods to calculate only a fraction of those, e.g. by magnitude (see the Method options for Eigenvalues). Perhaps some clever pencil-and-paper math might reduce the size of your problem. Feb 15, 2017 at 18:50
• thanks for the valuable informations. For sure I will check the possibilities as you have mentioned.
– L.K.
Feb 15, 2017 at 19:37