[MATRIX-SIG] SWIG, Numeric Python
Michael Bell
michaelb@gold.net.au
Fri, 15 Aug 97 08:54 WST
Hi Jens,
You wrote:
> Last Friday I began using Python for my data analysis work and in the meantime
> I've managed to interface my Fortran Code and a few NAG-Library Routines
> (nonlinear least square fitting, numerical integrations) with this wonderful
> language and the nice numeric package. For my further programming it would
> be useful to automate the interfacing. Unfortunately I didn't find any example
> with helper functions for SWIG and numeric python datatypes. Are there any
> packages around which I could look at? Any code fragment showing how to
> wrap double arrays and things like that would be useful for me to get an
> idea.
I use Python for data analysis, and I've recently wrapped some Fortran
code using SWIG. It's not pretty, but I've managed to get the job
done that I needed to.
Here is the first part of my SWIG interface file which wrapped the
PGPLOT graphing library. I've included the typemaps which handle two
dimensional NumPy arrays. I hope this is what you are looking for.
Remember, I said the code wasn't pretty!
cheers,
Michael
--
Michael Bell
Kalgoorlie Consolidated Gold Mines, Western Australia
"To know the laws that govern the winds, and to know that you know them, will
give you an easy mind on your voyage around the world; otherwise you may
tremble at the appearance of every cloud." Joshua Slocum
====================
%module pgplot
%{
#include "cpgplot.h"
#include "arrayobject.h"
%}
typedef int Logical;
%typemap(python, in) float* IN_2D {
PyObject *obj;
PyArrayObject *arr;
int i, j;
unsigned char *ptr;
/* Check that obj is really an 2D array of bytes */
if (!PyArray_Check($source)) {
PyErr_SetString(PyExc_TypeError,"First argument is not an array");
return NULL;
}
/* check type (could also use arr->descr->type_num) */
if (PyArray_ObjectType($source,0) != PyArray_FLOAT) {
PyErr_SetString(PyExc_TypeError,"Incorrect array type: we need an array of FLOAT16");
return NULL;
}
arr = (PyArrayObject *)$source;
if (arr->nd != 2) { /* we are really strict ! */
PyErr_SetString(PyExc_TypeError,"Incorrect number of dims: we want a 2d array");
return NULL;
}
$target = (float *)arr->data;
}
%typemap(python, in) float* IN_1D {
PyObject *obj;
PyArrayObject *arr;
int i, j;
unsigned char *ptr;
/* Check that obj is really a 1D array of bytes */
if (!PyArray_Check($source)) {
PyErr_SetString(PyExc_TypeError,"First argument is not an array");
return NULL;
}
/* check type (could also use arr->descr->type_num) */
if (PyArray_ObjectType($source,0) != PyArray_FLOAT) {
PyErr_SetString(PyExc_TypeError,"Incorrect array type: we need an array of FLOAT16");
return NULL;
}
arr = (PyArrayObject *)$source;
if (arr->nd != 1) { /* we are really strict ! */
PyErr_SetString(PyExc_TypeError,"Incorrect number of dims: we want a 13d array");
return NULL;
}
$target = (float *)arr->data;
}
Qaddafi plutonium Semtex Peking KGB Ortega Panama terrorist quiche
BATF Nazi Soviet Uzi Ft. Bragg NSA
_______________
MATRIX-SIG - SIG on Matrix Math for Python
send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
_______________