Numeric C extension: bug or error ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philippe Grosse

    Numeric C extension: bug or error ?

    Hi,

    I don't understand this strange behaviour:

    I compile this code :

    #include <Python.h>
    #include"Numeri c/arrayobject.h"

    static PyObject *
    return_vector(P yObject *self, PyObject *args)
    {
    PyObject *input1;
    PyArrayObject *vector;

    if (!PyArg_ParseTu ple(args, "O", &input1))
    return NULL;

    vector = (PyArrayObject
    *)PyArray_Conti guousFromObject (input1,PyArray _DOUBLE, 1, 1);

    if (vector == NULL)
    return NULL;

    return PyArray_Return( vector);

    }

    /* registration table */
    static struct PyMethodDef testMethods[] = {
    {"return_vector ", return_vector, 1}, /* method name, C
    funcptr, always-tuple */
    {NULL, NULL} /* end of table marker */
    };

    /* module initializer */
    void inittest() /* called on first import */
    { /* name matters if loaded
    dynamically */
    (void) Py_InitModule(" test",testMetho ds); /* mod name, table ptr */
    import_array(); /* indispensable pour utiliser les arrays */
    }

    Very simple: this module takes a Numeric array (vector) as argument and
    send this array back to python...

    If I compile it under macOSX, the result in python is:
    [color=blue][color=green][color=darkred]
    >>> import test
    >>> from Numeric import *
    >>> v=array([1.0,2.0,3.0,4.0],Float)
    >>> v[/color][/color][/color]
    array([ 1., 2., 3., 4.])[color=blue][color=green][color=darkred]
    >>> test.return_vec tor(v)[/color][/color][/color]
    array([ 1., 2., 3., 4.])

    but in linux the result is:
    [color=blue][color=green][color=darkred]
    >>> import test
    >>> from Numeric import *
    >>> v=array([1.0,2.0,3.0,4.0],Float)
    >>> v[/color][/color][/color]
    array([ 1., 2., 3., 4.])[color=blue][color=green][color=darkred]
    >>> test.return_vec tor(v)[/color][/color][/color]
    array([ 1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j])

    Strange !!! the result is a complex array.
    The C vector is really complex because if I try to use it in the C module,
    the result is...incomplete .
    For example, if I try to multiply each item of the vector array by a
    scalar ( pi for example) in a loop, I get this result in python:

    [color=blue][color=green][color=darkred]
    >>> import test
    >>> from Numeric import *
    >>> v=array([1.0,2.0,3.0,4.0],Float)
    >>> test.return_vec tor(v)[/color][/color][/color]
    array([ 3.14159265+0.j, 0.+0.j, 6.28318531+0.j, 0.+0.j])

    It's the result of pi* 1.0, 0.0, 2.0, 0.0 ( the four first elements of the
    internal representation of the complex array ([ 1.+0.j, 2.+0.j, 3.+0.j,
    4.+0.j]).

    Anyone any idea where I'm going wrong?
    Thank you for any help.

    Philippe
  • David M. Cooke

    #2
    Re: Numeric C extension: bug or error ?

    At some point, veronique.gross e@wanadoo.fr (Philippe Grosse) wrote:[color=blue]
    > Hi,
    >
    > I don't understand this strange behaviour:
    >
    > I compile this code :[/color]
    [code clipped; it looks good]
    ....[color=blue]
    > /* registration table */
    > static struct PyMethodDef testMethods[] = {
    > {"return_vector ", return_vector, 1}, /* method name, C
    > funcptr, always-tuple */
    > {NULL, NULL} /* end of table marker */
    > };[/color]

    Instead of the magic constant "1", use METH_VARARGS.

    ....[color=blue]
    > Very simple: this module takes a Numeric array (vector) as argument and
    > send this array back to python...
    >
    > If I compile it under macOSX, the result in python is:
    >[color=green][color=darkred]
    >>>> import test
    >>>> from Numeric import *
    >>>> v=array([1.0,2.0,3.0,4.0],Float)
    >>>> v[/color][/color]
    > array([ 1., 2., 3., 4.])[color=green][color=darkred]
    >>>> test.return_vec tor(v)[/color][/color]
    > array([ 1., 2., 3., 4.])
    >
    > but in linux the result is:
    >[color=green][color=darkred]
    >>>> import test
    >>>> from Numeric import *
    >>>> v=array([1.0,2.0,3.0,4.0],Float)
    >>>> v[/color][/color]
    > array([ 1., 2., 3., 4.])[color=green][color=darkred]
    >>>> test.return_vec tor(v)[/color][/color]
    > array([ 1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j])[/color]

    This isn't what I get; I get the same result under linux as you get
    from Mac OS X. Have you checked that the copies of the code are the
    same?

    --
    |>|\/|<
    /--------------------------------------------------------------------------\
    |David M. Cooke
    |cookedm(at)phy sics(dot)mcmast er(dot)ca

    Comment

    Working...