problem with using some Functions with LocatorPane Dynamic point - wolfram-mathematica

I can't figure out why Mathematica behaves this way, may be someone can see the problem. I am no expert on Dynamics, so I might be overlooking something.
I show the code first and then say what the problem is.
Clear[t, s, n, z];
Grid[{
{LocatorPane[Dynamic[p], Graphics[Circle[{0, 0}, 1], ImageSize -> 200],{{-1, -1}, {1, 1}}]},
{Dynamic[{Print#Date[]; Print[ZTransform[n^2/2^n, n, z]]; p}]}}
]
When running the above, you will see that it keeps looping all the time, since it keeps printing. You will see the print messages come out without doing anything to the LocatorPane or moving the mouse.
But when I changed the function above which is ZTransform, to something else, say Laplace, then the looping stops:
Clear[t, s, n, z];
Grid[{
{LocatorPane[Dynamic[p], Graphics[Circle[{0, 0}, 1], ImageSize -> 200],{{-1, -1}, {1, 1}}]},
{Dynamic[{Print#Date[]; Print[LaplaceTransform[t^4*Sin[t], t, s]]; p}]}}
]
It seems functions related Fourier causes this, since I tried this one also and it had the same problem:
Clear[t, s, n, z,w];
Grid[{
{LocatorPane[Dynamic[p], Graphics[Circle[{0, 0}, 1], ImageSize -> 200],{{-1, -1}, {1, 1}}]},
{Dynamic[{Print#Date[]; Print[FourierSequenceTransform[(1/2)^n UnitStep[n], n, w]]; p}]}}
]
Another way of doing the above is using a Module:
process[p_] := Module[{n, z, t, s, w},
Print[Date[]];
ZTransform[n^2 2^(-n), n, z];
p
]
Grid[{
{LocatorPane[Dynamic[p],
Graphics[Circle[{0, 0}, 1],ImageSize -> 200], {{-1, -1}, {1, 1}}]},
{Dynamic[process[p]]}
}]
And again, same problem, I see the looping again. I have to wrap the call to process[p] above with Dynamics to pass the current value of 'p'.
So, my question is why when I use some functions such as ZTransform, Dynamics continue to update, but some other functions such as Laplace, I do not see this problem.
And what can I do to fix this? I do need to call ZTransform using the updated point 'p' in this example.
This is on version 8.01 on windows.
thanks
EDIT1:
I found something which might help. When I add FinishDynamic[] after the call to ZTransform[], it blocks. But not with another call such as Laplace. This means, according to documenation, that ZTransform generates a Dynamic which has not finished updating. What object is this?
Here is an example:
process[p_] := Module[{n, z},
Print[Date[]];
ZTransform[n^2 2^(-n), n, z]; (*bad*)
FinishDynamic[]; (*BLOCKS*)
p
]
Grid[{
{LocatorPane[Dynamic[p],
Graphics[Circle[{0, 0}, 1],
ImageSize -> 200], {{-1, -1}, {1, 1}}]},
{Dynamic[process[p]]}
}]
Again, changing ZTransform[] to some other call, say Laplace[] does not have this problem.
So, it seems to me that ZTransform[] is generating some Dynamic which never finish updating or something along these lines?
EDIT2:
I found the solution. Add trackedSymbols. Here it is
process[p_] := Module[{n, z},
Print[Date[]];
ZTransform[n^2 2^(-n), n, z];(*bad*)
p]
Grid[{
{LocatorPane[Dynamic[p], Graphics[Circle[{0, 0}, 1],ImageSize -> 200], {{-1, -1}, {1, 1}}]},
{Dynamic[process[p], TrackedSymbols :> {p}]
}
}]
Not sure why it is needed when calling ZTransform and not needed with other functions. My guess is this: ZTransform generated a dynamic internally that never finished 'updating' (that is why it blocked as per EDIT1 above).
By explicitly adding TrackedSymbols only on the LocatorPane variable 'p', it now works since whatever other dynamic was causing the problem inside ZTrasnform is not tracked now.

Have you tried turning off SynchronousUpdating? (see documentation)
I am not entirely sure why some functions are doing this and not others, but it might be that the particular kind of transform you are doing doesn't finish evaluating before the Dynamic tries to update. If so, another thing to try would be to change the value of the SynchronousInitialization option to False (default is True). Then the transform is queued even while the Dynamic or Manipulate construct is updating, instead of (according to the documentation), waiting until the evaluation of the initialization expression is complete before proceeding.

Related

Eliminating the effect of locator in Mathematica when it is outside a range

I want to limit the effect of locator, so when I click on the space but not between some required range, the locator does nothing. Right now it will be located at the nearest point of the range to the point that I have clicked. How is it possible?
Presumably you are using Manipulate which include this behaviour, e.g.
Manipulate[Graphics[Line[{{0, 0}, p}],
PlotRange -> 2], {{p, {1, 1}}, Locator}]
One solution would be to use DynamicModule instead:
DynamicModule[{p = {1, 1}}, Deploy#Framed#
Graphics[{Line[{{0, 0}, Dynamic[p]}],
Locator[Dynamic[p]]}, PlotRange -> 2]]
Also related:
What is the equivalent of a prototypical Manipulate in lower level functions?

How to define part of a Manipulate control variable definition to reduce code duplication

This is a little related to this question
Define control as variable in Mathematica
But the above question did not answer my problem, as it talks about the full control definition. (I also tried some of the tricks shown there, but they do not work for my problem).
I am now asking about definition for only part of the control. (It is also very hard to follow up on an old question using this forum format. Because using the tiny comment area, it hard to ask and show more like when asking a new question where the space is larger, and one can paste code and images).
All the tries I have made do not work. I'll start by simple example to explain the problem.
Assume one want to write
Clear["Global`*"];
Manipulate[Plot[f*g, {x, -1, 1}],
Grid[{
{Style["f(x)="],
PopupMenu[Dynamic[f], {x, x^2, x^3}, ImageSize -> Tiny]},{Style["g(x)="],
PopupMenu[Dynamic[g], {x, x^2, x^3}, ImageSize -> Tiny]}
}]
]
you can see there is allot of code duplication in each control definition. (things like ImageSize, Spacings-> and many other decoration settings, are repeated over and over for each control.
What will be great, if I can write something like
Manipulate[Plot[f*g, {x, -1, 1}],
Grid[{
{Style["f(x)="], PopupMenu[Dynamic[f], Evaluate#Sequence#v]},
{Style["g(x)="], PopupMenu[Dynamic[g], Evaluate#Sequence#v]}
}],
Initialization :>
(
v = {{x, x^2, x^3}, ImageSize -> Tiny}
)
]
But this does not work. I tries many other things along the above line, and nothing works. Like
{Style["f(x)="], PopupMenu[Dynamic[f], v]},
and
{Style["f(x)="], PopupMenu[Dynamic[f], Evaluate#v]}
and
Manipulate[Plot[f*g, {x, -1, 1}],
{{v, {{x, x^2, x^3}, ImageSize -> Tiny}}, None},
Grid[{
{Style["f(x)="], PopupMenu[Dynamic[f], Evaluate#v]},
{Style["g(x)="], PopupMenu[Dynamic[g], v]}
}]
]
can't get it to work.
But here are the rules of the game: This will be for a demo, hence, code must start with Manipulate. Can't have Module outside Manipulate. Also, can not use Hold and its friends. But can use Unevaluated.
I was hoping the experts here might have a trick to do this. The will reduce the code size if it is possible to do, as many of the control I have, contain many 'options' like the above that are the same, and being able to do the above will make the code easier to read and manage.
thanks,
ps. What I am asking for, is sort of similar to what one does for say Plot options, where one can use SetOptions to set some common default options so they do not have to duplicate them for each Plot command each time. But there is no such thing in this case.
update
Using the method shown by Leonid below, (the Macro trick), I wanted to use it to help me define number of controls, all using one common setting. This is what I tried:
Manipulate[{x, y},
Evaluate#With[
{
control1 = Function[{var, initialValue, str, from, to, incr},
{
{{var, initialValue, str}, from, to, incr, ImageSize -> Tiny}
}
,
HoldAll
]
},
{
First#control1[x, 0, "x=", 0, 1, .1],
First#control1[y, 0, "y=", 0, 2, .1],
First#control1[z, 0, "z=", 0, 10, .1]
},
]
]
The problem is just an extra {} around the whole thing, else it will work. Will keep trying to solve this. But getting close. Tried Sequence[], and Flatten[..,1] and such, but can not do it yet. Making more coffee, should help.
Update 2
This is below an example using Simon method to use to help define common definition across more than one control. This way, one can use it to reduce code duplication for common options on set of separate controls
Notice, had to use Control[] to get it to control.
Manipulate[{x, y, z},
Dynamic[Grid[{
{control1[x, 0, "x=", 0, 1, .1]},
{control1[y, 0, "y=", 0, 2, .1]},
{control1[z, 0, "z=", 0, 10, .1]}
}]],
{{control1,
Function[{var, initialValue, str, from, to, incr},
Control[{{var, initialValue, str}, from, to, incr,
ImageSize -> Tiny}], HoldFirst]}, None}
]
Update 3
And got Leonid method to work also on more than one control. The trick is to use Control[]. Can't use plain old {{x,0,"x"},...} [EDIT, yes, you can, just need the Sequence## method as shown below by Leonid update.].
Here it is:
Manipulate[{x, y, z},
Evaluate#With[
{
control1 = Function[{var, initialValue, str, from, to, incr},
Control[{{var, initialValue, str}, from, to, incr,
ImageSize -> Tiny}]
, HoldAll
]
},
Grid[{
{control1[x, 0, "x=", 0, 1, .1]},
{control1[y, 0, "y=", 0, 2, .1]},
{control1[z, 0, "z=", 0, 10, .1]}
}]
]
]
I'll try to integrate one of these methods into my main demo (has over 600 lines of code just for the control layout so far and growing by the minute, hopefully these methods will shrink this by quite a bit)
Update 9/26/11. 7 pm
I thought I post a 'birds eye' view of the code saving by using 'macros' to define controls which contains many common boiler-plate code. Here is a screen shot of before and after.
Thanks again for all the answer and help.
What about this
Manipulate[Plot[f*g, {x, -1, 1}],
Evaluate#
With[{styleAndpopup =
Function[{st, fun},
{
Style[st],
PopupMenu[Dynamic[fun], {x, x^2, x^3}, ImageSize -> Tiny]
},
HoldAll]},
Grid[{styleAndpopup["f(x)=", f], styleAndpopup["g(x)=", g]}]]]
This is actually a tiny example of the code-generation at work, since if you look at the FullForm of the resulting Manipulate, you will see the same expression you originally started with. The styleAndpopup is actually not a function here, but a macro, locally defined using With.
EDIT
Per request of the OP - generalizing to many controls. The easiest fix is to insert Sequence##... as Sequence ## {First#control1[.... However, there is some extraneous stuff that can be removed as well:
Manipulate[{x, y},
Evaluate#With[{control1 =
Function[{var, initialValue, str, from, to, incr},
Unevaluated#{{var, initialValue, str}, from, to, incr, ImageSize -> Tiny},
HoldAll]},
Sequence ## {
control1[x, 0, "x=", 0, 1, .1],
control1[y, 0, "y=", 0, 2, .1],
control1[z, 0, "z=", 0, 10, .1]}]]
I was going to give a solution almost the same as Leonid's and use With to insert the code, but he beat me to it, so here's an alternative way. Define a dynamic local function using ControlType -> None that does your styling:
Manipulate[Plot[{f, g + 1}, {x, -1, 1}],
Dynamic[Grid[{{Style["f(x)="], pu[f]},
{Style["g(x)="], pu[g]}}]],
{{pu, Function[{f}, PopupMenu[Dynamic[f], {x, x^2, x^3}, ImageSize -> Tiny],
HoldFirst]}, None}]
By the way, the Style[] in Style["f(x)="] is redundant, as you are not actually setting any styles...
One could do this:
Manipulate[
Plot[f*g, {x, -1, 1}]
, Grid[
{ {Style["f(x)="], PopupMenu[Dynamic[f], opts]}
, {Style["g(x)="], PopupMenu[Dynamic[g], opts]}
}
]
] /. opts -> Sequence[{x, x^2, x^3}, ImageSize -> Tiny]
If one is in the habit of assigning down-values to symbols whose names do not begin with $, then it would be prudent to wrap the whole thing in Block[{x, opts}, ...] in case x and opts have globally-defined values.
A similar technique is possible for the case of multiple controls:
Manipulate[
{x, y, z}
, Grid[
{ {control1[x, 0, "x=", 0, 1, .1]}
, {control1[y, 0, "y=", 0, 2, .1]}
, {control1[z, 0, "z=", 0, 10, .1]}
}
]
] /. control1[var_, initialValue_, str_, from_, to_, incr_] :>
Control[{{var, initialValue, str}, from, to, incr, ImageSize -> Tiny}]

Update Manipulate[]'d plots when parameters change

I've been fighting with Mathematica's Manipulate function for the last few days for a project.
I'm working on tweaking assumptions and boundary conditions that go into a physical model. For this, I want to be able to plot different equations and adjust the parameters and have the graphs update on the fly. Manipulate seems to be the perfect tool for the job -- except that I can't get it to work. The plots won't update when the parameters are changed.
Basic example:
a =.;
b =.;
c =.;
func1[x_] := a*x;
func2[x_] := a*x^2 + b*x + c;
funcNamesList := {"Linear", "Quadratic"};
funcList := {func1[x], func2[x]}
Manipulate[
Plot[function, {x, -5, 5}], {function,MapThread[Function[#1 -> #2],
{funcList, funcNamesList}]}, {a, -5, 5}, {b, -5, 5}, {c, -5, 5},
LocalizeVariables -> False
]
I can get, for example, func1 to refresh by clicking func1, adjusting a, and then clickingfunc1 again, but I'm hoping to have it update when I adjust a because the real functions I'm using are rather temperamental with respect to their parameters.
-Because I'll be dealing with long functions that have different parameters, using a list of functions is useful.
EDIT:
In case it produces any ideas for anyone, here are some working examples of the individual components of what I want to do (from the Wolfram documentation):
Plot graphs and have them update when parameters are changed:
Manipulate[
Plot[Sin[a x + b], {x, 0, 6}], {{a, 2, "Multiplier"}, 1, 4},
{{b, 0, "Phase Parameter"}, 0, 10}
]
Note: This breaks when the function is taken outside:
func[x] := Sin[a x + b];
Manipulate[
Plot[func[x], {x, 0, 6}], {{a, 2, "Multiplier"}, 1, 4},
{{b, 0, "Phase Parameter"}, 0, 10}, LocalizeVariables -> False
]
Example of changing the function being plotted:
Manipulate[
Plot[f[x], {x, 0, 2 Pi}], {f, {Sin -> "sine", Cos -> "cosine", Tan -> "tangent"}}
]
Edit 2
Changed func2 from a*x^2 to a*x^2 + b*x + c to reflect the fact that the functions may have different parameters.
Edit 3 Added the tidbit I use to get nice names on the function buttons.
There are two problems that prevent your Manipulate statement from working.
First, while the Manipulate variable a is global due to the LocalizeVariables -> False setting, the Plot variable x is not. x is local to the Plot expression.
The second problem is that Manipulate, by default, assumes TrackedSymbols -> Full. This means that only symbols that explicitly appear in the manipulated expression are tracked. Note that a does not appear in the expression, so it is not tracked.
We can correct both problems thus:
a =.;
function =.;
func1[x_] := a*x;
func2[x_] := a*x^2;
funcList := {func1, func2}
Manipulate[
Plot[function[x], {x, -5, 5}], {function, funcList}, {a, -5, 5},
LocalizeVariables -> False, TrackedSymbols :> {a, function}
]
The changes are:
funcList was changed to {func1, func2}
The Plot expression was changed to function[x], thereby referencing the local x variable.
The Manipulate option TrackedSymbols :> {a, function} was added.
function is initially unset.
I'd do this in a slightly different way:
func1[x_, a_] := a*x;
func2[x_, a_] := a*x^2;
funcList = {func1, func2};
Manipulate[
Plot[Evaluate[function[x, b]],
{x, -5, 5},
PlotLabel \[Rule] funcList
],
{function, funcList},
{b, -5, 5}
]
but this may be unsuitable for what you want. Do your functions have different signatures?
EDIT: I've renamed the parameter to b to make it clearer that is it just a parameter being passed, as opposed to a global variable as you were using it.

how to build a list on the fly with condition, the functional way

I am still not good working with lists in Mathematica the functional way. Here is a small problem that I'd like to ask what is a good functional way to solve.
I have say the following list made up of points. Hence each element is coordinates (x,y) of one point.
a = {{1, 2}, {3, 4}, {5, 6}}
I'd like to traverse this list, and every time I find a point whose y-coordinate is say > 3.5, I want to generate a complex conjugate point of it. At the end, I want to return a list of the points generated. So, in the above example, there are 2 points which will meet this condition. Hence the final list will have 5 points in it, the 3 original ones, and 2 complex conjugtes ones.
I tried this:
If[#[[2]] > 3.5, {#, {#[[1]], -#[[2]]}}, #] & /# a
but I get this
{{1, 2}, {{3, 4}, {3, -4}}, {{5, 6}, {5, -6}}}
You see the extra {} in the middle, around the points where I had to add a complex conjugate point. I'd like the result to be like this:
{{1, 2}, {3, 4}, {3, -4}, {5, 6}, {5, -6}}
I tried inserting Flatten, but did not work, So, I find myself sometimes going back to my old procedural way, and using things like Table and Do loop like this:
a = {{1, 2}, {3, 4}, {5, 6}}
result = {};
Do[
If[a[[i, 2]] > 3.5,
{
AppendTo[result, a[[i]]]; AppendTo[result, {a[[i, 1]], -a[[i, 2]]}]
},
AppendTo[result, a[[i]]]
],
{i, 1, Length[a]}
]
Which gives me what I want, but not functional solution, and I do not like it.
What would be the best functional way to solve such a list operation?
update 1
Using the same data above, let assume I want to make a calculation per each point as I traverse the list, and use this calculation in building the list. Let assume I want to find the Norm of the point (position vector), and use that to build a list, whose each element will now be {norm, point}. And follow the same logic as above. Hence, the only difference is that I am making an extra calculation at each step.
This is what I did using the solution provided:
a = {{1, 2}, {3, 4}, {5, 6}}
If[#[[2]] > 3.5,
Unevaluated#Sequence[ {Norm[#], #}, {Norm[#], {#[[1]], -#[[2]]}}],
{Norm[#], #}
] & /# a
Which gives what I want:
{ {Sqrt[5],{1,2}}, {5,{3,4}}, {5,{3,-4}}, {Sqrt[61],{5,6}}, {Sqrt[61],{5,-6}} }
The only issue I have with this, is that I am duplicating the call to Norm[#] for the same point in 3 places. Is there a way to do this without this duplication of computation?
This is how I currently do the above, again, using my old procedural way:
a = {{1, 2}, {3, 4}, {5, 6}}
result = {};
Do[
o = Norm[a[[i]]];
If[a[[i, 2]] > 3.5,
{
AppendTo[result, {o, a[[i]]}]; AppendTo[result, {o, {a[[i, 1]], -a[[i, 2]]}}]
},
AppendTo[result, {o, a[[i]]}]
],
{i, 1, Length[a]}
]
And I get the same result as the functional way, but in the above, since I used a temporary variable, I am doing the calculation one time per point.
Is this a place for things like sow and reap? I really never understood well these 2 functions. If not, how would you do this in functional way?
thanks
One way is to use Sequence.
Just a minor modification to your solution:
If[#1[[2]] > 3.5, Unevaluated#Sequence[#1, {#1[[1]], -#1[[2]]}], #1] & /# a
However, a plain ReplaceAll might be simpler:
a /. {x_, y_} /; y > 3.5 :> Sequence[{x, y}, {x, -y}]
This type of usage is the precise reason Rule and RuleDelayed have attribute SequenceHold.
Answer to update 1
I'd do it in two steps:
b = a /. {x_, y_} /; y > 3.5 :> Sequence[{x, y}, {x, -y}]
{Norm[#], #}& /# b
In a real calculation there's a chance you'd want to use the norm separately, so a Norm /# b might do
While Mathematica can simulate functional programming paradigms quite well, you might consider using Mathematica's native paradigm -- pattern matching:
a = {{1,2},{3,4},{5,6}}
b = a /. p:{x_, y_ /; y > 3.5} :> Sequence[p, {x, -y}]
You can then further transform the result to include the Norms:
c = Cases[b, p_ :> {Norm#p, p}]
There is no doubt that using Sequence to generate a very large list is not as efficient as, say, pre-allocating an array of the correct size and then updating it using element assignments. However, I usually prefer clarity of expression over such micro-optimization unless said optimization is measured to be crucial to my application.
Flatten takens a second argument that specifies the depth to which to flatten. Thus, you could also do the following.
a = {{1, 2}, {3, 4}, {5, 6}};
Flatten[If[#[[2]] > 3.5, {#, {#[[1]], -#[[2]]}}, {#}] & /# a, 1]
The most serious problem with your Do loop is the use of AppendTo. This will be very slow if result grows long. The standard way to deal with lists that grow as the result of a procedure like this is to use Reap and Sow. In this example, you can do something like so.
new = Reap[
Do[If[el[[2]] > 3.5, Sow[{el[[1]], -el[[2]]}]],
{el, a}]][[2, 1]];
Join[a, new]
To answer your edit, use With (or Module) if you're going to use something expensive more than once.
Here's my version of the problem in your edit:
a = {{1, 2}, {3, 4}, {5, 6}};
Table[With[{n = Norm[x]},
Unevaluated#Sequence[{n, x},
If[x[[2]] > 3.5, {n, {1, -1} x}, Unevaluated#Sequence[]]]],
{x, a}]
The structure of the above could be modified for use in a Map or ReplaceAll version, but I think that Table is clearer in this case. The unevaluated sequences are a little annoying. You could instead use some undefined function f then replace f with Sequence at the end.
Mark's Sow/Reap code does not return the elements in the order requested. This does:
a = {{1, 2}, {3, 4}, {5, 6}};
Reap[
If[Sow[#][[2]] > 3.5, Sow[# {1, -1}]] & /# a;
][[2, 1]]
You may use join with Apply(##):
Join ## ((If[#[[2]] > 3.5, {#, {#[[1]], -#[[2]]}}, {#}]) & /# a)

Mathematica: Asynchronous incremental generation of dynamical graphics

What is the simplest way to asynchronously apply consecutive improvements to a Graphics object in a dynamic setting (and abort the evaluation of the unneeded results if input changes while they are being computed)?
As a simple example, consider this:
speed[r_] := Graphics#{Red, Circle[{0, 0}, r]}
qualityA[r_] := (Pause[1]; Graphics#{Red, Disk[{0, 0}, r]})
qualityB[r_] := (Pause[1]; Graphics#{Black, Circle[{0, 0}, r]})
Manipulate[Show[
ControlActive[speed[r], {qualityA[r], qualityB[r]}],
PlotRange -> {{-1, 1}, {-1, 1}}
], {{r, .5}, 0, 1}]
How can I evaluate qualityA and qualityB consecutively, and append their output to the display when it is ready?
Bonus points for Abort'ing the evaluation of unneeded results, and for allowing a part of the result to be calculated multiple times, so that after releasing the control I would see e.g. {qualityA[r]} then {qualityA[r],qualityB[r]}, and finally {qualityA2[r],qualityB[r]}.
My colleague Lou, an expert on Dynamic, suggested this neat answer:
Manipulate[
ControlActive[
Graphics[{LightRed, Circle[{0, 0}, r]},
PlotRange -> {{-1, 1}, {-1, 1}}],
DynamicModule[{exprs = {Red, Circle[{0, 0}, r]}, rr = r},
Graphics[Dynamic[exprs], PlotRange -> {{-1, 1}, {-1, 1}}],
Initialization :> (Pause[1];
AppendTo[exprs, {Red, Disk[{0, 0}, rr]}]; Pause[1];
AppendTo[exprs, {Black, Circle[{0, 0}, rr]}]),
SynchronousInitialization -> False]], {{r, 0.5}, 0, 1}]
How it works:
When not ControlActive, the result of the dynamic expression is a DynamicModule. The code for refining the graphics is contained in the Initialization option of this DynamicModule. The SynchronousInitialization -> False makes this initialization run asynchronously.
Renaming rr = r in the DynamicModule serves two purposes. First, it makes the result always depend on the Manipulate variable r. Second, you can check rr != r to decide whether the user has moved the slider during initialization, and abort early, saving computation time:
Manipulate[
ControlActive[
Graphics[{LightRed, Circle[{0, 0}, r]},
PlotRange -> {{-1, 1}, {-1, 1}}],
DynamicModule[{exprs = {Red, Circle[{0, 0}, r]}, rr = r},
Graphics[Dynamic[exprs], PlotRange -> {{-1, 1}, {-1, 1}}],
Initialization :> (If[rr =!= r, Abort[]]; Pause[1];
AppendTo[exprs, {Red, Disk[{0, 0}, rr]}]; If[rr =!= r, Abort[]];
Pause[1]; AppendTo[exprs, {Black, Circle[{0, 0}, rr]}]),
SynchronousInitialization -> False]], {{r, 0.5}, 0, 1}]
I hope this helps.
Really good question.
I may be overlooking a simpler way. There often is one when it comes to Dynamic... But here is my suggestion:
DynamicModule[{quality = 0, exprs = {}},
Manipulate[
Show[
ControlActive[
exprs = {}; quality = 0; Graphics#{Red, Circle[{0, 0}, r]},
Switch[quality,
0, Pause[1]; quality = 1;
AppendTo[exprs, Graphics#{Red, Disk[{0, 0}, r]}],
1, Pause[1]; quality = 2;
AppendTo[exprs, Graphics#{Black, Circle[{0, 0}, r]}],
_, r];
exprs
],
PlotRange -> {{-1, 1}, {-1, 1}}],
{{r, .5}, 0, 1}
]
]
First we define some variables controlling increasingly high quality graphics: quality (ranging to 0 to the maximum quality, 2 in this case), and exprs (a list of expressions to Show, just as in your example).
Now note what happens in the two cases of ControlActive:
When ControlActive, the result is the same as yours, except we take the opportunity to reset quality and exprs relating to the "high quality" graphics.
When not ControlActive, the Dynamic expression evaluates to
code; exprs
This expression has the following key properties.
It returns the list exprs every time.
Each time code is evaluated, it improves the graphics by appending something to exprs.
Each time code is evaluated, at least one of the variables lexically contained in code; exprs (such as quality) is changed. This means Dynamic will go ahead and evaluate our dynamic expression again, and again, and again, until ...
Eventually code evaluates without any of the variables lexically contained in code; exprs changing. This means Dynamic will stop re-evaluating.
The final evaluation lexically contains r. (Via the otherwise useless default case in the Switch, _, r.) This is important to make the slider still trigger updates.
Give it a try and let me know if that works for you.
Edit: What $Version of Mathematica are you using? I see some version dependence in the behavior of my code above.
Edit 2: I asked an expert on Dynamic and he found a better way, which I will describe in a separate answer.

Resources