[SciPy-user] Passing numpy array to a c function

Christopher Brown c-b@asu....
Sun Apr 20 18:48:42 CDT 2008


Hi Stéfan,

Thanks. That solved one problem. The length is now printing correctly. 
But the bigger problem seems to be with the float64 type.

I added the following to my function:

   if (array->descr->type_num == PyArray_DOUBLE) {
     printf("Double!\n");
   } else if (array->descr->type_num == PyArray_LONG) {
     printf("Long!\n");
   } else if (array->descr->type_num == PyArray_INT) {
     printf("Int!\n");
   } else if (array->descr->type_num == PyArray_FLOAT) {
     printf("Float!\n");
   } else if (array->descr->type_num == PyArray_SHORT) {
     printf("Short!\n");
   } else if (array->descr->type_num == PyArray_USHORT) {
     printf("UShort!\n");
   } else if (array->descr->type_num == PyArray_UBYTE) {
     printf("UByte!\n");
   } else if (array->descr->type_num == PyArray_SBYTE) {
     printf("SByte!\n");
   } else if (array->descr->type_num == PyArray_UINT) {
     printf("UInt!\n");
   } else if (array->descr->type_num == PyArray_CFLOAT) {
     printf("CFloat!\n");
   } else if (array->descr->type_num == PyArray_CDOUBLE) {
     printf("CDouble!\n");
   } else if (array->descr->type_num == PyArray_OBJECT) {
     printf("Object!\n");
   } else if (array->descr->type_num == PyArray_NTYPES) {
     printf("NTypes!\n");
   } else if (array->descr->type_num == PyArray_NOTYPE) {
     printf("NoType!\n");
   } else
     printf("Type Unknown!\n");
   printf("length is: %5.0d\n",n);
   printf("type_num is: %3.0d\n\n",array->descr->type_num);
   for (i = 0; i < 10; i++)
     printf("%8.5e\n",array[i]);

And the output looks like:

In [6]: for i in range(0,10):
    ...:     print buff[i]
    ...:
    ...:
-0.000244140625
-3.0517578125e-05
0.000152587890625
0.000152587890625
9.1552734375e-05
3.0517578125e-05
6.103515625e-05
-3.0517578125e-05
-0.0001220703125
-0.000152587890625

In [7]: test = mymodule.my_c_function(buff,len(buff))
Type Unknown!
length is: 20191
type_num is:

-4.96427e-42
2.56761e-312
2.22810e-312
2.12200e-312
8.16968e-312
-4.11026e-41
2.80103e-312
2.55310e-313
0.00000e+00
0.00000e+00

A *completely* uneducated guess: Might it be the case that c doesn't 
know about float64's, and is treating the elements of the array as 
float32's?

-- 
Chris



More information about the SciPy-user mailing list