Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I cannot get what's the problem with Compile function in my program. If I simply use the Abs function, I get this answer:

Abs[NestWhile[1/(#^2 + 2 #) &, 
  3 + 4*I, ((Abs[#1] > 0.01) && (#1 != -2 ) && (Abs[#1 - #2] > 
       0.001)) &, 2]]
1108116250625/(4097 Sqrt[4415238971393])

But if I make a function called PointFunction, it compiles, and when I try to find its value at the point 3 + 4*I, I get this error message:

PointFunction = 
  Compile[{{c, _Complex}}, 
    Abs[NestWhile[1/(#^2 + 2 #) &, 
    c, ((Abs[#1] > 0.01) && (#1 != -2 ) && (Abs[#1 - #2] > 0.001)) &, 
    2]], {{x, _Complex}}]

PointFunction[3 + 4*I]

CompiledFunction::cfse: Compiled expression -128.657+3.99803 I should be a machine-size integer. >>
CompiledFunction::cfex: Could not complete external evaluation at instruction 1; proceeding with uncompiled evaluation. >>

1108116250625/(4097 Sqrt[4415238971393])

What is the problem? I have tried different options but they didn't work, and I have no idea at all.

share|improve this question

closed as off-topic by m_goldberg, ciao, Öskå, RunnyKine, Michael E2 Jul 5 '14 at 15:27

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, ciao, Öskå, RunnyKine, Michael E2
If this question can be reworded to fit the rules in the help center, please edit the question.

Note that the form NestWhile[func, init, test] will compile but that NestWhile[func, init, test, 2] will not. The compiler calls Mma via MainEvaluate to execute your NestWhile, so you don't save time. You can inspect the compiled code with Needs["CompiledFunctionTools`"]; CompilePrint@PointFunction. –  Michael E2 Jul 5 '14 at 5:30
I changed {{x, _Complex}}, the last argument you give to Compile, to {{_, _Complex}} and it seems to work fine. PointFunction[3 + 4*I] now gives 128.719, which is the numeric value of 1108116250625/(4097 Sqrt[4415238971393]). –  m_goldberg Jul 5 '14 at 5:35

Browse other questions tagged or ask your own question.