       Re: Can anyone see a faster way to compute quantities for a pair or

• To: mathgroup at smc.vnet.net
• Subject: [mg127430] Re: Can anyone see a faster way to compute quantities for a pair or
• From: Ray Koopman <koopman at sfu.ca>
• Date: Tue, 24 Jul 2012 04:15:38 -0400 (EDT)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com
• Delivered-to: mathgroup-newout@smc.vnet.net
• Delivered-to: mathgroup-newsend@smc.vnet.net
• References: <juim0j\$k9a\$1@smc.vnet.net>

```On Jul 22, 10:04 pm, W Craig Carter <ccar... at MIT.EDU> wrote:
> Hello,
> I am computing the gradient on a grid, then computing the gradient's
> angle, and its magnitude. The computations below are the bottleneck for
> a longer bit of code.  I would be grateful for any insights on how to
> speed these up.
>
> (*
> Let gradfield be the gradient that I have computed and placed in two matrices. Here I will just use random numbers as a proxy:
> *)
>
> (*i.e., df/dx, df/dy*)
> gradfield =  { RandomReal[{-1, 1}, {256, 256}], RandomReal[{-1, 1}, {256, 256}]};
>
> (*my gradients has many zeroes, so I need to handle these*)
>
> SetAttributes[myArcTan, {Listable, NumericFunction}];
> myArcTan[0.0, 0.0] = 0.0;
> myArcTan[x_, y_] := ArcTan[x, y]
>
> (*the angles, this is slow*)
>
> (*the magnitudes, this is slower*)
>
> (*examples*)
>
> W Craig Carter
> Professor of Materials Science, MIT

My previous post should have said