Random sum of functions

16 views
Skip to first unread message

Laurent Decreusefond

unread,
Jul 17, 2013, 5:07:11 AM7/17/13
to sage-s...@googlegroups.com
Hi everyone,

say I have a function of both an integer n and a complex z

def f(n,z):
     return z**n

For any tuple of integer (a_1,a_2, ..., a_k), (actually, k and a_i are random), I want to form the function 

z -> sum_{i=1}^k |f(a_i, z)|^2

The result must still be a function. I guess it is something which can be done by redefining a new class but I'm a totally newbie at object programming.
Can you help me ?

D. S. McNeil

unread,
Jul 17, 2013, 8:19:55 AM7/17/13
to sage-s...@googlegroups.com
I don't think you need to make an explicit class here.  You can build a function from within another function, and return that:

sage: def f(n, z):
....:     return z**n
....: 
sage: def maker(tup):
....:     def g(z):
....:         return sum(abs(f(a_i,z))**2 for a_i in tup)
....:     return g
....: 

After which

sage: f(1, 3+2*I)
2*I + 3
sage: maker((1,5,3))
<function __main__.g>
sage: g = maker((1,5,3))
sage: g(5)
9781275
sage: abs(f(1,5))**2 + abs(f(5,5))**2 + abs(f(3,5))**2
9781275

I used Python functions here because that's what you started with.  A similar approach would have worked if you were making a Sage function instead, namely write a function which accepts a tuple and returns a new function.


Doug



--
You received this message because you are subscribed to the Google Groups "sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
To post to this group, send email to sage-s...@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Anton Sherwood

unread,
Jul 17, 2013, 2:34:35 PM7/17/13
to sage-s...@googlegroups.com
On 2013-7-17 02:07, Laurent Decreusefond wrote:
> For any tuple of integer (a_1,a_2, ..., a_k), [...]
> I want to form the function
>
> z -> sum_{i=1}^k |f(a_i, z)|^2

Won't this work?

def ff(atuple,z):
return sum([ abs(f(n,z))**2 for n in atuple ])

--
*\\* Anton Sherwood *\\* www.bendwavy.org
Reply all
Reply to author
Forward
0 new messages