2
$\begingroup$

I have a large matrix (4Wn*4Wn) resulted from multiplication of different matrices and I would like to evaluate the trace numerically to speed up the process. Here is the matrix

tc = {{0, 0, 0, 0}, {0, 0, 0, 0}, {-1, 0, 0, 0}, {0, -1, 0, 0}};
m1[x_, y_] = {{0, I Sin[x] + Sin[y], 
    13/4 - Cos[x] - Cos[y], -1}, {-I Sin[x] + Sin[y], 0, -1, 
    13/4 - Cos[x] - Cos[y]}, {13/4 - Cos[x] - Cos[y], -1, 
    0, -I Sin[x] - Sin[y]}, {-1, 13/4 - Cos[x] - Cos[y], 
    I Sin[x] - Sin[y], 0}};
m2[x_, y_, Wn_] := 
  SparseArray[{Band[{1, 1}, {4 Wn, 4 Wn}] -> {m1[x, y]}, 
    Band[{1, 5}, {4 Wn, 4 Wn}] -> {tc}, 
    Band[{5, 1}, {4 Wn, 4 Wn}] -> {ConjugateTranspose[tc]}}];
M[x_, y_] := m2[x, y, Wn]; Wn = 20;
O1[x_, y_] = D[M[x, y], y];
fn[x_, y_, r_] := 
 Block[{ks = M[x, y], 
   id = IdentityMatrix[
     4 Wn]}, {(Inverse[(r - I*0.01)*id - 
      ks]), (Inverse[(r + I*0.01)*id - ks])}]
trace[x_, y_, r_] := 
 Block[{f = fn[x, y, r]}, 
  Re[Tr[-(1/(4 \[Pi])) (1/(
      4 \[Pi]^2)) (O1[x, y].(f[[2]] - f[[1]]).O1[x, 
        y].(f[[2]] - f[[1]]))]]]   

the target is Wn=100 but for a test we set it to Wn=20to calculate the trace and then I want to evaluate the sum with respect to {x,y}:

XT[r_, nc_] := 
 ParallelSum[
  4 ((2 \[Pi])/nc)^2 trace[x, y, r], {x, \[Pi]/nc, \[Pi], (2 \[Pi])/
   nc}, {y, \[Pi]/nc, \[Pi], (2 \[Pi])/nc}]    

Now, the last step is to evaluate this as a function of r and I will use nc=100 for simplicty (target is 400)

Table[XT[r,100.]//AbsoluteTiming,{r,-2,2,1}]//AbsoluteTiming
{5.39561,{{1.1231,156.671},{1.06852,36.2431},{1.06719,0.275971},{1.06907,36.2431},{1.06769,156.671}}}  

Then, If I sue Evaluate on XT to speed it up, it takes few minutes without output

Table[Evaluate[XT[r, 100.]], {r, -2, 2, 1}] // AbsoluteTiming 
$\endgroup$
  • $\begingroup$ What unit is Wn? $\endgroup$ – Αλέξανδρος Ζεγγ Aug 18 at 2:32
  • 1
    $\begingroup$ Can you use AbsoluteTiming on individual parts of your calculation to try to determine where most of the time is being spent? Because Mathematica caches data and takes time to start up, you may need to restart Mathematica and do a small unrelated calculation and then do measure the time of a particular step. I'm guessing that most of your calculation takes almost zero time and a few key steps take almost all the time. Knowing that can help you and everyone else decide how to focus on the key parts. ParallelSum can help or hurt, depending on the structure of the problem you have. $\endgroup$ – Bill Aug 18 at 6:19
  • 1
    $\begingroup$ @ΑλέξανδροςΖεγγ, it is a number and defined above to be 20 in the above example but the target is to be 100 $\endgroup$ – HD2006 Aug 18 at 7:00
  • 2
    $\begingroup$ Matrices with a shape of hundreds by hundreds are not very large. $\endgroup$ – Αλέξανδρος Ζεγγ Aug 18 at 7:04
  • 1
    $\begingroup$ I booted MMA, scrape-n-pasted your code, set Wn=2 without Parallel without Evaluate and got {6.78404,{{1.55934,17.2432},{1.31069,7.37015},{1.30124,0.00424746},{1.30446,7.37015},{1.30829,17.2432}}} I booted MMA, scrape-n-pasted your code, set Wn=2 without Parallel with Evaluate and got {7.10071,{{1.*^-6,17.2432},{1.*^-6,7.37015},{0.,0.00424746},{0.,7.37015},{0.,17.2432}}} Can you reproduce EXACTLY this? And then bump Wn up a little at a time and try with and without Parallel and with and without Evaluate? Let's see the numbers. $\endgroup$ – Bill Aug 18 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.