2
$\begingroup$

I would like to write a compile function

cf = 
  Compile[{{tracefab, _Complex, 1}}, 
    Module[{fab, i, j},
      fab = Table[0., {i, 1, 5, 1}, {j, 1, 5, 1}];
      For[i = 1, i <= 5, i++,
        For[j = 1, j <= 5, j++,
           fab[[i, j]] = tracefab[[i]];];];
      fab]];

However, by using CompilePrint I found that the compiled function calls MainEvluate,

18  C0 = Part[ T(C1)0, I7]
19  V17T(R2)2I7I2 = MainEvaluate[Function[{tracefab, fabCompile$322, iCompile$323, jCompile$324}, Block[{fab = fabCompile$322, i = iCompile$323, j = jCompile$324}, {fab[[i,j]] = tracefab[[i]], fab, i, j}]][ T(C1)0, T(R2)2, I7, I2]]
20  I5 = I2

Is there a way to avoid calling Mainevaluate in my case?

Any suggestion is welcome!

$\endgroup$
2
  • 5
    $\begingroup$ Use fab = Table[0. I, {i, 5}, {j, 5}] instead so that the type is not being coerced from real to complex. $\endgroup$
    – Carl Woll
    Sep 1, 2018 at 15:37
  • $\begingroup$ It works finally!!! Thank you very much! @Carl Woll! $\endgroup$
    – cwei
    Sep 1, 2018 at 20:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.