Estoy desarrollando un módulo para usar c inline
en código Python basado en swig
. Para eso me gustaría hacer arreglos numpy
accesibles en C
Hasta ahora utilicé tipos C como unsigned short
pero me gustaría usar tipos como uint16_t
de stdint.h
para guardar cualquier comstackdor que encuentre mi módulo.
Desafortunadamente, las funciones de c++
no se ajustan correctamente cuando se usan tipos stdint.h
. El error dado es: _setc() takes exactly 2 arguments (1 given)
. Eso significa que la función no está numpy
para aceptar matrices numpy
. El error no se produce, cuando uso, por ejemplo, unsigned short
.
¿Tiene alguna idea de cómo puedo hacer que los arreglos numpy
map se numpy
en stdint-types
?
interface.i
NO funciona:
/* interface.i */ extern int __g(); %} %include "stdint.i" %include "numpy.i" %init %{ import_array(); %} %apply (uint16_t* INPLACE_ARRAY3, int DIM1) {(uint16_t* seq, int n1)}; extern int __g();
c++
función c++
NO funciona:
#include "Python.h" #include #include extern uint16_t* c; extern int Dc; extern int Nc[4]; void _setc(uint16_t *seq, int n1, int n2, int n3) { c = seq; Nc[0] = n1; Nc[1] = n2; Nc[2] = n3; }
interface.i
trabajando:
/* interface.i */ extern int __g(); %} %include "stdint.i" %include "numpy.i" %init %{ import_array(); %} %apply (unsigned short* INPLACE_ARRAY3, int DIM1) {(unsigned short* seq, int n1)}; extern int __g();
Función c++
funcionando:
#include "Python.h" #include #include extern unsigned short* c; extern int Dc; extern int Nc[4]; void _setc(unsigned short *seq, int n1, int n2, int n3) { c = seq; Nc[0] = n1; Nc[1] = n2; Nc[2] = n3; }
Jaja, encontré una “solución” solo unos minutos después de que me rendí y publiqué esta pregunta.
Edité el numpy.i
para ajustarlo a mi causa: sustituí los tipos C
antiguos con los tipos stdint.h
en las líneas 3044 ff:
[..] /* Concrete instances of the %numpy_typemaps() macro: Each invocation * below applies all of the typemaps above to the specified data type. */ %numpy_typemaps(int8_t , NPY_BYTE , int) %numpy_typemaps(uint8_t , NPY_UBYTE , int) %numpy_typemaps(int16_t , NPY_SHORT , int) %numpy_typemaps(uint16_t , NPY_USHORT , int) %numpy_typemaps(int32_t , NPY_INT , int) %numpy_typemaps(uint32_t , NPY_UINT , int) %numpy_typemaps(long , NPY_LONG , int) %numpy_typemaps(unsigned long , NPY_ULONG , int) %numpy_typemaps(int64_t , NPY_LONGLONG , int) %numpy_typemaps(uint64_t, NPY_ULONGLONG, int) %numpy_typemaps(float , NPY_FLOAT , int) %numpy_typemaps(double , NPY_DOUBLE , int) [..]
Me pregunto si alguien tiene una idea mejor que editar el numpy.i
Saludos Jochen