[Matrix-SIG] thisishowi use PyArray_As2D in a sentence.

Les Schaffer godzilla@netmeg.net (Les Schaffer)
Sat, 30 Jan 1999 18:02:57 -0500 (EST)


Okay, passing 2D arrays to C extensions using a double ** pointer is easy using
PyArray_As2D.

Here is the typemap in case anyone else is wondering how to do this:

%typemap(python, in) double ** {
	 
	PyArrayObject *arr;    /* will point to our array object.      */
	PyObject ** op;        /* pointer to pointer to array object   */
                               /* cast as PyObject;                    */
			       /* (needed by PyArray_As2d).            */

	int d1, d2, type;      /* ints neeed by PyArray_As2D.          */
	char ***ptr;           /* need by PyArray_As2D.                */
                             
	double ** arrayPtr_2D; /* this is our double ** array pointer. */
                               /* C extensions can then access         */
                               /* arbitrary sized 2D arrays as         */
                               /* arrayPtr[i][j].                      */

	/* point ptr to the address of ArrayPtr_2D. */
	/* PyArray_As2D will then place in it a     */
        /* char **  pointer aimed at a set of       */
        /* pointers  to the rows of our array.      */
	ptr = (char ***) & arrayPtr_2D;

	/* here is our array object from caller	*/
	arr = (PyArrayObject *) $source;   

	/* point op at our array object pointer */
	op = (PyObject **)&arr;

	/* i use double almost always */
	type = (int) PyArray_DOUBLE;   

	/* now let PyArray_As2D re-arrange, if necessary (?),  */
	/* our array and set the pointers to the rows of array */
	PyArray_As2D( op, ptr, &d1, &d2, type); 
	
	/* done. hand arrayPtr2D to our C ext */
	$target = arrayPtr_2D; 
}


-- 
____    Dr. Les Schaffer              ___| --->> Engineering R&D <<---
Theoretical & Applied Mechanics          |  Designspring, Inc. 
Center for Radiophysics & Space Research |  http://www.designspring.com/
Cornell Univ.  schaffer@tam.cornell.edu  |  les@designspring.com