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.

See following three snippet code:

(*can compile*)
range = Range[-2, 2, 0.005];
Compile[{},
   With[{r = range}, Table[ArcTan[x, y], {x, r}, {y, r}]]][];

(*can compile*)
Compile[{}, 
   With[{r = Range[-2, 2, 0.005 - 10^-8]}, Table[ArcTan[x, y], {x, r}, {y, r}]]][];

(*can't compile*)
Compile[{},
   With[{r = Range[-2, 2, 0.005]}, Table[ArcTan[x, y], {x, r}, {y, r}]]][];

Why the last code can't be compiled? I used Mathematica 9.0.1.

share|improve this question
    
Your first "can compiled" doesn't compile in V.9.0.1. Gives the errors: "CompiledFunction::cfn: Numerical error encountered at instruction 12; proceeding with uncompiled evaluation. >>" and "ArcTan::indet: Indeterminate expression ArcTan[0.,0.] encountered. >>". –  m_goldberg Sep 6 '13 at 17:54
add comment

2 Answers 2

up vote 11 down vote accepted

In addition to Mr. Wizard's analysis, one can also avoid the indeterminacy by replacing ArcTan as follows:

Compile[{}, 
   With[{r = Range[-2, 2, 0.005]}, 
    Table[Arg[Complex[x, y]], {x, r}, {y, r}]]][];

The fact that ArcTan[0,0] is undefined is a real nuisance, and I never saw the point of it because that form of the function is mainly used for practical applications such as plotting, where the purely mathematical reasons against defining that special value don't really bother anyone.

share|improve this answer
    
I didn't know of this workaround. Nice. +1 –  Mr.Wizard Sep 6 '13 at 17:53
    
    
@AndreasLauschke Could you explain what the links above are intended for? –  Jens Sep 6 '13 at 18:43
    
@AndreasLauschke I'm guessing you want to confirm the equivalence of the Arg and ArcTan methods. But the links don't seem to address the discrepancy between the definitions at the origin. –  Jens Sep 6 '13 at 18:51
    
@Jens: indeed, just the equivalence of the two functions. I'd be glad to remove my comments if you think it's more misleading than helpful. –  Andreas Lauschke Sep 6 '13 at 19:01
show 3 more comments

The warning messages tell you:

CompiledFunction::cfn: Numerical error encountered at instruction 33; proceeding with uncompiled evaluation. >>

ArcTan::indet: Indeterminate expression ArcTan[0.,0.] encountered. >>

By using an offset of 10^-8 you avoid having exactly zero in the list, and avoid the error.

The first version does not compile in version 7 so I can't tell you about that one.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.