Re: Can anyone see a faster way to compute quantities for a pair or
- To: mathgroup at smc.vnet.net
- Subject: [mg127425] Re: Can anyone see a faster way to compute quantities for a pair or
- From: daniel.lichtblau0 at gmail.com
- Date: Mon, 23 Jul 2012 19:56:52 -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 Monday, July 23, 2012 12:04:19 AM UTC-5, W Craig Carter 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*) > psiField = MapThread[myArcTan, gradfield, 2]; > > (*the magnitudes, this is slower*) > magfield = MapThread[Norm[{#}] &, gradfield, 2]; > > > (*examples*) > Do[psiField = MapThread[myArcTan, gradfield, 2], {100}] // Timing > Do[magfield = MapThread[Norm[{#}] &, gradfield, 2], {100}] // Timing > > W Craig Carter > Professor of Materials Science, MIT Could use Compile to get a speed improvement. below shows one way to go about this. In[445]:= myArcTanC2 = Compile[{{xy, _Real, 1}}, With[{x = xy[[1]], y = xy[[2]]}, If[x == 0. && y == 0., 0., ArcTan[x, y]]]]; In[452]:= psiFieldFinder = Compile[{{gfield, _Real, 3}}, Map[myArcTanC2, Transpose[gfield, {3, 1, 2}], {2}], CompilationOptions -> {"ExpressionOptimization" -> True, "InlineCompiledFunctions" -> True}, CompilationTarget -> "C"]; In[453]:= Do[psiField2 = psiFieldFinder[gradfield], {100}] // Timing Out[453]= {0.85, Null} If you intend to do this on the same array then pre-transposing, and removing that step from the compiled function, might give a further slight improvement. For norms you might get better speed using Sqrt[arg1.arg2]. Daniel Lichtblau Wolfram Research