MathGroup Archive 1999

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

Search the Archive

Re: Plotting multiple outputs from cpu intensive function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg16805] Re: [mg16745] Plotting multiple outputs from cpu intensive function
  • From: "Wolf, Hartmut" <hwolf at debis.com>
  • Date: Tue, 30 Mar 1999 02:35:12 -0500
  • Organization: debis Systemhaus
  • References: <199903240723.CAA09319@smc.vnet.net.>
  • Sender: owner-wri-mathgroup at wolfram.com

Dear Joel,

jns1 schrieb:
> 
> Here is one that has stumped me for a while.
> Now I need to do it again, and would appreciate any advice.
> 
> Say you are using a module to calculate something that takes quite a
> LONG while.
> 
> It has 2 or more outputs in an output list, or whatever is convenient:
> 
> f[x_]:=Module[{...},
>  ...long cpu intensive stuff...;
> {out1,out2}]
> 
> out1 and out2 are calculated together because they are closely related
> and share a lot of the needed cputime. How would you go about plotting
> out1 and out2 together in the same graph without duplicating the
> cputime?
> 

> This suggests trying Evaluate[f[x]] instead of f[x] in the plot
> statement, which
> does work if it is o.k. to immediately evaluate f[x] before the
> plotting. But of
> course this seldom is doable. In my case there are numerical
> integrations or inputting
> from files going on, so delayed evaluation must be used.
> 

I don't think it is right to say that f[x] is "evaluated immediately
before plotting". Plot doesn't evaluate the standard way! To demonstrate
this I give you an example:

In[3]:= chi[x_]:=2 Mod[Ceiling[x/Pi],2]-1

In[4]:= f2[x_]:={c=-chi[x-Pi/2];s=Sin[x], c Sqrt[1-s^2]}

In[5]:= g2=Plot[Evaluate[f2[x]], {x,0,2 Pi}]

- graphics not shown here -

You see the second componend of the list-valued function doesn't contain
the variable x, but it computes the cosine efficiently by using the
result of Sin when the first component was calculated.

But if you dare to look at g2//FullForm you'll see there two long Lines,
for Sin and our "efficient cosine" respectively. But you also will see,
the plotting points are different. And they have to be! That's the very
intelligence of the Plot-function. So in fact we did not "gain
efficiency": the plotpoints for both function were calculated
seperately, since they have to have a different spacing according to
differently bending lines. If you reconsider that, then Plot did not
waste your computational resources, it just did what was neccessary.

What Plot could not do was using already calculated values in regions
where the spacing of the interpolating points for both components is
nearly the same. So if you wan't to do that, you must take it into your
own hands. You know your function better than Mathematica does: find out
the regions for dense or spare plotting -- and perhaps you find your own
way of clever interpolating? -- and construct your own graphic elements
for the final Show.
Or, what amounts to the same, use ListPlot, the way you proposed ---

> So I am stuck with 2 alternatives:
> 
> 1) Make a table of values of {out1,out2}'s for different x's and then
> use ListPlot or
> 
---(added with intelligent spacing and interpolating).

But I would not suggest to---

> 2) Make 2 separate functions, f1[x_]:=f[x][[1]], and f2[x_]:=f[x][[2]]
> and use
> Plot[{f1[x],f2[x]},{x,x1,x2}]. This works fine, but it DOUBLES the cpu
> time, for no reason.
> 
---I have given the reason.

> Is there perhaps some combination of Hold's, Evaluate's, or whatever
> where I can plot
> things like this together conveniently?
>
---I couldn't help you with that, but hopefully have clarified the
views.

Kind regards,
	Hartmut Wolf



  • Prev by Date: Re: An open letter
  • Next by Date: Re: system of differential couple equations...how?
  • Previous by thread: Plotting multiple outputs from cpu intensive function
  • Next by thread: Mathlink problem