On Thu, 18 Sep 2014 at 08:20AM +0200, Vincent Delecroix wrote:
> I had a look at NestList in Mathematica and there is nothing out of
> the box to compute
>   [x, f(x), f(f(x)), f(f(f(x))), ...]
> in Python. But still you can do the following one line program

I have a utility function that I use often that does this:

def applyntimes(f, arg, n):
    """
    Returns f(f(f(...f(arg)))); that is, f composed with itself n times,
    starting with arg. If n <= 0, we just return arg.
    """
    if n <= 0:
        return arg
    else:
        return reduce(lambda x, y: f(x), range(n), arg)


So the above list is

[applyntimes(f, x, n) for n in range(whatever)]



Dan

-- 
---  Dan Drake
-----  www.math.wisc.edu/~ddrake/
-------

Attachment: signature.asc
Description: Digital signature

Reply via email to