2
$\begingroup$

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?

$\endgroup$
0

3 Answers 3

4
$\begingroup$

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]]]]]]]]}
$\endgroup$
4
  • $\begingroup$ You are so good at manipulating lists. $\endgroup$
    – yulinlinyu
    Jun 21, 2012 at 7:01
  • $\begingroup$ I like this method but your code seems pointlessly baroque. Why not use flist = {Cos@Cos@#&, Sin} ? $\endgroup$
    – Mr.Wizard
    Jun 22, 2012 at 2:46
  • $\begingroup$ @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. $\endgroup$ Jun 22, 2012 at 2:53
  • $\begingroup$ I think you should include that in your answer; then it will make sense. $\endgroup$
    – Mr.Wizard
    Jun 22, 2012 at 2:55
4
$\begingroup$
 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]]]]]]]]]]}  *)
$\endgroup$
3
  • $\begingroup$ I don't believe you intended to use the Cos@Cos construction... $\endgroup$ Jun 21, 2012 at 8:27
  • $\begingroup$ @J.M. Thank you J.M.; you are right, I copy/pasted the wrong lines. $\endgroup$
    – kglr
    Jun 21, 2012 at 8:30
  • $\begingroup$ Nice! I'd like to remind you that Mod[] is listable, so Mod[Range[n], 2, 1] works nicely. $\endgroup$ Jun 21, 2012 at 11:50
3
$\begingroup$

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]]]]]]]]]} *)
$\endgroup$
1
  • $\begingroup$ 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? $\endgroup$
    – yulinlinyu
    Jun 21, 2012 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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