Fix a warning coming from tests/run/cython_includes.pyx.
[cython.git] / Demos / numeric_demo.pyx
1 #
2 #  This example demonstrates how to access the internals
3 #  of a Numeric array object.
4 #
5
6 cdef extern from "Numeric/arrayobject.h":
7
8   struct PyArray_Descr:
9     int type_num, elsize
10     char type
11
12   ctypedef class Numeric.ArrayType [object PyArrayObject]:
13     cdef char *data
14     cdef int nd
15     cdef int *dimensions, *strides
16     cdef object base
17     cdef PyArray_Descr *descr
18     cdef int flags
19
20 def print_2d_array(ArrayType a):
21   print "Type:", chr(a.descr.type)
22   if chr(a.descr.type) <> "f":
23     raise TypeError("Float array required")
24   if a.nd <> 2:
25     raise ValueError("2 dimensional array required")
26   cdef int nrows, ncols
27   cdef float *elems, x
28   nrows = a.dimensions[0]
29   ncols = a.dimensions[1]
30   elems = <float *>a.data
31   hyphen = "-"
32   divider = ("+" + 10 * hyphen) * ncols + "+"
33   print divider
34   for row in range(nrows):
35     for col in range(ncols):
36       x = elems[row * ncols + col]
37       print "| %8f" % x,
38     print "|"
39     print divider