RE: Confusing Behavior of LogPlot vs. Plot
- To: mathgroup at smc.vnet.net
- Subject: [mg24852] RE: [mg24838] Confusing Behavior of LogPlot vs. Plot
- From: Wolf Hartmut <hwolf at debis.com>
- Date: Wed, 16 Aug 2000 03:24:14 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
> -----Original Message----- > From: AES [SMTP:siegman at stanford.edu] To: mathgroup at smc.vnet.net > Sent: Tuesday, August 15, 2000 9:04 AM > To: mathgroup at smc.vnet.net > Subject: [mg24838] Confusing Behavior of LogPlot vs. Plot > > If I run the following input cells with the "Table" cell commented out > as shown, I get four valid and correct plots. > > Remove["Global`*"] > > Needs["Graphics`"] > > f[a_, x_] := Exp[-a x^2] > > a = 1; Plot[f[a, x], {x, -1, 1}] > > a = 1; LogPlot[f[a, x], {x, -1, 1}]; > > (* x = 3.; Table[{a, f[x, a]}, {a, 0, 2}] // TableForm *) > > a = 4; Plot[f[a, x], {x, -1, 1}] > > a = 4; LogPlot[f[a, x], {x, -1, 1}] > > If, however, I remove the (* and *) and run the notebook including the > "Table" input cell, both "Plot" plots are still fine, still give correct > results, but the second "LogPlot" cell doesn't give me a plot at all, it > gives me the list of options for something called "ScaledPlot". > > (With various other test circumstances, the repeated "Plot" cells always > continue to function OK, but the repeated "LogPlot" cell gives other > weird results, e.g, a totally incorrect plot.) > > If I've somehow goofed on this, I apologize -- but if this is how > LogPlot really works, I'd say that's inexcusably bad interface design. > If LogPlot looks, smells, and feels like Plot, and does essentially the > same job as Plot, it should *function* just like Plot. > > [Hartmut Wolf] Dear AE, as you will have seen, "after removal of the (* and *)", the global symbol x has got the value 3. and since LogPlot (as well as LinearLogPlot and ScaledPlot, which are called from LogPlot) only have the HoldFirst attribute, the call of LogPlot[f[a,x],{x,-1,1}] is equivalent to the call LogPlot[f[a,x],{3.,-1,1}], which of course doesn't work. The miracle indeed is that Plot[f[a,x],{x,-1,1}] _does_ work. Ok, it has the HoldAll attribute, but that won't suffice for LogPlot, LinearLogPlot and ScaledPlot; the behaviour is still wrong, however in a different way. The right way to treat the problem of course is, to clear the global symbol x before using it as the range variable. However you might ask how to fix the problem such that LogPlot gets the same behaviour as Plot. The idea to just overload the symbol ScaledPlot (and to refer to the symbol from the package Graphics`Graphics` as Graphics`Graphics`ScaledPlot) doesn't work, since if the new definition is made _after_ loading the package both names designate the same symbol, if however the new definition is made _before_ loading the package (and shadowing occurs) then LinearLogPlot will reference the package symbol, not the global one. What did succeed was, after loading the package to introduce a symbol OldScaledPlot and transfer the DownValues of ScaledPlot to that, then Clear ScaledPlot and redefine it: In[1]:= f[a_, x_] := Exp[-a x^2] In[2]:= x = 3.; Table[{a, f[x, a]}, {a, 0, 2}] // TableForm In[3]:= a = 4; Plot[f[a, x], {x, -1, 1}] (this works anyways) In[4]:= Needs["Graphics`Graphics`"] In[5]:= SetAttributes[{LogPlot, LinearLogPlot, ScaledPlot}, HoldAll] In[9]:= Attributes[OldScaledPlot] = {HoldAll} In[10]:= DownValues[OldScaledPlot] = DownValues[ScaledPlot] /. ScaledPlot -> OldScaledPlot In[11]:= Clear[ScaledPlot] now redefine ScaledPlot (protect the range variable with Block) In[14]:= ScaledPlot[f_, {x_Symbol, x0_, x1_}, opts___] := Block[{x}, OldScaledPlot[f, {x, x0, x1}, opts]] In[15]:= a = 4; LogPlot[f[a, x], {x, -1, 1}] This now works! -- Hartmut