MathGroup Archive 2010

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

Search the Archive

Re: A problem with Thread[]

  • To: mathgroup at smc.vnet.net
  • Subject: [mg111417] Re: A problem with Thread[]
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Sat, 31 Jul 2010 02:42:35 -0400 (EDT)
  • References: <i2ub41$jds$1@smc.vnet.net>

Am 30.07.2010 12:56, schrieb guerom00:
> Hello all,
> 
> 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...

The problem is that Thread evaluates its argument and MyArcTan does
evaluate with whatever argument you give it, so Thread just ends up to
see the result$... and has nothing left to do. You can see this from
evaluating the argument only:

MyArcTan[{-1, 0, 1}, 0]

The following does help Thread to see what you intend in this case:

Thread[Unevaluated[MyArcTan[{-1, 0, 1}, 0]]]

Note that the SetDelayed (:=) and the Module are not causing anything
but overhead in your case, this should always give the same results:

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

There are other possibilities to solve your problem, but it is not clear
which one would be the best without knowing more about what the big
picture is...

hth,

albert



  • Prev by Date: Re: Which inside Module causes problems with ReplaceAll
  • Next by Date: Re: AxesLabel parallel to 3D axes?
  • Previous by thread: Re: A problem with Thread[]
  • Next by thread: Cannot load example data