Re: Re.Iterations.. (function composition) the whole story
- To: mathgroup at yoda.physics.unc.edu
- Subject: Re: Re.Iterations.. (function composition) the whole story
- From: gaylord at ux1.cso.uiuc.edu
- Date: Fri, 24 Apr 1992 05:17:25 -0500
sorry about the incomplete response before. here's the rest of the story =================================================== >spagiola at leland.stanford.edu writes: > >I'm looking for a way to achieve the following result a little more >elegantly: > >a[s] >g[%] >a[%] >g[%] >a[%] >g[%] >... >You get the idea. I have a function a[] that takes an initial value >of s as its argument. A second function g[] takes the result of a[] >and modifies the value of s, which then passes back to a[], etc. >I also need to recover every value, so that i can plot a[] and s[] >(separately). I'd also like to be able to either specify the number >of iterations or let it converge to a point where it no longer >changes. ================================================= dog = NestList[Composition[g, a], s, 3] {s, g[a[s]], g[a[g[a[s]]]], g[a[g[a[g[a[s]]]]]]} cat = NestList[Composition[a, g], a[s], 3] {a[s], a[g[a[s]]], a[g[a[g[a[s]]]]], a[g[a[g[a[g[a[s]]]]]]]} and depending on whether you want to end with g or a: Flatten[Transpose[{dog, cat}]] {s, a[s], g[a[s]], a[g[a[s]]], g[a[g[a[s]]]], a[g[a[g[a[s]]]]], g[a[g[a[g[a[s]]]]]], a[g[a[g[a[g[a[s]]]]]]]} Drop[Flatten[Transpose[{dog,cat}]],-1] {s, a[s], g[a[s]], a[g[a[s]]], g[a[g[a[s]]]], a[g[a[g[a[s]]]]], g[a[g[a[g[a[s]]]]]]} --------------------------------------------------- also, Flatten[Transpose[{dog, cat}]] is equivalent to Flatten[Thread[Sequence[{dog, cat}]]] {s, a[s], g[a[s]], a[g[a[s]]], g[a[g[a[s]]]], a[g[a[g[a[s]]]]], g[a[g[a[g[a[s]]]]]], a[g[a[g[a[g[a[s]]]]]]]} Flatten[Thread[List[dog, cat]]] {s, a[s], g[a[s]], a[g[a[s]]], g[a[g[a[s]]]], a[g[a[g[a[s]]]]], g[a[g[a[g[a[s]]]]]], a[g[a[g[a[g[a[s]]]]]]]} ------------------------------------------------------ and as i said before, you can use FixedPointList instead of NestList for convergence or a max of n steps whichever occurs first. ?FixedPointList FixedPointList[f, expr] generates a list giving the results of applying f repeatedly, starting with expr, until the results no longer change. FixedPointList[f, expr, n] stops after at most n steps. ------------------------------------- M is functional programming ... plus a whole lot more.