[MATRIX-SIG] Extending NumPy

Aaron Watters arw@dante.mh.lucent.com
Tue, 19 Aug 1997 11:39:48 -0400


If you can deal with a bit of overhead, maybe
you should use the abstract object interface
(thanks again Jim Fulton!) to extract Python
objects from sequences and get their values.
Look at abstract.h.  Here, I think, you could
pass any Python sequence down to the C interface
and extract the elements.  For Numeric this might
add some unneeded malloc overhead when an
int or float is extracted from a NumPy array, and
that will slow down the code, but it would almost
certainly be the easiest thing to do.

In the reverse direction you can create and
populate a listobject, and have Python convert
it to an array.  Again, not the most space/time
efficient approach, but it'll work and soon.
Look at listobject.h.  Careful with refcounts,
of course.

Life is compromise.  -- Aaron Watters

----------
> From: Hoon Yoon - IPT Quant <hyoon@nyptsrv1.etsd.ml.com>
> To: matrix-sig@python.org
> Subject: [MATRIX-SIG] Extending NumPy
> Date: Tuesday, August 19, 1997 9:28 AM
> 
> Hello,
> 
>    I am finally embarking on extending Python with C programs and at
>    this point I am trying to push in Lists of strings and get back 
>    lists of strings and Numerical arrays back from a C function. 
>    Unfortunately, I am having problems finding examples of these
>    codes in C.  I guess the following from gdmodule does show how
>    to kinda pass in a list or tuple into a C prog, but I am lost on
>    how to do this for string arrays (multiple) and get back string 
>    arrays and most of all Numeric Arrays:
>    
> *****************************************************************
> Nice example of how to do this with number tuple
> Can I use **points to do this for string tuple and args1, args2, args3?
> *****************************************************************   
>    static PyObject *image_lines(self, args)
>     imageobject *self;
>     PyObject *args;
> {
>     PyObject *point, *points, *bit;
>     int color, i, x,y,ox,oy;
>  
>     if (!PyArg_ParseTuple(args, "O!i", &PyTuple_Type, &points, &color))
>     {
>         PyErr_Clear();
>         if (PyArg_ParseTuple(args, "O!i", &PyList_Type, &points, &color))
>             points = PyList_AsTuple(points);
>         else return NULL;
>     }
>  
>     point = PyTuple_GET_ITEM(points,0);
>     ox = PyInt_AS_LONG((PyIntObject *)PyTuple_GET_ITEM(point,0));
>     oy = PyInt_AS_LONG((PyIntObject *)PyTuple_GET_ITEM(point,1));
>     for (i=1; i<PyTuple_Size(points); i++)
>     {
>         point = PyTuple_GET_ITEM(points,i);
>         x = PyInt_AS_LONG((PyIntObject *)PyTuple_GET_ITEM(point,0));
>         y = PyInt_AS_LONG((PyIntObject *)PyTuple_GET_ITEM(point,1));
>         gdImageLine(self->imagedata, X(ox), Y(oy), X(x), Y(y), color);
>         ox=x;oy=y;
>     }
>  
>     Py_INCREF(Py_None);
>     return Py_None;
> }
> **********************************************************************
>    Can anyone show me some example of these extentions, I've mentioned
>    here? At least I would like to get back list in list 
>    [[1,2,3],[4,5,6]] type of thing, which I could easily convert back
>    to a Num Array.
>    
> Much appreciated,
> 
> Hoon,
> 
> _______________
> MATRIX-SIG  - SIG on Matrix Math for Python
> 
> send messages to: matrix-sig@python.org
> administrivia to: matrix-sig-request@python.org
> _______________
> 

_______________
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
_______________