Re: simple nest
- To: mathgroup at smc.vnet.net
- Subject: [mg106676] Re: [mg106661] simple nest
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Thu, 21 Jan 2010 04:50:03 -0500 (EST)
- References: <201001201150.GAA09275@smc.vnet.net>
Hi Francisco, When you use NestWhile and wish to access several past results, they should correspond to different arguments of your test function. Here is some specific function: Clear[func]; func[x_, y_, z_, p_, q_] := IntegerPart[{z, p, q}^0.9] Here is how we can use it to achieve what you want: In[2]:= NestWhileList[ func[1, 2, #[[1]], #[[2]], #[[3]]] &, {1, 100, 1000}, UnsameQ[#1[[2]], #2[[2]]] &, 2] Out[2]= {{1, 100, 1000}, {1, 63, 501}, {1, 41, 269}, {1, 28, 153}, {1, 20, 92}, {1, 14, 58}, {1, 10, 38}, {1, 7, 26}, {1, 5, 18}, {1, 4, 13}, {1, 3, 10}, {1, 2, 7}, {1, 1, 5}, {1, 1, 4}} I used NestWhileList to make it more transparent what's going on. Or, in this case, it may be less confusing to use pure function with named arguments for the test: In[3]:= NestWhileList[ func[1, 2, #[[1]], #[[2]], #[[3]]] &, {1, 100, 1000}, Function[{this, prev}, UnsameQ[this[[2]], prev[[2]]]], 2] Out[3]= {{1, 100, 1000}, {1, 63, 501}, {1, 41, 269}, {1, 28, 153}, {1, 20, 92}, {1, 14, 58}, {1, 10, 38}, {1, 7, 26}, {1, 5, 18}, {1, 4, 13}, {1, 3, 10}, {1, 2, 7}, {1, 1, 5}, {1, 1, 4}} Note that the first argument is the "oldest" result and the last argument is the most recent one, and the total number of arguments to the test function is regulated by the fourth parameter to NestWhile(List) - 2 in our case. Note also that pure functions silently ignore excessive arguments passed to them. These two facts imply that you may get obscure mistakes if you specify the wrong (too large) number of most recent results *and* use a pure function for testing. Hope this helps. Regards, Leonid On Wed, Jan 20, 2010 at 2:50 PM, Francisco Gutierrez <fgutiers2002 at yahoo.com > wrote: > Dear group: > I have a function that gets five arguments, two of which are fixed, and > three vary. Its output are three numbers. So it can be nested, for example > over some initial values. > Say: > Nest[func[arg1,arg2,#[[1]],#[[2]],#[[3]]]&,{init1,init2,init3},k] > > Simple enough, works well. > Now I want to nest the same function, while the second non fixed argument > changes at each step. I thought the "natural" way of doing this was: > > NestWhile[func[arg1,arg2,#[[1]],#[[2]],#[[3]]]&,{init1,init2,init3},UnsameQ[#[[2]]&,2], > but this evidently does not work. > > So: in a NestWhile how do I create tests over parts of the outputs of a > function? > Thanks! > Fg > >
- References:
- simple nest
- From: Francisco Gutierrez <fgutiers2002@yahoo.com>
- simple nest