[PYTHON MATRIX-SIG] LAPACK module questions

James Hugunin jjh@Goldilocks.LCS.MIT.EDU
Tue, 13 Feb 96 13:32:24 EST


I'm probably the only one here who can answer these questions (yes I
know that documentation for the C API would be nice).

First a question.  Are you building this on top of Guido's bgen tool?
I really think that this is the right way to go.  Hopefully bgen can
be eventually turned into a nice generic tool for specifying
interfaces to any C/FORTRAN library.  It already handles a large
collection of interface specifications.  It has the very nice modular
design of one automatic part that tries to extract the interface
definition which produces interface definition files that can be
easily edited by the user for the cases where it guesses wrong.
Adding array objects to bgen would not be a whole lot of work (so many
things I'd like to do if I just had a free 10 hours or so).

I think that creating a raw C level binding to these libraries is the
right thing to do, and then to write a python module that provides a
nice friendly interface on top of the raw functions created in the
module.  The basic point here is that this should be doable without
writing any C code.

I'd be interested in seeing a copy of what you've done so far.

Arrays are not part of the abstract object interface.  The right way
to write a function that takes in a single argument and passes it on
to a C function expecting a 1d array of doubles and returning a 1d
array of doubles is as follows:

static PyObject *function(PyObject *ignored, PyObject *args) {
  PyArrayObject *ap, *apr;
  PyObject *op;
  int n;

  TRY(PyArg_ParseTuple(args, "O", &op));
  TRY(ap = (PyArrayObject *)PyArray_ContiguousFromObject(op, PyArray_DOUBLE, 1, 1));

  n = ap->dimensions[0];

  TRY(apr = (PyArrayObject *)PyArray_FromDims(1, &n, PyArray_DOUBLE));

  c_function((double *)ap->data, (double *)apr->data, n)  

  Py_DECREF(ap);  /* Needed because PyArray_ContiguousFromObject INCREFs it. */

  return (PyObject *)apr;
}

In fact, if there is an error in allocating the return array this will
leak memory, but I figure anytime you're getting exceptions during
array allocation, enough things are wrong with your system that a
little memory leak won't be a problem.

Hopefully from the general complexity of this you can see the value in
a bgen style approach.

-Jim


=================
MATRIX-SIG  - SIG on Matrix Math for Python

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