# How can I speed up the evaluation of my equations?

Calculation of eigenvalues and eigenvectors in dependence of the parameters

ESysNO[NB02_?NumericQ, NB04_?NumericQ, NB06_?NumericQ, NB34_?NumericQ,
NB36_?NumericQ, NB66_?NumericQ, NBz_?NumericQ, NµB_?NumericQ] := Chop[CFEigensystem[N[Apply[HZCF, Thread[{B02, B04, B06, B34, B36, B66, Bz, µB} = {NB02, NB04, NB06, NB34, NB36, NB66, NBz, NµB}]]], 16]];

EMatNO[NB02_?NumericQ, NB04_?NumericQ, NB06_?NumericQ, NB34_?NumericQ, NB36_?NumericQ, NB66_?NumericQ, NBz_?NumericQ,NµB_?NumericQ] := ESysNO[NB02, NB04, NB06, NB34, NB36, NB66, NBz, NµB][[1]];

VecMatNO[NB02_?NumericQ, NB04_?NumericQ, NB06_?NumericQ, NB34_?NumericQ, NB36_?NumericQ, NB66_?NumericQ, NBz_?NumericQ, NµB_?NumericQ] := ESysNO[NB02, NB04, NB06, NB34, NB36, NB66, NBz, NµB][[2]];


Calculation of the spectral weights

SpecWtLJJm1[Skk_, NB02_, NB04_, NB06_, NB34_, NB36_, NB66_, NBz_] := Sum[VecMatNO[NB02, NB04, NB06, NB34, NB36, NB66, NBz, 5.788381806638/10^5][[Skk,kk]]^2*TLJJm1[[kk]], {kk, 1, Length[funlist]}];
AbsoluteTiming[SpecWtLJJm1[1, -10, -10, -0.5, -6, 0.4, -1.7, 13]]
{0.0224183, 0.0753555}

SpecWtRJJm1[Skk_, NB02_, NB04_, NB06_, NB34_, NB36_, NB66_, NBz_] := Sum[VecMatNO[NB02, NB04, NB06, NB34, NB36, NB66, NBz, 5.788381806638/10^5][[Skk,kk]]^2*TRJJm1[[kk]], {kk, 1, Length[funlist]}];
AbsoluteTiming[SpecWtRJJm1[1, -10, -10, -0.5, -6, 0.4, -1.7, 13]]
{0.0226654, 209.262}


Calculation of the partition function

PartFun[NB02_, NB04_, NB06_, NB34_, NB36_, NB66_, NBz_, NT_] := Sum[E^(-(EMatNO[NB02, NB04, NB06, NB34, NB36, NB66, NBz, 5.788381806638/10^5][[Skk]]/(kB*NT))), {Skk, 1, Length[funlist]}];
AbsoluteTiming[PartFun[-10, -10, -0.5, -6, 0.4, -1.7, 13, 20.8]]
{0.0210252, 4.32403*10^6}


Calculation of the spectral modulation

SpecModLJJm1[NB02_, NB04_, NB06_, NB34_, NB36_, NB66_, NBz_, NT_] := Sum[SpecWtLJJm1[Skk, NB02, NB04, NB06, NB34, NB36, NB66, NBz]/E^(EMatNO[NB02, NB04, NB06, NB34, NB36, NB66, NBz, 5.788381806638/10^5][[Skk]]/(kB*NT)), {Skk, 1, Length[funlist]}]/PartFun[NB02, NB04, NB06, NB34, NB36, NB66, NBz, NT];
AbsoluteTiming[SpecModLJJm1[-10, -10, -0.5, -6, 0.4, -1.7, 13, 20.8]]
{0.363163, 0.189235}

SpecModRJJm1[NB02_, NB04_, NB06_, NB34_, NB36_, NB66_, NBz_, NT_] := Sum[SpecWtRJJm1[Skk, NB02, NB04, NB06, NB34, NB36, NB66, NBz]/E^(EMatNO[NB02, NB04, NB06, NB34, NB36, NB66, NBz, 5.788381806638/10^5][[Skk]]/(kB*NT)), {Skk, 1, Length[funlist]}]/PartFun[NB02, NB04, NB06, NB34, NB36, NB66, NBz, NT];
AbsoluteTiming[SpecModRJJm1[-10, -10, -0.5, -6, 0.4, -1.7, 13, 20.8]]
{0.37417, 209.145}


Calculation of the whole intensity

SpecModXMCAJJm1[NB02_, NB04_, NB06_, NB34_, NB36_, NB66_, NBz_, NT_] :=(SpecModRJJm1[NB02, NB04, NB06, NB34, NB36, NB66, NBz, NT] - SpecModLJJm1[NB02, NB04, NB06, NB34, NB36, NB66, NBz, NT])/(SpecModRJJm1[NB02, NB04, NB06, NB34, NB36, NB66, NBz, NT] + SpecModLJJm1[NB02, NB04, NB06, NB34, NB36, NB66, NBz, NT]);
AbsoluteTiming[SpecModXMCAJJm1[-10, -10, -0.5, -6, 0.4, -1.7, 13, 20.8]]
{1.47653, 0.998192}


As you see the evaluation time of the equations increases dramatically starting from the spectral weights over the partition function and resulting in 1.47sec for the evaluation of one single datapoint of the whole intensity. How can I optimize my code to get the max evaluation speed out of this? I want to plot 1000 datapoints and do a fit by varying the parameters. For this reason I need a very fast code.

• in short my equations calculate sums over sums of eigenvectors and eingenvalues – MathNut Nov 18 '16 at 16:15
• It looks like you are using Sum where fast numerical functions like Total and Dot may be more appropriate. It's hard to tell, because the code is incomplete. – Simon Woods Nov 18 '16 at 21:34