{ "metadata": { "signature": "sha256:79cc3cf6926758692befe44fb35be1ab903a035d56fccb8e8c73670b0a4d3e6d" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Pyrex And NumPy\n", "===============\n", "\n", "Please note that the code described here is slightly out of date, since\n", "today [cython](http://cython.org) is the actively maintained version of\n", "Pyrex, and numpy now ships with Cython examples.\n", "\n", "Rather than maintaining both the wiki and the source dir, we'll continue\n", "to update the sources, kept\n", "[here](http://projects.scipy.org/scipy/numpy/browser/trunk/numpy/doc/cython).\n", "\n", "Old Pyrex page\n", "--------------\n", "\n", "[Pyrex](http://nz.cosc.canterbury.ac.nz/~greg/python/Pyrex/) is a\n", "language for writing C extensions to Python. Its syntax is very similar\n", "to writing Python. A file is compiled to a file, which is then compiled\n", "like a standard C extension module for Python. Many people find writing\n", "extension modules with Pyrex preferable to writing them in C or using\n", "other tools, such as SWIG.\n", "\n", "This page is a starting point for accessing numpy arrays natively with\n", "Pyrex. Please note that with current versions of NumPy (SVN), the\n", "directory contains a complete working example with the code in this\n", "page, including also a proper file so you can install it with the\n", "standard Python mechanisms. This should help you get up and running\n", "quickly.\n", "\n", "Here's a file I call \"c\\_python.pxd\":" ] }, { "cell_type": "code", "collapsed": false, "input": [ "cdef extern from \"Python.h\":\n", " ctypedef int Py_intptr_t" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and here's \"c\\_numpy.pxd\":" ] }, { "cell_type": "code", "collapsed": false, "input": [ "cimport c_python\n", "\n", "cdef extern from \"numpy/arrayobject.h\":\n", " ctypedef class numpy.ndarray [object PyArrayObject]:\n", " cdef char *data\n", " cdef int nd\n", " cdef c_python.Py_intptr_t *dimensions\n", " cdef c_python.Py_intptr_t *strides\n", " cdef object base\n", " # descr not implemented yet here...\n", " cdef int flags\n", " cdef int itemsize\n", " cdef object weakreflist\n", "\n", " cdef void import_array()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's an example program, name this something like \"test.pyx\" suffix." ] }, { "cell_type": "code", "collapsed": false, "input": [ "cimport c_numpy\n", "cimport c_python\n", "import numpy\n", "\n", "c_numpy.import_array()\n", "\n", "def print_array_info(c_numpy.ndarray arr):\n", " cdef int i\n", "\n", " print '-='*10\n", " print 'printing array info for ndarray at 0x%0lx'%(arr,)\n", " print 'print number of dimensions:',arr.nd\n", " print 'address of strides: 0x%0lx'%(arr.strides,)\n", " print 'strides:'\n", " for i from 0<=iarr.strides[i]\n", " print 'memory dump:'\n", " print_elements( arr.data, arr.strides, arr.dimensions, arr.nd, sizeof(double), arr.dtype )\n", " print '-='*10\n", " print\n", " \n", "cdef print_elements(char *data,\n", " c_python.Py_intptr_t* strides,\n", " c_python.Py_intptr_t* dimensions,\n", " int nd,\n", " int elsize,\n", " object dtype):\n", " cdef c_python.Py_intptr_t i,j\n", " cdef void* elptr\n", " \n", " if dtype not in [numpy.dtype(numpy.object_),\n", " numpy.dtype(numpy.float64)]:\n", " print ' print_elements() not (yet) implemented for dtype %s'%dtype.name\n", " return\n", " \n", " if nd ==0:\n", " if dtype==numpy.dtype(numpy.object_):\n", " elptr = (data)[0] #[0] dereferences pointer in Pyrex\n", " print ' ',elptr\n", " elif dtype==numpy.dtype(numpy.float64):\n", " print ' ',(data)[0]\n", " elif nd == 1:\n", " for i from 0<=idata)[0]\n", " print ' ',elptr\n", " elif dtype==numpy.dtype(numpy.float64):\n", " print ' ',(data)[0]\n", " data = data + strides[0]\n", " else:\n", " for i from 0<=i