Module using Numeric array: strange behaviour

Discussion in 'Python' started by Phil, Sep 27, 2003.

  1. Phil

    Phil Guest

    Hi,

    I don't understand this strange behaviour:

    I compile this code :

    #include <Python.h>
    #include"Numeric/arrayobject.h"

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

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

    vector = (PyArrayObject *)PyArray_ContiguousFromObject(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 func
    ptr, 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",testMethods); /* 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:

    >>> import test
    >>> from Numeric import *
    >>> v=array([1.0,2.0,3.0,4.0],Float)
    >>> v

    array([ 1., 2., 3., 4.])
    >>> test.return_vector(v)

    array([ 1., 2., 3., 4.])

    but in linux the result is:

    >>> import test
    >>> from Numeric import *
    >>> v=array([1.0,2.0,3.0,4.0],Float)
    >>> v

    array([ 1., 2., 3., 4.])
    >>> test.return_vector(v)

    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:


    >>> import test
    >>> from Numeric import *
    >>> v=array([1.0,2.0,3.0,4.0],Float)
    >>> v

    array([ 1., 2., 3., 4.])
    >>> test.return_vector(v)

    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
    Phil, Sep 27, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rolf Wester
    Replies:
    0
    Views:
    283
    Rolf Wester
    Jul 12, 2006
  2. goetzie
    Replies:
    2
    Views:
    322
    goetzie
    Sep 21, 2006
  3. Replies:
    5
    Views:
    915
    X-Centric
    Jun 30, 2005
  4. darrel
    Replies:
    4
    Views:
    795
    darrel
    Jul 19, 2007
  5. jobs

    int to numeric numeric(18,2) ?

    jobs, Jul 21, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    942
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Jul 22, 2007
Loading...

Share This Page