ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 10 Dec 2020 16:29:03 +0100Is there a shortcut command to iterate an endomorphism?https://ask.sagemath.org/question/54479/is-there-a-shortcut-command-to-iterate-an-endomorphism/Suppose $f$ is a function from some set to itself. Is there a shortcut command in sage to compute an iterated composition of $f$, such as $f^3(x)=f(f(f(x)))$ where I could type something like `f^3(x)` into the console, and more generally for computing $f^n(x)$ for a given $n$?Tue, 01 Dec 2020 05:08:08 +0100https://ask.sagemath.org/question/54479/is-there-a-shortcut-command-to-iterate-an-endomorphism/Comment by slelievre for <p>Suppose $f$ is a function from some set to itself. Is there a shortcut command in sage to compute an iterated composition of $f$, such as $f^3(x)=f(f(f(x)))$ where I could type something like <code>f^3(x)</code> into the console, and more generally for computing $f^n(x)$ for a given $n$?</p>
https://ask.sagemath.org/question/54479/is-there-a-shortcut-command-to-iterate-an-endomorphism/?comment=54480#post-id-54480Welcome to Ask Sage!
Thank you for your question.Tue, 01 Dec 2020 05:19:33 +0100https://ask.sagemath.org/question/54479/is-there-a-shortcut-command-to-iterate-an-endomorphism/?comment=54480#post-id-54480Comment by slelievre for <p>Suppose $f$ is a function from some set to itself. Is there a shortcut command in sage to compute an iterated composition of $f$, such as $f^3(x)=f(f(f(x)))$ where I could type something like <code>f^3(x)</code> into the console, and more generally for computing $f^n(x)$ for a given $n$?</p>
https://ask.sagemath.org/question/54479/is-there-a-shortcut-command-to-iterate-an-endomorphism/?comment=54481#post-id-54481Ideally, provide an explicit `f`, `x`, `n` to get others started on your question.Tue, 01 Dec 2020 05:20:16 +0100https://ask.sagemath.org/question/54479/is-there-a-shortcut-command-to-iterate-an-endomorphism/?comment=54481#post-id-54481Comment by slelievre for <p>Suppose $f$ is a function from some set to itself. Is there a shortcut command in sage to compute an iterated composition of $f$, such as $f^3(x)=f(f(f(x)))$ where I could type something like <code>f^3(x)</code> into the console, and more generally for computing $f^n(x)$ for a given $n$?</p>
https://ask.sagemath.org/question/54479/is-there-a-shortcut-command-to-iterate-an-endomorphism/?comment=54487#post-id-54487Two previous related questions:
- [Ask Sage question 35989: Apply functions iteratively](https://ask.sagemath.org/question/35989)
- [Ask Sage question 36159: Apply functions iteratively (modified re-post)](https://ask.sagemath.org/question/36159)
One keyword: dynamical systems.Tue, 01 Dec 2020 05:32:28 +0100https://ask.sagemath.org/question/54479/is-there-a-shortcut-command-to-iterate-an-endomorphism/?comment=54487#post-id-54487Answer by Sylvain for <p>Suppose $f$ is a function from some set to itself. Is there a shortcut command in sage to compute an iterated composition of $f$, such as $f^3(x)=f(f(f(x)))$ where I could type something like <code>f^3(x)</code> into the console, and more generally for computing $f^n(x)$ for a given $n$?</p>
https://ask.sagemath.org/question/54479/is-there-a-shortcut-command-to-iterate-an-endomorphism/?answer=54630#post-id-54630You can define a function that takes a function `f` and an integer `n`,
and returns the `n`-th iterate of `f`.
This can be done using def and lambda:
sage: def iterate(f, n):
....: r"""
....: Return the n-th iterate of ``f``.
....: """
....: if n not in NN:
....: raise ValueError("can only define n-th iterate for n in NN")
....: return lambda x: x if n == 0 else iterate(f, n-1)(f(x))
or using nested lambdas:
sage: iterate = lambda f, n: lambda x: x if n == 0 else iterate(f, n-1)(f(x))
Then, given a function, for instance:
sage: def square(x):
....: return x**2
you can define its iterate:
sage: f = iterate(square, 4)
sage: f
<function ...>
and compute with it:
sage: f(x)
x^16
or directly:
sage: iterate(square, 4)(x)
x^16Thu, 10 Dec 2020 16:29:03 +0100https://ask.sagemath.org/question/54479/is-there-a-shortcut-command-to-iterate-an-endomorphism/?answer=54630#post-id-54630