0
$\begingroup$

My calculations are running terribly slow (for a larger number of variables than in the given code). I can't compile the function correctly. Can anyone help me?

De[e_,h_,v_]:=((e h^3)/(12 (1-v^2)))
Alf[a_]:=((m Pi)/a)
Bet[b_]:=((n Pi)/b)
q[p1_,p2_,a_,b_,x0_,y0_]:=
 (
  ((4 p1)/(a b)) Sin[Alf[a] x0] Sin[Bet[b] y0]+
    ((4 p2)/(a b)) Sin[Alf[a] (x0+2.021)] Sin[Bet[b] (y0+0.065)]+
        ((4 p1)/(a b)) Sin[Alf[a] x0] Sin[Bet[b] (y0+1.050)]+
            ((4 p2)/(a b)) Sin[Alf[a] (x0+2.021)] Sin[Bet[b] (y0+0.985)]

  )
Delt[e_,h_,v_,a_,b_,k_]:=(De[e,h,v] (Alf[a]^2+Bet[b]^2)^2+k)
w[p1_,p2_,a_,b_,x0_,y0_,e_,h_,v_,k_]:=q[p1,p2,a,b,x0,y0]/Delt[e,h,v,a,b,k]
Mx[e_,h_,v_,a_,b_,p1_,p2_,k_,x0_,y0_,x_/;0<x<6,y_/;0<y<6]:=De[e,h,v]*Sum[Sum[((Alf[a]^2+v Bet[b]^2) w[p1,p2,a,b,x0,y0,e,h,v,k]) Sin[Alf[a] x] Sin[Bet[b] y],{n,1,20}],{m,1,20}]

k={4.846825833322111`*^6,5.238931521109236`*^6,5.077350158653542`*^6,4.536267905643828`*^6,4.375764694611544`*^6,4.564669230813377`*^6,5.839164643396563`*^6,5.2185564799462985`*^6,4.3317022672116915`*^6,4.595977212066057`*^6,4.394030477319662`*^6,4.662831240535733`*^6}

pairs={{5.200301369200046`,3.164827201205645`},{4.163004893697675`,1.7409758270465447`},{5.695791313032204`,1.0023134075279154`},{0.8082870663144348`,4.173725169069691`},{3.6129799469236428`,3.6510371020047483`},{5.050196700105754`,5.042860219651104`}}

Mx[27000000000,0.2,0.2,6,6,45597,5793,#1,Sequence@@#2,Sequence@@#2]&@@@Tuples[{k,pairs}]

$\endgroup$
4
  • 1
    $\begingroup$ De are undefined, hence symbolic. That makes compilation pointless. Please provide all relevant data (and only that). $\endgroup$ – Henrik Schumacher Dec 15 '19 at 23:25
  • 1
    $\begingroup$ Moreover, m and n are used implicitly in Alf and Bet, but they are the running variables in the double sum. That leaves me to say that I just cannot tell what you want. $\endgroup$ – Henrik Schumacher Dec 15 '19 at 23:28
  • $\begingroup$ De went missing while editing. I have already corrected. $\endgroup$ – p_federbusch Dec 15 '19 at 23:57
  • 1
    $\begingroup$ Still, the issue with m and n persists. Maybe you want Alf to be function of a and m? (and Bet to be a function of b and n?) $\endgroup$ – Henrik Schumacher Dec 16 '19 at 7:16
2
$\begingroup$

As Henrik noted in the comments, your code is a low-key disaster, with m and n never being explicitly called, random parameters floating around but never passed into a function, tons of other parameters, etc. But whatever. Without much work it's possible to compile the specific case you present.

Here's a way to turn all your code into a single inert expression, then we can Compile that:

expr =
  Block[
   {Sum = sum},
   Mx[27000000000, 0.2, 0.2, 6, 6, 45597, 5793, #1, Sequence @@ #2, 
      Sequence @@ #2] & @@@ Tuples[{k, pairs}
     ]
   ];

Then we can compile this like so:

compVersion =
  With[{expr = expr},
   Hold[
      Compile[
       {},
       expr
       ]
      ] /. sum -> Sum // ReleaseHold
   ];

And this seems fast enough:

compVersion[] // RepeatedTiming

{1.03, {15086.2, 16534.2, 9884.55, 15243.3, 16329.8, 8927.37, 15028., 16385.9,
   9879.67, 15187.4, 16150.7, 8912.45, 15051.7, 16446., 9881.67, 15210.2, 
  16223.1, 8918.54, 15134.1, 16657.8, 9888.47, 15289.1, 16480.1, 8939.51, 
  15159.4, 16723.9, 9890.51, 15313.4, 16560.9, 8945.91, 15129.6, 16646.2, 
  9888.1, 15284.9, 16466., 8938.39, 14943.3, 16173.7, 9872.37, 15105.9, 
  15897., 8890.41, 15031., 16393.4, 9879.92, 15190.3, 16159.7, 8913.21, 
  15166.5, 16742.4, 9891.08, 15320.2, 16583.5, 8947.68, 15124.7, 16633.6, 
  9887.71, 15280.2, 16450.6, 8937.15, 15156.5, 16716.3, 9890.28, 15310.7, 
  16551.6, 8945.18, 15114.4, 16606.7, 9886.86, 15270.3, 16417.9, 8934.53}}
$\endgroup$
2
  • $\begingroup$ Thank you for answer. Now the calculations are running much faster! $\endgroup$ – p_federbusch Dec 16 '19 at 12:59
  • $\begingroup$ @b3m2a1 Hard words, but essentially what I tried to express... ^^ (+1 of course) $\endgroup$ – Henrik Schumacher Dec 16 '19 at 17:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.