[SciPy-user] weave and access to Numeric arrays

eric jones eric at enthought.com
Tue Jul 23 13:51:57 CDT 2002


Hey Fernando,

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.

def inline_decl_code(self):
        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*)
%(name)s->data;\n' 
        code = templ % locals()
        return code


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.

eric

##############################################################
#f.py
from scipy import weave

def my_sum(x):
    code = """

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

from scipy import *

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


> -----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 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
> machine
> 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
weave?
> I
> wasn't aware of this.
> 
> 2. Who creates x_data and _Nx? How can I know which magical variables
> exist,
> and what they correspond to in the Numeric C API? I've read what a
numeric
> array looks like in C, so if there's a way of knowing how the various
> fields
> of that structure are visible in the weave code, that would be great.
Esp.
> for
> multi-dimensional arrays are there various _N's?
> 
> If this is documented somewhere, feel free to tell me to RTFM. But
I've
> read
> in the past the weave docs and certainly all of this is news to me. I
have
> that page of weave examples I told you about and I could include and
> document
> some of this for public consumption once I understand it. I read all
the
> examples in weave/examples, and I see some of this _N stuff but zero
> comments
> about it.
> 
> I'd greatly appreciate it if you give me a couple of pointers on this.
> I'll
> pay you back by adding the newly gained wisdom to my weave examples
page
> and
> reposting it here on the list for other's benefit.
> 
> Thanks a lot,
> 
> f.
> 
> ps. And yes, what you gave me is exactly what I wanted. I just now
want to
> really understand how it ticks for other more complicated cases I need
to
> deal
> with.
> 
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user




More information about the SciPy-user mailing list