# [SciPy-user] weave and access to Numeric arrays

eric jones eric at enthought.com
Mon Jul 22 20:01:42 CDT 2002

```Hey Fernando,

Here is an example that hopefully does what you want.

eEric

# f.py
from scipy import weave

def sum(x):
code = """
float result=0;
for (i = 0; i < _Nx; i++)
result += x_data[i];
return_val = Py::new_reference_to(Py::Float(result));
"""
return weave.inline(code,['x'])

from scipy import *

x = ones(10,typecode=Float32)
print sum(x)

C:\home\eric\STSci\chaco>python f.py
10.0

> -----Original Message-----
> From: scipy-user-admin at scipy.net [mailto:scipy-user-admin at scipy.net]
On
> Behalf Of Fernando Perez
> Sent: Monday, July 22, 2002 5:09 PM
> To: scipy-user at scipy.org
> Subject: [SciPy-user] weave and access to Numeric arrays
>
> Hi folks,
>
> I have a weave question. Is it possible to access the data for a 1-d
> Numeric
> array as a raw C array through weave? Up to now I've successfully used
the
> blitz type converters, but I'm writing a sparse matrix algorithm where
I
> want
> to do the pointer arithmetic by hand, as it's very straightforward and
I
> need
> the thing to run really fast.
>
> So if say A=Numeric.array([1,4.5,3.6]), is there a way in inlined
weave
> code
> to do something like
>
> sum=0.0;
> for(i=0;i<N;++i) {
>   sum += *(A_data+i);
> }
>
> for some way of having A_data be the pointer to the 'real' data in the
> Numeric
> array A?
>
> Thanks,
>
> f.
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user

```