Re: documentation NestWhile
- To: mathgroup at smc.vnet.net
- Subject: [mg71523] Re: documentation NestWhile
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Tue, 21 Nov 2006 07:05:22 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <ejtcsq$i4f$1@smc.vnet.net>
ben wrote:
> Dear group,
>
> This is an info about a possible flaw in the documentation of
I am afraid that this is misinformation. See below.
>
> NestWhile[f, expr, test, m, max]
>
> On my machine the order of arguments as above doesn't work,
Are you sure about that? What would have been really nice is to post an
example of a non-working-as-claimed-by-the-doc expression.
> however
>
> NestWhile[f, expr, test, max,m]
>
> is fine. I suppose there is a flow in the manual.
No.
> Bye
> Ben
>
> the doc
>
> "NestWhile[f, expr, test] starts with expr, then repeatedly \
> applies f until applying test to the result no longer yields True. \
> NestWhile[f, expr, test, m] supplies the most recent m results as
> arguments \
> for test at each step. NestWhile[f, expr, test, All] supplies all
> results so \
> far as arguments for test at each step. NestWhile[f, expr, test, m,
> max] \
> applies f at most max times. NestWhile[f, expr, test, m, max, n]
> applies f an \
> extra n times. NestWhile[f, expr, test, m, max, -n] returns the result
> found \
> when f had been applied n fewer times."
>
> My Mathematica
> Version Number: 5.2.0.0
> Platform: X
>
Note that NestWhile and NestWhileList have the same sequence of arguments.
Here we divide by two until the resulting integer is odd.
In[1]:=
NestWhileList[#1/2 & , 123456, EvenQ]
Out[1]=
{123456, 61728, 30864, 15432, 7716, 3858, 1929}
If your claim were right, that is the fourth argument is the max number
of iterations, we should only get the initial number and the first
result in the following expression:
In[2]:=
NestWhileList[#1/2 & , 123456, EvenQ, 1]
Out[2]=
{123456, 61728, 30864, 15432, 7716, 3858, 1929}
Moreover, still with the same hypotheses, we should not get an error
message about too many arguments supplied to the test function EvenQ
In[3]:=
NestWhileList[#1/2 & , 123456, EvenQ, 2]
From In[3]:=
EvenQ::argx : EvenQ called with 2 arguments; 1 argument is expected.
Out[3]=
{123456, 61728}
Below, we can see that the behavior of NestWhileList and NestWhile is in
agreement with the documentation: EvenQ is provided with one argument
only and we stop after two iterations regardless of the possible outcome
of the next iteration.
In[4]:=
NestWhileList[#1/2 & , 123456, EvenQ, 1, 2]
Out[4]=
{123456, 61728, 30864}
In[5]:=
$Version
Out[5]=
"5.2 for Microsoft Windows (June 20, 2005)"
You will get the same results with NestWhile
In[6]:=
NestWhile[#1/2 & , 123456, EvenQ]
NestWhile[#1/2 & , 123456, EvenQ, 1]
NestWhile[#1/2 & , 123456, EvenQ, 2]
NestWhile[#1/2 & , 123456, EvenQ, 1, 2]
Now, it is still possible that there is something wrong on your system
in particular or in Mathematica for X, but, again, that would really
help if you document such a claim by giving an example.
Regards,
Jean-Marc