[Numpy-discussion] C extension in new numpy: help to port from numarray
Travis Oliphant
oliphant.travis at ieee.org
Wed May 10 11:43:03 EDT 2006
Sebastian Haase wrote:
> Hi,
> I'm a long time numarray user.
> I have some SWIG typemaps that I'm using for quite some time.
> They are C++ oriented and support creating template'd function.
> I only cover the case of contiguous "input" arrays of 1D,2D and 3D.
> (I would like to ensure that NO implicit type conversions are made so
> that I can use the same scheme to have arrays changed on the C/C++
> side and can later see/access those in python.)
>
> (as I just added some text to the scipy wiki:
> www.scipy.org/Converting_from_numarray)
> I use something like:
>
> PyArrayObject *NAarr = NA_InputArray(inputPyObject, PYARR_TC,
> NUM_C_ARRAY);
> arr = (double *) NA_OFFSETDATA(NAarr);
>
>
> What is new numpy's equivalent of NA_InputArray
In the scipy/Lib/ndimage package is a numcompat.c and numcompat.h file
that implements several of the equivalents.
I'd like to see a module like this get formalized and placed into numpy
itself so that most numarray extensions simply have to be re-compiled to
work with NumPy.
Here is the relevant information (although I don't know what PYARR_TC is...)
typedef enum
{
tAny,
tBool=PyArray_BOOL,
tInt8=PyArray_INT8,
tUInt8=PyArray_UINT8,
tInt16=PyArray_INT16,
tUInt16=PyArray_UINT16,
tInt32=PyArray_INT32,
tUInt32=PyArray_UINT32,
tInt64=PyArray_INT64,
tUInt64=PyArray_UINT64,
tFloat32=PyArray_FLOAT32,
tFloat64=PyArray_FLOAT64,
tComplex32=PyArray_COMPLEX64,
tComplex64=PyArray_COMPLEX128,
tObject=PyArray_OBJECT, /* placeholder... does nothing */
tDefault = tFloat64,
#if BITSOF_LONG == 64
tLong = tInt64,
#else
tLong = tInt32,
#endif
tMaxType
} NumarrayType;
typedef enum
{
NUM_CONTIGUOUS=CONTIGUOUS,
NUM_NOTSWAPPED=NOTSWAPPED,
NUM_ALIGNED=ALIGNED,
NUM_WRITABLE=WRITEABLE,
NUM_COPY=ENSURECOPY,
NUM_C_ARRAY = (NUM_CONTIGUOUS | NUM_ALIGNED | NUM_NOTSWAPPED),
NUM_UNCONVERTED = 0
} NumRequirements;
#define _NAtype_toDescr(type) (((type)==tAny) ? NULL : \
PyArray_DescrFromType(type))
#define NA_InputArray(obj, type, flags) \
(PyArrayObject *)\
PyArray_FromAny(obj, _NAtype_toDescr(type), 0, 0, flags,
NULL)
#define NA_OFFSETDATA(a) ((void *) PyArray_DATA(a))
More information about the NumPy-Discussion
mailing list