[Numpy-discussion] Accessing a C library from numarray

Simon Burton simon at arrowtheory.com
Wed Aug 25 17:28:08 CDT 2004


This sounds like a job for pyrex. Here is a recent message from the
pyrex list.

cheers,
Simon.

On Wed, 25 Aug 2004 15:37:58 +0100 (BST)
Michael Hoffman <hoffman at ebi.ac.uk> wrote:

> I wanted to use Pyrex with Numarray, which is the supported
> implementation of Numeric, which is no longer supported. Changing the
> demo that comes with Pyrex to use Numarray instead of Numeric was
> instructive. I thought the results of this might be useful to anyone
> else trying to do the same thing. Here are diffs:
> 
> --- numeric_demo.pyx	2003-07-09 11:37:15.000000000 +0100
> +++ numarray_demo.pyx	2004-08-25 15:30:12.000000000 +0100
> @@ -1,33 +1,92 @@
>  #
>  #  This example demonstrates how to access the internals
> -#  of a Numeric array object.
> +#  of a Numarray object.
>  #
> 
> -cdef extern from "Numeric/arrayobject.h":
> +cdef extern from "numarray/libnumarray.h":
> +  ctypedef int maybelong
> 
> -  struct PyArray_Descr:
> -    int type_num, elsize
> -    char type
> +  cdef struct PyArray_Descr:
> +    int type_num # PyArray_TYPES
> +    int elsize   # bytes for 1 element
> +    char type    # One of "cb1silfdFD "  Object array not supported
> +    # function pointers omitted
> 
> -  ctypedef class Numeric.ArrayType [object PyArrayObject]:
> +  ctypedef class numarray._numarray._numarray [object PyArrayObject]:
>      cdef char *data
>      cdef int nd
> -    cdef int *dimensions, *strides
> +    cdef maybelong *dimensions
> +    cdef maybelong *strides
>      cdef object base
>      cdef PyArray_Descr *descr
>      cdef int flags
> +
> +    # numarray extras
> +    cdef maybelong *_dimensions
> +    cdef maybelong *_strides
> 
> -def print_2d_array(ArrayType a):
> -  print "Type:", chr(a.descr.type)
> -  if chr(a.descr.type) <> "f":
> +    cdef object _data         # object must meet buffer API
> +    cdef object _shadows      # ill-behaved original array.
> +    cdef int    nstrides      # elements in strides array
> +    cdef long   byteoffset    # offset into buffer where array data begins
> +    cdef long   bytestride    # basic seperation of elements in bytes
> +    cdef long   itemsize      # length of 1 element in bytes
> +
> +    cdef char   byteorder     # NUM_BIG_ENDIAN, NUM_LITTLE_ENDIAN
> +
> +    cdef char   _aligned      # test override flag
> +    cdef char   _contiguous   # test override flag
> +
> +  ctypedef enum:
> +    NUM_UNCONVERTED # 0
> +    NUM_CONTIGUOUS  # 1
> +    NUM_NOTSWAPPED  # 2
> +    NUM_ALIGNED     # 4
> +    NUM_WRITABLE    # 8
> +    NUM_COPY        # 16
> +    NUM_C_ARRAY     #  = (NUM_CONTIGUOUS | NUM_ALIGNED | NUM_NOTSWAPPED)
> +
> +  ctypedef enum NumarrayType:
> +    tAny
> +    tBool
> +    tInt8
> +    tUInt8
> +    tInt16
> +    tUInt16
> +    tInt32
> +    tUInt32
> +    tInt64
> +    tUInt64
> +    tFloat32
> +    tFloat64
> +    tComplex32
> +    tComplex64
> +    tObject                   # placeholder... does nothing
> +    tDefault = tFloat64
> +    tLong = tInt32,
> +    tMaxType
> +
> +  void import_libnumarray()
> +  _numarray NA_InputArray (object, NumarrayType, int)
> +  void *NA_OFFSETDATA(_numarray)
> +
> +import_libnumarray()
> +
> +def print_2d_array(_numarray _a):
> +  print "Type:", chr(_a.descr.type)
> +  if chr(_a.descr.type) <> "f":
>      raise TypeError("Float array required")
> -  if a.nd <> 2:
> +  if _a.nd <> 2:
>      raise ValueError("2 dimensional array required")
> +
> +  cdef _numarray a
> +  a = NA_InputArray(_a, tFloat32, NUM_C_ARRAY)
> +
>    cdef int nrows, ncols
>    cdef float *elems, x
>    nrows = a.dimensions[0]
>    ncols = a.dimensions[1]
> -  elems = <float *>a.data
> +  elems = <float *>NA_OFFSETDATA(a)
>    hyphen = "-"
>    divider = ("+" + 10 * hyphen) * ncols + "+"
>    print divider
> 
> --- run_numeric_demo.py	2003-07-09 11:37:15.000000000 +0100
> +++ run_numarray_demo.py	2004-08-25 12:06:47.000000000 +0100
> @@ -1,5 +1,5 @@
> -import Numeric
> -import numeric_demo
> +import numarray
> +import numarray_demo
> 
> -a = Numeric.array([[1.0, 3.5, 8.4], [2.3, 6.6, 4.1]], "f")
> -numeric_demo.print_2d_array(a)
> +a = numarray.array([[1.0, 3.5, 8.4], [2.3, 6.6, 4.1]], "f")
> +numarray_demo.print_2d_array(a)
> -- 
> Michael Hoffman <hoffman at ebi.ac.uk>
> European Bioinformatics Institute
> 
> P.S. Thanks for Pyrex! I just coded a Python implementation of a
> bioinformatics algorithm with the inner loop in Pyrex and it runs as
> fast as the pure C implementation we use in my group.
> 
> _______________________________________________
> Pyrex mailing list
> Pyrex at lists.copyleft.no
> http://lists.copyleft.no/mailman/listinfo/pyrex



On Fri, 20 Aug 2004 09:51:10 -0500
Bruce Southey <southey at uiuc.edu> wrote:

> Hi,
> I need to access a GLP'ed C library in numarray. The library functions are
> scalar ints and doubles as inputs and usually doubles as outputs. 
> 
> Is there a recommended (and simple) way to achieve this? There are a few
> different  C API's that exist from the Python to numarrays.
> 
> Would it be sufficient to first write C code that uses the library and use that
> code for a C API in Python or numarray?
> 
> Thanks,
> 
> Bruce Southey
> 
> 

-- 
Simon Burton, B.Sc.
Licensed PO Box 8066
ANU Canberra 2601
Australia
Ph. 61 02 6249 6940
http://arrowtheory.com 




More information about the Numpy-discussion mailing list