Hi Jason,

Thanks for that. I still have a problem, as I use sage to derive a
complicated equation first, and then I need to get that function into
python. Say this function is called f, I tried the following:

import numpy
var('a b x')
f = a*x^2 + b
v = numpy.array([1,2,3])
w = numpy.array([4,5,6])
z = numpy.array([7,8,9])

def g(a,x,b):
    return f

g(a=v,x=w,b=z)
a*x^2 + b

It only works if I spell out the function in the definition of g:

def g(a,x,b):
    return a*x^2 + b

g(a=v,x=w,b=z)
array([23, 58, 117], dtype=object)

Is there a simple way of converting f to a python function?

Are you sure that numpy does loop? It is a lot faster than the list
comprehension method:

v = srange(0,999)
w = srange(1000,1999)
z = srange(2000,2999)
vn = numpy.array(v)
wn = numpy.array(w)
zn = numpy.array(z)

%time
result1 = g(a=vn,x=wn,b=zn)
     CPU time: 0.00 s,  Wall time: 0.00 s

%time
result2 = [f(a=i,x=j,b=k) for i,j,k in zip(v,w,z)]
     CPU time: 0.27 s,  Wall time: 0.35 s

Cheers
Stan

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to