Skip to main content
Formatted code
Source Link
MarcoB
  • 67.7k
  • 18
  • 96
  • 197
f = Compile[
     {{\[Sigma]tσt, _Real, 1}, {s\[Rho], _Complex, 
    1}, {dt, _Real, 
    1}, {x, _Real}, {nnt, _Integer}, 
      {dqt, _Real}, {divt, _Integer}},
  Module[{temp, dq = dqt, div = divt, j, h},
   temp[x_] := 
    Module[{kz = 1/2 x, \[Sigma]σ = \[Sigma]tσt, d = dt, i, kp, 
      c, \[Beta]β, r, k, \[Rho]ρ = s\[Rho] * 10^-6, nn = nnt},
     kp = kz + 0. I;
     k = Sqrt[kz^2 - 4 \[Pi]π (\[Rho][[2]]ρ[[2]] - \[Rho][[1]]ρ[[1]])];
      r = (kp - k)/(kp + k) E^(-2 kp k \[Sigma][[1]]^2σ[[1]]^2);
     c = {{1 + 0. I, r }, {r , 1}};
     kp = k;
     
     For[i = 2, i < 2 nn + 2, i++,
      
      If[EvenQ[i],
       k = Sqrt[kz^2 - 4 \[Pi]π (\[Rho][[3]]ρ[[3]] - \[Rho][[1]]ρ[[1]])];
       
        r = (kp - k)/(kp + k) E^(-2 kp k \[Sigma][[2]]^2σ[[2]]^2); \[Beta] = 
       β = I kp d[[1]],
       
       
       k = Sqrt[kz^2 - 4 \[Pi]π (\[Rho][[2]]ρ[[2]] - \[Rho][[1]]ρ[[1]])];
       
        r = (kp - k)/(kp + k) E^(-2 kp k \[Sigma][[3]]^2σ[[3]]^2); \[Beta] = 
       β = I kp d[[2]] 
       
       
       ];
      
      
      c = c.{{E^\[Beta]E^β, r E^\[Beta]E^β}, {r E^-\[Beta]β, E^-\[Beta]β}};
      
      (*previous k*)
      kp = k
      ];
     
     Abs[c[[2, 1]]/c[[1, 1]]]^2];
   
   h = 0.0;
   For[j = 1, j < div + 1, j++,
    
    h = h + temp[x - dq/2 + dq/div*j]
    
    ];
   
   h/div
   
   ]
  
  , CompilationTarget -> "C"]
f = Compile[{{\[Sigma]t, _Real, 1}, {s\[Rho], _Complex, 
    1}, {dt, _Real, 
    1}, {x, _Real}, {nnt, _Integer}, {dqt, _Real}, {divt, _Integer}},
  Module[{temp, dq = dqt, div = divt, j, h},
   temp[x_] := 
    Module[{kz = 1/2 x, \[Sigma] = \[Sigma]t, d = dt, i, kp, 
      c, \[Beta], r, k, \[Rho] = s\[Rho] * 10^-6, nn = nnt},
     kp = kz + 0. I;
     k = Sqrt[kz^2 - 4 \[Pi] (\[Rho][[2]] - \[Rho][[1]])];
      r = (kp - k)/(kp + k) E^(-2 kp k \[Sigma][[1]]^2);
     c = {{1 + 0. I, r }, {r , 1}};
     kp = k;
     
     For[i = 2, i < 2 nn + 2, i++,
      
      If[EvenQ[i],
       k = Sqrt[kz^2 - 4 \[Pi] (\[Rho][[3]] - \[Rho][[1]])];
       
        r = (kp - k)/(kp + k) E^(-2 kp k \[Sigma][[2]]^2); \[Beta] = 
        I kp d[[1]],
       
       
       k = Sqrt[kz^2 - 4 \[Pi] (\[Rho][[2]] - \[Rho][[1]])];
       
        r = (kp - k)/(kp + k) E^(-2 kp k \[Sigma][[3]]^2); \[Beta] = 
        I kp d[[2]] 
       
       
       ];
      
      
      c = c.{{E^\[Beta], r E^\[Beta]}, {r E^-\[Beta], E^-\[Beta]}};
      
      (*previous k*)
      kp = k
      ];
     
     Abs[c[[2, 1]]/c[[1, 1]]]^2];
   
   h = 0.0;
   For[j = 1, j < div + 1, j++,
    
    h = h + temp[x - dq/2 + dq/div*j]
    
    ];
   
   h/div
   
   ]
  
  , CompilationTarget -> "C"]
f = Compile[
     {{σt, _Real, 1}, {, _Complex, 1}, {dt, _Real, 1}, {x, _Real}, {nnt, _Integer}, 
      {dqt, _Real}, {divt, _Integer}},
  Module[{temp, dq = dqt, div = divt, j, h},
   temp[x_] := 
    Module[{kz = 1/2 x, σ = σt, d = dt, i, kp, c, β, r, k, ρ =  * 10^-6, nn = nnt},
     kp = kz + 0. I;
     k = Sqrt[kz^2 - 4 π (ρ[[2]] - ρ[[1]])];
      r = (kp - k)/(kp + k) E^(-2 kp k σ[[1]]^2);
     c = {{1 + 0. I, r }, {r , 1}};
     kp = k;
     For[i = 2, i < 2 nn + 2, i++,
      If[EvenQ[i],
       k = Sqrt[kz^2 - 4 π (ρ[[3]] - ρ[[1]])];
       r = (kp - k)/(kp + k) E^(-2 kp k σ[[2]]^2); 
       β = I kp d[[1]],
       k = Sqrt[kz^2 - 4 π (ρ[[2]] - ρ[[1]])];
       r = (kp - k)/(kp + k) E^(-2 kp k σ[[3]]^2); 
       β = I kp d[[2]] 
      ];
      
      c = c.{{E^β, r E^β}, {r E^-β, E^-β}};
      (*previous k*)
      kp = k
      ];
     Abs[c[[2, 1]]/c[[1, 1]]]^2];
   h = 0.0;
   For[j = 1, j < div + 1, j++,
    h = h + temp[x - dq/2 + dq/div*j]
   ];
   h/div
   ]
  , CompilationTarget -> "C"]
Source Link
Al Guy
  • 1.6k
  • 1
  • 10
  • 15

Module inside module in `Compile`

How can I make compilation to go through with a module inside module? I tried this, failed, a few other ways failed too.

f = Compile[{{\[Sigma]t, _Real, 1}, {s\[Rho], _Complex, 
    1}, {dt, _Real, 
    1}, {x, _Real}, {nnt, _Integer}, {dqt, _Real}, {divt, _Integer}},
  Module[{temp, dq = dqt, div = divt, j, h},
   temp[x_] := 
    Module[{kz = 1/2 x, \[Sigma] = \[Sigma]t, d = dt, i, kp, 
      c, \[Beta], r, k, \[Rho] = s\[Rho] * 10^-6, nn = nnt},
     kp = kz + 0. I;
     k = Sqrt[kz^2 - 4 \[Pi] (\[Rho][[2]] - \[Rho][[1]])];
      r = (kp - k)/(kp + k) E^(-2 kp k \[Sigma][[1]]^2);
     c = {{1 + 0. I, r }, {r , 1}};
     kp = k;
     
     For[i = 2, i < 2 nn + 2, i++,
      
      If[EvenQ[i],
       k = Sqrt[kz^2 - 4 \[Pi] (\[Rho][[3]] - \[Rho][[1]])];
       
        r = (kp - k)/(kp + k) E^(-2 kp k \[Sigma][[2]]^2); \[Beta] = 
        I kp d[[1]],
       
       
       k = Sqrt[kz^2 - 4 \[Pi] (\[Rho][[2]] - \[Rho][[1]])];
       
        r = (kp - k)/(kp + k) E^(-2 kp k \[Sigma][[3]]^2); \[Beta] = 
        I kp d[[2]] 
       
       
       ];
      
      
      c = c.{{E^\[Beta], r E^\[Beta]}, {r E^-\[Beta], E^-\[Beta]}};
      
      (*previous k*)
      kp = k
      ];
     
     Abs[c[[2, 1]]/c[[1, 1]]]^2];
   
   h = 0.0;
   For[j = 1, j < div + 1, j++,
    
    h = h + temp[x - dq/2 + dq/div*j]
    
    ];
   
   h/div
   
   ]
  
  , CompilationTarget -> "C"]