Re: Plotting Speed: Plot and other functions not

*To*: mathgroup at smc.vnet.net*Subject*: [mg102366] Re: [mg102352] Plotting Speed: Plot and other functions not*From*: DrMajorBob <btreat1 at austin.rr.com>*Date*: Fri, 7 Aug 2009 05:28:37 -0400 (EDT)*References*: <200908061032.GAA01807@smc.vnet.net>*Reply-to*: drmajorbob at bigfoot.com

I didn't get into the Compile attempts, but I did notice that the Timing example with Pi/2 is slower than the other no matter WHEN you execute the two. For instance: f[x_, y_] := 2 x + 3 y; myRotate[pa_, e_, xoff_, yoff_, \[Theta]_, r_] := {xoff, yoff} + {r Cos[\[Pi] + \[Theta]], r e Sin[\[Pi] + \[Theta]]}.RotationMatrix[-pa]; {t2, plot} = Timing[Show[ Table[Plot[ f @@ {3 + 0.5` r Sin[\[Theta]], 4 - r Cos[\[Theta]]}, {\[Theta], 0, 2 \[Pi]}], {r, {1, 3, 5, 9}}]];] {0.031034, Null} myRotate[\[Pi]/2, .5, 3, 4, \[Theta], r] {3 + 0.5 r Sin[\[Theta]], 4 - r Cos[\[Theta]]} {t1, plot} = Timing[Show[ Table[Plot[ f @@ myRotate[\[Pi]/2, .5, 3, 4, \[Theta], r], {\[Theta], 0, 2 \[Pi]}], {r, {1, 3, 5, 9}}]];] {0.072181, Null} or Quit f[x_, y_] := 2 x + 3 y; myRotate[pa_, e_, xoff_, yoff_, \[Theta]_, r_] := {xoff, yoff} + {r Cos[\[Pi] + \[Theta]], r e Sin[\[Pi] + \[Theta]]}.RotationMatrix[-pa]; {t1, plot} = Timing[Show[ Table[Plot[ f @@ myRotate[\[Pi]/2, .5, 3, 4, \[Theta], r], {\[Theta], 0, 2 \[Pi]}], {r, {1, 3, 5, 9}}]];] {0.016648, Null} myRotate[\[Pi]/2, .5, 3, 4, \[Theta], r] myRotate[\[Pi]/2, 0.5, 3, 4, \[Theta], r] {t2, plot} = Timing[Show[ Table[Plot[ f @@ {3 + 0.5` r Sin[\[Theta]], 4 - r Cos[\[Theta]]}, {\[Theta], 0, 2 \[Pi]}], {r, {1, 3, 5, 9}}]];] {0.006654, Null} Real arithmetic is faster than exact; it just is. Also, Set is faster than SetDelayed, so: f[x_, y_] = 2 x + 3 y; myRotate[pa_, e_, xoff_, yoff_, \[Theta]_, r_] = {xoff, yoff} + {r Cos[\[Pi] + \[Theta]], r e Sin[\[Pi] + \[Theta]]}.RotationMatrix[-pa]; {t2, plot} = Timing[Show[ Table[Plot[ f @@ {3 + 0.5` r Sin[\[Theta]], 4 - r Cos[\[Theta]]}, {\[Theta], 0, 2 \[Pi]}], {r, {1, 3, 5, 9}}]];] {0.02081, Null} {t1, plot} = Timing[Show[ Table[Plot[ f @@ myRotate[\[Pi]/2, .5, 3, 4, \[Theta], r], {\[Theta], 0, 2 \[Pi]}], {r, {1, 3, 5, 9}}]];] {0.037412, Null} Bobby On Thu, 06 Aug 2009 05:32:28 -0500, Uayeb <uayebswinburne at gmail.com> wrote: > All, > > This problem has bugged me for some time, particularly when I want to > put plots inside manipulate functions. I like to define intermediate > functions to abstract more complex computations from becoming pages of > mathematica code, however, Mathematica seems to take a serious > performance hit when I do this. Any ideas would be greatly > appreciated. I've copied the notes and input/output from a notebook > demonstrating this problem below. > > First we define a rediculously simple function of two variables. > > f[x_, y_] := 2 x + 3 y > > Then we define a coordinate transformation, which is more complex. > > myRotate[pa_, e_, xoff_, yoff_, \[Theta]_, > r_] := {xoff, > yoff} + {r Cos[\[Pi] + \[Theta]], > r e Sin[\[Pi] + \[Theta]]}.RotationMatrix[-pa] > > Asking Mathematica to make some plots using this rotation is slow. > > {t1, plot} = Timing[ > Show[ > Table[ > Plot[f @@ myRotate[\[Pi]/2, .5, 3, 4, \[Theta], r], {\[Theta], > 0, > 2 \[Pi]}], > {r, {1, 3, 5, 9}}]];] > >>>> {0.567545, Null} > > But, by executing the rotation with an undefined variable first, and > replacing the function call inside plot with the "precomputed" result, > the plot is 20 times faster. > > myRotate[\[Pi]/2, .5, 3, 4, \[Theta], r] > >>>> {3 + 0.5 r Sin[\[Theta]], 4 - r Cos[\[Theta]]} > > {t2, plot} = Timing[ > Show[ > Table[ > Plot[f @@ {3 + 0.5` r Sin[\[Theta]], 4 - r Cos[\[Theta]]}, {\ > [Theta], 0, > 2 \[Pi]}], > {r, {1, 3, 5, 9}}]];] > >>>> {0.025042, Null} > > t1/t2 > >>>> 21.9529 > > Naievely attempting to "complie" the function only makes the problem > worse, because Mathematica seems to be trying to simplify the plotting > function symbolicly first, although the above analysis doesn't support > this. > > cmyRotate = > Compile[{{pa, _Real}, {e, _Real}, {xoff, _Real}, {yoff, _Real}, {\ > [Theta], \ > _Real}, {r, _Real}}, {xoff, > yoff} + {r Cos[\[Pi] + \[Theta]], > r e Sin[\[Pi] + \[Theta]]}.RotationMatrix[-pa]]; > > Timing[ > Show[ > Table[ > Plot[f @@ cmyRotate[\[Pi]/2, .5, 3, 4, \[Theta], r], {\[Theta], > 0, > 2 \[Pi]}], > {r, {1, 3, 5, 9}}]];] > > CompiledFunction::cfse: Compiled expression {{6.12323*10^-17,1.}, > {-1.,6.12323*10^-17}} should be a machine-size real number. >> > > CompiledFunction::cfex: Could not complete external evaluation at > instruction 13; proceeding with uncompiled evaluation. >> > > CompiledFunction::cfse: Compiled expression {{6.12323*10^-17,1.}, > {-1.,6.12323*10^-17}} should be a machine-size real number. >> > > CompiledFunction::cfex: Could not complete external evaluation at > instruction 13; proceeding with uncompiled evaluation. >> > > CompiledFunction::cfse: Compiled expression {{6.12323*10^-17,1.}, > {-1.,6.12323*10^-17}} should be a machine-size real number. >> > > General::stop: Further output of CompiledFunction::cfse will be > suppressed during this calculation. >> > > CompiledFunction::cfex: Could not complete external evaluation at > instruction 13; proceeding with uncompiled evaluation. >> > > General::stop: Further output of CompiledFunction::cfex will be > suppressed during this calculation. >> > > {1.42678, Null} > -- DrMajorBob at bigfoot.com

**References**:**Plotting Speed: Plot and other functions not pre-compiling arguments?***From:*Uayeb <uayebswinburne@gmail.com>