# Why does initialization slow down manipulate in this case?

I'm trying to manipulate a plot of function with some parameters. The function is a long expression, so I tried to put it inside the Initialization options of Manipulate. However, once I did that, the performance is really bad, compared to one that have the function explicitly written out. Here is a simplified version:

Here is the original one:

Manipulate[
DensityPlot[
Re[( C10 Exp[I 2 τ] )/(0.1 - I (ω - C10/10)) + (
C10 Exp[I 4 τ] )/(0.1 - I (ω - C10/10)) + (
C10 Exp[I 8 τ] )/(0.1 - I (ω - C10/10))], {τ,
0, π}, {ω, -0.5, 0.5}, PlotRange -> All,
PlotPoints -> 60],
{{C10, 0.5}, 0.1, 1.}]


and here is the one with definition inside Initialization option:

Manipulate[
DensityPlot[
f[ω, τ, C10], {τ, 0, π}, {ω, -0.5, 0.5},
PlotRange -> All, PlotPoints -> 60],
{{C10, 0.5}, 0.1, 1.},
Initialization :> {f[ω_, τ_, C10_] :=
Re[( C10 Exp[I 2 τ] )/(0.1 - I (ω - C10/10)) + (
C10 Exp[I 4 τ] )/(0.1 - I (ω - C10/10)) + (
C10 Exp[I 8 τ] )/(0.1 - I (ω - C10/10))]}]


Here is the screenshot for comparison:

They original one seems much faster than the later one. Why does this happen and how to solve it?

-

This seems just due to not having Evaluate inside the DensityPlot applied to the function.

Try this:

Manipulate[
DensityPlot[
Evaluate@f[\[Omega], \[Tau], C10], {\[Tau], 0, \[Pi]}, {\[Omega], -0.5, 0.5},
PlotRange -> All, PlotPoints -> 60],
{{C10, 0.5}, 0.1, 1.},
Initialization :> {f[\[Omega]_, \[Tau]_, C10_] :=
Re[( C10 Exp[I 2 \[Tau]] )/(0.1 - I (\[Omega] - C10/10)) + (
C10 Exp[I 4 \[Tau]] )/(0.1 - I (\[Omega] - C10/10)) + (
C10 Exp[I 8 \[Tau]] )/(0.1 - I (\[Omega] - C10/10))]}]


?? DensityPlot has HoldAll, same as Plot. This is a common thing to watch for when having functions inside functions that has Hold in them. see this http://reference.wolfram.com/mathematica/tutorial/NonStandardEvaluation.html

and Here are related questions that explains it well. Why do I have to put Evaluate[] here and this How and when to use Evaluate? and many more

So, this has nothing to do with Initialization. It is just the common issue of not using Evaluate when it is needed inside the Plot functions.

-
Thanks, that's very helpful. I never thought about Evaluate, because it gives the results but slower :) – xslittlegrass Feb 5 '14 at 14:54