Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Using lambda notation you can define another function for the compistion:

sage: iterate_comp = lambda f, n: lambda x: x if n == 0 else recApply(f, n-1)(f(x))

Then you define your function:

sage: def f(x): return x**2

Finally the composition:

sage: iterate_comp(f,4)(x)
x^16

Using lambda notation you can define another function for the compistion:

sage: iterate_comp = lambda f, n: lambda x: x if n == 0 else recApply(f, iterate_comp(f, n-1)(f(x))

Then you define your function:

sage: def f(x): return x**2

Finally the composition:

sage: iterate_comp(f,4)(x)
x^16

Using lambda notation you You can define another 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 the compistion:

sage: iterate_comp 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_comp(f, iterate(f, n-1)(f(x))

Then you define your function:Then, given a function, for instance:

sage: def f(x): square(x):
....:     return x**2

Finally the composition:

sage: iterate_comp(f,4)(x)
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^16