Evaluating x^0
- To: mathgroup at smc.vnet.net
- Subject: [mg63553] Evaluating x^0
- From: ted.ersek at tqci.net
- Date: Thu, 5 Jan 2006 03:12:20 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
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). In[17]:= MatchQ[ power0[2+x,0], _Power] Out[17]= False So if you use this last approach you might use a definition such as foo[ Power[x_,y_]|power0[x_,y_] ]:= blah instead of this simpler definition foo[Power[x_,y_]]:= blah So we can deal with the only problem I know of, but it's a bit messy. ---------------------- Any of my definitions above could be modified to deal with cases such as x^0.0, x^(0.0+0.0*I) ----------------------- Any comments? Ted Ersek
- Follow-Ups:
- Re: Evaluating x^0
- From: Andrzej Kozlowski <akoz@mimuw.edu.pl>
- Re: Evaluating x^0