Re: DynamicModule, Speed, and locally static variables.
- To: mathgroup at smc.vnet.net
- Subject: [mg82052] Re: [mg82003] DynamicModule, Speed, and locally static variables.
- From: John Fultz <jfultz at wolfram.com>
- Date: Wed, 10 Oct 2007 04:32:43 -0400 (EDT)
- Reply-to: jfultz at wolfram.com
I think that, for your purposes, it would be really good to understand how nested Dynamics work. Make sure to read the following tutorial sections... In tutorial/AdvancedDynamicFunctionality see the "Nesting Dynamic" section. In tutorial/AdvancedManipulateFunctionality see the "Using Dynamic inside Manipulate" section. The choice between DynamicModule and Module scoping has very little effect on performance in an example like yours, but the choice of how to encapsulate Dynamic[] computations can have a large effect. The important thing to understand is that when you have an expression wrapped in Dynamic (and, keep in mind, that Manipulate always wraps its first argument in Dynamic), it sets up a dependency on every single variable in the Dynamic. The change of any variable will cause the entire Dynamic to re-evaluate. However, a change in a Dynamic will not cause parent Dynamics to automatically fire. So, for example, Dynamic[{a, Dynamic[b]}] The outer Dynamic will be triggered by changes in a, and will re-evaluate completely, including recreating and reevaluating Dynamic[b]. However, if only b changes, then the outer Dynamic will not trigger, and so the kernel potentially does much less work. An additional technique to consider is to try to cache as much work as possible outside of the Dynamic. With[] is often a good tool for plopping computed results inside of Dynamic. For example... ExpensiveFunc[x_]:=(Pause[5];x+1) CheapFunc[x_]:=x^2 (* Time-wasting example because ExpensiveFunc is called every time the Dynamic updates, but note that ExpensiveFunc could be precomputed *) Dynamic[CheapFunc[x]+ExpensiveFunc[1]] (* Less time-wasting example, ExpensiveFund is computed only at the time of Shift-Return *) With[{result=ExpensiveFunc[1]}, Dynamic[CheapFunc[x]+result]] Sincerely, John Fultz jfultz at wolfram.com User Interface Group Wolfram Research, Inc. On Tue, 9 Oct 2007 05:36:51 -0400 (EDT), W. Craig Carter wrote: > Dear Mathematica Group, > > Sorry for the long question on combining Module, > DynamicModule, LocatorPane, Manipulate to produce a > GraphicsRow. > > I am in the middle of designing small program that uses > Locator to find the position of a dynamic point and > Manipulate to control the shape of the locator, and then > compute an expensive result based on the position (from LocatorPlane) > and the size (from Manipulate). > > Some calculations are expensive and don't depend on the > locator position or shape. Other calculations are > expensive and do depend on the locator's position and size. > > I am wondering how to scope this. Do I pay a penalty for > local variables in DynamicModule that won't change? > > Here is what I am trying, I've made up an artificial > example below that will (hopefully) show what I am trying to do. > It is not working; I am not sure why, but guessing that it > has to do with variable scope. > > I am hoping for two kinds of help: > 1) that someone can comment about computational efficiency and > dynamic variable scope. > 2) that someone could point to a demo which does something similar. > 3) I believe that I should be using ControlActive to > separate the time scales from locator movement and graphics > production. I can't quite see how that should appear here, > yet. > > Does it make sense to have the following > Module[DynamicModule[Manipulate[Locator]]] scoping? > ----example----- > demo[f_] := > Module[{StaticExpression=ExpensiveFunction[f], > StaticImage3D,StaticContourPlot, > PositionInit={1,2}, LocatorSizeInit=1}, (*vars that don't change*) > StaticImage3D=Plot3D[StaticExpression]; > StaticContourPlot=ContourPlot[StaticExpression] > GraphicsRow[ (*draw three graphics, 1-fixed, 2-manipulate, 3-result of > dynamic variable and manipulate*) > Flatten[ > { > StaticImage, (*first-image*) >= DynamicModule[newdata,{position=PositionInit,LocatorShape=Graphics[g[Positi= o > nInit,LocatorSizeInit]]}, > {Manipulate[ (*manipulate returns a graphics object?*) > LocatorShape=Graphics[position,size]; (*dynamic*) > newdata=ExpensiveCalculation[position,size,StaticExpression]; > LocatorPlane[Dynamic[position],StaticContourPlot,Appearance- > >LocatorShape],(*draw dynamic locator on static image*) > {{size,LocatorSizeInit},0,1}], > ArrayPlot[ExpensivistCalc[position, size, StaticExpression]] (*the final > graphics object and most expensive*) > } > } > ] (*dynamic module*) > ] (*flatten*) > ] (*graphicsrow*) > ] (*module*) > > > W. Craig Carter