Numeric Python and C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ognen Duzlevski

    Numeric Python and C

    Hi,
    can someone explain how to change a value within an array from a C function?

    For example, I have the following array:

    scoremat = zeros((10,10))

    and the following C function:

    PyObject *DynAlign(PyObj ect *self, PyObject *args)
    {
    PyArrayObject *p;
    int ok;

    /* get the array */
    ok = PyArg_ParseTupl e(args,"O!", &PyArray_Typ e, &p);
    /* this gets a value p[5][5] */
    v55 = *(double *)p->data + 5*p->strides[0] + 5*p->strides[1]);
    }

    How to I change the value at p[5][5] and retain it in the same array after function exit?

    So, from Python I might have:

    from Numeric import *

    scoremat = zeros((10,10))
    DynAlign(scorem at)
    print scoremat[5][5]

    Thank you,
    Ognen
  • Simon Burton

    #2
    Re: Numeric Python and C

    On Wed, 14 Jan 2004 19:17:55 +0000, Ognen Duzlevski wrote:
    [color=blue]
    > Hi,
    > can someone explain how to change a value within an array from a C
    > function?
    >[/color]

    What about using PyObject_GetIte m (is that what it's called?)

    I know numarray uses functions for this:

    PyArrayObject* NA_InputArray( PyObject *numarray, NumarrayType t, int requires)
    The purpose of NA_InputArray is to transfer array data from Pythonto C.

    PyArrayObject* NA_OutputArray( PyObject *numarray, NumarrayType t, int requires)
    The purpose of NA_OutputArray is to transfer data from C to Python. Practically speaking, the output numarray must be a PyArrayObject, and cannot be an arbitrary Python sequence.

    PyArrayObject* NA_IoArray( PyObject *numarray, NumarrayType t, int requires)
    NA_IoArray has fully bidirectional data transfer, creating the illusion of call-by-reference.


    Simon.

    Comment

    • John Hunter

      #3
      Re: Numeric Python and C

      >>>>> "Ognen" == Ognen Duzlevski <maketo@norge.f reeshell.org> writes:

      Ognen> Hi, can someone explain how to change a value within an
      Ognen> array from a C function?

      Warning: I am not a Numeric C API guru.

      For 1D arrays I use the following macro

      // get x[i] from a 1d array of type xtype
      #define get1d(x,i,xtype ) \
      *(xtype *)(x->data+i*x->strides[0])


      You can assign to the return value of this, as in

      count = 0;
      while ((row = mysql_fetch_row (res)) != NULL) {
      get1d(p,count,f loat) = atof(row[0]);
      get1d(v,count,i nt) = atoi(row[1]);
      ++count;
      }

      where p and v are 1D numeric arrays.

      You'll have to do a bit more work for 2D arrays, but this may speed
      you on your way....

      JDH

      Comment

      • Ognen Duzlevski

        #4
        Re: Numeric Python and C

        John Hunter <jdhunter@ace.b sd.uchicago.edu > wrote:[color=blue][color=green][color=darkred]
        >>>>>> "Ognen" == Ognen Duzlevski <maketo@norge.f reeshell.org> writes:[/color][/color][/color]
        [color=blue]
        > Ognen> Hi, can someone explain how to change a value within an
        > Ognen> array from a C function?[/color]
        [color=blue]
        > Warning: I am not a Numeric C API guru.[/color]
        [color=blue]
        > For 1D arrays I use the following macro[/color]
        [snipped][color=blue]
        > You'll have to do a bit more work for 2D arrays, but this may speed
        > you on your way....[/color]

        Hi, for 2D arrays it is much similar:

        get(ndx1,ndx2): array->data + ndx1*array->strides[0] + ndx2*array->strides[1]
        set(ndx1,ndx2): I use memcpy(same as get, &val, sizeof(val);

        Thanks,
        Ognen

        Comment

        Working...