473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Module using Numeric array: strange behaviour

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
Jul 18 '05 #1
0 1766

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
5
by: bandw | last post by:
I am having a problem using Numeric-24.0b2 in conjunction with the NetCDF module from ScientificPython (version 2.4.9). This problem does not surface using Numeric-23.8. The problem arises in...
5
by: C. Barnes | last post by:
Hi, I'm in the process of writing a Python linear algebra module. The current targeted interface is: http://oregonstate.edu/~barnesc/temp/linalg/ The interface was originally based on...
30
by: James Daughtry | last post by:
char array; scanf("%19s", &array); I know this is wrong because it's a type mismatch, where scanf expects a pointer to char and gets a pointer to an array of 20 char. I know that question 6.12...
0
by: Rolf Wester | last post by:
Hi, the code: from Numeric import * def my_minimum(a): n=shape(a) x = 1.0e20 for i in range(n):
8
by: mantrid | last post by:
Hello Im having problems working out why the following code does not work. I dont think its the sql as the error occurs on the first update which ever one is put there ($q1 or $q2). Ive swapped...
2
by: goetzie | last post by:
I am using Python 2.4.1 and Numeric 23.8 and running on Windows XP. I am passing a Numeric array of strings (objects) to a C Extension module using the following python code: import Numeric...
9
by: Irmen de Jong | last post by:
Hi, Recently I was bitten by an apparent bug in the BSD socket layer on Open VMS. Specifically, it appears that VMS defines MSG_WAITALL in socket.h but does not implement it (it is not in the...
6
by: Theo v. Werkhoven | last post by:
Goodday, Something strange going on here. A piece of code I wrote bombs out in one of de directories under $HOME, but not in others. Here's a snipped: #v+ def bin2asc(c): s=''
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.