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},
Module[{data},
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.
someFunction[0.5]
(*{0.5, 0.523599}*)
someFunction[2]
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.
somefunction = Compile[{{x, _Complex}}, Module[{data}, data = ArcSin[x]; {x, data}] ]work for you? – Michael E2 Mar 21 '21 at 22:35somefunction = Compile[{{x, _Real}}, Module[{data, y = 0. I}, y = x; data = ArcSin[y]; {y, data}] ]? – Michael E2 Mar 21 '21 at 22:46xin 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) ? – Rohan Mar 22 '21 at 05:28Chop[somefunction[2]]? NoteCompilecan 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. – Michael E2 Mar 22 '21 at 05:45A,R,C,Iin the output ofCompilePrint? This would help me to understand howCompileworks and debug such errors by myself. – Rohan Mar 22 '21 at 06:01