[Numpy-discussion] arrays are column-wise or row-wise?

Brian Blais bblais at bryant.edu
Tue Mar 28 06:03:14 CST 2006


Hello,

I thought I had read somewhere that the values in a numpy contiguous array are stored 
column-wise (moving down from the upper left to the lower left first) like C as 
opposed to row-wise (moving right from the upper left to the upper right first). 
However, when I write a simply Pyrex function to access the data pointer and print it 
out, it seems to go across the row.  Is this correct?  I am using numpy 0.9.3 and 
Python 2.3 on Linux.  This also occurs for Numeric.  I post the code below, with the 
sample output.


		thanks,

			bb

-- 
-----------------

             bblais at bryant.edu
             http://web.bryant.edu/~bblais


#test_numpy.pyx:

cdef extern from "numpy/arrayobject.h":

   struct PyArray_Descr:
     int type_num, elsize
     char type

   ctypedef class numpy.ArrayType [object PyArrayObject]:
     cdef char *data
     cdef int nd
     cdef int *dimensions, *strides
     cdef object base
     cdef PyArray_Descr *descr
     cdef int flags


def test_numpy(ArrayType x):

     cdef int i
     cdef double *data
     cdef int length

     data=<double *>x.data
     length=x.dimensions[0]*x.dimensions[1]
     for i from 0 <= i < length:
         print data[i]

#------------------------------------------------------

#testit.py:

import numpy
from test_numpy import *

a=numpy.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]],numpy.Float64)
print a

test_numpy(a)

#------------------------------------------------------

#output from program:

 >>> execfile('testit.py')
[[  1.   2.   3.]
  [  4.   5.   6.]
  [  7.   8.   9.]
  [ 10.  11.  12.]]
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
10.0
11.0
12.0













More information about the Numpy-discussion mailing list