Home > Archive > Mathematica > November 2005 > Confusing results with N[expr]?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Confusing results with N[expr]?
|
|
| Thomas Bohl 2005-11-20, 3:59 am |
| 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.
| |
| Steven HANCOCK 2005-11-22, 4:01 am |
| There may be a clue in the following, equally distressing nonsense...
In[1]:=
$Version
Out[1]=
5.1 for Microsoft Windows (October 25, 2004)
(* Map[f, expr] or f /@ expr applies f to each element on the first
level in expr. *)
In[2]:=
Map[f, a^b]
Out[2]=
f[b]
f[a]
In[3]:=
f /@ a^b
Out[3]=
b
a
(* MapAll[f, expr] or f //@ expr applies f to every subexpression in
expr. *)
In[4]:=
MapAll[f, a^b]
Out[4]=
f[b]
f[f[a] ]
In[5]:=
f //@ a^b
Out[5]=
b
f[a]
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]
>
>
>
> k = z^(a*Pi)
> N[k]
>
>
>
>
> 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.
>
>
| |
| Peter Pein 2005-11-22, 4:01 am |
| Peter Pein schrieb:
> Thomas Bohl schrieb:
> Hi Thomas,
>
> that's really strange. The reason seems to be Power[]:
>
> N /@ k
>
> but when I tried
>
> Unprotect[Power];
> Power /: N[p_Power] := N /@ p;
> Protect[Power];
>
> the kernel died at the evaluation of N[k].
>
> The best I have to offer is in the moment:
>
> 1.) use "N //@ expr" to evaluate a in Exp[Exp[a Pi]]
> or
> 2.)
> Unprotect[Power];
> Power /: N[(x_)^(y_)] := Pow$[N[x], N[y]];
> Protect[Power];
> $Post = #1 /. Pow$ -> Power & ;
>
> Regards,
> Peter
>
Andrzeij pointed out that Solve[N[x2 - 1 == 0], x] and even
Plot[1,{x,0,1}] do not work any more as expected when using "2.)" above.
Well it seems, I tried to cure a mosquito bite by amputation...
I am sorry,
Peter
| |
| Peter Pein 2005-11-22, 4:01 am |
| 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]
>
> k = z^(a*Pi)
> N[k]
>
>
> 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
| |
| tt@tt.com 2005-11-23, 3:59 am |
| In the following code, just put parenthesis around a^b and you get the
good result.
f/@ a^b is the same as Map[f,a]^b (and Map[f,a]=a)
f/@(a^b) is the same as Map[f,a^b]
/@ has precedence over ^.
GL
On Tue, 22 Nov 2005 09:44:25 +0000 (UTC), Steven HANCOCK
<Steven.Hancock@cern.ch> wrote:
>There may be a clue in the following, equally distressing nonsense...
>
>In[1]:=
>$Version
>
>Out[1]=
>5.1 for Microsoft Windows (October 25, 2004)
>
>(* Map[f, expr] or f /@ expr applies f to each element on the first
>level in expr. *)
>
>In[2]:=
>Map[f, a^b]
>
>Out[2]=
> f[b]
>f[a]
>
>In[3]:=
>f /@ a^b
>
>Out[3]=
> b
>a
>
| |
| Bill Rowe 2005-11-23, 3:59 am |
| On 11/22/05 at 4:41 AM, Steven.Hancock@cern.ch (Steven HANCOCK)
wrote:
>There may be a clue in the following, equally distressing
>nonsense...
>In[1]:= $Version
>Out[1]= 5.1 for Microsoft Windows (October 25, 2004)
>(* Map[f, expr] or f /@ expr applies f to each element on the first
>level in expr. *)
>In[2]:= Map[f, a^b]
>Out[2]=
> f[b]
>f[a]
>In[3]:= f /@ a^b
>Out[3]=
> b
>a
>(* MapAll[f, expr] or f //@ expr applies f to every subexpression
>in expr. *)
>In[4]:= MapAll[f, a^b]
>Out[4]=
> f[b]
>f[f[a] ]
>In[5]:= f //@ a^b
>Out[5]=
> b
>f[a]
The issues above with Map and MapAll is simply an operator precedence issue. The expression f/@a^b can be taken as either f/@(a^b) or (f/@a)^b. Likewise, the expression f//@a^b can be taken as (f//@a)^b or f//@(a^b). In both cases, Mathematica applies the
operators in the order in which they appear.
But the precendence with either Map[f, a^b] or MapAll[f, a^b] is different. This grouping makes it clear Power is to be done first.
The following shows more clearly this is what is occurring
In[1]:=Hold[f/@a^b]//FullForm
Out[1]//FullForm=
Hold[Power[Map[f,a],b]]
In[2]:=Hold[Map[f,a^b]]//FullForm
Out[2]//FullForm=
Hold[Map[f,Power[a,b]]]
In[3]:=Hold[f//@a^b]//FullForm
Out[3]//FullForm=
Hold[Power[MapAll[f,a],b]]
In[4]:=Hold[MapAll[f,a^b]]//FullForm
Out[4]//FullForm=
Hold[MapAll[f,Power[a,b]]]
--
To reply via email subtract one hundred and four
| |
|
| On Tue, 22 Nov 2005 09:52:51 +0000 (UTC), Peter Pein <petsie@dordos.net>
wrote:
> Thomas Bohl schrieb:
> 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@inbox.ru
| |
| marks@wolfram.com 2005-11-24, 7:59 am |
| This is a bug in NValues that has been fixed in our development
version.
Generally N works adaptively for arbitrary precision so that
when you given an input like Exp[1/1000] the function is
capable enough to know that the argument should be evaluated
to a given Accuracy in order to obtain the requested Precision
in the result.
For example:
In[1]:= Exp[0.001``10]
Out[1]= 1.001000500
In[2]:= Precision[%]
Out[2]= 10.
If Precision were used in coercing the argument, then the Precision
of the result would be too large (resulting in excessive intermediary
work):
In[3]:= Exp[0.001`10]
Out[3]= 1.001000500167
In[4]:= Precision[%]
Out[4]= 13.
Now when working with machine precision, only machine numbers
are used, but the code for NValues was not taking this into account.
A simple workaround is to add the second line here to map Accuracy to
Precision for NValues with MachinePrecision.
In[1]:= N[a] = 2.;
In[2]:= N[a, {Infinity, MachinePrecision}]:= N[a, {MachinePrecision,
Infinity}];
In[3]:= k = z^(a*Pi);
In[4]:= N[k]
6.28319
Out[4]= z
Similarly this fix works for converting arguments to Exp:
In[1]:= N[a] = 2.;
In[2]:= N[a, {Infinity, MachinePrecision}]:= N[a, {MachinePrecision,
Infinity}];
In[3]:= N[Exp[z a Pi]]
6.28319 z
Out[3]= 2.71828
Mark Sofroniou
Research and Development
Wolfram Research
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]
>
> k = z^(a*Pi)
> N[k]
>
>
> 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.
|
|
|
|
|