Re: Locator question
- To: mathgroup at smc.vnet.net
- Subject: [mg79450] Re: Locator question
- From: Murray Eisenberg <murray at math.umass.edu>
- Date: Fri, 27 Jul 2007 05:47:05 -0400 (EDT)
- Organization: Mathematics & Statistics, Univ. of Mass./Amherst
- References: <f86r55$pc7$1@smc.vnet.net> <200707250930.FAA26363@smc.vnet.net> <f89pve$5jn$1@smc.vnet.net> <46A8751F.8000907@gmail.com>
- Reply-to: murray at math.umass.edu
Responses in-line below and at bottom.... Jean-Marc Gulliet wrote: > Murray Eisenberg wrote: >> I'd like to define a function that would do that, taking as argument >> the function name, the interval on which to plot, and an optional >> point at which to place the locator. >> >> Here are two attempts, each with unpleasant consequences... >> >> Attempt 1: >> >> trackPointOnPlot[f_, {a_, b_}, start_: {a, f[a]}] := >> DynamicModule[{p = start}, >> Column[{LocatorPane[Dynamic[p, (p = {First@#, f[First@#]}) &], >> Plot[Sin[x], {x, a, b}]], Dynamic[p]}]] >> >> With a third argument supplied, this works OK, e.g.: >> >> trackPointOnPlot[Sin, {0, 10}, {Pi, Sin[Pi]}] >> >> However, if I evaluate, say, >> >> trackPointOnPlot[Sin,{0,10}] >> >> then the locator appears initially to the left of the x-axis. So I ask >> whether that syntax on the left for trackPointOnPlot is legitimate? >> That is, can the default value for variable 'start' refer legitimately >> to the value of a preceding argument? > [snip] > > Hi Murray, > > The short answer to your question is 'No'. You cannot refer to > 'previous' patterns in your function declaration. The same restriction > applies to initializations within *Module*, *With*, or *Block* > constructs. For instance, one cannot expect correct result from, say, > Module[{a = 1, b = 2, c = a + b}, somecode]. (Note that if a and/or b > have also some global values, this is these values that are going to be > taken at this stage for computing c.) A valid expression would be > Module{{a = 1, b = 2, c}, c = a + b; somecode], expression that > guarantee that c is equal to 3. (There is a discussion about that in the > Mathematica Book, Section 2.7 "Modularity and the Naming of Things".) I was aware of the prohibition in the initializations in Module, etc., but I did not recall having seen that mentioned with respect to the arguments on the left side of a SetDelayed function definition. Can you point me to where in the Mathematica 6.0 documentation this prohibition for function definitions is stated. (I can find it for Module, etc.) Actually, reference within the initializations of a Module is somewhat muddier than first appears. Indeed, there's nothing wrong with: Module[{a = 1, b = 2, c = a + b}, c] a+b (It's just that the assignment c = a + b refers to global a and global b but, since there are no global values assigned to them, the values of a and b are themselves.) The docs for 6.0 give an even more direct example: Module[{t = 6, u = t}, u^2] Now back to the topic at hand... > > Back to your function, the following modified version will work as you > wanted (I hope!). > > trackPointOnPlot[f_, {a_, b_}, start_: {0, 0}] := > DynamicModule[{p = If[start == {0, 0}, {a, f[a]}, start]}, > Column[{LocatorPane[Dynamic[p, (p = {First@#, f[First@#]}) &], > Plot[f[x], {x, a, b}]], Dynamic[p]}]] > > trackPointOnPlot[Cos, {0, 10}, {Pi, Cos[Pi]}] > trackPointOnPlot[Sin, {0, 10}, {Pi, Sin[Pi]}] > trackPointOnPlot[Cos, {0, 10}] > trackPointOnPlot[Sin, {0, 10}] I don't think that does quite what I expect: E.g., if you evaluate trackPointOnPlot[Cos, {Pi, 10}, {0, 0}] then the locator starts NOT at the explicitly specified point {0,0}, but instead at {Pi,f[Pi]}. And this is not an artifact of the plot interval, since if in the Plot expression I insert AxesOrigin->{0,0}, still the locator will be at {Pi,f[Pi]} instead of -- Murray Eisenberg murray at math.umass.edu Mathematics & Statistics Dept. Lederle Graduate Research Tower phone 413 549-1020 (H) University of Massachusetts 413 545-2859 (W) 710 North Pleasant Street fax 413 545-1801 Amherst, MA 01003-9305
- References:
- Re: Locator question
- From: Albert <awnl@arcor.net>
- Re: Locator question