MathGroup Archive 2009

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

Search the Archive

Plotting Speed: Plot and other functions not pre-compiling arguments?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg102352] Plotting Speed: Plot and other functions not pre-compiling arguments?
  • From: Uayeb <uayebswinburne at gmail.com>
  • Date: Thu, 6 Aug 2009 06:32:28 -0400 (EDT)

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}


  • Prev by Date: Re: Re: error with Sum and Infinity
  • Next by Date: Re: A Sum-like notation for iteration
  • Previous by thread: Re: NDSolve memory management problem
  • Next by thread: Re: Plotting Speed: Plot and other functions not