Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Numeric C extension: bug or error ?

Reply
Thread Tools

Numeric C extension: bug or error ?

 
 
Philippe Grosse
Guest
Posts: n/a
 
      09-30-2003
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_DOUB LE, 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",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)
>>> 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
 
Reply With Quote
 
 
 
 
David M. Cooke
Guest
Posts: n/a
 
      09-30-2003
At some point, http://www.velocityreviews.com/forums/(E-Mail Removed) (Philippe Grosse) wrote:
> Hi,
>
> I don't understand this strange behaviour:
>
> I compile this code :

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


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

....
> 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])


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)physics(dot)mcmaster(dot)ca
 
Reply With Quote
 
 
 
 
Philippe Grosse
Guest
Posts: n/a
 
      10-06-2003
In article <(E-Mail Removed)>,
(E-Mail Removed) (David M. Cooke) wrote:

> At some point, (E-Mail Removed) (Philippe Grosse) wrote:
> > Hi,
> >
> > I don't understand this strange behaviour:
> >
> > I compile this code :

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

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


I have tried METH_VARARGS: same result

>
> ...
> > 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])

>
> 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?


Yes, I'm sure, the codes are exactly the same.

I use this command to compile under linux:

[grosse@intercalcul dev_fortran]$ gcc -DNDEBUG -O3 -fomit-frame-pointer
-pipe -mcpu=pentiumpro -march=i586 -ffast-math -fno-strength-reduce -fPIC
-g -I/usr/include/python2.2 -c test.c -o test.o

[grosse@intercalcul dev_fortran]$ gcc -shared -g -o test.so test.o

I can understand where is the problem, I'm totaly confused.

Thank you for your help
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
int to numeric numeric(18,2) ? jobs ASP .Net 2 07-22-2007 12:32 AM
Arithmetic overflow error converting numeric to data type numeric. darrel ASP .Net 4 07-19-2007 09:57 PM
*bug* *bug* *bug* David Raleigh Arnold Firefox 12 04-02-2007 03:13 AM
Subtle architecture-dependent bug in Numeric.RandomArray Johannes Nix Python 0 04-03-2006 11:15 AM
check if string contains numeric, and check string length of numeric value ief@specialfruit.be C++ 5 06-30-2005 01:08 PM



Advertisments