Re: Confusing results with N[expr]?

*To*: mathgroup at smc.vnet.net*Subject*: [mg62419] Re: Confusing results with N[expr]?*From*: Maxim <m.r at inbox.ru>*Date*: Wed, 23 Nov 2005 06:27:30 -0500 (EST)*References*: <dlp320$1bs$1@smc.vnet.net> <dluppj$ndk$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

On Tue, 22 Nov 2005 09:52:51 +0000 (UTC), Peter Pein <petsie at dordos.net> wrote: > Thomas Bohl schrieb: >> 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. >> >> > Hi, > > I think, I've got a workaround, which does not need changes in every > occurance of N[f[a]]: > > In[1]:= a /: N[a, pa_] := N[2, pa] > In[2]:= N [{x^a, x^(Pi*a),E^(x + a*Pi)},3] > Out[2]= {x^2.00, x^6.28, 2.72^(6.28 + x)} > > In[3]:= N[x^a, 32] > Out[3]= x^2.`32. > > In[4]:= (#1[Last[%]] & ) /@ {Accuracy, Precision} > Out[4]= {31.69897000433602, 32.} > > In[5]:= Solve[N[x^a == 1], x] > Out[5]= {{x -> -1.}, {x -> 1.}} > > Most likely some of you finds issues, which have been overlooked by me > :-\ > > Peter > Your example indicates that there may be an inconsistency after all, because N[2^a] actually tries to evaluate N[a] but it uses non-default settings for N: In[1]:= N[a, {MachinePrecision, Infinity}] := (Print["default case"]; 2.) N[a, {Infinity, MachinePrecision}] := (Print["power case"]; 2.) In[3]:= N[a^2] From In[3]:= "default case" Out[3]= 4. In[4]:= N[2^a] From In[4]:= "power case" Out[4]= 4. For some reason setting NumericQ[a] = True makes N[2^a] first try the "power case" and then, if a was left unchanged, revert to the "default case". It is interesting to trace why Solve and Plot didn't work as expected with redefined Power. All the computations had to be performed with the undefined function Pow$ instead of Power, and it's not clear how Solve handles numeric arguments then: In[5]:= Solve[Pow$[x, 2.] == 1, x] Out[5]= {{x -> InverseFunction[Pow$, 1, 2][1, 2.]}} In[6]:= Solve[Pow$[x, 2.] == 1., x] Out[6]= {} Plot[1, {x, 0, 1}] tried to evaluate N[1/GoldenRatio], which became Pow$[N[GoldenRatio], N[-1]], yielding a non-numeric result. Plot[1, {x, 0, 1}, AspectRatio -> 1] would work. Maxim Rytin m.r at inbox.ru