Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: A problem with Thread[]

  • To: mathgroup at smc.vnet.net
  • Subject: [mg111405] Re: A problem with Thread[]
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Sat, 31 Jul 2010 02:40:25 -0400 (EDT)

On 7/30/10 at 6:56 AM, guerom00 at gmail.com (guerom00) wrote:

>Here is a snippet of code:

>MyArcTan := Function[{x, y},
>Module[{result},
>If[x == 0 && y == 0,
>result = 0,
>result = ArcTan[x, y]
>];
>result
>]
>];

>In[725]:= Thread[MyArcTan[{-1, 0, 1}, 0]]

>Out[725]= result$58407737

>Somehow, when I thread my custom function MyArcTan[], it does not
>execute the If[] test hence do not define the result...

>How can I solve that ?

No, the problem isn't with Thread or If. The problem is related
to the way Module localizes variables. For this function, there
is no need for Module or the local variable result. Your
function can be more simply written as:

MyArcTan := Function[{x, y}, If[x == 0 && y == 0, 0, ArcTan[x, y]=
]];

or

MyArcTan[x_, y_}]:= If[x == 0 && y == 0, 0, ArcTan[x, y]]

with either of these you can do:

In[3]:= Thread[f[{-1, 0, 1}, 0]] /. f -> MyArcTan

Out[3]= {Pi, 0, 0}

or

In[4]:= MapThread[MyArcTan, {{-1, 0, 0}, {0, 0, 0}}]

Out[4]= {Pi, 0, 0}

to get the desired output.



  • Prev by Date: Re: How to remove cells with a style?
  • Next by Date: Re: Which inside Module causes problems with ReplaceAll
  • Previous by thread: Re: A problem with Thread[]
  • Next by thread: Re: A problem with Thread[]