[SciPy-user] Weave and scipy/other libs

eric jones eric@enthought....
Fri Feb 22 16:49:04 CST 2008

Here is a quick example showing how to do it.  Also look at 
functional.py in the examples directory.  Note that it will not be any 
faster than standard Python, because the big cost is the call into 
Python.  However, in combination with other weaved calculations, perhaps 
it is potentially faster -- although I haven't found occasion to use 
this capability much.


# example
from numpy import array, sum
from scipy import weave

a = array((1,2,3,4))
func = sum

# Note the use of py_a in this example, because a is the int* pointer
# to the actual data, and weave doesn't know how to coerce it back to
# a numpy array.  Instead, use the equivalent python object (py_a);
code = """ py::tuple args(1);
           args[0] = py_a; 
           return_val = func.call(args);
result = weave.inline(code,['func','a'])
print result

# A list doesn't take as much work, because it is represented as an py::list
a = [1,2,3,4]
code = """ py::tuple args(1);
           args[0] = a; 
           return_val = func.call(args);
result = weave.inline(code,['func','a'])
print result

Jose Luis Gomez Dans wrote:
> Hi!
> I am thinking of rewriting some bit of heavy processing using weave. However, it would be nice to be able to use some of scipy's functionality, as well as that of other python libraries. From within the weave C++ code, is it possible to call these python functions? 
> Thanks!
> Jose 

More information about the SciPy-user mailing list