Dynamic function application problem
- To: mathgroup at smc.vnet.net
- Subject: [mg108041] Dynamic function application problem
- From: István <replicatorzed at gmail.com>
- Date: Sun, 7 Mar 2010 04:03:06 -0500 (EST)
Dear All, every time I construct a simple interface for something like the common demonstration "Understanding function x" to get a grip on the behaviour of the function, I run into some unresolvable problem of Dynamic. I mean: EVERY time. This is the newest one. The interface uses a master slider that controls the length (n) of a list (set). Each element in this list has an independent slider to control its value. What I want to do is to allow the values in set to be saved in some way. That is, if the user increases n from default 2 to e.g. 4, and then changes the value of set[[2]], and then decides to go back to n = 2, the set should keep the changed value. That's why I use the 'truncate' function. To conclude: time1: n = 2, set = {1, 2} time2: n = 4, set = {1, 2, 3, 4} (user increased n) time3: n = 4, set = {1, 7, 3, 4} (user changed set[[2]]) time4: n = 2, set = {1, 7} (user decreased n) And the code is below. Be aware that this is a TOY modell with the sole purpose of demonstraing a problem I don't understand, please don't ask questions like "your function 'update' is an unnecessary intermediate step, why do you use it??". DynamicModule[{ n = 2, m = 8, def, set, update, truncate }, Panel@Column@{ Slider[Dynamic[n, (n = #; update[#]) &], {1, m, 1}, ImageSize -> 110], Dynamic@ Column[Slider[Dynamic@set[[#]], {1, 30, 1}, ImageSize -> 110] & / @ Range@n], Dynamic@Column@{n, set} }, Initialization :> ( truncate[set_, n_, def_] := If[n <= Length@set, Take[set, n], Join[set, Take[def, {n + 1, Length@def}]]]; update[n_] := ( set = truncate[set, n, def]; ); def = Range@m; update@n; ) ] At present, the code simply cannot deal with truncate, and crashes (with fresh kernel), or at least produces severe errors due to recursion problems (with old kernel). Could you please suggest a resolution for this matter? Thank you in advance Istvan