[SciPy-user] Need very simple example showing how to return numpy array from C/C++ function
Matthieu Brucher
matthieu.brucher@gmail....
Fri Dec 28 16:50:59 CST 2007
To make your .i work, you have two things to do :
- first initialize numpy :
%{
#define SWIG_FILE_WITH_INIT
%}
%include "numpy.i"
%init %{
import_array();
%}
Then result (which is $1 in the typemap) is in fact a wrapper around the
vector, and to get it, you have to dereference it, so the typemap becomes :
%typemap(out) std::vector<double> {
int length = $1.size();
$result = PyArray_FromDims(1, &length, PyArray_DOUBLE);
memcpy(PyArray_DATA($result),&((*(&$1))[0]),sizeof(double)*length);
}
I think I will add this one to my blog, if nobody minds.
Matthieu
2007/12/28, Jeremy Conlin <jeremit0@gmail.com>:
>
>
>
> On Dec 28, 2007 2:06 PM, Matthieu Brucher <matthieu.brucher@gmail.com>
> wrote:
>
> > Thank you, that is beginning to make more sense to one inexperienced in
> > > wrapping. There is one error that I haven't been able to work around.
> > >
> > > error: no match for 'operator[]' in 'result[0]'
> > >
> > > This comes from the memcpy line, I guess. It looks like it is getting
> > > mixed up between the $1 and the $result.
> > >
> >
> >
> > This is strange... Is there more indications like the type of result ?
> >
> >
> There wasn't much else in the compiler messages. I am just figuring this
> out before I add it to my real code. As such, the files I use are simple.
> I have attached them to this email.
>
>
> > Do I need to apply this typemap to avoid this error or is it
> > > automatically applied because the type (double) is specified?
> > >
> >
> >
> > The fact that you have a compile error shows that it is applied ;)
> >
> Of course!
>
> Thanks again,
> Jeremy
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>
>
>
--
French PhD student
Website : http://matthieu-brucher.developpez.com/
Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92
LinkedIn : http://www.linkedin.com/in/matthieubrucher
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20071228/e1830727/attachment-0001.html
More information about the SciPy-user
mailing list