From rodgers@vs.lmco.com Fri May 2 16:46:33 1997 From: rodgers@vs.lmco.com (Kevin Rodgers (x39079, WT-07)) Date: Fri, 02 May 1997 10:46:33 -0500 Subject: [PYTHON MATRIX-SIG] Python OpenGL Module and NumPy Message-ID: <199705021546.KAA25385@daedalus> (I'm crossposting to both the main list and the matrix-sig because I'm not sure where this best fits) I'm using the OpenGL module to do a flight simulation program, and have run into a situation where I'm confused about the integration of NumPy and the OpenGL module. I'd like to read in a vehicle definition that may consist of hundreds of polygons, each with potentially hundreds of vertices, then render these with OpenGL. This seems like a natural integration of PyOpenGL and NumPy; for each polygon, you could pass a Numeric array of dimension (3,nPoly) where nPoly is the number of polygons and 3 is the number of coordinates per vertex; the C implementation of PyOpenGL would then call gl_Vertex3* as appropriate. This would obviously be much faster than writing the calls to gl.Vertex3* as a loop in Python. I've looked through the source to PyOpenGL, though, and it appears that only the gl.ColorVertex2 function actually implements this. Am I correct? If not, then how does one use Numeric with PyOpenGL? If I am correct, has anyone implemented the functionality I want, or should I bite the bullet and do it myself? Thanks in advance for any help. -- Kevin Rodgers Lockheed Martin Vought Systems rodgers@vs.lmco.com "This one goes up to eleven." -- Nigel Tufnel ------------------------------------------------------------------------------- _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From tom.schwaller@linux-magazin.de Fri May 2 18:02:21 1997 From: tom.schwaller@linux-magazin.de (Tom Schwaller) Date: Fri, 02 May 1997 19:02:21 +0200 Subject: [PYTHON MATRIX-SIG] Python OpenGL Module and NumPy References: <199705021546.KAA25385@daedalus> Message-ID: <336A1E1D.777D5E81@linux-magazin.de> Kevin Rodgers (x39079, WT-07) wrote: > > (I'm crossposting to both the main list and the matrix-sig because I'm not > sure where this best fits) > > I'm using the OpenGL module to do a flight simulation program, and have run > into a situation where I'm confused about the integration of NumPy and the > OpenGL module. I'd like to read in a vehicle definition that may consist of > hundreds of polygons, each with potentially hundreds of vertices, then render > these with OpenGL. This seems like a natural integration of PyOpenGL and > NumPy; for each polygon, you could pass a Numeric array of dimension (3,nPoly) > where nPoly is the number of polygons and 3 is the number of coordinates per > vertex; the C implementation of PyOpenGL would then call gl_Vertex3* as > appropriate. This would obviously be much faster than writing the calls to > gl.Vertex3* as a loop in Python. I've looked through the source to PyOpenGL, > though, and it appears that only the gl.ColorVertex2 function actually > implements this. Am I correct? If not, then how does one use Numeric with > PyOpenGL? If I am correct, has anyone implemented the functionality I want, > or should I bite the bullet and do it myself? Thanks in advance for any help. > -- > Kevin Rodgers Lockheed Martin Vought Systems rodgers@vs.lmco.com > "This one goes up to eleven." -- Nigel Tufnel >  One should implement the OpenGL1.1 features, I was using Mesa and could not try that out for a long time, but that's history now :-) I startet with it a while ago, but got some strange problems, which are still unresolved. Maybe you give it a try.. Forget the ColorVertex2 stuff, this should become obsolete with a clean OpenGL 1.1 implementation /* ############################### OpenGL 1.1 ######################## */ #ifdef NUMERIC static int type2gl[] = {-1, GL_UNSIGNED_BYTE, GL_BYTE, GL_SHORT, GL_INT, -1, GL_FLOAT, GL_DOUBLE, -1, -1, -1, -1}; static PyObject* gl_VertexPointer(PyObject* self, PyObject* args) { GLint size; GLenum type; GLsizei stride; PyObject *op; PyArrayObject *ap; TRY (PyArg_ParseTuple(args, "iiO", &size, &stride, &op)); if (PyArray_Check(op)) ap = (PyArrayObject *)op; else { TRY(ap = (PyArrayObject *)PyArray_ContiguousFromObject(op, PyArray_DOUBLE, 1, 0)); } type = type2gl[ap->descr->type_num]; ASSERT(type != -1, "Can't convert this type of array!"); glVertexPointer(size, type, stride, ap->data); Py_INCREF(Py_None); return Py_None; } static PyObject* gl_ColorPointer(PyObject* self, PyObject* args) { GLint size; GLenum type; GLsizei stride; PyObject *op; PyArrayObject *ap; TRY (PyArg_ParseTuple(args, "iiO", &size, &stride, &op)); if (PyArray_Check(op)) ap = (PyArrayObject *)op; else { TRY(ap = (PyArrayObject *)PyArray_ContiguousFromObject(op, PyArray_DOUBLE, 1, 0)); } type = type2gl[ap->descr->type_num]; ASSERT(type != -1, "Can't convert this type of array!"); glColorPointer(size, type, stride, ap->data); Py_INCREF(Py_None); return Py_None; } static PyObject* gl_DrawArrays(PyObject* self, PyObject* args) { GLenum mode; GLint first; GLsizei count; TRY (PyArg_ParseTuple(args, "iii", &mode, &first, &count)); glDrawArrays(mode, first, count); Py_INCREF(Py_None); return Py_None; } #endif /* Not NUMERIC */ ................... {"TranslateScene", gl_TranslateScene, 1 }, {"RotateScene", gl_RotateScene, 1 }, {"DistFromLine", gl_DistFromLine, 1 }, This is the old code which does not work corrrrectly, just go on with it.. It's the way to go.. -- Tom _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From Zachary_Roadhouse@brown.edu Sun May 4 04:57:05 1997 From: Zachary_Roadhouse@brown.edu (Zachary_Roadhouse@brown.edu) Date: Sat, 03 May 1997 23:57:05 -0400 (EDT) Subject: [PYTHON MATRIX-SIG] NumPy and PIL extension module Message-ID: Hello! With exams nearly over, I am attempting to delve into writing Python extension modules. I would like to write a module nPIL which has two functions in it for converting between PyArrayObject and Imaging objects. array ImageToArray(image) image ArrayToImage(array) I have a module written in python that does what I'm asking but at the expense of converting to a string first. It also doesn't handle multi-banded images (just grey scale). I'm running into problems in figuring out what type I should use for the array and how to set the (x,y,colorplanes...) in the array. Here is what I have so far: static PyObject* ImageToArray(PyObject* args) { PyObject* op; Imaging im; /* The image can have up to three dimensions */ /* x,y and number of color planes */ int dims[3]; PyObject* array; UINT8 *p; /* Check to see that one arguement was passed in */ if (!PyArg_ParseTuple(args, "O", &op)) return NULL; /* Check to see if that object is an Imaging */ im = PyImaging_AsImaging(op); if (!im) return NULL; /* Set the dimensions of the array to be the */ /* x and y dimensions of the image */ dims[0] = im->xsize; dims[1] = im->ysize; /* single layer */ if(im->image8) { int value; array = PyArray_FromDims(2,dims,PyArray_INT); value = im->image8[y][x]; } /* multilayer */ else { if(im->bands == 3) { dims[2] = 3; array = PyArray_FromDims(3,dims,PyArray_INT); p = (UINT8*) &im->image32[y][x]; } } - Zack E-MAIL: Zachary_Roadhouse@brown.edu WEB: http://althor.netspace.org/~zack/ Brown University, Box 220, Providence, RI 02912 Phone: (401) 863 - 5435 _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From hinsen@ibs.ibs.fr Mon May 5 13:07:42 1997 From: hinsen@ibs.ibs.fr (Konrad Hinsen) Date: Mon, 5 May 1997 14:07:42 +0200 Subject: [PYTHON MATRIX-SIG] NumPy and PIL extension module In-Reply-To: Message-ID: <199705051207.OAA20769@lmspc2.ibs.fr> > I'm running into problems in figuring out what type I should use for the > array and how to set the (x,y,colorplanes...) in the array. Here is what > I have so far: *Why* do you want to store images in arrays? Which array operations do you expect to use? Depending on the answer, you might need different arrangements of data in the array. -- ------------------------------------------------------------------------------- Konrad Hinsen | E-Mail: hinsen@ibs.ibs.fr Laboratoire de Dynamique Moleculaire | Tel.: +33-4.76.88.99.28 Institut de Biologie Structurale | Fax: +33-4.76.88.54.94 41, av. des Martyrs | Deutsch/Esperanto/English/ 38027 Grenoble Cedex 1, France | Nederlands/Francais ------------------------------------------------------------------------------- _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From Zachary_Roadhouse@brown.edu Mon May 5 15:35:13 1997 From: Zachary_Roadhouse@brown.edu (Zachary_Roadhouse@brown.edu) Date: Mon, 05 May 1997 10:35:13 -0400 (EDT) Subject: [PYTHON MATRIX-SIG] NumPy and PIL extension module In-Reply-To: <199705051207.OAA20769@lmspc2.ibs.fr> Message-ID: On 05-May-97 Konrad Hinsen wrote: >*Why* do you want to store images in arrays? Which array operations >do you expect to use? Depending on the answer, you might need different >arrangements of data in the array. Most of what I need the data for will probably require floating point values -- this isn't really the big issue for me though. Could someone post how to access and change an array element in an efficient manner in C (ie. not a lot of Python overhead)? The code in multiarraymodule isn't well commented so I don't really understand what is going on. - Zack E-MAIL: Zachary_Roadhouse@brown.edu WEB: http://althor.netspace.org/~zack/ Brown University, Box 220, Providence, RI 02912 Phone: (401) 863 - 5435 _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From hinsen@ibs.ibs.fr Mon May 5 15:32:34 1997 From: hinsen@ibs.ibs.fr (Konrad Hinsen) Date: Mon, 5 May 1997 16:32:34 +0200 Subject: [PYTHON MATRIX-SIG] NumPy and PIL extension module In-Reply-To: Message-ID: <199705051432.QAA21306@lmspc2.ibs.fr> > Most of what I need the data for will probably require floating point > values -- this isn't really the big issue for me though. Could someone > post how to access and change an array element in an efficient manner in C > (ie. not a lot of Python overhead)? The code in multiarraymodule isn't > well commented so I don't really understand what is going on. If you have to access an arbitrary array, things become a bit complicated. It is much easier to deal with contiguous arrays. A newly created array is always contiguous, for example, as are arrays that are the result of any computation. Only indexing and other structural operations create non-contiguous arrays. You can ensure that you are dealing with a contiguous array by calling PyArray_ContiguousFromObject(). This function will return a contiguous array unchanged, and return a contiguous copy for a non-contiguous array. Only if you can't affort the overhead of a potential copy you should try to deal with arbitrary arrays - in which case I can only wish you good luck ;-) Now for accessing contiguous arrays. If "array" is a variable of type PyArrayObject, then array->data points to its data space. You will have to cast this to the correct pointer type according to the type of the elements. The data is organized just like in a statically allocated C array, i.e. the last index varies fastest. So all you need is the information about the shape to be able to access anything you want. And that's easy: the number of dimensions is array->nd, and the list of dimensions is pointed to by array->dimensions. I expect that eventually we'll want better C API support for accessing arrays, but I suppose it won't happen soon... -- ------------------------------------------------------------------------------- Konrad Hinsen | E-Mail: hinsen@ibs.ibs.fr Laboratoire de Dynamique Moleculaire | Tel.: +33-4.76.88.99.28 Institut de Biologie Structurale | Fax: +33-4.76.88.54.94 41, av. des Martyrs | Deutsch/Esperanto/English/ 38027 Grenoble Cedex 1, France | Nederlands/Francais ------------------------------------------------------------------------------- _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From viennet@ura1507.univ-paris13.fr Mon May 5 21:47:56 1997 From: viennet@ura1507.univ-paris13.fr (viennet@ura1507.univ-paris13.fr) Date: Mon, 5 May 1997 21:47:56 +0100 (WET DST) Subject: [PYTHON MATRIX-SIG] NumPy and PIL extension module In-Reply-To: References: <199705051207.OAA20769@lmspc2.ibs.fr> Message-ID: <199705052047.VAA17306@montana.univ-paris13.fr> Zachary Roadhouse writes: > > Most of what I need the data for will probably require floating point > values -- this isn't really the big issue for me though. Could someone > post how to access and change an array element in an efficient manner in C > (ie. not a lot of Python overhead)? The code in multiarraymodule isn't > well commented so I don't really understand what is going on. > Not well commented, but very readable, a really good job :-) I'll try to answer, with two useless but illustrative examples: - First, your function get a Python object using : PyArg_ParseTuple( args, "O", &obj ) - Then, there is two way to get an array in C: 1- PyArray_ContiguousFromObject( obj, type, mindim, maxdim ) will try to convert the object to an array of the specified type. It returns a contiguous COPY of the array. This is the most generic and easy to use way, but it is not suitable if you want to modify in place an array. Example: to compute the sum of the elements of an array of floats. /* stupid example: computes the sum of an array */ static PyObject *py_arraysum( PyObject *self, PyObject *args ) { PyObject *obj; PyArrayObject *arr; int nelem, i; double *ptr, sum = 0; if (!PyArg_ParseTuple( args, "O" , &obj )) return NULL; /* Get array (contiguous) */ arr = (PyArrayObject *)PyArray_ContiguousFromObject(obj,PyArray_DOUBLE,1,1); if (!arr) { PYSETERROR( "can't get contiguous Float array"); return NULL; } /* Computes sum */ nelem = arr->dimensions[0]; ptr = (double *)arr->data; /* pointer on first elem */ for (i=0; i < nelem; i++) sum += *(ptr++); Py_DECREF(arr); /* release our copy */ return Py_BuildValue("f", sum ); } Note that this function works for arrays, but also for lists and tuples, which are converted. 2- The other solution is to deal directly with the array object, without asking NumPy to do the conversion. The usual situation is when you know in advance the type of the array you will get, and want to avoid any duplication of the data (big array for instance), or you want to modify the array in place. The main problem is that a NumPy array is not always contiguous; the stride[] attribute gives the number of bytes between to consecutive rows. /* another useless example: set an non-contiguous 2D BYTE array to zero */ static PyObject *py_arrayzero( PyObject *self, PyObject *args ) { PyObject *obj; PyArrayObject *arr; int i, j; unsigned char *ptr; if (!PyArg_ParseTuple( args, "O" , &obj )) return NULL; /* Check that obj is really an 2D array of bytes */ if (!PyArray_Check(obj)) { PYSETERROR("first argument is not an array"); return NULL; } /* check type (could also use arr->descr->type_num) */ if (PyArray_ObjectType(obj,0) != PyArray_UBYTE) { PYSETERROR("incorrect array type"); return NULL; } arr = (PyArrayObject *)obj; if (arr->nd != 2) { /* we are really strict ! */ PYSETERROR("incorrect number of dims"); return NULL; } /* Set elems to zero */ ptr = (unsigned char *)arr->data; for (i=0; i < arr->dimensions[0]; i++) { for (j=0; j < arr->dimensions[1]; j++) ptr[j] = 0; ptr += arr->strides[0]; /* next row */ } Py_INCREF(Py_None); return Py_None; /* arr has been modified in place, we return None */ } This function works efficiently for 2D byte arrays, and nothing else. This approach is rarely needed, except for truly large arrays. It is often better to create a new array and return it. The user writes A = myfunction(A) and not myfunction(A) # modify A in place I hope to have answered your question. Emmanuel -- Emmanuel Viennet: LIPN - Institut Galilee - Universite Paris-Nord 93430 Villetaneuse - France _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From jbaddor@phy.ulaval.ca Tue May 6 00:39:32 1997 From: jbaddor@phy.ulaval.ca (Jean-Bernard ADDOR) Date: Mon, 5 May 1997 19:39:32 -0400 (EDT) Subject: [PYTHON MATRIX-SIG] NumPy and PIL extension module In-Reply-To: <199705051432.QAA21306@lmspc2.ibs.fr> Message-ID: If I understand right, array are not accessed throught iterators ? A bientot, Jean-Bernard On Mon, 5 May 1997, Konrad Hinsen wrote: > > Most of what I need the data for will probably require floating point > > values -- this isn't really the big issue for me though. Could someone > > post how to access and change an array element in an efficient manner in C > > (ie. not a lot of Python overhead)? The code in multiarraymodule isn't > > well commented so I don't really understand what is going on. > > If you have to access an arbitrary array, things become a bit > complicated. It is much easier to deal with contiguous arrays. > A newly created array is always contiguous, for example, as are > arrays that are the result of any computation. Only indexing and > other structural operations create non-contiguous arrays. > > You can ensure that you are dealing with a contiguous array by calling > PyArray_ContiguousFromObject(). This function will return a contiguous > array unchanged, and return a contiguous copy for a non-contiguous > array. Only if you can't affort the overhead of a potential copy > you should try to deal with arbitrary arrays - in which case I > can only wish you good luck ;-) > > Now for accessing contiguous arrays. If "array" is a variable > of type PyArrayObject, then array->data points to its data space. > You will have to cast this to the correct pointer type according > to the type of the elements. The data is organized just like in > a statically allocated C array, i.e. the last index varies fastest. > > So all you need is the information about the shape to be able to > access anything you want. And that's easy: the number of dimensions is > array->nd, and the list of dimensions is pointed to by > array->dimensions. > > I expect that eventually we'll want better C API support for > accessing arrays, but I suppose it won't happen soon... > -- > ------------------------------------------------------------------------------- > Konrad Hinsen | E-Mail: hinsen@ibs.ibs.fr > Laboratoire de Dynamique Moleculaire | Tel.: +33-4.76.88.99.28 > Institut de Biologie Structurale | Fax: +33-4.76.88.54.94 > 41, av. des Martyrs | Deutsch/Esperanto/English/ > 38027 Grenoble Cedex 1, France | Nederlands/Francais > ------------------------------------------------------------------------------- > > _______________ > 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 _______________ From Zachary_Roadhouse@brown.edu Tue May 6 03:01:01 1997 From: Zachary_Roadhouse@brown.edu (Zachary_Roadhouse@brown.edu) Date: Mon, 05 May 1997 22:01:01 -0400 (EDT) Subject: [PYTHON MATRIX-SIG] NumPy and PIL extension module In-Reply-To: <199705052047.VAA17306@montana.univ-paris13.fr> Message-ID: Okay, taking a crack at it. Does this do what I want it too (copy the data from the image which is char** to the array which is int*)? array = PyArray_FromDims(2,dims,PyArray_INT); i_ptr = (int*)array->data; for (yy = 0; yy < array->dimensions[0]; ++yy) { for(xx = 0; xx < array->dimensions[1]; ++xx) ptr[xx] = im->image8[yy][xx]; ptr += array->dimensions[1]; } return PyArray_Return(array); - Zack E-MAIL: Zachary_Roadhouse@brown.edu WEB: http://althor.netspace.org/~zack/ Brown University, Box 220, Providence, RI 02912 Phone: (401) 863 - 5435 _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From viennet@ura1507.univ-paris13.fr Tue May 6 08:50:41 1997 From: viennet@ura1507.univ-paris13.fr (viennet@ura1507.univ-paris13.fr) Date: Tue, 6 May 1997 08:50:41 +0100 (WET DST) Subject: [PYTHON MATRIX-SIG] NumPy and PIL extension module In-Reply-To: References: <199705052047.VAA17306@montana.univ-paris13.fr> Message-ID: <199705060750.IAA17611@montana.univ-paris13.fr> Zachary Roadhouse writes: > Okay, taking a crack at it. Does this do what I want it too (copy the > data from the image which is char** to the array which is int*)? > > array = PyArray_FromDims(2,dims,PyArray_INT); > > i_ptr = (int*)array->data; > > for (yy = 0; yy < array->dimensions[0]; ++yy) { > for(xx = 0; xx < array->dimensions[1]; ++xx) > ptr[xx] = im->image8[yy][xx]; > ptr += array->dimensions[1]; > } > > return PyArray_Return(array); At first glance, this seems ok. But why don't you use UCHAR typecode ? NumPy will later automagically convert an UCHAR array to INT if necessary. Emmanuel -- Emmanuel Viennet: LIPN - Institut Galilee - Universite Paris-Nord 93430 Villetaneuse - France _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From hinsen@ibs.ibs.fr Tue May 6 09:03:40 1997 From: hinsen@ibs.ibs.fr (Konrad Hinsen) Date: Tue, 6 May 1997 10:03:40 +0200 Subject: [PYTHON MATRIX-SIG] NumPy and PIL extension module In-Reply-To: (message from Jean-Bernard ADDOR on Mon, 5 May 1997 19:39:32 -0400 (EDT)) Message-ID: <199705060803.KAA24863@lmspc2.ibs.fr> > If I understand right, array are not accessed throught iterators ? Not at the C level, where there are no iterators! -- ------------------------------------------------------------------------------- Konrad Hinsen | E-Mail: hinsen@ibs.ibs.fr Laboratoire de Dynamique Moleculaire | Tel.: +33-4.76.88.99.28 Institut de Biologie Structurale | Fax: +33-4.76.88.54.94 41, av. des Martyrs | Deutsch/Esperanto/English/ 38027 Grenoble Cedex 1, France | Nederlands/Francais ------------------------------------------------------------------------------- _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From hinsen@ibs.ibs.fr Wed May 7 15:29:58 1997 From: hinsen@ibs.ibs.fr (Konrad Hinsen) Date: Wed, 7 May 1997 16:29:58 +0200 Subject: [PYTHON MATRIX-SIG] Array -> String Message-ID: <199705071429.QAA01790@lmspc2.ibs.fr> Has anyone found an efficient method to convert a character array into a string? The only solution I could come up with is string = reduce(operator.add, array) which is of course terribly inefficient. -- ------------------------------------------------------------------------------- Konrad Hinsen | E-Mail: hinsen@ibs.ibs.fr Laboratoire de Dynamique Moleculaire | Tel.: +33-4.76.88.99.28 Institut de Biologie Structurale | Fax: +33-4.76.88.54.94 41, av. des Martyrs | Deutsch/Esperanto/English/ 38027 Grenoble Cedex 1, France | Nederlands/Francais ------------------------------------------------------------------------------- _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From hinsen@ibs.ibs.fr Wed May 7 15:29:58 1997 From: hinsen@ibs.ibs.fr (Konrad Hinsen) Date: Wed, 7 May 1997 16:29:58 +0200 Subject: [PYTHON MATRIX-SIG] Array -> String Message-ID: <199705071429.QAA01790@lmspc2.ibs.fr> Has anyone found an efficient method to convert a character array into a string? The only solution I could come up with is string = reduce(operator.add, array) which is of course terribly inefficient. -- ------------------------------------------------------------------------------- Konrad Hinsen | E-Mail: hinsen@ibs.ibs.fr Laboratoire de Dynamique Moleculaire | Tel.: +33-4.76.88.99.28 Institut de Biologie Structurale | Fax: +33-4.76.88.54.94 41, av. des Martyrs | Deutsch/Esperanto/English/ 38027 Grenoble Cedex 1, France | Nederlands/Francais ------------------------------------------------------------------------------- _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From emmanuel.viennet@ura1507.univ-paris13.fr Wed May 7 16:45:05 1997 From: emmanuel.viennet@ura1507.univ-paris13.fr (Emmanuel Viennet) Date: Wed, 7 May 1997 16:45:05 +0100 (WET DST) Subject: [PYTHON MATRIX-SIG] Array -> String In-Reply-To: <199705071429.QAA01790@lmspc2.ibs.fr> References: <199705071429.QAA01790@lmspc2.ibs.fr> Message-ID: <199705071545.QAA19464@montana.univ-paris13.fr> Konrad Hinsen writes: > Has anyone found an efficient method to convert a character array into > a string? The only solution I could come up with is > > string = reduce(operator.add, array) > Can't you use the .tostring() method on array objects ? -- Emmanuel Viennet: LIPN - Institut Galilee - Universite Paris-Nord 93430 Villetaneuse - France _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From hugunin@mit.edu Wed May 7 15:54:25 1997 From: hugunin@mit.edu (Jim Hugunin) Date: Wed, 7 May 1997 10:54:25 -0400 Subject: [PYTHON MATRIX-SIG] Array -> String Message-ID: <9705071454.AA10371@goldilocks> > Has anyone found an efficient method to convert a character array into > a string? The only solution I could come up with is > > string = reduce(operator.add, array) > > which is of course terribly inefficient. array.tostring() doesn't work? -Jim _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From jim.fulton@digicool.com Wed May 7 16:03:38 1997 From: jim.fulton@digicool.com (Jim Fulton) Date: Wed, 07 May 1997 11:03:38 -0400 Subject: [PYTHON MATRIX-SIG] Array -> String References: <199705071429.QAA01790@lmspc2.ibs.fr> Message-ID: <337099CA.6966@digicool.com> Konrad Hinsen wrote: > > Has anyone found an efficient method to convert a character array into > a string? The only solution I could come up with is > > string = reduce(operator.add, array) > > which is of course terribly inefficient. How about string=string.join(list(array),'') Of course, ot would be nicer of string.join accepted any type of sequence. Jim -- Jim Fulton Digital Creations jim@digicool.com 540.371.6909 ## Python is my favorite language ## ## http://www.python.org/ ## _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From hinsen@ibs.ibs.fr Wed May 7 16:57:42 1997 From: hinsen@ibs.ibs.fr (Konrad Hinsen) Date: Wed, 7 May 1997 17:57:42 +0200 Subject: [PYTHON MATRIX-SIG] Array -> String In-Reply-To: <199705071545.QAA19464@montana.univ-paris13.fr> (message from Emmanuel Viennet on Wed, 7 May 1997 16:45:05 +0100 (WET DST)) Message-ID: <199705071557.RAA02239@lmspc2.ibs.fr> Emmanuel Viennet wrote: > > Has anyone found an efficient method to convert a character array into > > a string? The only solution I could come up with is > > > > string = reduce(operator.add, array) > > > > Can't you use the .tostring() method on array objects ? I guess I can, meaning it seems to work. But in my mind this is a low-level function meant for I/O etc.; I am not supposed to make any assumptions about the internal data organization. Of course in the case of strings it is hard to see why the internal arrangement should be anything else than a sequence of characters, but you never know... Jim Fulton wrote: > How about > > string=string.join(list(array),'') > > Of course, ot would be nicer of string.join accepted any type of > sequence. I am not sure this is more efficient than my original solution. The list must be constructed explicitly first, and string.join need not be much better than reduce(operator.add,...). Just in case anyone wonders *why* I am doing this: I need to store long string in netCDF files, which have no special string type, just arrays of characters. And those strings can easily reach a length of 500000! Konrad. -- ------------------------------------------------------------------------------- Konrad Hinsen | E-Mail: hinsen@ibs.ibs.fr Laboratoire de Dynamique Moleculaire | Tel.: +33-4.76.88.99.28 Institut de Biologie Structurale | Fax: +33-4.76.88.54.94 41, av. des Martyrs | Deutsch/Esperanto/English/ 38027 Grenoble Cedex 1, France | Nederlands/Francais ------------------------------------------------------------------------------- _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From rburnham@cri-inc.com Fri May 9 17:02:36 1997 From: rburnham@cri-inc.com (Roger Burnham) Date: Wed, 7 May 1997 14:02:36 -5000 Subject: [PYTHON MATRIX-SIG] Numeric.clip oddity Message-ID: <199705071802.AA24074@world.std.com> Hi, I'm working with 2D arrays and found this: # mag.typecode() =='b' # delta.typecode() == 'd' ret = mag - delta minV = Numeric.minimum.reduce(Numeric.minimum.reduce(ret)) maxV = Numeric.maximum.reduce(Numeric.maximum.reduce(ret)) print 'before min/max', minV, maxV ret = Numeric.clip(ret+0.5, 0.0, 255.0) minV = Numeric.minimum.reduce(Numeric.minimum.reduce(ret)) maxV = Numeric.maximum.reduce(Numeric.maximum.reduce(ret)) print 'after min/max', minV, maxV outputs: before min/max -7.1836 234.0 after min/max 0.0 255.0 while clipping with ret = Numeric.clip((ret+0.5).astype(Numeric.Int), 0, 255) outputs what you would expect: before min/max -7.1836 234.0 after min/max 0 234 As this is part of a computation that produces an image, I'm able to see that in fact some of the pixels that should have came out near 0, are coming out of the 'clip' at 255. Is this a bug, or more probably, a flaw in my understanding? Thanks, Roger Burnham Cambridge Research & Instrumentation 21 Erie Street Cambridge, MA 02139 rburnham@cri-inc.com _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From jbaddor@phy.ulaval.ca Thu May 8 00:38:24 1997 From: jbaddor@phy.ulaval.ca (Jean-Bernard ADDOR) Date: Wed, 7 May 1997 19:38:24 -0400 (EDT) Subject: [PYTHON MATRIX-SIG] Array -> String: array length In-Reply-To: <199705071557.RAA02239@lmspc2.ibs.fr> Message-ID: I am very happy to hear about the length of the numpy arrays other users uses because I uses these day pow(2,22) = len(array) = 4194304 and I seriously ask me questions about python memory managment. My program uses hundreds Megs of RAM. My question is how many memory uses a Numeric.array(pow(2,22), Numeric.Float) is it much more than pow(2,24) bytes ? A bientot, Jean-Bernard On Wed, 7 May 1997, Konrad Hinsen wrote: > Just in case anyone wonders *why* I am doing this: I need to store > long string in netCDF files, which have no special string type, just > arrays of characters. And those strings can easily reach a length of > 500000! > > Konrad. > -- > ------------------------------------------------------------------------------- > Konrad Hinsen | E-Mail: hinsen@ibs.ibs.fr > Laboratoire de Dynamique Moleculaire | Tel.: +33-4.76.88.99.28 > Institut de Biologie Structurale | Fax: +33-4.76.88.54.94 > 41, av. des Martyrs | Deutsch/Esperanto/English/ > 38027 Grenoble Cedex 1, France | Nederlands/Francais > ------------------------------------------------------------------------------- > > _______________ > 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 _______________ From viennet@ura1507.univ-paris13.fr Thu May 8 12:03:27 1997 From: viennet@ura1507.univ-paris13.fr (viennet@ura1507.univ-paris13.fr) Date: Thu, 8 May 1997 12:03:27 +0100 (WET DST) Subject: [PYTHON MATRIX-SIG] Array -> String: array length In-Reply-To: References: <199705071557.RAA02239@lmspc2.ibs.fr> Message-ID: <199705081103.MAA00663@montana.univ-paris13.fr> Jean-Bernard ADDOR writes: > > I am very happy to hear about the length of the numpy arrays other users > uses because I uses these day pow(2,22) = len(array) = 4194304 and I > seriously ask me questions about python memory managment. My program uses > hundreds Megs of RAM. My question is how many memory uses a > Numeric.array(pow(2,22), Numeric.Float) is it much more than pow(2,24) > bytes ? No. You mean: x = zeros( pow(2,22), Float ) which takes exactly 8 x 2^22 = 32Mo. I frequently use NumPy with large arrays (like 10^5 x 256 matrix). One must be aware of the following points: - there is currently no way to create an unitialized array; - operations like x = x + b allocate temporary arrays for intermediate results. ufuncs (add, multiply) an be called with a third argument specifying the destination array: the form add( x, b, x ) will avoid unnecessary memory allocations. - I/O usualy involve duplication of the data (conversion from/to strings). A few months ago, there were interesting discussions about a general "buffer" mecanism, but nobody took the time to implement it. - If you computes with float values, you may want to use the Float32 typecode (single precision) to save space (in this case, be careful with automatic conversions towards double precision during operation with scalar values). Bonne chance Emmanuel -- Emmanuel Viennet: LIPN - Institut Galilee - Universite Paris-Nord 93430 Villetaneuse - France _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From hugunin@mit.edu Thu May 8 17:55:41 1997 From: hugunin@mit.edu (Jim Hugunin) Date: Thu, 8 May 1997 12:55:41 -0400 Subject: [PYTHON MATRIX-SIG] Array -> String: array length Message-ID: <9705081655.AA13523@goldilocks> > I am very happy to hear about the length of the numpy arrays other users > uses because I uses these day pow(2,22) = len(array) = 4194304 and I > seriously ask me questions about python memory managment. My program uses > hundreds Megs of RAM. My question is how many memory uses a > Numeric.array(pow(2,22), Numeric.Float) is it much more than pow(2,24) > bytes ? A length 2**22 (notice the use of Python 1.4's lovely new notation ;-) array of floats will take up about (2**24 + 44) bytes. That 44 is a constant that is independent of the size of the array (and very weakly dependent on the number of dimensions in it). So, for any reasonably large array, the additional memory needed beyond the basic data is completely negligible. -Jim _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From jim.fulton@digicool.com Wed May 7 16:03:38 1997 From: jim.fulton@digicool.com (Jim Fulton) Date: Wed, 07 May 1997 11:03:38 -0400 Subject: [PYTHON MATRIX-SIG] Array -> String References: <199705071429.QAA01790@lmspc2.ibs.fr> Message-ID: <337099CA.6966@digicool.com> Konrad Hinsen wrote: > > Has anyone found an efficient method to convert a character array into > a string? The only solution I could come up with is > > string = reduce(operator.add, array) > > which is of course terribly inefficient. How about string=string.join(list(array),'') Of course, ot would be nicer of string.join accepted any type of sequence. Jim -- Jim Fulton Digital Creations jim@digicool.com 540.371.6909 ## Python is my favorite language ## ## http://www.python.org/ ## _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From hinsen@ibs.ibs.fr Mon May 5 13:07:42 1997 From: hinsen@ibs.ibs.fr (Konrad Hinsen) Date: Mon, 5 May 1997 14:07:42 +0200 Subject: [PYTHON MATRIX-SIG] NumPy and PIL extension module In-Reply-To: Message-ID: <199705051207.OAA20769@lmspc2.ibs.fr> > I'm running into problems in figuring out what type I should use for the > array and how to set the (x,y,colorplanes...) in the array. Here is what > I have so far: *Why* do you want to store images in arrays? Which array operations do you expect to use? Depending on the answer, you might need different arrangements of data in the array. -- ------------------------------------------------------------------------------- Konrad Hinsen | E-Mail: hinsen@ibs.ibs.fr Laboratoire de Dynamique Moleculaire | Tel.: +33-4.76.88.99.28 Institut de Biologie Structurale | Fax: +33-4.76.88.54.94 41, av. des Martyrs | Deutsch/Esperanto/English/ 38027 Grenoble Cedex 1, France | Nederlands/Francais ------------------------------------------------------------------------------- _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From Zachary_Roadhouse@brown.edu Sun May 4 04:57:05 1997 From: Zachary_Roadhouse@brown.edu (Zachary_Roadhouse@brown.edu) Date: Sat, 03 May 1997 23:57:05 -0400 (EDT) Subject: [PYTHON MATRIX-SIG] NumPy and PIL extension module Message-ID: Hello! With exams nearly over, I am attempting to delve into writing Python extension modules. I would like to write a module nPIL which has two functions in it for converting between PyArrayObject and Imaging objects. array ImageToArray(image) image ArrayToImage(array) I have a module written in python that does what I'm asking but at the expense of converting to a string first. It also doesn't handle multi-banded images (just grey scale). I'm running into problems in figuring out what type I should use for the array and how to set the (x,y,colorplanes...) in the array. Here is what I have so far: static PyObject* ImageToArray(PyObject* args) { PyObject* op; Imaging im; /* The image can have up to three dimensions */ /* x,y and number of color planes */ int dims[3]; PyObject* array; UINT8 *p; /* Check to see that one arguement was passed in */ if (!PyArg_ParseTuple(args, "O", &op)) return NULL; /* Check to see if that object is an Imaging */ im = PyImaging_AsImaging(op); if (!im) return NULL; /* Set the dimensions of the array to be the */ /* x and y dimensions of the image */ dims[0] = im->xsize; dims[1] = im->ysize; /* single layer */ if(im->image8) { int value; array = PyArray_FromDims(2,dims,PyArray_INT); value = im->image8[y][x]; } /* multilayer */ else { if(im->bands == 3) { dims[2] = 3; array = PyArray_FromDims(3,dims,PyArray_INT); p = (UINT8*) &im->image32[y][x]; } } - Zack E-MAIL: Zachary_Roadhouse@brown.edu WEB: http://althor.netspace.org/~zack/ Brown University, Box 220, Providence, RI 02912 Phone: (401) 863 - 5435 _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From Robin.K.Friedrich@usahq.unitedspacealliance.com Fri May 9 13:52:42 1997 From: Robin.K.Friedrich@usahq.unitedspacealliance.com (Robin.K.Friedrich@usahq.unitedspacealliance.com) Date: Fri, 9 May 1997 07:52:42 -0500 Subject: [PYTHON STRING-SIG] Re: [PYTHON MATRIX-SIG] Array -> Str Message-ID: <000D23E4.1924@freedom.rsoc.rockwell.com> Well maybe your talking about the numeric's array and not python's original array module but what's wrong with using the array object method designed for that? charray.tostring() _________________ Reply Separator __________________ Subject: [PYTHON STRING-SIG] Re: [PYTHON MATRIX-SIG] Array -> String Author: Jim Fulton at INTERNET Date: 5/7/97 10:03 AM Konrad Hinsen wrote: > > Has anyone found an efficient method to convert a character array into > a string? The only solution I could come up with is > > string = reduce(operator.add, array) > > which is of course terribly inefficient. How about string=string.join(list(array),'') Of course, ot would be nicer of string.join accepted any type of sequence. Jim -- Jim Fulton Digital Creations jim@digicool.com 540.371.6909 ## Python is my favorite language ## ## http://www.python.org/ ## _______________ STRING-SIG - SIG for Enhanced String Processing in Python send messages to: string-sig@python.org administrivia to: string-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 _______________ From Robin.K.Friedrich@usahq.unitedspacealliance.com Fri May 9 13:52:42 1997 From: Robin.K.Friedrich@usahq.unitedspacealliance.com (Robin.K.Friedrich@usahq.unitedspacealliance.com) Date: Fri, 9 May 1997 07:52:42 -0500 Subject: [PYTHON STRING-SIG] Re: [PYTHON MATRIX-SIG] Array -> Str Message-ID: <000D23E4.1924@freedom.rsoc.rockwell.com> Well maybe your talking about the numeric's array and not python's original array module but what's wrong with using the array object method designed for that? charray.tostring() _________________ Reply Separator __________________ Subject: [PYTHON STRING-SIG] Re: [PYTHON MATRIX-SIG] Array -> String Author: Jim Fulton at INTERNET Date: 5/7/97 10:03 AM Konrad Hinsen wrote: > > Has anyone found an efficient method to convert a character array into > a string? The only solution I could come up with is > > string = reduce(operator.add, array) > > which is of course terribly inefficient. How about string=string.join(list(array),'') Of course, ot would be nicer of string.join accepted any type of sequence. Jim -- Jim Fulton Digital Creations jim@digicool.com 540.371.6909 ## Python is my favorite language ## ## http://www.python.org/ ## _______________ STRING-SIG - SIG for Enhanced String Processing in Python send messages to: string-sig@python.org administrivia to: string-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 _______________ From aaron_watters@msn.com Tue May 13 02:41:19 1997 From: aaron_watters@msn.com (aaron watters) Date: Tue, 13 May 97 01:41:19 UT Subject: [PYTHON MATRIX-SIG] opengl on win95? Message-ID: Trading sleep for catching up a bit. I looked at the opengl links on python.org and found only links to tk/opengl - whatever happened to opengl on win95 without tk? Any progress? I remember I got something from Dave Ascher once that was pretty fun... what's up? -- Aaron Watters _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From P.S.Craig@durham.ac.uk Tue May 13 14:59:33 1997 From: P.S.Craig@durham.ac.uk (P.S.Craig@durham.ac.uk) Date: Tue, 13 May 1997 14:59:33 +0100 (BST) Subject: [PYTHON MATRIX-SIG] array() Message-ID: <17400.199705131359@laplace> Hi all, I have been having some problems with the behaviour of the array function, and I wonder if anyone can suggest how get around them or how to patch NumPy. I have two situations where array does not do what I need: (1) I want to create an array of strings. Thus I would like to be able to do a = array(['a', 'bcd', 'fg', 'h'], 'O') and have a have shape (4,) with each element of a being one of the original strings. Unfortunately, what I get is a two dimensional array with each string being repeated several times. I have two ways to do what I want: a = array(['a', 'bcd', 'fg', 'h'], 'O')[:,0] or a = zeros(4, typecode='O') a[:] = ['a', 'bcd', 'fg', 'h'] Both work, but seem perverse to me. (2) I have two one-dimensional arrays of python objects a and b (same length) and I wish to create a one-dimensional array where each element is the list (or tuple) containing the corresponding pair of objects from a and b. Note that I want the one-dimensional array of lists and not the two-dimensional array of python objects. I can't seem to make this happen without a loop which is slow and lacking elegance. In case you wonder why, I wish to sort the one-d array, in other words to sort on more than one key. Suppose that b is a two-dimensional NumPy array with typecode 'O' and I wish to create a to be one-dimensional NumPy array with typecode 'O' and b[i] set to be a[i,:].tolist() (or equivalently a.tolist()[i]. You might expect the following to work: b = zeros(a.shape[0],typecode='O') b[:] = a.tolist() but it actually produces an array where each b[i] is a.tolist() I have a feeling that both of these problems could be solved by limiting the depth to which array() searches for array structure, but I haven't puzzled out a solution yet. So, any ideas? Any help much appreciated, Peter Craig #--------------------------------------------------------------------# | E-mail: P.S.Craig@durham.ac.uk Telephone: +44-91-3742376 (Work) | | Fax: +44-91-3747388 +44-91-3860448 (Home) | | | | WWW: http://fourier.dur.ac.uk:8000/stats/psc.html | | | | Snail: Peter Craig, Dept. of Math. Sciences, Univ. of Durham, | | South Road, Durham DH1 3LE, England | #--------------------------------------------------------------------# _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From rodgers@vs.lmco.com Tue May 13 15:11:44 1997 From: rodgers@vs.lmco.com (Kevin Rodgers (x39079, WT-07)) Date: Tue, 13 May 1997 09:11:44 -0500 Subject: [PYTHON MATRIX-SIG] opengl on win95? In-Reply-To: Your message of "Tue, 13 May 1997 01:41:19 GMT." Message-ID: <199705131411.JAA19950@daedalus> aaron_watters@msn.com said: > I looked at the opengl links on python.org and found only links to tk/ > opengl - whatever happened to opengl on win95 without tk? Any > progress? I remember I got something from Dave Ascher once that was > pretty fun... what's up? -- Aaron Watters I, too, am very interested in OpenGL on Windows{95,NT}, but as a port of the existing PyOpenGL (using the togl Tk widget developed by Brian Paul et al.). I have recently developed (well, still developing, but it's pretty useful as is) a flight visualization tool based on NumPy and PyOpenGL, on a Sun Ultra 2 using Mesa-2.1 as the OpenGL implementation (we haven't got an "official" OpenGL library yet). There exists the distinct probability that I will need to port this to WinNT and/or Win95 in the fairly near future (say 3-6 months). Anybody know if PyOpenGL (and, by implication) togl will be ported to Win{95,NT}? Enquiring minds want to know . . . -- Kevin Rodgers Lockheed Martin Vought Systems rodgers@vs.lmco.com "This one goes up to eleven." -- Nigel Tufnel ------------------------------------------------------------------------------- _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From hinsen@ibs.ibs.fr Tue May 13 16:43:12 1997 From: hinsen@ibs.ibs.fr (Konrad Hinsen) Date: Tue, 13 May 1997 17:43:12 +0200 Subject: [PYTHON MATRIX-SIG] array() In-Reply-To: <17400.199705131359@laplace> (P.S.Craig@durham.ac.uk) Message-ID: <199705131543.RAA01822@lmspc2.ibs.fr> > I have two ways to do what I want: > > a = array(['a', 'bcd', 'fg', 'h'], 'O')[:,0] > > or > > a = zeros(4, typecode='O') > a[:] = ['a', 'bcd', 'fg', 'h'] > > Both work, but seem perverse to me. I'd use the following: a = array(4*[None]) a[:] = ['a', 'bcd', 'fg', 'h'] Which of course is rather close to your second solution, but a bit clearer in my opinion. > (2) I have two one-dimensional arrays of python objects a and b (same length) > and I wish to create a one-dimensional array where each element is > the list (or tuple) containing the corresponding pair of objects > from a and b. Note that I want the one-dimensional array of lists > and not the two-dimensional array of python objects. I can't seem > to make this happen without a loop which is slow and lacking > elegance. In case you wonder why, I wish to sort the one-d array, > in other words to sort on more than one key. You'd be better off using standard lists for that purpose: l = map(None, a, b) l.sort() If you need to you can always convert back to an array at the end. -- ------------------------------------------------------------------------------- Konrad Hinsen | E-Mail: hinsen@ibs.ibs.fr Laboratoire de Dynamique Moleculaire | Tel.: +33-4.76.88.99.28 Institut de Biologie Structurale | Fax: +33-4.76.88.54.94 41, av. des Martyrs | Deutsch/Esperanto/English/ 38027 Grenoble Cedex 1, France | Nederlands/Francais ------------------------------------------------------------------------------- _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From dars@soton.ac.uk Tue May 13 21:18:14 1997 From: dars@soton.ac.uk (Dave Stinchcombe) Date: Tue, 13 May 1997 21:18:14 +0100 (BST) Subject: [PYTHON MATRIX-SIG] import Numeric Message-ID: <199705132018.VAA04274@oak.sucs.soton.ac.uk> Dear Folks, this may seem a really stupid query but here goes. When I use import Numeric, instead of from Numeric import * I get the following problem. >>> import Numeric >>> a = Numeric.array([1,2,3,4], Float) Traceback (innermost last): File "", line 1, in ? NameError: Float I get no such problem just using from Numeric import *. The versions of NumPy I have are NumPy-1.0b3 and NumPyLib-1.0b2. Can someone tell if I'm being dense or not please. Thanks Dave Stinchcombe _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From hochberg@wwa.com Tue May 13 22:20:05 1997 From: hochberg@wwa.com (Timothy A. Hochberg) Date: Tue, 13 May 1997 16:20:05 -0500 (CDT) Subject: [PYTHON MATRIX-SIG] import Numeric In-Reply-To: <199705132018.VAA04274@oak.sucs.soton.ac.uk> Message-ID: On Tue, 13 May 1997, Dave Stinchcombe wrote: > Dear Folks, > this may seem a really stupid query but here goes. > When I use import Numeric, instead of from Numeric import * > I get the following problem. > >>> import Numeric > >>> a = Numeric.array([1,2,3,4], Float) > Traceback (innermost last): > File "", line 1, in ? > NameError: Float > I get no such problem just using from Numeric import *. > The versions of NumPy I have are NumPy-1.0b3 and NumPyLib-1.0b2. > Can someone tell if I'm being dense or not please. Try Numeric.Float. (Float is defined in the Numeric module, so you need to access it explicitly if you don't import *). -tim _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From hochberg@wwa.com Tue May 13 21:10:25 1997 From: hochberg@wwa.com (Timothy A. Hochberg) Date: Tue, 13 May 1997 15:10:25 -0500 (CDT) Subject: [PYTHON MATRIX-SIG] array() In-Reply-To: <199705131543.RAA01822@lmspc2.ibs.fr> Message-ID: On Tue, 13 May 1997, Konrad Hinsen wrote: > > I have two ways to do what I want: > > > > a = array(['a', 'bcd', 'fg', 'h'], 'O')[:,0] > > > > or > > > > a = zeros(4, typecode='O') > > a[:] = ['a', 'bcd', 'fg', 'h'] > > > > Both work, but seem perverse to me. > > I'd use the following: > > a = array(4*[None]) > a[:] = ['a', 'bcd', 'fg', 'h'] a = array(['a', 'bcd', 'fg', 'h', None])[:-1] also works for people obsessed with doing things in 1 line....;) But, in the case of strings: I just got done looking at arrayobject.c, and I'm convinced this is unintentional, it looks like strings are not supposed to be treated as sequence types when making an object array. It seems as if discover_depth should be called as nd=discover_depth(s,99, type == PyArray_OBJECT ||type == 'O' ) rather than nd=discover_depth(s,99, type == PyArray_OBJECT) in Array_FromSequence. It's probably a result of string being treated one way sometimes, and another way other times that the string weirdness above appears. Note that a = array(['a', 'bcd', 'fg', 'h'], chr(10)) works as you'd expect right now given that chr(10) is PyArray_OBJECT. [SNIP] -tim _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From tom.schwaller@linux-magazin.de Wed May 14 11:49:37 1997 From: tom.schwaller@linux-magazin.de (Tom Schwaller) Date: Wed, 14 May 1997 12:49:37 +0200 Subject: [PYTHON MATRIX-SIG] opengl on win95 + Announcment PyOpenGL0.8a References: <199705131411.JAA19950@daedalus> Message-ID: <337998C1.43FFF0F5@linux-magazin.de> Kevin Rodgers (x39079, WT-07) wrote: > > aaron_watters@msn.com said: > > I looked at the opengl links on python.org and found only links to tk/ > > opengl - whatever happened to opengl on win95 without tk? Any > > progress? I remember I got something from Dave Ascher once that was > > pretty fun... what's up? -- Aaron Watters > > I, too, am very interested in OpenGL on Windows{95,NT}, but as a port of the > existing PyOpenGL (using the togl Tk widget developed by Brian Paul et al.). > I have recently developed (well, still developing, but it's pretty useful as > is) a flight visualization tool based on NumPy and PyOpenGL, on a Sun Ultra 2 > using Mesa-2.1 as the OpenGL implementation (we haven't got an "official" > OpenGL library yet). There exists the distinct probability that I will need > to port this to WinNT and/or Win95 in the fairly near future (say 3-6 months). > Anybody know if PyOpenGL (and, by implication) togl will be ported to > Win{95,NT}? Enquiring minds want to know . . . > -- > Kevin Rodgers Lockheed Martin Vought Systems rodgers@vs.lmco.com > "This one goes up to eleven." -- Nigel Tufnel Brian has the Win95/NT port on his todo-list, unfortunately I have no idea when that will happen. In the new 0.8a Alpha release (See Announcment below) I used the Togl 1.3 stuff and there are new problems with the transition tk4.2 - tk8.0 (Mike, some time to check that?), although also some new procedures for saving in tiff, ppm and postscript files. I suggest using SavePPM and using the pbm-utils. Please check the new alpha version.. Some other quastions: 1) Some time ago a person wrote about a ImageMagick Python module. Is that project still alive or will be somehow integrated in the PIL project? 2) The Perldl Project has some very interesting material http://www.aao.gov.au/local/www/kgb/perldl/ What do you think about it? ###################################################################### This is the 0.8a pre-release of the Python Opengl/Tk Module. (Alpha means really Alpha! There are some nasty bugs.) You can get it at http://www.python.de/src/PyOpenGL-0.8a.tgz It works and was testet with Tcl8.0/Tk8.0. Although some rezising problems where eliminated, there's a new Z-Buffer problem (a bug), which is very annyoying and needs to be fixed for the 0.8 release (please help me!!). Look at cone.py for example to see the problem. Another problem, which occured is that the actual 0.8a version of PyOpenGL which is based on Togl1.3 does not work with Tcl4.2/Tk7.6 anymore. :-( (Just use the old version and update opengl.c in that case) Now the good news. There are some new procedures in openglmodule.c which might be interesting: gl_SaveTiff: save current context in a ppm Image #define LIBTIFF 1 or -DLIBTIFF and add -ltiff if you want to use it gl_SavePPM : save current context in a ppm Image gl_SaveEPS : save current context in a EPS Image look at conesave.py for a simple example (the meaning of the parmateres in SaveTiff is explained in openglmodule.c): gl.SaveTiff("cone.tif", 0, 0, 0, 400, 400) gl.SavePPM("cone.ppm", 400, 400) gl.SaveEPS("cone.ps", 400, 400) Experimental OpenGL 1.1 stuff: VertexPointer, ColorPointer, DrawArrays and also glCallLists which was just a quick hack and needs to be reworked. Anybody out there, who has some time to look at that? Other people wanted other new procedures. Now its the moment to contribute them. I will add them for the next release. The example detest.py uses the delaunaymodule, which in included for convenience. The much more interesting 2D Triangulation module I wrote a long time ago was not updated to the actual NumPy distribution and needs to be fixed. Last but not least: Please tell us about projects using PyOpenGL. I'd like to include some new examples... ###################################################################### Tom _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From da@maigret.cog.brown.edu Fri May 23 05:23:40 1997 From: da@maigret.cog.brown.edu (David Ascher) Date: Fri, 23 May 1997 00:23:40 -0400 (EDT) Subject: [PYTHON MATRIX-SIG] opengl on win95? In-Reply-To: Message-ID: > I looked at the opengl links on python.org > and found only links to tk/opengl - whatever > happened to opengl on win95 without tk? > Any progress? I remember I got something > from Dave Ascher once that was pretty > fun... what's up? -- Aaron Watters How's this for a late reply... You can use OpenGL with GLUT on Win95 and NT (or at least you could a few months ago -- I haven't done a thing about it recently). But you always need *something* to manage the windows. GLUT on Win32 has recently been upgraded (in beta), and I may check it out sometime (how's that for non-committal?). --da _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From jbaddor@phy.ulaval.ca Mon May 26 20:15:56 1997 From: jbaddor@phy.ulaval.ca (Jean-Bernard ADDOR) Date: Mon, 26 May 1997 15:15:56 -0400 (EDT) Subject: [PYTHON MATRIX-SIG] printf(%.2f d) -> python Message-ID: Hi! What is the better approch to obtain in python the same result as printf(...) in c ? A bientot, Jean-Bernard _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From phil@geog.ubc.ca Mon May 26 21:01:28 1997 From: phil@geog.ubc.ca (Phil Austin) Date: Mon, 26 May 1997 13:01:28 -0700 Subject: [PYTHON MATRIX-SIG] plotting software In-Reply-To: <199705220639.BAA05980@tillamook-sharp.eaton.net> References: <199705211242.IAA08520@jigster> <199705220639.BAA05980@tillamook-sharp.eaton.net> Message-ID: <199705262001.NAA23212@curlew.geog.ubc.ca> I'm appending a message I just sent the octave mailing list (octave is a free matlab clone), in the hope that it will restart a thread that saw some discussion here a few weeks ago. There seem to be several groups casting about for portable plotting software. In a month or so I'll post an alpha of our package for comments, we (meaning two coop students--Belmont Cheung and Amy Wan) are writing a set of plotting classes that can be used with gnuplot, gist, or NCAR graphics. We're very interested in any other work going on in this direction. Phil >>>>> "JWE" == John W Eaton writes: JWE> There are plans to improve Octave's plotting JWE> capabilities. Unfortunately, I've not had much time to work JWE> on this project yet. JWE> I don't plan to use pgplot, because apparently it is only JWE> freely available for non-commercial use. JWE> A similar library that is distributed under the GPL is JWE> plplot, but I'm not sure that it is being actively maintained JWE> these days. JWE> I also know of several other plotting packages, each with its JWE> own set of stregths and weaknesses. JWE> In any case, I intend to make Octave's internal plotting JWE> interface relatively generic so that it will be possible for JWE> users to different plotting packages if they wish. I plan to JWE> provide at least one sample implementation, but I'm still JWE> undecided about which plotting package I will use. Lack of a good free plotting package is a sticking point for several numerically-oriented interpreted languages. There has been some discussion of this on the Numeric Python mailing list as well, and perhaps a group effort spanning Octave, Python, Perl data language, etc. could produce something. We'd like to see the most portable possible solution, and the Tk driver in Plplot shows promise in that direction, although the move of Geoff Furnish, the Plplot maintainer, from Livermore to Los Alamos may have derailed that (there was also some talk several months about about merging Plplot with gist, a free Livermore graphics package that runs on X under Yorick or Python). One person on the Python list is writing plotting software in Java to run under Python (see http://estel.uindy.edu/PESSci/ph280/) Phil Austin INTERNET: phil@geog.ubc.ca (604) 822-2175 FAX: (604) 822-6150 http://www.geog.ubc.ca/~phil Associate Professor Atmospheric Sciences Programme Geography #217 University of British Columbia 1984 W Mall Vancouver, BC V6T 1Z2 CANADA _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From jbaddor@phy.ulaval.ca Mon May 26 21:01:43 1997 From: jbaddor@phy.ulaval.ca (Jean-Bernard ADDOR) Date: Mon, 26 May 1997 16:01:43 -0400 (EDT) Subject: [PYTHON MATRIX-SIG] printf(%.2f d) -> python In-Reply-To: Message-ID: OK, it was on p.32 of python tutorial. print '%2d %3d %4d' % (x, x*x, x*x*x) Jean-Bernard On Mon, 26 May 1997, Jean-Bernard ADDOR wrote: > Hi! > > What is the better approch to obtain in python the same result as > printf(...) in c ? > > > A bientot, > > Jean-Bernard > > > > _______________ > 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 _______________ From hinsen@ibs.ibs.fr Mon May 26 21:05:16 1997 From: hinsen@ibs.ibs.fr (Konrad Hinsen) Date: Mon, 26 May 1997 22:05:16 +0200 Subject: [PYTHON MATRIX-SIG] printf(%.2f d) -> python In-Reply-To: (message from Jean-Bernard ADDOR on Mon, 26 May 1997 15:15:56 -0400 (EDT)) Message-ID: <199705262005.WAA12658@lmspc1.ibs.fr> > What is the better approch to obtain in python the same result as > printf(...) in c ? Formatting with %: print "%s: %d" % ("The answer is", 42) Konrad. -- ------------------------------------------------------------------------------- Konrad Hinsen | E-Mail: hinsen@ibs.ibs.fr Laboratoire de Dynamique Moleculaire | Tel.: +33-4.76.88.99.28 Institut de Biologie Structurale | Fax: +33-4.76.88.54.94 41, av. des Martyrs | Deutsch/Esperanto/English/ 38027 Grenoble Cedex 1, France | Nederlands/Francais ------------------------------------------------------------------------------- _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From jbaddor@phy.ulaval.ca Tue May 27 16:08:32 1997 From: jbaddor@phy.ulaval.ca (Jean-Bernard ADDOR) Date: Tue, 27 May 1997 11:08:32 -0400 (EDT) Subject: [PYTHON MATRIX-SIG] Segmentation fault (core dumped) Message-ID: Hi! Is that the intended behaviour of NumPy? Is my configuration right? (Do you observ the same?) Python 1.4 (Apr 12 1997) [GCC 2.7.2] Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> import Numeric >>> from Numeric import * >>> a = array([2,3]) >>> inputs = array([]) >>> inputs = concatenate(inputs, a) Segmentation fault (core dumped) A bientot, Jean-Bernard _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From hinsen@ibs.ibs.fr Tue May 27 17:36:49 1997 From: hinsen@ibs.ibs.fr (Konrad Hinsen) Date: Tue, 27 May 1997 18:36:49 +0200 Subject: [PYTHON MATRIX-SIG] Segmentation fault (core dumped) In-Reply-To: (message from Jean-Bernard ADDOR on Tue, 27 May 1997 11:08:32 -0400 (EDT)) Message-ID: <199705271636.SAA16368@lmspc1.ibs.fr> > Is that the intended behaviour of NumPy? No! A crash is never intended. > Is my configuration right? (Do you observ the same?) It doesn't crash for me, but the result is not right: >>> import Numeric >>> from Numeric import * >>> a = array([2,3]) >>> inputs = array([]) >>> inputs = concatenate(inputs, a) >>> inputs zeros((0,), 'c') Konrad. -- ------------------------------------------------------------------------------- Konrad Hinsen | E-Mail: hinsen@ibs.ibs.fr Laboratoire de Dynamique Moleculaire | Tel.: +33-4.76.88.99.28 Institut de Biologie Structurale | Fax: +33-4.76.88.54.94 41, av. des Martyrs | Deutsch/Esperanto/English/ 38027 Grenoble Cedex 1, France | Nederlands/Francais ------------------------------------------------------------------------------- _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From hinsen@ibs.ibs.fr Tue May 27 17:36:49 1997 From: hinsen@ibs.ibs.fr (Konrad Hinsen) Date: Tue, 27 May 1997 18:36:49 +0200 Subject: [PYTHON MATRIX-SIG] Segmentation fault (core dumped) In-Reply-To: (message from Jean-Bernard ADDOR on Tue, 27 May 1997 11:08:32 -0400 (EDT)) Message-ID: <199705271636.SAA16368@lmspc1.ibs.fr> > Is that the intended behaviour of NumPy? No! A crash is never intended. > Is my configuration right? (Do you observ the same?) It doesn't crash for me, but the result is not right: >>> import Numeric >>> from Numeric import * >>> a = array([2,3]) >>> inputs = array([]) >>> inputs = concatenate(inputs, a) >>> inputs zeros((0,), 'c') Konrad. -- ------------------------------------------------------------------------------- Konrad Hinsen | E-Mail: hinsen@ibs.ibs.fr Laboratoire de Dynamique Moleculaire | Tel.: +33-4.76.88.99.28 Institut de Biologie Structurale | Fax: +33-4.76.88.54.94 41, av. des Martyrs | Deutsch/Esperanto/English/ 38027 Grenoble Cedex 1, France | Nederlands/Francais ------------------------------------------------------------------------------- _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From hugunin@mit.edu Tue May 27 19:45:54 1997 From: hugunin@mit.edu (Jim Hugunin) Date: Tue, 27 May 1997 14:45:54 -0400 Subject: [PYTHON MATRIX-SIG] Segmentation fault (core dumped) Message-ID: <9705271846.AA07749@goldilocks> > > Is that the intended behaviour of NumPy? > > No! A crash is never intended. I'm always amazed at how courteous users of NumPy can be when reporting errors. A segmentation fault is always a bug, there's no need to try and spare my feelings ;-) > >>> a = array([2,3]) > >>> inputs = array([]) > >>> inputs = concatenate(inputs, a) As said above, this should not be seg faulting, I'll fix this for the next beta (available any day now). It should be throwing an exception telling you that your axis specification is not an integer. The way to get the behavior you seem to want is the following: inputs = concatenate( (inputs, a) ) Read the documentation on concatenate for more details... Hope this helps - Jim _______________ MATRIX-SIG - SIG on Matrix Math for Python send messages to: matrix-sig@python.org administrivia to: matrix-sig-request@python.org _______________ From phil@geog.ubc.ca Thu May 29 00:35:21 1997 From: phil@geog.ubc.ca (Phil Austin) Date: Wed, 28 May 1997 16:35:21 -0700 Subject: [PYTHON MATRIX-SIG] SWIG, NumPy and C++ templated arrays Message-ID: <199705282335.QAA29653@curlew.geog.ubc.ca> Someone on the SWIG mailing list asked about SWIG and numeric Python. I've put together an example of our initial attempt using MV++ arrays (see ftp://ftp.geog.ubc.ca/pub/swig/example.tar.gz (11 Kbytes)). I'm including below the README and interface files from the example. Any comments/questions appreciated. SWIG-python example for templated C++ arrays and the numerical python extension. Michael Cherkassoff (mcherk@geog.ubc.ca) and Phil Austin (phil@geog.ubc.ca). This directory contains example code which glues Python NumPy arrays to C++ templated arrays (all necessary headers from Roldan Poza's MV++ library are included). This approach currently works for us, and we will soon be using it wholesale as we move to either MV++'s replacement (http://math.nist.gov/tnt/lib/) or one of the new expression-template based array classes (like Blitz++ http://monet.uwaterloo.ca/blitz/oon.html, or A++ http://www.c3.lanl.gov/~dquinlan/A++P++.html ). Below I describe how the example works, but for the savants, we'd appreciate help with three unresolved issues with the interface file (plus, of course, any other comments on either the python or c++). 1) Because SWIG doesn't handle templates, we fool it with a second set of typedefs (see cutmod.i and cutmodfake.h)--is there a more elegant approach? 2) We were unable to 'do the right thing' in the typemap to go from $source to a PyArrayObject*. That is, in cutmod.i (see comments therein): aa=(PyArrayObject *)PyArray_ContiguousFromObject($source, PyArray_Float,1,1); gives a zero pointer, and so we resorted to: aa = (PyArrayObject *) $source; 3) We're still confused about reference counting. Should we be calling PyDECREF(aa) in cutmod.i? The code: Cutmod.cpp defines a C++ function "cut", which cuts data into a series of histogram bins. Cutmod.i gives the SWIG interface, and test.py contains a simple test. The cut function (modeled on cut in Splus) takes either two python lists or two arrays (data and bins), and returns a list of lists containing the indexes of the data elements that fit into the specified bin intervals. i.e. in Python, typing _______________ from Numeric import * import cutmod data=array( [10, 50, 24, 45, 70, 59, 21, 33, 44, 65, 13, 11, 99, 15, 14, 78, 93, 45, 18, 32, 37, 16, 18, 84, 75, 48, 33, 17, 61, 90]) bins =array( [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]) output=cutmod.cut(data,bins) _________________ produces: output [[0, 11, 10, 14, 13, 21, 27, 18, 22], [6, 2], [19, 7, 26, 20], [8, 3, 17, 25], [1, 5], [28, 9], [4, 24, 15], [23], [29, 16, 12]] to install, just untar the file and make with make cutmode.so invoke python and import test the output should be test.output tested only on Linux 2.0.27 using gcc 2.7.2.1 Appendix: For those unfamiliar with MV++: Here's the way I would implement cut in Python: def cut(data,bins): #input: data (numarray), bins (numarray) to cut data. #output: binlist (list of lists), indexes in each bin binlength=bins.shape[0] datalength=data.shape[0] #populate an empty list of lists binlist=[0]*(binlength-1) for i in range(len(binlist)): binlist[i]=[] index=range(bins.shape[0]) sortedIndex=argsort(data) i=0 #move through the bins, dropping the sorted indices in the appropriate bin for leftbinmarker in range(binlength-1): if i > (datalength-1): return binlist while data[sortedIndex[i]] < bins[leftbinmarker+1]: binlist[leftbinmarker].append(sortedIndex[i]) i=i+1 if i > (datalength-1): return binlist cutmod.cc is a straight translation of this code using MV++ vectors instead of lists, with a little extra error checking. ==> cutmod.i <== // Code for SWIG to generate wrapper for C++ function that // is to be called from Python. // On the Python's end the function takes two arguments, each of // them either 1-D NumPy array or a Python list of floats. It returns a // list of lists of integers. // On the C++ end the function takes two MV++ vectors of floats. // It returns a MV++ vector of vectors of integers. /* Here are the contents of cutmod.h: #include "mvmtp.h" typedef MV_Vector VectorInt; typedef MV_Vector VectorFloat; typedef MV_Vector VectorList; ******************************************************/ %module cutmod %{ #include "cutmod.h" // this is going to be included without #include "arrayobject.h" // changes into wrapper code %} %include "cutmodfake.h" // this is for SWIG to look at. // cutmod.h cannot be put here since // SWIG doesn't like templates. And without // something here it generates // PyArg_ParseTuple(args,"ss",&_obj0,&_obj1) // instead of desired // PyArg_ParseTuple(args,"OO",&_obj0,&_obj1) %typemap(python,in) VectorFloat { int i, size; enum TYPE {List, Array}; TYPE type; PyArrayObject * aa; float * bb; if (PyList_Check($source)) { size = PyList_Size($source); type = List; } else if (PyArray_Check($source)) { size = PyArray_Size($source); type = Array; aa = (PyArrayObject *) $source; // This is a hack, but it bb = (float *) aa->data; // works. The proper way: // with PyArray_ContiguousFromObject() does not. 8-( /* The proper code would have been: aa=(PyArrayObject *)PyArray_ContiguousFromObject($source, PyArray_Float,1,1); but this gives zero pointer for some reason obscure for me */ } else { PyErr_SetString(PyExc_TypeError, "neither List nor Array"); return NULL; } VectorFloat a(size); // Cannot declare $target since it is // already being done by SWIG. for (i=0; i Someone on the SWIG mailing list asked about SWIG and numeric Python. I've put together an example of our initial attempt using MV++ arrays (see ftp://ftp.geog.ubc.ca/pub/swig/example.tar.gz (11 Kbytes)). I'm including below the README and interface files from the example. Any comments/questions appreciated. SWIG-python example for templated C++ arrays and the numerical python extension. Michael Cherkassoff (mcherk@geog.ubc.ca) and Phil Austin (phil@geog.ubc.ca). This directory contains example code which glues Python NumPy arrays to C++ templated arrays (all necessary headers from Roldan Poza's MV++ library are included). This approach currently works for us, and we will soon be using it wholesale as we move to either MV++'s replacement (http://math.nist.gov/tnt/lib/) or one of the new expression-template based array classes (like Blitz++ http://monet.uwaterloo.ca/blitz/oon.html, or A++ http://www.c3.lanl.gov/~dquinlan/A++P++.html ). Below I describe how the example works, but for the savants, we'd appreciate help with three unresolved issues with the interface file (plus, of course, any other comments on either the python or c++). 1) Because SWIG doesn't handle templates, we fool it with a second set of typedefs (see cutmod.i and cutmodfake.h)--is there a more elegant approach? 2) We were unable to 'do the right thing' in the typemap to go from $source to a PyArrayObject*. That is, in cutmod.i (see comments therein): aa=(PyArrayObject *)PyArray_ContiguousFromObject($source, PyArray_Float,1,1); gives a zero pointer, and so we resorted to: aa = (PyArrayObject *) $source; 3) We're still confused about reference counting. Should we be calling PyDECREF(aa) in cutmod.i? The code: Cutmod.cpp defines a C++ function "cut", which cuts data into a series of histogram bins. Cutmod.i gives the SWIG interface, and test.py contains a simple test. The cut function (modeled on cut in Splus) takes either two python lists or two arrays (data and bins), and returns a list of lists containing the indexes of the data elements that fit into the specified bin intervals. i.e. in Python, typing _______________ from Numeric import * import cutmod data=array( [10, 50, 24, 45, 70, 59, 21, 33, 44, 65, 13, 11, 99, 15, 14, 78, 93, 45, 18, 32, 37, 16, 18, 84, 75, 48, 33, 17, 61, 90]) bins =array( [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]) output=cutmod.cut(data,bins) _________________ produces: output [[0, 11, 10, 14, 13, 21, 27, 18, 22], [6, 2], [19, 7, 26, 20], [8, 3, 17, 25], [1, 5], [28, 9], [4, 24, 15], [23], [29, 16, 12]] to install, just untar the file and make with make cutmode.so invoke python and import test the output should be test.output tested only on Linux 2.0.27 using gcc 2.7.2.1 Appendix: For those unfamiliar with MV++: Here's the way I would implement cut in Python: def cut(data,bins): #input: data (numarray), bins (numarray) to cut data. #output: binlist (list of lists), indexes in each bin binlength=bins.shape[0] datalength=data.shape[0] #populate an empty list of lists binlist=[0]*(binlength-1) for i in range(len(binlist)): binlist[i]=[] index=range(bins.shape[0]) sortedIndex=argsort(data) i=0 #move through the bins, dropping the sorted indices in the appropriate bin for leftbinmarker in range(binlength-1): if i > (datalength-1): return binlist while data[sortedIndex[i]] < bins[leftbinmarker+1]: binlist[leftbinmarker].append(sortedIndex[i]) i=i+1 if i > (datalength-1): return binlist cutmod.cc is a straight translation of this code using MV++ vectors instead of lists, with a little extra error checking. ==> cutmod.i <== // Code for SWIG to generate wrapper for C++ function that // is to be called from Python. // On the Python's end the function takes two arguments, each of // them either 1-D NumPy array or a Python list of floats. It returns a // list of lists of integers. // On the C++ end the function takes two MV++ vectors of floats. // It returns a MV++ vector of vectors of integers. /* Here are the contents of cutmod.h: #include "mvmtp.h" typedef MV_Vector VectorInt; typedef MV_Vector VectorFloat; typedef MV_Vector VectorList; ******************************************************/ %module cutmod %{ #include "cutmod.h" // this is going to be included without #include "arrayobject.h" // changes into wrapper code %} %include "cutmodfake.h" // this is for SWIG to look at. // cutmod.h cannot be put here since // SWIG doesn't like templates. And without // something here it generates // PyArg_ParseTuple(args,"ss",&_obj0,&_obj1) // instead of desired // PyArg_ParseTuple(args,"OO",&_obj0,&_obj1) %typemap(python,in) VectorFloat { int i, size; enum TYPE {List, Array}; TYPE type; PyArrayObject * aa; float * bb; if (PyList_Check($source)) { size = PyList_Size($source); type = List; } else if (PyArray_Check($source)) { size = PyArray_Size($source); type = Array; aa = (PyArrayObject *) $source; // This is a hack, but it bb = (float *) aa->data; // works. The proper way: // with PyArray_ContiguousFromObject() does not. 8-( /* The proper code would have been: aa=(PyArrayObject *)PyArray_ContiguousFromObject($source, PyArray_Float,1,1); but this gives zero pointer for some reason obscure for me */ } else { PyErr_SetString(PyExc_TypeError, "neither List nor Array"); return NULL; } VectorFloat a(size); // Cannot declare $target since it is // already being done by SWIG. for (i=0; i