On Dec 19, 2009, at 11:14 AM, Christopher Olah wrote:

> Greetings!
>
> I can't seem to figure out how (elegantly) to make a function that is
> the repeated composite of another. eg. Suppose I have a function f,
> how do I get f*f*f*f...
>
> In math, I could just f^n,

One difficulty with supporting that would be that it's bit ambiguous  
as powering is also often used for powering after application, not  
only repeated application (e.g. sin^2(x) is usually interpreted as  
(sin(x))^2) and it would make the gulf between functions and  
expressions even bigger.

sage: f(x) = sin(x)
sage: f^2
x |--> sin(x)^2
sage: f = sin(x)
sage: f^2
sin(x)^2

> in normal lambda calculus, I'd just use n.f
> (church numeral), but in sage the only way I could come up with was
> using a loop to generate "f(f(...(x)))" and then printing it and copy
> pasting it to the end of "g=lambda x: " and using that (and even then,
> when I used ~100 iterations, sage crashed). There's got to be a more
> elegant way...

Though it's not perfect, you could do

sage: def compose(f, n, x): return x if n == 0 else compose(f, n-1,  
f(x))
....:
sage: compose(sin, 3, x)
sin(sin(sin(x)))
sage: compose(sin, 100, x)
sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(x))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
sage: foo = compose(sin, 500, x)
sage: foo(x=1.5)
0.0770617846688116


>
> Thanks,
>
> Christopher
>
> PS. If your wondering why I'm doing this, have a look at
> http://christopherolah.wordpress.com/2009/12/19/formation-of-escape-time-fractals/
> , I'm making some really neat pictures of the formation of escape time
> fractals!

Neat.

- Robert


-- 
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org

Reply via email to