Re: Evaluating x^0
- To: mathgroup at smc.vnet.net
- Subject: [mg63589] Re: [mg63553] Evaluating x^0
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Fri, 6 Jan 2006 05:24:33 -0500 (EST)
- References: <200601050812.DAA19824@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 5 Jan 2006, at 17:12, ted.ersek at tqci.net wrote: > Many users loathe the idea of to modifying built-in functions. > Especially > functions such as Set, Plus that are frequently used. At the very > least > we make calculations run slower when we make such changes, and I agree > that one must be careful about unintended consequences. Indeed we have > found that clearing attributes that normally come with built-in > functions > often leads to trouble. > > However, I enjoy finding ways to change built-in functions that > some would > view as an improvement, and see how well my changes work. > -------------------------------------------- > > Consider evaluating Clear[x];x^0 > Mathematica returns the Integer 1. > In doing so Mathematica is assuming (x != 0) since 0^0 is > Indeterminate. > > > I wrote to Wolfram Technical Support about this, and their reply > included: > >> >> This is a typical auto-simplification issue. Which simplifications >> should be made automatically and which ones shouldn't can often be >> subjective. Generally, the convention Mathematica takes is this. >> Don't make an assumption if it is invalid for a class of values. But >> it is ok to make an assumption if it is only violated by a small set >> of specific values. >> >> So Sqrt[x^2] does not simplify to Abs[x] (since this is not true for >> complex numbers). But x/x does simplify to 1 (even though this is not >> true for 0 or Indeterminate). >> > > That sounds reasonable. However, sometimes a user might want (x^0) > to evaluate > to itself, but it can't be done. It might be better if the rules for > simplifications that are invalid some of the time could be removed/ > disabled > by the user (e.g. stored in DownValues[Power]). I wonder if > calculations would > run a lot slower if we could do that? > > --------------------- > There are things we can do to make (x^0) evaluate more carefully. > One approach > is to make Power post a message warning you about the assumption > being made. > You can always turn off the message if you want to. Here is some > code to make > Power do this. > > > In[1]:= > Unprotect[Power]; > Clear[Power]; > > Power::assum="The expression Power[`1`,`2`] is evaluating to 1 > which is > only true when (`1`) is not zero."; > > Power[x_?(!NumericQ[#]&), 0]/;Message[Power::assum,x,0]:= "Never > get here." > > > ----------------------------- > Another approach is to make (x^0) evaluate to an If[...] > expression using > the code that follows. This however isn't pretty, because it makes > (x^0) > evaluate to something more complicated than (x^0). > > > In[5]:= > Unprotect[Power]; > Clear[Power]; > Power[x_,0]:= If[x!=0,1,Indeterminate] > > > ----------------------------- > Still another approach is to make (x^0) evaluate to some other > expression. > We then ensure the output is displayed as if (x^0) evaluated to > itself. > The code that follows does this. > > > In[8]:= > Unprotect[Power]; > Clear[Power]; > > Power[x_?(!NumericQ[#]&),0]:=power0[x,0]; > power0[x_?NumericQ,y_]:=Power[x,y]; > > MakeBoxes[power0[x_Plus,y_], form:(StandardForm|TraditionalForm)]:= > SuperscriptBox[RowBox[{"(",MakeBoxes[x,form],")"}],MakeBoxes > [y,form]]; > > MakeBoxes[power0[x_,y_], form:(StandardForm|TraditionalForm)]:= > SuperscriptBox[MakeBoxes[x,form],MakeBoxes[y,form]]; > > > ---------------------------- > Normally the next output would be the Integer 1, > but not with my definitions above. > > In[14]:= > Clear[y]; > tst=(2+y)^0 > > > Out[15]= > (2 + y)^0 (* Displayed as a SuperscriptBox in the front-end. *) > > > In the output above it looks like (2+y)^0 evaluated to itself, > but the True InputForm is given below. > > > In[16]:= > InputForm[tst]; > > Out[16]= > power0[2+y,0] > > > ----------------------------- > As far as I can tell, the only "problem" with my last approach is > that an > expression with the head (power0) doesn't match the pattern (_Power). Well, I don't know... how about this: x^n/y^n - x^n/x^m /. {x -> y, n -> m} 1 - y^0 I think the answer 0 is better in such cases. Andrzej Kozlowski
- References:
- Evaluating x^0
- From: ted.ersek@tqci.net
- Evaluating x^0