pythonic equivalent of Mathematica's FixedPoint function

J

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.

thanks, jelle.
 
R

Russell Blau

jelle said:

I don't think that Tim's FixedPoint class is doing the same thing as
Mathematica's FixedPoint function (or even anything remotely similar).
Well, except for the fact that they both operate on numbers....

You could probably write your own FixedPoint function without too much
difficulty, with the only tricky part being for it to know when to stop!

Russ
 
T

Tim Peters

[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.,
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]

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

jelle

Ah, i see, that clears up the monetary context.
Thank you for your FixedPoint example.
Can i help myself out by mentioning that the most simple things are
always most difficult ;-)

Thanks, Jelle.
 
J

jelle

You could probably write your own FixedPoint function without too much
difficulty, with the only tricky part being for it to know when to
stop!

It would be quite interesting to have this kind of function. But
likely its far from trivial.
FixedPoint seems to be one of the core functions in Mathematica, where
many other functions are based on. Some additions such as FixedPoint
would make functional programming in Python more functional is suppose.
Or would anyone consider it sugar syntax for ??
 
T

Terry Reedy

jelle said:
stop!

It would be quite interesting to have this kind of function. But
likely its far from trivial.

Fixed point iteration is a standard technique in numerical analysis --
floating point calculations as approximation of real-number functions. The
first problem is getting an equation form that will converge -- which has
a stable, preferably unique fixed point. The second is how to start --
getting a first approximation that leads to convergence. For stopping, one
must choose between relative and absolute changes in the variable or
expression (in x or y) or some combiniation thereof.
FixedPoint seems to be one of the core functions in Mathematica, where
many other functions are based on. Some additions such as FixedPoint
would make functional programming in Python [easier].

It belongs in a module of floating-point equation solvers. Perhaps one is
already in scipy.

Terry J. Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top