[Numpy-discussion] Error in NA_IoArray?

Paulo J. S. Silva pjssilva at ime.usp.br
Fri Nov 19 12:07:01 CST 2004


Hello,

I am using swig to wrap some numerical code. I have managed to make swig
understand and convert between numarrays and C arrays. However if I use
NA_IoArray to make the convertion and pass an integer array when a
double array is expected the changes are not copied back to the original
array. Let me give a "minimal" example.

When I want to wrap the following function:

void inc(int len, double *v) {
  int i;
  for (i = 0; i < len; i++) v[i]++;
}

Swig generates the following code, that looks right to me.

static PyObject *_wrap_inc(PyObject *self, PyObject *args) {
    PyObject *resultobj;
    int arg1 ;
    double *arg2 = (double *) 0 ;
    PyArrayObject *NAimg2 = NULL ;
    PyObject * obj1 = 0 ;
    
    if(!PyArg_ParseTuple(args,(char *)"iO:inc",&arg1,&obj1)) goto fail;
    {
        PyArrayObject *NAimg2  = NA_IoArray(obj1, tFloat64,
NUM_C_ARRAY);
        if (!NAimg2) {
            printf("**** no (double) numarray *****\n");
            SWIG_exception(SWIG_RuntimeError, "No double Array Found");
            return 0;
        }
        arg2 = (double *) NA_OFFSETDATA(NAimg2);
    }
    inc(arg1,arg2);
    
    Py_INCREF(Py_None); resultobj = Py_None;
    {
        Py_XDECREF(NAimg2);
    }
    return resultobj;
    fail:
    {
        Py_XDECREF(NAimg2);
    }
    return NULL;
}

However if I run the following python code:

>>> import test_na_ioarray
>>> from numarray import *
>>> v = zeros((5,))
>>> test_na_ioarray(5, v)
>>> print v
[0 0 0 0 0]

Shouldn't the resultant array be full of ones (when casting the Float64
array of ones to int)?

Thanks,

Paulo

Obs: The code runs fine if the original vector is already a Float64
array.





More information about the Numpy-discussion mailing list