[SciPy-user] weave and access to Numeric arrays
eric at enthought.com
Tue Jul 23 13:51:57 CDT 2002
I sent that code out to fast -- when it works you believe it even when
you shouldn't. :)
The "sum" that your printing is actually imported by the "from scipy
import *" -- not the weavified sum. So of course it works.
I've included a new example and with verbose=2, you see the code compile
(and can be sure it actually is used). You'll note that "i" is now
defined. :) As far as the magic variables, I don't think they are
documented anywhere so the only place to look is the source. Look in
weave.standard_array_spec.py. The following code snippet reveals
(somewhat cryptically) the magic variables.
type = numeric_to_c_type_mapping[self.numeric_type]
name = self.name
#dims = self.dims
var_name = self.retrieve_py_variable(inline=1)
templ = '// %(name)s array declaration\n' \
'int* _N%(name)s = %(name)s->dimensions;\n' \
'int* _S%(name)s = %(name)s->strides;\n' \
'int _D%(name)s = %(name)s->nd;\n' \
'%(type)s* %(name)s_data = (%(type)s*)
code = templ % locals()
For our example where "x" is an array, _Nx is the array with lengths to
each dimension, _Sx is the strides array, _Dx is the number of
dimensions, and x_data is the array data.
from scipy import weave
code = """
for (int i = 0; i < _Nx; i++)
result += x_data[i];
return_val = Py::new_reference_to(Py::Float(result));
from scipy import *
x = ones(10,typecode=Float32)
> -----Original Message-----
> From: scipy-user-admin at scipy.net [mailto:scipy-user-admin at scipy.net]
> Behalf Of Fernando Perez
> Sent: Monday, July 22, 2002 8:19 PM
> To: scipy-user at scipy.net
> Subject: RE: [SciPy-user] weave and access to Numeric arrays
> On Mon, 22 Jul 2002, eric jones wrote:
> > # 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
> Wow, Eric, hold it! How does that code work? I had to test it on my
> before I actually believed it! A few questions:
> 1. Why don't you need to declare i? I was always declaring my counters
> everywhere. What are the rules for what gets magically created by
> wasn't aware of this.
> 2. Who creates x_data and _Nx? How can I know which magical variables
> and what they correspond to in the Numeric C API? I've read what a
> array looks like in C, so if there's a way of knowing how the various
> of that structure are visible in the weave code, that would be great.
> multi-dimensional arrays are there various _N's?
> If this is documented somewhere, feel free to tell me to RTFM. But
> in the past the weave docs and certainly all of this is news to me. I
> that page of weave examples I told you about and I could include and
> some of this for public consumption once I understand it. I read all
> examples in weave/examples, and I see some of this _N stuff but zero
> about it.
> I'd greatly appreciate it if you give me a couple of pointers on this.
> pay you back by adding the newly gained wisdom to my weave examples
> reposting it here on the list for other's benefit.
> Thanks a lot,
> ps. And yes, what you gave me is exactly what I wanted. I just now
> really understand how it ticks for other more complicated cases I need
> SciPy-user mailing list
> SciPy-user at scipy.net
More information about the SciPy-user