Re: Re: Confusing results with N[expr]?

*To*: mathgroup at smc.vnet.net*Subject*: [mg62335] Re: [mg62328] Re: [mg62318] Confusing results with N[expr]?*From*: Andrzej Kozlowski <akoz at mimuw.edu.pl>*Date*: Mon, 21 Nov 2005 03:54:17 -0500 (EST)*References*: <200511200419.XAA28539@smc.vnet.net> <200511200950.EAA04490@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

On 20 Nov 2005, at 18:50, Andrzej Kozlowski wrote: > On 20 Nov 2005, at 13:19, Thomas Bohl wrote: > >> I have the impression that since some time (since Vers. 5.0?) N >> [expr] does >> not give anymore the results I expect. >> >> The following code should illustrate this: >> >> N[a] = 2.; >> >> k = z*(a*Pi); >> N[k] >>> 6.28319 z (* "a" and "Pi" are replaced by their numerical >>> value as >>> expected *) >> >> k = z^(a*Pi) >> N[k] >>> z^(3.14159 a) (* "Pi" is replaced by its numerical value but >>> not "a", >>> why not? *) >> >> >> There are more examples of this behaviour: If the expression is Log >> [z*a*Pi], >> the numerical values of "a" and "Pi" are evaluated, if the >> expression is >> Exp[z*a*Pi] the numerical value of "a" is not evaluated. Why not? >> >> The motivation behind my question is that I was used to write >> expressions >> symbolically and assign numerical values with N[expr] = num. value. >> This way >> you could keep those expressions symbolically and just apply //N >> when you >> needed numerical values. Now it seems that I have lost this >> possibility. >> >> Could you please comment my observation and maybe suggest a way out? >> >> Thank you very much for any idea. >> >> Kind regards, >> Thomas. >> >> > > > I don't remember if it used to be different before version 5, but it > looks to me like a bug that affects the second argument in Power. For > a general function f everything works as expected. First, let's > define an NValue for the symbol a, just as you have been doing: > > > N[a] = 2.; > > Now, let's assign to a general function f the attribute > NumericFunction: > > In[2]:= > SetAttributes[f, NumericFunction] > > In[3]:= > N[f[a, b]] > > Out[3]= > f[2., b] > > In[4]:= > N[f[b, a]] > > Out[4]= > f[b, 2.] > > So we see no problem. However, although Power does have the > attribute NumericFunction: > > In[5]:= > Attributes[Power] > > Out[5]= > {Listable,NumericFunction,OneIdentity,Protected,ReadProtected} > > if we set f above to Power things will no longer work properly with > the second argument of Power: > > In[6]:= > f = Power; > In[7]:= > N[f[a, b]] > > Out[7]= > 2.^b > > but > > In[8]:= > N[f[b, a]] > > Out[8]= > b^a > > So it looks like a bug to me. Curiously, if you set > > NumericQ[a] = True; > > you own example will now work as expected: > > > N[z^(a*Pi)] > > z^6.283185307179586 > > but if you omit Pi it again does not work: > > > N[z^a] > > z^a > > There is a very simple workaround for the problem; instead of just > using N use MapAll[N, ]: > > MapAll[N,z^a] > > z^2. > > If your expressions are very complicated there may be a noticeable > loss of performance, but in simple cases like the above everything is > instantaneous. > > Andrzej Kozlowski > > I think I see a few things that were wrong with my post. First, a minor matter: I mistakenly assumed that the behaviour N[a] = 2.; N[f[a]] f[2.] would only occur if f had the Attribute NumericFunction. This is of course not the case. But furthermore, I failed to not the following special feature of Power: N[x^2] x^2 Note that we get a 2 in the exponent and not 2. There is an obvious reason for this: if this was not the case applying N to a polynomial would produce a something that was no longer a polynomial! Not that however N[x^Pi] x^3.141592653589793 as one would wish. So now I am inclined to believe that the behaviour you are seeing is perhaps related to the fact that N is not applied to the second argument of Power when that argument is an integer. Still, it looks like a bug since when you evaluate N[a] = 2; N[x^a] It is clear that you are not trying to obtain a numerical polynomial. Andrzej Kozlowski

**References**:**Confusing results with N[expr]?***From:*"Thomas Bohl" <Thomas.Bohl@cern.ch>

**Re: Confusing results with N[expr]?***From:*Andrzej Kozlowski <akoz@mimuw.edu.pl>