Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I need results like this:

{Cos[Cos[x]], Sin[Cos[Cos[x]]],Cos[Cos[Sin[Cos[Cos[x]]]]], 
 Sin[Cos[Cos[Sin[Cos[Cos[x]]]]]], ... }

I've tried to use Nest inside NestList, but failed:

NestList[Sin, Nest[Cos, x, 2], 3]

(* {Cos[Cos[x]], Sin[Cos[Cos[x]]], Sin[Sin[Cos[Cos[x]]]], 
 Sin[Sin[Sin[Cos[Cos[x]]]]]}  *)

Any idea?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

Here's one possibility:

n = 5; flist = {Composition @@ ConstantArray[Cos, 2], Sin};
ComposeList[PadRight[flist, n, flist], 1]
{1, Cos[Cos[1]], Sin[Cos[Cos[1]]], Cos[Cos[Sin[Cos[Cos[1]]]]], 
 Sin[Cos[Cos[Sin[Cos[Cos[1]]]]]], Cos[Cos[Sin[Cos[Cos[Sin[Cos[Cos[1]]]]]]]]}
share|improve this answer
    
You are so good at manipulating lists. –  yulinlinyu Jun 21 '12 at 7:01
    
I like this method but your code seems pointlessly baroque. Why not use flist = {Cos@Cos@#&, Sin} ? –  Mr.Wizard Jun 22 '12 at 2:46
    
@Mr. Wizard: it extends nicely to something like Composition @@@ {ConstantArray[Cos, 3], ConstantArray[Sin, 2]};, to use kguler's example. I was already expecting someone else to use Nest[], also, as kguler did. –  J. M. Jun 22 '12 at 2:53
    
I think you should include that in your answer; then it will make sense. –  Mr.Wizard Jun 22 '12 at 2:55
 iterate[f1_, f2_, x_, n_] := 
    Rest@FoldList[({f1[#1], f2[#1]}[[Mod[#2, 2, 1]]]) &, x, Range[n]];
 iterate[Cos[Cos[#]] &, Sin, x, 5]
 (* ==> {Cos[Cos[x]], Sin[Cos[Cos[x]]], Cos[Cos[Sin[Cos[Cos[x]]]]], Sin[Cos[Cos[Sin[Cos[Cos[x]]]]]], Cos[Cos[Sin[Cos[Cos[Sin[Cos[Cos[x]]]]]]]]}*)

EDIT: Just learned about ComposeList (thanks J.M.!). Here is a variation using ComposeList and Nest:

iterate2[f1_, steps1_, f2_, steps2_, x_, n_] := 
  With[{indices = Mod[Range[n], 2, 1]}, 
  Rest@ComposeList[{Nest[f1, #, steps1] &, Nest[f2, #, steps2] &}[[indices]], x]];
iterate2[Cos, 3, Sin, 2, x, 4]
(* ==> {Cos[Cos[Cos[x]]], Sin[Sin[Cos[Cos[Cos[x]]]]], Cos[Cos[Cos[Sin[Sin[Cos[Cos[Cos[x]]]]]]]], Sin[Sin[Cos[Cos[Cos[Sin[Sin[Cos[Cos[Cos[x]]]]]]]]]]}  *)
share|improve this answer
    
I don't believe you intended to use the Cos@Cos construction... –  J. M. Jun 21 '12 at 8:27
    
@J.M. Thank you J.M.; you are right, I copy/pasted the wrong lines. –  kguler Jun 21 '12 at 8:30
    
Nice! I'd like to remind you that Mod[] is listable, so Mod[Range[n], 2, 1] works nicely. –  J. M. Jun 21 '12 at 11:50
    
Thanks again @J.M. for 3 TILs! –  kguler Jun 21 '12 at 11:59

Another option:

With[{n = 3}, Flatten[Rest[NestList[{Cos[Cos[#]], Sin[Cos[Cos[#]]]} &@Last[#] &, {x}, n]]]]

(* out: {Cos[Cos[x]], Sin[Cos[Cos[x]]], Cos[Cos[Sin[Cos[Cos[x]]]]], 
         Sin[Cos[Cos[Sin[Cos[Cos[x]]]]]], 
         Cos[Cos[Sin[Cos[Cos[Sin[Cos[Cos[x]]]]]]]], 
         Sin[Cos[Cos[Sin[Cos[Cos[Sin[Cos[Cos[x]]]]]]]]]} *)
share|improve this answer
    
your codes works well for my example, and I think it can be simplified as NestList[Sin[Cos[Cos[#]]] &, {x}, n]. Anyway, I like it very much. But If the number of succesive Cos is very large(e.g., 10^3), you like to use NestList[Compose[Sin, Nest[Cos, #, 10^3]] &, x, 3] or anything better? –  yulinlinyu Jun 21 '12 at 7:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.