I am trying to use the Compile command in Mathematica to reduce the computation time because of its fast execution within Mathematica and ability to use the C compiler for even faster computation. As this is my first time using the command, I referred to the documentation and some usage by Leonid Shilfrin and others as an answer to this post.

After searching for a lot, I was not able to get some satisfying answers regarding the Compile command involving both Real and Complex outputs. I am trying to write a code analogous to my actual code which involves a similar problem just to get rid of many parameters and increased complexity. The function mentioned here takes real (random numbers generated by RandomReal in actual problem) inputs mentioned explicitly and returns a mandatory real and complex (maybe real in some case) output.

somefunction = Compile[{x,_Real},
          data = ArcSin[x]//N;
          {x, data}]

which works fine without any error within the input range -1 <= x <= 1, but raises an error beyond the specified range due to the complex output.

(*{0.5, 0.523599}*)
CompiledFunction::cfn: Numerical error encountered at instruction 1; proceeding with uncompiled evaluation.
(*{2, 1.5708 - 1.31696 I}*)

The answer is indeed correct in the latter case but the error produced while executing creates problems (making the computation even slower in the actual case). Using CompiledFunctionTools`CompilePrint[someFunction] to debug this issue, I found that there is no room to accommodate Complex number as an output of ArcSin in this case.

I need to get rid of this error. In trying to do so, I have tried using CompilationOptions with {"InlineExternalDefinitions" -> False} and {"InlineExternalDefinitions" -> True} following some answers here without actually knowing its utility. I had also tried to use Join to evade MainEvaluate in my original problem but none of this worked quite well. I need to use a Module inside Compile and encountered errors while trying to define any sort of pure function. The output is also within Module to minimize memory usage. I would also like to know what the letters R, A, C, I etc. mean in the output of the CompilePrint command so that I can debug similar issues by myself in future.

My actual computation is very CPU and Memory intensive because the actual function (similar to someFunction mentioned above) takes 12 (up to 20 in some case) arguments (generated by RandomReal) with 8 outputs (some of which may be complex in some cases) and it loops over 10^9 times which I am going to implement via a script file with .m extension. This is the reason behind using Compile and Module rather than using a pure function with SetDelayed symbol.

It would be extremely helpful if someone can guide me through this problem or mention any similar posts related to this kind of problem which I might have missed. Thanks for any suggestions in advance.

  • 2
    $\begingroup$ Does somefunction = Compile[{{x, _Complex}}, Module[{data}, data = ArcSin[x]; {x, data}] ] work for you? $\endgroup$ – Michael E2 Mar 21 at 22:35
  • $\begingroup$ Or somefunction = Compile[{{x, _Real}}, Module[{data, y = 0. I}, y = x; data = ArcSin[y]; {y, data}] ]? $\endgroup$ – Michael E2 Mar 21 at 22:46
  • $\begingroup$ This works fine. Thanks @Michael E2, for this simple and elegant solution. Although this makes x in a general complex in the output. Is there a way for better formatting in the output (use the complex number only if the value is complex by itself) ? $\endgroup$ – Rohan Mar 22 at 5:28
  • $\begingroup$ Maybe Chop[somefunction[2]]? Note Compile can return a list consisting only of values all of the same type. It cannot deal with arbitrary expressions the way general Mathematica functions can. It’s a restriction that helps compiled functions be more efficient. $\endgroup$ – Michael E2 Mar 22 at 5:45
  • $\begingroup$ Thanks @MichaelE2 . This was exactly what I wanted. On a side note, it would be helpful for me if you kindly mention briefly about the letters A, R, C, I in the output of CompilePrint? This would help me to understand how Compile works and debug such errors by myself. $\endgroup$ – Rohan Mar 22 at 6:01

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.