473,570 Members | 2,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NumPy (numarray) problem with C code

Following the NumPy documentation, I took over some C code,
but run into an error.
Does anybody have a suggestion?
Thanks,
marc
gdlpython.cpp:2 25: `PyArray_Type' undeclared (first use this function)

#include <python2.3/Python.h>
// python numarray is used
#include <python2.3/numarray/arrayobject.h>

// ...

BaseGDL* FromPython( PyObject* pyObj)
{
PyObject *object;

// following is line 225
if( !PyArg_ParseTup le(pyObj, "O!", &PyArray_Typ e, &object))

// please forget the rest or have a look and tell me if that will work
// like I expect or if there is an obvious mistake
// its my first time to interface to python

{
// non-array - try other
PyErr_Clear();
// string
char* s;
if( PyArg_ParseTupl e(pyObj, "s", s))
return new DStringGDL( string(s));
PyErr_Clear();
// integer
DLong l;
if( PyArg_ParseTupl e(pyObj, "l", &l))
return new DLongGDL( l);
PyErr_Clear();
// complex
Py_complex c;
if( PyArg_ParseTupl e(pyObj, "D", &c))
{
DComplexDbl cc( c.real, c.imag);
return new DComplexDblGDL( cc);
}
PyErr_Clear();
// float
DDouble d;
if( PyArg_ParseTupl e(pyObj, "d", &d))
return new DDoubleGDL( d);

throw GDLException( "FromPython : Cannot convert python object.") ;
}

// array type
PyArrayObject* array = (PyArrayObject* ) object;

int nDim = array->nd;
int item_type = array->descr->type_num;

// make array contiguous
// array = static_cast< PyArrayObject*>
// (PyArray_Contig uousFromObject( object,
// item_type,
// 0,0));
array = (PyArrayObject* ) PyArray_Contigu ousFromObject( object,
item_type,
0,0);
if( array == NULL)
throw GDLException( "FromPython : Error making array contiguous.") ;

size_t dimArr[ MAXRANK];
if( nDim > MAXRANK)
{
Warning( "FromPython : Array has more than "+MAXRANK_S TR+
" dimensions. Extending last one.");
size_t lastDim = array->dimensions[ MAXRANK-1];
for( size_t i=MAXRANK; i<nDim; ++i) lastDim *= array->dimensions[ i];
for( size_t i=0; i<MAXRANK-1; ++i)
dimArr[ i] = array->dimensions[ i];
dimArr[ MAXRANK-1] = lastDim;
nDim = MAXRANK;
}
else
{
for( size_t i=0; i<nDim; ++i)
dimArr[ i] = array->dimensions[ i];
}

dimension dim( dimArr, nDim);

switch( item_type)
{
// case PyArray_NOTYPE: //UNDEF***
case PyArray_UBYTE: //BYTE
return NewFromPyArrayO bject< DByteGDL>( dim, array);
case PyArray_SHORT: //INT
return NewFromPyArrayO bject< DIntGDL>( dim, array);
case PyArray_INT: //LONG
return NewFromPyArrayO bject< DLongGDL>( dim, array);
case PyArray_FLOAT: //FLOAT
return NewFromPyArrayO bject< DFloatGDL>( dim, array);
case PyArray_DOUBLE: //DOUBLE
return NewFromPyArrayO bject< DDoubleGDL>( dim, array);
case PyArray_CFLOAT: //COMPLEX
return NewFromPyArrayO bject< DComplexGDL>( dim, array);
// case PyArray_NOTYPE: //STRING***
// case PyArray_NOTYPE: //STRUCT***
case PyArray_CDOUBLE : //COMPLEXDBL
return NewFromPyArrayO bject< DComplexDblGDL> ( dim, array);
// case PyArray_NOTYPE: //PTR***
// case PyArray_NOTYPE: //OBJECT***
case tUInt16: //UINT*
return NewFromPyArrayO bject< DUIntGDL>( dim, array);
case tUInt32: //ULONG*
return NewFromPyArrayO bject< DULongGDL>( dim, array);
default:
throw GDLException( "FromPython : Unknown array type.") ;
}

return NULL; // compiler shut-up
}

Jul 18 '05 #1
2 2941
At some point, Marc Schellens <m_*********@ho tmail.com> wrote:
Following the NumPy documentation, I took over some C code,
but run into an error.
Does anybody have a suggestion?
Thanks,
marc
gdlpython.cpp:2 25: `PyArray_Type' undeclared (first use this function)

#include <python2.3/Python.h>
// python numarray is used
#include <python2.3/numarray/arrayobject.h>
I don't know how you're compiling, but you're probably better off
using
#include "Python.h"
#include "numarray/arrayobject.h"

and specifying the proper include directory. This way you're not
hardcoded to the Python version. distutils automatically adds the
proper directory to the compile line, for instance.

You're using numarray, so you may want to use the numarray interface
instead of the Numeric compability interface. Include
"numarray/libnumarray.h" instead of "numarray/arrayobject.h"
// ...

BaseGDL* FromPython( PyObject* pyObj)
{
PyObject *object;

// following is line 225
if( !PyArg_ParseTup le(pyObj, "O!", &PyArray_Typ e, &object))
There is no such thing as PyArray_Type in numarray, and actually,
there isn't one array type. I'd check for the specific
string/long/complex/double types first (your code below)..
{
// non-array - try other
PyErr_Clear();
// string
char* s;
if( PyArg_ParseTupl e(pyObj, "s", s))
return new DStringGDL( string(s));
PyErr_Clear();
// integer
DLong l;
if( PyArg_ParseTupl e(pyObj, "l", &l))
return new DLongGDL( l);
PyErr_Clear();
// complex
Py_complex c;
if( PyArg_ParseTupl e(pyObj, "D", &c))
{
DComplexDbl cc( c.real, c.imag);
return new DComplexDblGDL( cc);
}
PyErr_Clear();
// float
DDouble d;
if( PyArg_ParseTupl e(pyObj, "d", &d))
return new DDoubleGDL( d);

throw GDLException( "FromPython : Cannot convert python object.") ;
}
and use NA_InputArray() to get the array. This returns NULL if it
can't make the argument an array. You're using the Numeric
compatibility functions below; you might as well use the numarray
equivalents instead.
// array type
PyArrayObject* array = (PyArrayObject* ) object;

int nDim = array->nd;
int item_type = array->descr->type_num;

// make array contiguous
// array = static_cast< PyArrayObject*>
// (PyArray_Contig uousFromObject( object,
// item_type,
// 0,0));
array = (PyArrayObject* ) PyArray_Contigu ousFromObject( object,
item_type,
0,0);
if( array == NULL)
throw GDLException( "FromPython : Error making array contiguous.") ;
Note that array will be NULL also if it's not an array; this would be
a better way to use it.

Replace the above with something like

PyArrayObject* array = NA_InputArray(o bject, tAny, NUM_C_ARRAY);
if (array == NULLL) {
throw GDLException( "FromPython : Error making array contiguous.") ;
}
int item_type = array->descr->type_num;

Double-check the numarray C API documentation as to whether you should use
NA_InputArray or NA_IoArray.

Haven't looked at the following carefully: size_t dimArr[ MAXRANK];
if( nDim > MAXRANK)
{
Warning( "FromPython : Array has more than "+MAXRANK_S TR+
" dimensions. Extending last one.");
size_t lastDim = array->dimensions[ MAXRANK-1];
for( size_t i=MAXRANK; i<nDim; ++i) lastDim *= array->dimensions[ i];
for( size_t i=0; i<MAXRANK-1; ++i)
dimArr[ i] = array->dimensions[ i];
dimArr[ MAXRANK-1] = lastDim;
nDim = MAXRANK;
}
else
{
for( size_t i=0; i<nDim; ++i)
dimArr[ i] = array->dimensions[ i];
}

dimension dim( dimArr, nDim);

Use the numarray types instead: tUInt8, tInt8, tInt16, tFloat64, etc:
switch( item_type)
{
// case PyArray_NOTYPE: //UNDEF***
case PyArray_UBYTE: //BYTE
return NewFromPyArrayO bject< DByteGDL>( dim, array);
case PyArray_SHORT: //INT
return NewFromPyArrayO bject< DIntGDL>( dim, array);
case PyArray_INT: //LONG
return NewFromPyArrayO bject< DLongGDL>( dim, array);
case PyArray_FLOAT: //FLOAT
return NewFromPyArrayO bject< DFloatGDL>( dim, array);
case PyArray_DOUBLE: //DOUBLE
return NewFromPyArrayO bject< DDoubleGDL>( dim, array);
case PyArray_CFLOAT: //COMPLEX
return NewFromPyArrayO bject< DComplexGDL>( dim, array);
// case PyArray_NOTYPE: //STRING***
// case PyArray_NOTYPE: //STRUCT***
case PyArray_CDOUBLE : //COMPLEXDBL
return NewFromPyArrayO bject< DComplexDblGDL> ( dim, array);
// case PyArray_NOTYPE: //PTR***
// case PyArray_NOTYPE: //OBJECT***
case tUInt16: //UINT*
return NewFromPyArrayO bject< DUIntGDL>( dim, array);
case tUInt32: //ULONG*
return NewFromPyArrayO bject< DULongGDL>( dim, array);
default:
throw GDLException( "FromPython : Unknown array type.") ;
}

return NULL; // compiler shut-up
}


--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)phy sics(dot)mcmast er(dot)ca
Jul 18 '05 #2
Thanks,
I think I was using the wrong documentation.
marc
David M. Cooke wrote:
At some point, Marc Schellens <m_*********@ho tmail.com> wrote:

Following the NumPy documentation, I took over some C code,
but run into an error.
Does anybody have a suggestion?
Thanks,
marc
gdlpython.cpp :225: `PyArray_Type' undeclared (first use this function)

#include <python2.3/Python.h>
// python numarray is used
#include <python2.3/numarray/arrayobject.h>

I don't know how you're compiling, but you're probably better off
using
#include "Python.h"
#include "numarray/arrayobject.h"

and specifying the proper include directory. This way you're not
hardcoded to the Python version. distutils automatically adds the
proper directory to the compile line, for instance.

You're using numarray, so you may want to use the numarray interface
instead of the Numeric compability interface. Include
"numarray/libnumarray.h" instead of "numarray/arrayobject.h"

// ...

BaseGDL* FromPython( PyObject* pyObj)
{
PyObject *object;

// following is line 225
if( !PyArg_ParseTup le(pyObj, "O!", &PyArray_Typ e, &object))

There is no such thing as PyArray_Type in numarray, and actually,
there isn't one array type. I'd check for the specific
string/long/complex/double types first (your code below)..

{
// non-array - try other
PyErr_Clear();
// string
char* s;
if( PyArg_ParseTupl e(pyObj, "s", s))
return new DStringGDL( string(s));
PyErr_Clear();
// integer
DLong l;
if( PyArg_ParseTupl e(pyObj, "l", &l))
return new DLongGDL( l);
PyErr_Clear();
// complex
Py_complex c;
if( PyArg_ParseTupl e(pyObj, "D", &c))
{
DComplexDbl cc( c.real, c.imag);
return new DComplexDblGDL( cc);
}
PyErr_Clear();
// float
DDouble d;
if( PyArg_ParseTupl e(pyObj, "d", &d))
return new DDoubleGDL( d);

throw GDLException( "FromPython : Cannot convert python object.") ;
}

and use NA_InputArray() to get the array. This returns NULL if it
can't make the argument an array. You're using the Numeric
compatibility functions below; you might as well use the numarray
equivalents instead.

// array type
PyArrayObject* array = (PyArrayObject* ) object;

int nDim = array->nd;
int item_type = array->descr->type_num;

// make array contiguous
// array = static_cast< PyArrayObject*>
// (PyArray_Contig uousFromObject( object,
// item_type,
// 0,0));
array = (PyArrayObject* ) PyArray_Contigu ousFromObject( object,
item_type,
0,0);
if( array == NULL)
throw GDLException( "FromPython : Error making array contiguous.") ;

Note that array will be NULL also if it's not an array; this would be
a better way to use it.

Replace the above with something like

PyArrayObject* array = NA_InputArray(o bject, tAny, NUM_C_ARRAY);
if (array == NULLL) {
throw GDLException( "FromPython : Error making array contiguous.") ;
}
int item_type = array->descr->type_num;

Double-check the numarray C API documentation as to whether you should use
NA_InputArray or NA_IoArray.

Haven't looked at the following carefully:
size_t dimArr[ MAXRANK];
if( nDim > MAXRANK)
{
Warning( "FromPython : Array has more than "+MAXRANK_S TR+
" dimensions. Extending last one.");
size_t lastDim = array->dimensions[ MAXRANK-1];
for( size_t i=MAXRANK; i<nDim; ++i) lastDim *= array->dimensions[ i];
for( size_t i=0; i<MAXRANK-1; ++i)
dimArr[ i] = array->dimensions[ i];
dimArr[ MAXRANK-1] = lastDim;
nDim = MAXRANK;
}
else
{
for( size_t i=0; i<nDim; ++i)
dimArr[ i] = array->dimensions[ i];
}

dimension dim( dimArr, nDim);


Use the numarray types instead: tUInt8, tInt8, tInt16, tFloat64, etc:

switch( item_type)
{
// case PyArray_NOTYPE: //UNDEF***
case PyArray_UBYTE: //BYTE
return NewFromPyArrayO bject< DByteGDL>( dim, array);
case PyArray_SHORT: //INT
return NewFromPyArrayO bject< DIntGDL>( dim, array);
case PyArray_INT: //LONG
return NewFromPyArrayO bject< DLongGDL>( dim, array);
case PyArray_FLOAT: //FLOAT
return NewFromPyArrayO bject< DFloatGDL>( dim, array);
case PyArray_DOUBLE: //DOUBLE
return NewFromPyArrayO bject< DDoubleGDL>( dim, array);
case PyArray_CFLOAT: //COMPLEX
return NewFromPyArrayO bject< DComplexGDL>( dim, array);
// case PyArray_NOTYPE: //STRING***
// case PyArray_NOTYPE: //STRUCT***
case PyArray_CDOUBLE : //COMPLEXDBL
return NewFromPyArrayO bject< DComplexDblGDL> ( dim, array);
// case PyArray_NOTYPE: //PTR***
// case PyArray_NOTYPE: //OBJECT***
case tUInt16: //UINT*
return NewFromPyArrayO bject< DUIntGDL>( dim, array);
case tUInt32: //ULONG*
return NewFromPyArrayO bject< DULongGDL>( dim, array);
default:
throw GDLException( "FromPython : Unknown array type.") ;
}

return NULL; // compiler shut-up
}



Jul 18 '05 #3

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

Similar topics

2
3267
by: Pepijn Kenter | last post by:
Hi all. I'm new to python and want to use it for an assignment. I have succesfully implemented a galois field class including the _repr__, __mul__, __div__, __add__ and __sub__ methods. Basically a galois field is an integer modulo a prime number. Now I want to make a matrix of these galois field objects using the numpy library....
22
1991
by: J | last post by:
Hi I hope the title of this message indicates my question. I am looking for basic array functionality in Python and it turns out that there are all these packages which are somehow related. Some are allegedly discontinued but still seem to get updated. Could we start a discussion about which package will or may or should survive ?
20
2556
by: mclaugb | last post by:
Has anyone recompiled the Scientific Computing package using NumPy instead of Numeric? I need a least squares algorithm and a Newton Rhaphson algorithm which is contained in Numeric but all the documentation out there says that Numeric is crap and all code should be using NumPy. Thanks, Bryan
10
2218
by: Bryan | last post by:
hi, what is the difference among numeric, numpy and numarray? i'm going to start using matplotlib soon and i'm not sure which one i should use. this page says, "Numarray is a re-implementation of an older Python array module called Numeric" http://www.stsci.edu/resources/software_hardware/numarray
15
2507
by: greg.landrum | last post by:
After using numeric for almost ten years, I decided to attempt to switch a large codebase (python and C++) to using numpy. Here's are some comments about how that went. - The code to automatically switch python stuff over just kind of works. But it was a 90% solution, I could do the rest by hand. Of course, the problem is that then the code...
0
1987
by: robert | last post by:
just a note - some speed comparisons : 0.60627370238398726 0.42836673376223189 0.36965815487747022 0.016557970357098384 0.15692469294117473 0.01951756438393204
2
3959
by: Chris Smith | last post by:
Howdy, I'm a college student and for one of we are writing programs to numerically compute the parameters of antenna arrays. I decided to use Python to code up my programs. Up to now I haven't had a problem, however we have a problem set where we are creating a large matrix and finding it's inverse to solve the problem. To invert the matrix...
4
3368
by: Christian Convey | last post by:
I need to bang out an image processing library (it's schoolwork, so I can't just use an existing one). But I see three libraries competing for my love: numpy, numarray, and numeric. Can anyone recommend which one I should use? If one is considered the officially blessed one going forward, that would be my ideal. Thanks, Christian
3
2447
by: tavis | last post by:
Hi. I am trying to set up Cantera on my system (Mac 10.4.11, python 2.4.4). I have python set up and running, and numpy installed, but I since have found out that Cantera doesn't support numpy and I have to use numarray. I tried installing numarray, but I keep getting errors. Do I have to remove numpy before I install numarray? If so, what is the...
0
7724
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7944
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8149
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6323
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5523
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3681
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1238
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
971
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.