1
$\begingroup$

Good day to everyone!

I have two (just for simple example) functions and they compose a map:

m[t_]:=RotationMatrix[t].#&
s[k_]:={#[[1]],#[[2]]-k2 #[[1]]^2}&
map=Composition[m[0.2],s[2]]

If I use this map in NestList all works nice:

NestList[map,{0.01,0.001},1000]

But now I want to change the first argument t of m[t] at each (or with some step) iteration. For this I was hoping to use FoldList, but this does not work:

FoldList[Composition[m[0.2+#2]&,s[2]],{0.01,0.001},Range[0.01,0.02,0.001]]

How can I use Composition in FoldList?

EDIT

I'm not sure can I add some more wishes or should I post another question.

To speed-up iterations, I can Compile functions:

mc=Compile[{t,{x,_Real,1}},{{Cos[t],-Sin[t]},{Sin[t],Cos[t]}}.x]
sc=Compile[{k2,{x,_Real,1}},{x[[1]],x[[2]]-k2 x[[1]]^2}]

Using With technique that @kglr advised in comment I can write:

FoldList[With[{xx = #2},RightComposition[mc[.2+xx,#]&,sc[2,#]&]],
{0.01,0.001},Range[0.01,0.02,0.001]]

This works. But what confuses me is that #2 is in purple and Composition does not work when replaced RightComposition. What is the difference in this case between Composition and RightComposition? Am I using Compile correctly?

$\endgroup$
12
  • 2
    $\begingroup$ you probably meant s[k2_]:=...? $\endgroup$
    – kglr
    Apr 16, 2021 at 12:35
  • 1
    $\begingroup$ FoldList[With[{xx = #2}, Composition[m[.2 + xx], s[2]]@#] &,...]? $\endgroup$
    – kglr
    Apr 16, 2021 at 12:49
  • $\begingroup$ @kglr, yes, that works! You can post this as answer, and porbably add explanation of why direct using #2 fails. $\endgroup$
    – macros
    Apr 16, 2021 at 14:24
  • $\begingroup$ @kglr, I edited question, could you give me some hints, or answers? $\endgroup$
    – macros
    Apr 16, 2021 at 15:49
  • 1
    $\begingroup$ Also, you haven't correctly understood @kglr 's comment. If I rewrite it as FoldList[Function[{foldarg1, foldarg2}, With[{xx = foldarg2}, Composition[m[.2 + xx], s[2]]@foldarg1]], ...], is it easier to understand? $\endgroup$
    – xzczd
    Apr 18, 2021 at 2:30

0

Your Answer

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

Browse other questions tagged or ask your own question.