pythonic equivalent of Mathematica's FixedPoint function

Tim Peters tim.peters at gmail.com
Tue Feb 1 11:52:08 EST 2005


[jelle]
> I now some hostility to functional programming is flaming up now and
> then; still could someone suggest me a pythonic equivalent for
> Mathematica's FixedPoint function? For those not familiar with
> Mathematica:
>
> FixedPoint[f, expr] starts with expr, then applies f repeatedly until
> the result no longer changes.

If that's all there is to it, sounds like a simple loop:

def FixedPoint(f, expr):
    old = expr
    while True:
        new = f(old)
        if old == new:
            return new
        old = new

Then, e.g.,

>>> FixedPoint(lambda x: (x + 25/x)/2, 400)
5

But I bet there's more to it than just that (e.g., maybe an optional
limit on max # of iterations; maybe a way to terminate on "approximate
equality").  As-is, that function is very prone to falling into an
infinite loop.

[and later]
> doh...
>
> https://sourceforge.net/projects/fixedpoint

Nope, that has to do with decimal arithmetic using a fixed number of
decimal digits after the decimal point ("fixed-point decimal").



More information about the Python-list mailing list