4

The code sample:

Manipulate[
e = RandomVariate[NormalDistribution[0, sigma], n];
{a, e},
{{n, 3}, 1, 5, 1, Appearance -> "Labeled"},
{{sigma, 1}, 1, 2, Appearance -> "Labeled"},
{{a, 0}, 0, 10, 1, Appearance -> "Labeled"}
]

The problem: how to make a to change without updating e given n

and sigma unchanged.

Difference with suggested solutions: random vars depend on other controls (i.e. n and sigma) of Manipulate body.

garej
  • 4,865
  • 2
  • 19
  • 42

1 Answers1

6

Body of Manipulate is wrapped by Dynamic and Dynamic doesn't know what's inside inner Dynamics, that's how we can screen a variable to not prompt the very outer Dynamic to evaluate:

Manipulate[
 e = RandomVariate[NormalDistribution[0, sigma], n];
 {Dynamic@a, e}, 
 {{n, 3}, 1, 5, 1,   Appearance -> "Labeled"}, 
 {{sigma, 1}, 1, 2,  Appearance -> "Labeled"}, 
 {{a, 0}, 0, 10, 1,  Appearance -> "Labeled"}
]
Kuba
  • 136,707
  • 13
  • 279
  • 740
  • Thank you for the answer. Just as a follow up, what if I want to use a and e in the same expression, say, a + e. How Dynamic wrapper can be used in this case? – garej Jul 29 '15 at 07:56
  • 1
    @garej for full controll over evaluation I'd go with DynamicModule and manually written solution. But in this qase when you put Dynamic[a + e] it still works right, doesn't it? – Kuba Jul 29 '15 at 08:03
  • right! Now I clearily see the point. Thank's for a tip. – garej Jul 29 '15 at 08:08
  • @garej great :) good luck. – Kuba Jul 29 '15 at 08:12
  • @Kuba A quick question. Why doesn't wrapping e in Refresh work here? Refresh[e = RandomVariate[NormalDistribution[0, sigma], n], TrackedSymbols :> {sigma, n}]; I thought for sure this was an alternative but it does not work. – Edmund Jul 29 '15 at 10:10
  • @Edmund The problem is there you have the body which is more or less: Dynamic[Refresh[...]; {a, e}] so when you change only a it will prompt the whole content of Dynamic to be recreated. – Kuba Jul 29 '15 at 10:16
  • @Edmund here's an example with Refresh that may be considered useful: Manipulate[ Refresh[e = amp RandomVariate[ NormalDistribution[0, sigma], n], TrackedSymbols :> {sigma, n}]; {Dynamic@a, e}, {{n, 3}, 1, 5, 1, Appearance -> "Labeled"}, {{sigma, 1}, 1, 2, Appearance -> "Labeled"}, {{a, 0}, 0, 10, 1, Appearance -> "Labeled"}, {amp, 0, 1}] – Kuba Jul 29 '15 at 10:20
  • @Kuba I don't get why your comment example with the addition of amp in the Refresh works and mine does not. I also see that your comment example will work without the Dynamic on a. Also, my Refresh will work with a Dynamic on a ; but that makes the Refresh redundant since your question answer has it working with Dynamic but without Refresh. – Edmund Jul 29 '15 at 10:31
  • @Edmund the difference is that amp isn't outside Refresh when a is. – Kuba Jul 29 '15 at 10:33
  • @Kuba I have been thinking of Refresh as a gatekeeper to the code it contains. I my mind e should never ever update unless the TrackedSymbols condition has been met. That is how I have understood the concept of Refresh. However, this apparently is not the case as this rather simple (or so I thought) example has shown. Somewhere I've missed this interaction and still don't appreciate why this is happening. It seems counter-intuitive from the purpose of Refresh. – Edmund Jul 29 '15 at 10:39
  • @Edmund Documentation about Refresh sucks. Or it's me. In addition, there are multiple cases when Dynamic is not as smart as you'd expect, sometimes it is smarter. The net result is, it's lottery, I hate it :P, I'm not using it almost at all: related topic. – Kuba Jul 29 '15 at 10:48
  • @Edmund p.s. but this case is quite clear. Also change amp range to 1-2 not 0-1 when it's 0 it may trick you that Dynamic[a] doesn't matter while it really does. – Kuba Jul 29 '15 at 10:52