Making the Most of the Visualization Functions

To Evaluate, or not to Evaluate...

Ulises Cervantes-Pimentel
Brett Champion

Wolfram Research, Inc.

When to use Evaluate?

Using Evaluate can be subtle

Evaluate can speed up things tremendously

Evaluate can slow things down tremendously

Evaluate can result in bad plots

A lot of the things here can also apply to FindMinimum, NIntegrate, etc...

Evaluate to the Rescue!

Useful when

Result of Evaluation is relatively small

Object to be plotted involves lots of computation

Plot[x/.Solve[x^2+y^2==1,x],{y,-1,1}]//Timing

Plot[Evaluate[x/.Solve[x^2+y^2==1,x]],{y,-1,1}]//Timing

Evaluate can be Slooooooowwwww....

DensityPlot[Abs@Nest[#1+(1+2^#1-#1^2)/(2*#1-2^#1*Log[2.])&,x+I*y,10],{y,-10,10},{x,-10,10},ColorFunction->Hue,ClippingStyle->Automatic,PlotPoints->50]//Timing

DensityPlot[Evaluate[Abs[Nest[#1+(1+2^#1-#1^2)/(2*#1-2^#1*Log[2.])&,x+I*y,10]]],{y,-10,10},{x,-10,10},ColorFunction->Hue,ClippingStyle->Automatic,PlotPoints->50]//Timing

This expression is huge when symbolic.  When computed numerically, everything collapses at each step.

Note that this version below only uses two levels of nesting, instead of the ten used in the plots above.

Abs[Nest[#1+(1+2^#1-#1^2)/(2*#1-2^#1*Log[2.])&,x+I*y,2]]

Evaluate can screw things up!

Different forms of the same thing may evaluate differently:

Plot[BesselJ[11.5,x],{x,0,1}]//Timing

Plot[Evaluate[BesselJ[11.5,x]],{x,0,1}]//Timing

BesselJ[11.5,x]

Beware of "programs"

Plot[Length[Factor[x^Floor[k]-1]],{k,1,10},PlotRange->{0,5},Exclusions->Range[10]]

Length[Factor[x^Round[k] - 1]] returns a number, even for symbolic versions of k.

Plot[Evaluate[Length[Factor[x^Round[k]-1]]],{k,1,10},PlotRange->{0,5}]