diff options
Diffstat (limited to 'doc/numpybook/comparison/pyrex')
-rw-r--r-- | doc/numpybook/comparison/pyrex/add.c | 560 | ||||
-rw-r--r-- | doc/numpybook/comparison/pyrex/add.pyx | 75 | ||||
-rw-r--r-- | doc/numpybook/comparison/pyrex/c_numpy.pxd | 106 | ||||
-rw-r--r-- | doc/numpybook/comparison/pyrex/filter.c | 388 | ||||
-rw-r--r-- | doc/numpybook/comparison/pyrex/filter.pyx | 44 | ||||
-rw-r--r-- | doc/numpybook/comparison/pyrex/setup.py | 29 | ||||
-rwxr-xr-x | doc/numpybook/comparison/pyrex/timeme | 2 |
7 files changed, 1204 insertions, 0 deletions
diff --git a/doc/numpybook/comparison/pyrex/add.c b/doc/numpybook/comparison/pyrex/add.c new file mode 100644 index 000000000..d5e3bd725 --- /dev/null +++ b/doc/numpybook/comparison/pyrex/add.c @@ -0,0 +1,560 @@ +/* Generated by Pyrex 0.9.4.1 on Thu Aug 17 02:11:41 2006 */ + +#include "Python.h" +#include "structmember.h" +#ifndef PY_LONG_LONG + #define PY_LONG_LONG LONG_LONG +#endif +#ifdef __cplusplus +#define __PYX_EXTERN_C extern "C" +#else +#define __PYX_EXTERN_C extern +#endif +__PYX_EXTERN_C double pow(double, double); +#include "numpy/arrayobject.h" + + +typedef struct {PyObject **p; char *s;} __Pyx_InternTabEntry; /*proto*/ +typedef struct {PyObject **p; char *s; long n;} __Pyx_StringTabEntry; /*proto*/ +static PyObject *__Pyx_UnpackItem(PyObject *, int); /*proto*/ +static int __Pyx_EndUnpack(PyObject *, int); /*proto*/ +static int __Pyx_PrintItem(PyObject *); /*proto*/ +static int __Pyx_PrintNewline(void); /*proto*/ +static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb); /*proto*/ +static void __Pyx_ReRaise(void); /*proto*/ +static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list); /*proto*/ +static PyObject *__Pyx_GetExcValue(void); /*proto*/ +static int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed, char *name); /*proto*/ +static int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type); /*proto*/ +static int __Pyx_GetStarArgs(PyObject **args, PyObject **kwds, char *kwd_list[], int nargs, PyObject **args2, PyObject **kwds2); /*proto*/ +static void __Pyx_WriteUnraisable(char *name); /*proto*/ +static void __Pyx_AddTraceback(char *funcname); /*proto*/ +static PyTypeObject *__Pyx_ImportType(char *module_name, char *class_name, long size); /*proto*/ +static int __Pyx_SetVtable(PyObject *dict, void *vtable); /*proto*/ +static int __Pyx_GetVtable(PyObject *dict, void *vtabptr); /*proto*/ +static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name, char *modname); /*proto*/ +static int __Pyx_InternStrings(__Pyx_InternTabEntry *t); /*proto*/ +static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/ +static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/ + +static PyObject *__pyx_m; +static PyObject *__pyx_b; +static int __pyx_lineno; +static char *__pyx_filename; +static char **__pyx_f; + +/* Declarations from c_numpy */ + +static PyTypeObject *__pyx_ptype_7c_numpy_dtype = 0; +static PyTypeObject *__pyx_ptype_7c_numpy_ndarray = 0; + +/* Declarations from add */ + + +/* Implementation of add */ + +static PyObject *__pyx_n_c_numpy; +static PyObject *__pyx_n_zadd; +static PyObject *__pyx_n_cadd; +static PyObject *__pyx_n_dadd; +static PyObject *__pyx_n_sadd; + +static PyObject *__pyx_f_3add_zadd(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static PyObject *__pyx_f_3add_zadd(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + PyObject *__pyx_v_ao = 0; + PyObject *__pyx_v_bo = 0; + PyArrayObject *__pyx_v_c; + PyArrayObject *__pyx_v_a; + PyArrayObject *__pyx_v_b; + npy_intp __pyx_v_i; + PyObject *__pyx_r; + PyObject *__pyx_1 = 0; + npy_intp __pyx_2; + static char *__pyx_argnames[] = {"ao","bo",0}; + if (!PyArg_ParseTupleAndKeywords(__pyx_args, __pyx_kwds, "OO", __pyx_argnames, &__pyx_v_ao, &__pyx_v_bo)) return 0; + Py_INCREF(__pyx_v_ao); + Py_INCREF(__pyx_v_bo); + __pyx_v_c = ((PyArrayObject *)Py_None); Py_INCREF(Py_None); + __pyx_v_a = ((PyArrayObject *)Py_None); Py_INCREF(Py_None); + __pyx_v_b = ((PyArrayObject *)Py_None); Py_INCREF(Py_None); + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":15 */ + __pyx_1 = PyArray_ContiguousFromAny(__pyx_v_ao,NPY_CDOUBLE,1,1); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 15; goto __pyx_L1;} + if (!__Pyx_TypeTest(__pyx_1, __pyx_ptype_7c_numpy_ndarray)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 15; goto __pyx_L1;} + Py_DECREF(((PyObject *)__pyx_v_a)); + __pyx_v_a = ((PyArrayObject *)__pyx_1); + __pyx_1 = 0; + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":16 */ + __pyx_1 = PyArray_ContiguousFromAny(__pyx_v_bo,NPY_CDOUBLE,1,1); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; goto __pyx_L1;} + if (!__Pyx_TypeTest(__pyx_1, __pyx_ptype_7c_numpy_ndarray)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; goto __pyx_L1;} + Py_DECREF(((PyObject *)__pyx_v_b)); + __pyx_v_b = ((PyArrayObject *)__pyx_1); + __pyx_1 = 0; + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":18 */ + __pyx_1 = PyArray_SimpleNew(__pyx_v_a->nd,__pyx_v_a->dimensions,__pyx_v_a->descr->type_num); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; goto __pyx_L1;} + if (!__Pyx_TypeTest(__pyx_1, __pyx_ptype_7c_numpy_ndarray)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; goto __pyx_L1;} + Py_DECREF(((PyObject *)__pyx_v_c)); + __pyx_v_c = ((PyArrayObject *)__pyx_1); + __pyx_1 = 0; + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":21 */ + __pyx_2 = (__pyx_v_a->dimensions[0]); + for (__pyx_v_i = 0; __pyx_v_i < __pyx_2; ++__pyx_v_i) { + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":22 */ + (((npy_cdouble (*))__pyx_v_c->data)[__pyx_v_i]).real = ((((npy_cdouble (*))__pyx_v_a->data)[__pyx_v_i]).real + (((npy_cdouble (*))__pyx_v_b->data)[__pyx_v_i]).real); + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":24 */ + (((npy_cdouble (*))__pyx_v_c->data)[__pyx_v_i]).imag = ((((npy_cdouble (*))__pyx_v_a->data)[__pyx_v_i]).imag + (((npy_cdouble (*))__pyx_v_b->data)[__pyx_v_i]).imag); + __pyx_L2:; + } + __pyx_L3:; + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":26 */ + Py_INCREF(((PyObject *)__pyx_v_c)); + __pyx_r = ((PyObject *)__pyx_v_c); + goto __pyx_L0; + + __pyx_r = Py_None; Py_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1:; + Py_XDECREF(__pyx_1); + __Pyx_AddTraceback("add.zadd"); + __pyx_r = 0; + __pyx_L0:; + Py_DECREF(__pyx_v_c); + Py_DECREF(__pyx_v_a); + Py_DECREF(__pyx_v_b); + Py_DECREF(__pyx_v_ao); + Py_DECREF(__pyx_v_bo); + return __pyx_r; +} + +static PyObject *__pyx_f_3add_cadd(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static PyObject *__pyx_f_3add_cadd(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + PyObject *__pyx_v_ao = 0; + PyObject *__pyx_v_bo = 0; + PyArrayObject *__pyx_v_c; + PyArrayObject *__pyx_v_a; + PyArrayObject *__pyx_v_b; + npy_intp __pyx_v_i; + PyObject *__pyx_r; + PyObject *__pyx_1 = 0; + npy_intp __pyx_2; + static char *__pyx_argnames[] = {"ao","bo",0}; + if (!PyArg_ParseTupleAndKeywords(__pyx_args, __pyx_kwds, "OO", __pyx_argnames, &__pyx_v_ao, &__pyx_v_bo)) return 0; + Py_INCREF(__pyx_v_ao); + Py_INCREF(__pyx_v_bo); + __pyx_v_c = ((PyArrayObject *)Py_None); Py_INCREF(Py_None); + __pyx_v_a = ((PyArrayObject *)Py_None); Py_INCREF(Py_None); + __pyx_v_b = ((PyArrayObject *)Py_None); Py_INCREF(Py_None); + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":32 */ + __pyx_1 = PyArray_ContiguousFromAny(__pyx_v_ao,NPY_CFLOAT,1,1); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; goto __pyx_L1;} + if (!__Pyx_TypeTest(__pyx_1, __pyx_ptype_7c_numpy_ndarray)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; goto __pyx_L1;} + Py_DECREF(((PyObject *)__pyx_v_a)); + __pyx_v_a = ((PyArrayObject *)__pyx_1); + __pyx_1 = 0; + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":33 */ + __pyx_1 = PyArray_ContiguousFromAny(__pyx_v_bo,NPY_CFLOAT,1,1); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; goto __pyx_L1;} + if (!__Pyx_TypeTest(__pyx_1, __pyx_ptype_7c_numpy_ndarray)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; goto __pyx_L1;} + Py_DECREF(((PyObject *)__pyx_v_b)); + __pyx_v_b = ((PyArrayObject *)__pyx_1); + __pyx_1 = 0; + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":35 */ + __pyx_1 = PyArray_SimpleNew(__pyx_v_a->nd,__pyx_v_a->dimensions,__pyx_v_a->descr->type_num); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 35; goto __pyx_L1;} + if (!__Pyx_TypeTest(__pyx_1, __pyx_ptype_7c_numpy_ndarray)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 35; goto __pyx_L1;} + Py_DECREF(((PyObject *)__pyx_v_c)); + __pyx_v_c = ((PyArrayObject *)__pyx_1); + __pyx_1 = 0; + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":38 */ + __pyx_2 = (__pyx_v_a->dimensions[0]); + for (__pyx_v_i = 0; __pyx_v_i < __pyx_2; ++__pyx_v_i) { + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":39 */ + (((npy_cfloat (*))__pyx_v_c->data)[__pyx_v_i]).real = ((((npy_cfloat (*))__pyx_v_a->data)[__pyx_v_i]).real + (((npy_cfloat (*))__pyx_v_b->data)[__pyx_v_i]).real); + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":41 */ + (((npy_cfloat (*))__pyx_v_c->data)[__pyx_v_i]).imag = ((((npy_cfloat (*))__pyx_v_a->data)[__pyx_v_i]).imag + (((npy_cfloat (*))__pyx_v_b->data)[__pyx_v_i]).imag); + __pyx_L2:; + } + __pyx_L3:; + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":43 */ + Py_INCREF(((PyObject *)__pyx_v_c)); + __pyx_r = ((PyObject *)__pyx_v_c); + goto __pyx_L0; + + __pyx_r = Py_None; Py_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1:; + Py_XDECREF(__pyx_1); + __Pyx_AddTraceback("add.cadd"); + __pyx_r = 0; + __pyx_L0:; + Py_DECREF(__pyx_v_c); + Py_DECREF(__pyx_v_a); + Py_DECREF(__pyx_v_b); + Py_DECREF(__pyx_v_ao); + Py_DECREF(__pyx_v_bo); + return __pyx_r; +} + +static PyObject *__pyx_f_3add_dadd(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static PyObject *__pyx_f_3add_dadd(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + PyObject *__pyx_v_ao = 0; + PyObject *__pyx_v_bo = 0; + PyArrayObject *__pyx_v_c; + PyArrayObject *__pyx_v_a; + PyArrayObject *__pyx_v_b; + npy_intp __pyx_v_i; + PyObject *__pyx_r; + PyObject *__pyx_1 = 0; + npy_intp __pyx_2; + static char *__pyx_argnames[] = {"ao","bo",0}; + if (!PyArg_ParseTupleAndKeywords(__pyx_args, __pyx_kwds, "OO", __pyx_argnames, &__pyx_v_ao, &__pyx_v_bo)) return 0; + Py_INCREF(__pyx_v_ao); + Py_INCREF(__pyx_v_bo); + __pyx_v_c = ((PyArrayObject *)Py_None); Py_INCREF(Py_None); + __pyx_v_a = ((PyArrayObject *)Py_None); Py_INCREF(Py_None); + __pyx_v_b = ((PyArrayObject *)Py_None); Py_INCREF(Py_None); + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":50 */ + __pyx_1 = PyArray_ContiguousFromAny(__pyx_v_ao,NPY_DOUBLE,1,1); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 50; goto __pyx_L1;} + if (!__Pyx_TypeTest(__pyx_1, __pyx_ptype_7c_numpy_ndarray)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 50; goto __pyx_L1;} + Py_DECREF(((PyObject *)__pyx_v_a)); + __pyx_v_a = ((PyArrayObject *)__pyx_1); + __pyx_1 = 0; + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":51 */ + __pyx_1 = PyArray_ContiguousFromAny(__pyx_v_bo,NPY_DOUBLE,1,1); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 51; goto __pyx_L1;} + if (!__Pyx_TypeTest(__pyx_1, __pyx_ptype_7c_numpy_ndarray)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 51; goto __pyx_L1;} + Py_DECREF(((PyObject *)__pyx_v_b)); + __pyx_v_b = ((PyArrayObject *)__pyx_1); + __pyx_1 = 0; + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":53 */ + __pyx_1 = PyArray_SimpleNew(__pyx_v_a->nd,__pyx_v_a->dimensions,__pyx_v_a->descr->type_num); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; goto __pyx_L1;} + if (!__Pyx_TypeTest(__pyx_1, __pyx_ptype_7c_numpy_ndarray)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; goto __pyx_L1;} + Py_DECREF(((PyObject *)__pyx_v_c)); + __pyx_v_c = ((PyArrayObject *)__pyx_1); + __pyx_1 = 0; + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":56 */ + __pyx_2 = (__pyx_v_a->dimensions[0]); + for (__pyx_v_i = 0; __pyx_v_i < __pyx_2; ++__pyx_v_i) { + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":57 */ + (((double (*))__pyx_v_c->data)[__pyx_v_i]) = ((((double (*))__pyx_v_a->data)[__pyx_v_i]) + (((double (*))__pyx_v_b->data)[__pyx_v_i])); + __pyx_L2:; + } + __pyx_L3:; + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":59 */ + Py_INCREF(((PyObject *)__pyx_v_c)); + __pyx_r = ((PyObject *)__pyx_v_c); + goto __pyx_L0; + + __pyx_r = Py_None; Py_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1:; + Py_XDECREF(__pyx_1); + __Pyx_AddTraceback("add.dadd"); + __pyx_r = 0; + __pyx_L0:; + Py_DECREF(__pyx_v_c); + Py_DECREF(__pyx_v_a); + Py_DECREF(__pyx_v_b); + Py_DECREF(__pyx_v_ao); + Py_DECREF(__pyx_v_bo); + return __pyx_r; +} + +static PyObject *__pyx_f_3add_sadd(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static PyObject *__pyx_f_3add_sadd(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + PyObject *__pyx_v_ao = 0; + PyObject *__pyx_v_bo = 0; + PyArrayObject *__pyx_v_c; + PyArrayObject *__pyx_v_a; + PyArrayObject *__pyx_v_b; + npy_intp __pyx_v_i; + PyObject *__pyx_r; + PyObject *__pyx_1 = 0; + npy_intp __pyx_2; + static char *__pyx_argnames[] = {"ao","bo",0}; + if (!PyArg_ParseTupleAndKeywords(__pyx_args, __pyx_kwds, "OO", __pyx_argnames, &__pyx_v_ao, &__pyx_v_bo)) return 0; + Py_INCREF(__pyx_v_ao); + Py_INCREF(__pyx_v_bo); + __pyx_v_c = ((PyArrayObject *)Py_None); Py_INCREF(Py_None); + __pyx_v_a = ((PyArrayObject *)Py_None); Py_INCREF(Py_None); + __pyx_v_b = ((PyArrayObject *)Py_None); Py_INCREF(Py_None); + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":66 */ + __pyx_1 = PyArray_ContiguousFromAny(__pyx_v_ao,NPY_FLOAT,1,1); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; goto __pyx_L1;} + if (!__Pyx_TypeTest(__pyx_1, __pyx_ptype_7c_numpy_ndarray)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; goto __pyx_L1;} + Py_DECREF(((PyObject *)__pyx_v_a)); + __pyx_v_a = ((PyArrayObject *)__pyx_1); + __pyx_1 = 0; + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":67 */ + __pyx_1 = PyArray_ContiguousFromAny(__pyx_v_bo,NPY_FLOAT,1,1); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 67; goto __pyx_L1;} + if (!__Pyx_TypeTest(__pyx_1, __pyx_ptype_7c_numpy_ndarray)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 67; goto __pyx_L1;} + Py_DECREF(((PyObject *)__pyx_v_b)); + __pyx_v_b = ((PyArrayObject *)__pyx_1); + __pyx_1 = 0; + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":69 */ + __pyx_1 = PyArray_SimpleNew(__pyx_v_a->nd,__pyx_v_a->dimensions,__pyx_v_a->descr->type_num); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 69; goto __pyx_L1;} + if (!__Pyx_TypeTest(__pyx_1, __pyx_ptype_7c_numpy_ndarray)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 69; goto __pyx_L1;} + Py_DECREF(((PyObject *)__pyx_v_c)); + __pyx_v_c = ((PyArrayObject *)__pyx_1); + __pyx_1 = 0; + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":72 */ + __pyx_2 = (__pyx_v_a->dimensions[0]); + for (__pyx_v_i = 0; __pyx_v_i < __pyx_2; ++__pyx_v_i) { + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":73 */ + (((float (*))__pyx_v_c->data)[__pyx_v_i]) = ((((float (*))__pyx_v_a->data)[__pyx_v_i]) + (((float (*))__pyx_v_b->data)[__pyx_v_i])); + __pyx_L2:; + } + __pyx_L3:; + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":75 */ + Py_INCREF(((PyObject *)__pyx_v_c)); + __pyx_r = ((PyObject *)__pyx_v_c); + goto __pyx_L0; + + __pyx_r = Py_None; Py_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1:; + Py_XDECREF(__pyx_1); + __Pyx_AddTraceback("add.sadd"); + __pyx_r = 0; + __pyx_L0:; + Py_DECREF(__pyx_v_c); + Py_DECREF(__pyx_v_a); + Py_DECREF(__pyx_v_b); + Py_DECREF(__pyx_v_ao); + Py_DECREF(__pyx_v_bo); + return __pyx_r; +} + +static __Pyx_InternTabEntry __pyx_intern_tab[] = { + {&__pyx_n_c_numpy, "c_numpy"}, + {&__pyx_n_cadd, "cadd"}, + {&__pyx_n_dadd, "dadd"}, + {&__pyx_n_sadd, "sadd"}, + {&__pyx_n_zadd, "zadd"}, + {0, 0} +}; + +static struct PyMethodDef __pyx_methods[] = { + {"zadd", (PyCFunction)__pyx_f_3add_zadd, METH_VARARGS|METH_KEYWORDS, 0}, + {"cadd", (PyCFunction)__pyx_f_3add_cadd, METH_VARARGS|METH_KEYWORDS, 0}, + {"dadd", (PyCFunction)__pyx_f_3add_dadd, METH_VARARGS|METH_KEYWORDS, 0}, + {"sadd", (PyCFunction)__pyx_f_3add_sadd, METH_VARARGS|METH_KEYWORDS, 0}, + {0, 0, 0, 0} +}; + +static void __pyx_init_filenames(void); /*proto*/ + +PyMODINIT_FUNC initadd(void); /*proto*/ +PyMODINIT_FUNC initadd(void) { + __pyx_init_filenames(); + __pyx_m = Py_InitModule4("add", __pyx_methods, 0, 0, PYTHON_API_VERSION); + if (!__pyx_m) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; goto __pyx_L1;}; + __pyx_b = PyImport_AddModule("__builtin__"); + if (!__pyx_b) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; goto __pyx_L1;}; + if (PyObject_SetAttrString(__pyx_m, "__builtins__", __pyx_b) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; goto __pyx_L1;}; + if (__Pyx_InternStrings(__pyx_intern_tab) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; goto __pyx_L1;}; + __pyx_ptype_7c_numpy_dtype = __Pyx_ImportType("numpy", "dtype", sizeof(PyArray_Descr)); if (!__pyx_ptype_7c_numpy_dtype) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 74; goto __pyx_L1;} + __pyx_ptype_7c_numpy_ndarray = __Pyx_ImportType("numpy", "ndarray", sizeof(PyArrayObject)); if (!__pyx_ptype_7c_numpy_ndarray) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 79; goto __pyx_L1;} + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":9 */ + import_array(); + + /* "/Users/oliphant/numpybook/pyrex/add.pyx":62 */ + return; + __pyx_L1:; + __Pyx_AddTraceback("add"); +} + +static char *__pyx_filenames[] = { + "add.pyx", + "c_numpy.pxd", +}; + +/* Runtime support code */ + +static void __pyx_init_filenames(void) { + __pyx_f = __pyx_filenames; +} + +static int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type) { + if (!type) { + PyErr_Format(PyExc_SystemError, "Missing type object"); + return 0; + } + if (obj == Py_None || PyObject_TypeCheck(obj, type)) + return 1; + PyErr_Format(PyExc_TypeError, "Cannot convert %s to %s", + obj->ob_type->tp_name, type->tp_name); + return 0; +} + +static int __Pyx_InternStrings(__Pyx_InternTabEntry *t) { + while (t->p) { + *t->p = PyString_InternFromString(t->s); + if (!*t->p) + return -1; + ++t; + } + return 0; +} + +static PyTypeObject *__Pyx_ImportType(char *module_name, char *class_name, + long size) +{ + PyObject *py_module_name = 0; + PyObject *py_class_name = 0; + PyObject *py_name_list = 0; + PyObject *py_module = 0; + PyObject *result = 0; + + py_module_name = PyString_FromString(module_name); + if (!py_module_name) + goto bad; + py_class_name = PyString_FromString(class_name); + if (!py_class_name) + goto bad; + py_name_list = PyList_New(1); + if (!py_name_list) + goto bad; + Py_INCREF(py_class_name); + if (PyList_SetItem(py_name_list, 0, py_class_name) < 0) + goto bad; + py_module = __Pyx_Import(py_module_name, py_name_list); + if (!py_module) + goto bad; + result = PyObject_GetAttr(py_module, py_class_name); + if (!result) + goto bad; + if (!PyType_Check(result)) { + PyErr_Format(PyExc_TypeError, + "%s.%s is not a type object", + module_name, class_name); + goto bad; + } + if (((PyTypeObject *)result)->tp_basicsize != size) { + PyErr_Format(PyExc_ValueError, + "%s.%s does not appear to be the correct type object", + module_name, class_name); + goto bad; + } + goto done; +bad: + Py_XDECREF(result); + result = 0; +done: + Py_XDECREF(py_module_name); + Py_XDECREF(py_class_name); + Py_XDECREF(py_name_list); + return (PyTypeObject *)result; +} + +static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list) { + PyObject *__import__ = 0; + PyObject *empty_list = 0; + PyObject *module = 0; + PyObject *global_dict = 0; + PyObject *empty_dict = 0; + PyObject *list; + __import__ = PyObject_GetAttrString(__pyx_b, "__import__"); + if (!__import__) + goto bad; + if (from_list) + list = from_list; + else { + empty_list = PyList_New(0); + if (!empty_list) + goto bad; + list = empty_list; + } + global_dict = PyModule_GetDict(__pyx_m); + if (!global_dict) + goto bad; + empty_dict = PyDict_New(); + if (!empty_dict) + goto bad; + module = PyObject_CallFunction(__import__, "OOOO", + name, global_dict, empty_dict, list); +bad: + Py_XDECREF(empty_list); + Py_XDECREF(__import__); + Py_XDECREF(empty_dict); + return module; +} + +#include "compile.h" +#include "frameobject.h" +#include "traceback.h" + +static void __Pyx_AddTraceback(char *funcname) { + PyObject *py_srcfile = 0; + PyObject *py_funcname = 0; + PyObject *py_globals = 0; + PyObject *empty_tuple = 0; + PyObject *empty_string = 0; + PyCodeObject *py_code = 0; + PyFrameObject *py_frame = 0; + + py_srcfile = PyString_FromString(__pyx_filename); + if (!py_srcfile) goto bad; + py_funcname = PyString_FromString(funcname); + if (!py_funcname) goto bad; + py_globals = PyModule_GetDict(__pyx_m); + if (!py_globals) goto bad; + empty_tuple = PyTuple_New(0); + if (!empty_tuple) goto bad; + empty_string = PyString_FromString(""); + if (!empty_string) goto bad; + py_code = PyCode_New( + 0, /*int argcount,*/ + 0, /*int nlocals,*/ + 0, /*int stacksize,*/ + 0, /*int flags,*/ + empty_string, /*PyObject *code,*/ + empty_tuple, /*PyObject *consts,*/ + empty_tuple, /*PyObject *names,*/ + empty_tuple, /*PyObject *varnames,*/ + empty_tuple, /*PyObject *freevars,*/ + empty_tuple, /*PyObject *cellvars,*/ + py_srcfile, /*PyObject *filename,*/ + py_funcname, /*PyObject *name,*/ + __pyx_lineno, /*int firstlineno,*/ + empty_string /*PyObject *lnotab*/ + ); + if (!py_code) goto bad; + py_frame = PyFrame_New( + PyThreadState_Get(), /*PyThreadState *tstate,*/ + py_code, /*PyCodeObject *code,*/ + py_globals, /*PyObject *globals,*/ + 0 /*PyObject *locals*/ + ); + if (!py_frame) goto bad; + py_frame->f_lineno = __pyx_lineno; + PyTraceBack_Here(py_frame); +bad: + Py_XDECREF(py_srcfile); + Py_XDECREF(py_funcname); + Py_XDECREF(empty_tuple); + Py_XDECREF(empty_string); + Py_XDECREF(py_code); + Py_XDECREF(py_frame); +} diff --git a/doc/numpybook/comparison/pyrex/add.pyx b/doc/numpybook/comparison/pyrex/add.pyx new file mode 100644 index 000000000..63c231641 --- /dev/null +++ b/doc/numpybook/comparison/pyrex/add.pyx @@ -0,0 +1,75 @@ +# -*- Mode: Python -*- Not really, but close enough + +cimport c_numpy +from c_numpy cimport import_array, ndarray, npy_intp, npy_cdouble, \ + npy_cfloat, NPY_DOUBLE, NPY_CDOUBLE, NPY_FLOAT, \ + NPY_CFLOAT + +#We need to initialize NumPy +import_array() + +def zadd(object ao, object bo): + cdef ndarray c, a, b + cdef npy_intp i + + a = c_numpy.PyArray_ContiguousFromAny(ao, NPY_CDOUBLE, 1, 1) + b = c_numpy.PyArray_ContiguousFromAny(bo, NPY_CDOUBLE, 1, 1) + + c = c_numpy.PyArray_SimpleNew(a.nd, a.dimensions, + a.descr.type_num) + + for i from 0 <= i < a.dimensions[0]: + (<npy_cdouble *>c.data)[i].real = (<npy_cdouble *>a.data)[i].real + \ + (<npy_cdouble *>b.data)[i].real + (<npy_cdouble *>c.data)[i].imag = (<npy_cdouble *>a.data)[i].imag + \ + (<npy_cdouble *>b.data)[i].imag + return c + +def cadd(object ao, object bo): + cdef ndarray c, a, b + cdef npy_intp i + + a = c_numpy.PyArray_ContiguousFromAny(ao, NPY_CFLOAT, 1, 1) + b = c_numpy.PyArray_ContiguousFromAny(bo, NPY_CFLOAT, 1, 1) + + c = c_numpy.PyArray_SimpleNew(a.nd, a.dimensions, + a.descr.type_num) + + for i from 0 <= i < a.dimensions[0]: + (<npy_cfloat *>c.data)[i].real = (<npy_cfloat *>a.data)[i].real + \ + (<npy_cfloat *>b.data)[i].real + (<npy_cfloat *>c.data)[i].imag = (<npy_cfloat *>a.data)[i].imag + \ + (<npy_cfloat *>b.data)[i].imag + return c + + +def dadd(object ao, object bo): + cdef ndarray c, a, b + cdef npy_intp i + + a = c_numpy.PyArray_ContiguousFromAny(ao, NPY_DOUBLE, 1, 1) + b = c_numpy.PyArray_ContiguousFromAny(bo, NPY_DOUBLE, 1, 1) + + c = c_numpy.PyArray_SimpleNew(a.nd, a.dimensions, + a.descr.type_num) + + for i from 0 <= i < a.dimensions[0]: + (<double *>c.data)[i] = (<double *>a.data)[i] + \ + (<double *>b.data)[i] + return c + + +def sadd(object ao, object bo): + cdef ndarray c, a, b + cdef npy_intp i + + a = c_numpy.PyArray_ContiguousFromAny(ao, NPY_FLOAT, 1, 1) + b = c_numpy.PyArray_ContiguousFromAny(bo, NPY_FLOAT, 1, 1) + + c = c_numpy.PyArray_SimpleNew(a.nd, a.dimensions, + a.descr.type_num) + + for i from 0 <= i < a.dimensions[0]: + (<float *>c.data)[i] = (<float *>a.data)[i] + \ + (<float *>b.data)[i] + return c diff --git a/doc/numpybook/comparison/pyrex/c_numpy.pxd b/doc/numpybook/comparison/pyrex/c_numpy.pxd new file mode 100644 index 000000000..e772f6fee --- /dev/null +++ b/doc/numpybook/comparison/pyrex/c_numpy.pxd @@ -0,0 +1,106 @@ +# :Author: Robert Kern +# :Copyright: 2004, Enthought, Inc. +# :License: BSD Style + + +cdef extern from "numpy/arrayobject.h": + + cdef enum NPY_TYPES: + NPY_BOOL + NPY_BYTE + NPY_UBYTE + NPY_SHORT + NPY_USHORT + NPY_INT + NPY_UINT + NPY_LONG + NPY_ULONG + NPY_LONGLONG + NPY_ULONGLONG + NPY_FLOAT + NPY_DOUBLE + NPY_LONGDOUBLE + NPY_CFLOAT + NPY_CDOUBLE + NPY_CLONGDOUBLE + NPY_OBJECT + NPY_STRING + NPY_UNICODE + NPY_VOID + NPY_NTYPES + NPY_NOTYPE + + cdef enum requirements: + NPY_CONTIGUOUS + NPY_FORTRAN + NPY_OWNDATA + NPY_FORCECAST + NPY_ENSURECOPY + NPY_ENSUREARRAY + NPY_ELEMENTSTRIDES + NPY_ALIGNED + NPY_NOTSWAPPED + NPY_WRITEABLE + NPY_UPDATEIFCOPY + NPY_ARR_HAS_DESCR + + NPY_BEHAVED + NPY_BEHAVED_NS + NPY_CARRAY + NPY_CARRAY_RO + NPY_FARRAY + NPY_FARRAY_RO + NPY_DEFAULT + + NPY_IN_ARRAY + NPY_OUT_ARRAY + NPY_INOUT_ARRAY + NPY_IN_FARRAY + NPY_OUT_FARRAY + NPY_INOUT_FARRAY + + NPY_UPDATE_ALL + + ctypedef struct npy_cdouble: + double real + double imag + + ctypedef struct npy_cfloat: + double real + double imag + + ctypedef int npy_intp + + ctypedef extern class numpy.dtype [object PyArray_Descr]: + cdef int type_num, elsize, alignment + cdef char type, kind, byteorder, hasobject + cdef object fields, typeobj + + ctypedef extern class numpy.ndarray [object PyArrayObject]: + cdef char *data + cdef int nd + cdef npy_intp *dimensions + cdef npy_intp *strides + cdef object base + cdef dtype descr + cdef int flags + + object PyArray_ZEROS(int ndims, npy_intp* dims, NPY_TYPES type_num, int fortran) + object PyArray_EMPTY(int ndims, npy_intp* dims, NPY_TYPES type_num, int fortran) + dtype PyArray_DescrFromTypeNum(NPY_TYPES type_num) + object PyArray_SimpleNew(int ndims, npy_intp* dims, NPY_TYPES type_num) + int PyArray_Check(object obj) + object PyArray_ContiguousFromAny(object obj, NPY_TYPES type, + int mindim, int maxdim) + npy_intp PyArray_SIZE(ndarray arr) + npy_intp PyArray_NBYTES(ndarray arr) + void *PyArray_DATA(ndarray arr) + object PyArray_FromAny(object obj, dtype newtype, int mindim, int maxdim, + int requirements, object context) + object PyArray_FROMANY(object obj, NPY_TYPES type_num, int min, + int max, int requirements) + object PyArray_NewFromDescr(object subtype, dtype newtype, int nd, + npy_intp* dims, npy_intp* strides, void* data, + int flags, object parent) + + void import_array() diff --git a/doc/numpybook/comparison/pyrex/filter.c b/doc/numpybook/comparison/pyrex/filter.c new file mode 100644 index 000000000..1c25eb677 --- /dev/null +++ b/doc/numpybook/comparison/pyrex/filter.c @@ -0,0 +1,388 @@ +/* Generated by Pyrex 0.9.4.1 on Thu Aug 17 02:11:42 2006 */ + +#include "Python.h" +#include "structmember.h" +#ifndef PY_LONG_LONG + #define PY_LONG_LONG LONG_LONG +#endif +#ifdef __cplusplus +#define __PYX_EXTERN_C extern "C" +#else +#define __PYX_EXTERN_C extern +#endif +__PYX_EXTERN_C double pow(double, double); +#include "numpy/arrayobject.h" + + +typedef struct {PyObject **p; char *s;} __Pyx_InternTabEntry; /*proto*/ +typedef struct {PyObject **p; char *s; long n;} __Pyx_StringTabEntry; /*proto*/ +static PyObject *__Pyx_UnpackItem(PyObject *, int); /*proto*/ +static int __Pyx_EndUnpack(PyObject *, int); /*proto*/ +static int __Pyx_PrintItem(PyObject *); /*proto*/ +static int __Pyx_PrintNewline(void); /*proto*/ +static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb); /*proto*/ +static void __Pyx_ReRaise(void); /*proto*/ +static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list); /*proto*/ +static PyObject *__Pyx_GetExcValue(void); /*proto*/ +static int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed, char *name); /*proto*/ +static int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type); /*proto*/ +static int __Pyx_GetStarArgs(PyObject **args, PyObject **kwds, char *kwd_list[], int nargs, PyObject **args2, PyObject **kwds2); /*proto*/ +static void __Pyx_WriteUnraisable(char *name); /*proto*/ +static void __Pyx_AddTraceback(char *funcname); /*proto*/ +static PyTypeObject *__Pyx_ImportType(char *module_name, char *class_name, long size); /*proto*/ +static int __Pyx_SetVtable(PyObject *dict, void *vtable); /*proto*/ +static int __Pyx_GetVtable(PyObject *dict, void *vtabptr); /*proto*/ +static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name, char *modname); /*proto*/ +static int __Pyx_InternStrings(__Pyx_InternTabEntry *t); /*proto*/ +static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/ +static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/ + +static PyObject *__pyx_m; +static PyObject *__pyx_b; +static int __pyx_lineno; +static char *__pyx_filename; +static char **__pyx_f; + +/* Declarations from c_numpy */ + +static PyTypeObject *__pyx_ptype_7c_numpy_dtype = 0; +static PyTypeObject *__pyx_ptype_7c_numpy_ndarray = 0; + +/* Declarations from filter */ + + +/* Implementation of filter */ + +static PyObject *__pyx_n_c_numpy; +static PyObject *__pyx_n_filter; + +static PyObject *__pyx_f_6filter_filter(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static PyObject *__pyx_f_6filter_filter(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + PyObject *__pyx_v_ao = 0; + PyArrayObject *__pyx_v_a; + PyArrayObject *__pyx_v_b; + npy_intp __pyx_v_i; + npy_intp __pyx_v_j; + npy_intp __pyx_v_M; + npy_intp __pyx_v_N; + npy_intp __pyx_v_S0; + npy_intp __pyx_v_S1; + npy_intp __pyx_v_r; + npy_intp __pyx_v_rm1; + npy_intp __pyx_v_rp1; + npy_intp __pyx_v_c; + npy_intp __pyx_v_cm1; + npy_intp __pyx_v_cp1; + PyObject *__pyx_v_oS; + PyObject *__pyx_r; + PyObject *__pyx_1 = 0; + long __pyx_2; + long __pyx_3; + PyObject *__pyx_4 = 0; + int __pyx_5; + static char *__pyx_argnames[] = {"ao",0}; + if (!PyArg_ParseTupleAndKeywords(__pyx_args, __pyx_kwds, "O", __pyx_argnames, &__pyx_v_ao)) return 0; + Py_INCREF(__pyx_v_ao); + __pyx_v_a = ((PyArrayObject *)Py_None); Py_INCREF(Py_None); + __pyx_v_b = ((PyArrayObject *)Py_None); Py_INCREF(Py_None); + __pyx_v_oS = Py_None; Py_INCREF(Py_None); + + /* "/Users/oliphant/numpybook/pyrex/filter.pyx":18 */ + __pyx_1 = PyArray_FROMANY(__pyx_v_ao,NPY_DOUBLE,2,2,NPY_ALIGNED); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; goto __pyx_L1;} + if (!__Pyx_TypeTest(__pyx_1, __pyx_ptype_7c_numpy_ndarray)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; goto __pyx_L1;} + Py_DECREF(((PyObject *)__pyx_v_a)); + __pyx_v_a = ((PyArrayObject *)__pyx_1); + __pyx_1 = 0; + + /* "/Users/oliphant/numpybook/pyrex/filter.pyx":19 */ + __pyx_1 = PyArray_ZEROS(__pyx_v_a->nd,__pyx_v_a->dimensions,__pyx_v_a->descr->type_num,0); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 19; goto __pyx_L1;} + if (!__Pyx_TypeTest(__pyx_1, __pyx_ptype_7c_numpy_ndarray)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 19; goto __pyx_L1;} + Py_DECREF(((PyObject *)__pyx_v_b)); + __pyx_v_b = ((PyArrayObject *)__pyx_1); + __pyx_1 = 0; + + /* "/Users/oliphant/numpybook/pyrex/filter.pyx":20 */ + __pyx_v_M = (__pyx_v_a->dimensions[0]); + + /* "/Users/oliphant/numpybook/pyrex/filter.pyx":21 */ + __pyx_v_N = (__pyx_v_a->dimensions[1]); + + /* "/Users/oliphant/numpybook/pyrex/filter.pyx":22 */ + __pyx_v_S0 = (__pyx_v_a->strides[0]); + + /* "/Users/oliphant/numpybook/pyrex/filter.pyx":23 */ + __pyx_v_S1 = (__pyx_v_a->strides[1]); + + /* "/Users/oliphant/numpybook/pyrex/filter.pyx":24 */ + __pyx_2 = (__pyx_v_M - 1); + for (__pyx_v_i = 1; __pyx_v_i < __pyx_2; ++__pyx_v_i) { + + /* "/Users/oliphant/numpybook/pyrex/filter.pyx":25 */ + __pyx_v_r = (__pyx_v_i * __pyx_v_S0); + + /* "/Users/oliphant/numpybook/pyrex/filter.pyx":26 */ + __pyx_v_rm1 = (__pyx_v_r - __pyx_v_S0); + + /* "/Users/oliphant/numpybook/pyrex/filter.pyx":27 */ + __pyx_v_rp1 = (__pyx_v_r + __pyx_v_S0); + + /* "/Users/oliphant/numpybook/pyrex/filter.pyx":28 */ + __pyx_1 = PyInt_FromLong((__pyx_v_i * __pyx_v_N)); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 28; goto __pyx_L1;} + Py_DECREF(__pyx_v_oS); + __pyx_v_oS = __pyx_1; + __pyx_1 = 0; + + /* "/Users/oliphant/numpybook/pyrex/filter.pyx":29 */ + __pyx_3 = (__pyx_v_N - 1); + for (__pyx_v_j = 1; __pyx_v_j < __pyx_3; ++__pyx_v_j) { + + /* "/Users/oliphant/numpybook/pyrex/filter.pyx":30 */ + __pyx_v_c = (__pyx_v_j * __pyx_v_S1); + + /* "/Users/oliphant/numpybook/pyrex/filter.pyx":31 */ + __pyx_v_cm1 = (__pyx_v_c - __pyx_v_S1); + + /* "/Users/oliphant/numpybook/pyrex/filter.pyx":32 */ + __pyx_v_cp1 = (__pyx_v_c + __pyx_v_S1); + + /* "/Users/oliphant/numpybook/pyrex/filter.pyx":38 */ + __pyx_1 = PyInt_FromLong(__pyx_v_j); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; goto __pyx_L1;} + __pyx_4 = PyNumber_Add(__pyx_v_oS, __pyx_1); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; goto __pyx_L1;} + Py_DECREF(__pyx_1); __pyx_1 = 0; + __pyx_5 = PyInt_AsLong(__pyx_4); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; goto __pyx_L1;} + Py_DECREF(__pyx_4); __pyx_4 = 0; + (((double (*))__pyx_v_b->data)[__pyx_5]) = (((((double (*))((__pyx_v_a->data + __pyx_v_r) + __pyx_v_c))[0]) + (((((((double (*))((__pyx_v_a->data + __pyx_v_rm1) + __pyx_v_c))[0]) + (((double (*))((__pyx_v_a->data + __pyx_v_rp1) + __pyx_v_c))[0])) + (((double (*))((__pyx_v_a->data + __pyx_v_r) + __pyx_v_cm1))[0])) + (((double (*))((__pyx_v_a->data + __pyx_v_r) + __pyx_v_cp1))[0])) * 0.5)) + (((((((double (*))((__pyx_v_a->data + __pyx_v_rm1) + __pyx_v_cm1))[0]) + (((double (*))((__pyx_v_a->data + __pyx_v_rp1) + __pyx_v_cm1))[0])) + (((double (*))((__pyx_v_a->data + __pyx_v_rp1) + __pyx_v_cp1))[0])) + (((double (*))((__pyx_v_a->data + __pyx_v_rm1) + __pyx_v_cp1))[0])) * 0.25)); + __pyx_L4:; + } + __pyx_L5:; + __pyx_L2:; + } + __pyx_L3:; + + /* "/Users/oliphant/numpybook/pyrex/filter.pyx":43 */ + Py_INCREF(((PyObject *)__pyx_v_b)); + __pyx_r = ((PyObject *)__pyx_v_b); + goto __pyx_L0; + + __pyx_r = Py_None; Py_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1:; + Py_XDECREF(__pyx_1); + Py_XDECREF(__pyx_4); + __Pyx_AddTraceback("filter.filter"); + __pyx_r = 0; + __pyx_L0:; + Py_DECREF(__pyx_v_a); + Py_DECREF(__pyx_v_b); + Py_DECREF(__pyx_v_oS); + Py_DECREF(__pyx_v_ao); + return __pyx_r; +} + +static __Pyx_InternTabEntry __pyx_intern_tab[] = { + {&__pyx_n_c_numpy, "c_numpy"}, + {&__pyx_n_filter, "filter"}, + {0, 0} +}; + +static struct PyMethodDef __pyx_methods[] = { + {"filter", (PyCFunction)__pyx_f_6filter_filter, METH_VARARGS|METH_KEYWORDS, 0}, + {0, 0, 0, 0} +}; + +static void __pyx_init_filenames(void); /*proto*/ + +PyMODINIT_FUNC initfilter(void); /*proto*/ +PyMODINIT_FUNC initfilter(void) { + __pyx_init_filenames(); + __pyx_m = Py_InitModule4("filter", __pyx_methods, 0, 0, PYTHON_API_VERSION); + if (!__pyx_m) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; goto __pyx_L1;}; + __pyx_b = PyImport_AddModule("__builtin__"); + if (!__pyx_b) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; goto __pyx_L1;}; + if (PyObject_SetAttrString(__pyx_m, "__builtins__", __pyx_b) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; goto __pyx_L1;}; + if (__Pyx_InternStrings(__pyx_intern_tab) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; goto __pyx_L1;}; + __pyx_ptype_7c_numpy_dtype = __Pyx_ImportType("numpy", "dtype", sizeof(PyArray_Descr)); if (!__pyx_ptype_7c_numpy_dtype) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 74; goto __pyx_L1;} + __pyx_ptype_7c_numpy_ndarray = __Pyx_ImportType("numpy", "ndarray", sizeof(PyArrayObject)); if (!__pyx_ptype_7c_numpy_ndarray) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 79; goto __pyx_L1;} + + /* "/Users/oliphant/numpybook/pyrex/filter.pyx":9 */ + import_array(); + + /* "/Users/oliphant/numpybook/pyrex/filter.pyx":11 */ + return; + __pyx_L1:; + __Pyx_AddTraceback("filter"); +} + +static char *__pyx_filenames[] = { + "filter.pyx", + "c_numpy.pxd", +}; + +/* Runtime support code */ + +static void __pyx_init_filenames(void) { + __pyx_f = __pyx_filenames; +} + +static int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type) { + if (!type) { + PyErr_Format(PyExc_SystemError, "Missing type object"); + return 0; + } + if (obj == Py_None || PyObject_TypeCheck(obj, type)) + return 1; + PyErr_Format(PyExc_TypeError, "Cannot convert %s to %s", + obj->ob_type->tp_name, type->tp_name); + return 0; +} + +static int __Pyx_InternStrings(__Pyx_InternTabEntry *t) { + while (t->p) { + *t->p = PyString_InternFromString(t->s); + if (!*t->p) + return -1; + ++t; + } + return 0; +} + +static PyTypeObject *__Pyx_ImportType(char *module_name, char *class_name, + long size) +{ + PyObject *py_module_name = 0; + PyObject *py_class_name = 0; + PyObject *py_name_list = 0; + PyObject *py_module = 0; + PyObject *result = 0; + + py_module_name = PyString_FromString(module_name); + if (!py_module_name) + goto bad; + py_class_name = PyString_FromString(class_name); + if (!py_class_name) + goto bad; + py_name_list = PyList_New(1); + if (!py_name_list) + goto bad; + Py_INCREF(py_class_name); + if (PyList_SetItem(py_name_list, 0, py_class_name) < 0) + goto bad; + py_module = __Pyx_Import(py_module_name, py_name_list); + if (!py_module) + goto bad; + result = PyObject_GetAttr(py_module, py_class_name); + if (!result) + goto bad; + if (!PyType_Check(result)) { + PyErr_Format(PyExc_TypeError, + "%s.%s is not a type object", + module_name, class_name); + goto bad; + } + if (((PyTypeObject *)result)->tp_basicsize != size) { + PyErr_Format(PyExc_ValueError, + "%s.%s does not appear to be the correct type object", + module_name, class_name); + goto bad; + } + goto done; +bad: + Py_XDECREF(result); + result = 0; +done: + Py_XDECREF(py_module_name); + Py_XDECREF(py_class_name); + Py_XDECREF(py_name_list); + return (PyTypeObject *)result; +} + +static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list) { + PyObject *__import__ = 0; + PyObject *empty_list = 0; + PyObject *module = 0; + PyObject *global_dict = 0; + PyObject *empty_dict = 0; + PyObject *list; + __import__ = PyObject_GetAttrString(__pyx_b, "__import__"); + if (!__import__) + goto bad; + if (from_list) + list = from_list; + else { + empty_list = PyList_New(0); + if (!empty_list) + goto bad; + list = empty_list; + } + global_dict = PyModule_GetDict(__pyx_m); + if (!global_dict) + goto bad; + empty_dict = PyDict_New(); + if (!empty_dict) + goto bad; + module = PyObject_CallFunction(__import__, "OOOO", + name, global_dict, empty_dict, list); +bad: + Py_XDECREF(empty_list); + Py_XDECREF(__import__); + Py_XDECREF(empty_dict); + return module; +} + +#include "compile.h" +#include "frameobject.h" +#include "traceback.h" + +static void __Pyx_AddTraceback(char *funcname) { + PyObject *py_srcfile = 0; + PyObject *py_funcname = 0; + PyObject *py_globals = 0; + PyObject *empty_tuple = 0; + PyObject *empty_string = 0; + PyCodeObject *py_code = 0; + PyFrameObject *py_frame = 0; + + py_srcfile = PyString_FromString(__pyx_filename); + if (!py_srcfile) goto bad; + py_funcname = PyString_FromString(funcname); + if (!py_funcname) goto bad; + py_globals = PyModule_GetDict(__pyx_m); + if (!py_globals) goto bad; + empty_tuple = PyTuple_New(0); + if (!empty_tuple) goto bad; + empty_string = PyString_FromString(""); + if (!empty_string) goto bad; + py_code = PyCode_New( + 0, /*int argcount,*/ + 0, /*int nlocals,*/ + 0, /*int stacksize,*/ + 0, /*int flags,*/ + empty_string, /*PyObject *code,*/ + empty_tuple, /*PyObject *consts,*/ + empty_tuple, /*PyObject *names,*/ + empty_tuple, /*PyObject *varnames,*/ + empty_tuple, /*PyObject *freevars,*/ + empty_tuple, /*PyObject *cellvars,*/ + py_srcfile, /*PyObject *filename,*/ + py_funcname, /*PyObject *name,*/ + __pyx_lineno, /*int firstlineno,*/ + empty_string /*PyObject *lnotab*/ + ); + if (!py_code) goto bad; + py_frame = PyFrame_New( + PyThreadState_Get(), /*PyThreadState *tstate,*/ + py_code, /*PyCodeObject *code,*/ + py_globals, /*PyObject *globals,*/ + 0 /*PyObject *locals*/ + ); + if (!py_frame) goto bad; + py_frame->f_lineno = __pyx_lineno; + PyTraceBack_Here(py_frame); +bad: + Py_XDECREF(py_srcfile); + Py_XDECREF(py_funcname); + Py_XDECREF(empty_tuple); + Py_XDECREF(empty_string); + Py_XDECREF(py_code); + Py_XDECREF(py_frame); +} diff --git a/doc/numpybook/comparison/pyrex/filter.pyx b/doc/numpybook/comparison/pyrex/filter.pyx new file mode 100644 index 000000000..492bc43de --- /dev/null +++ b/doc/numpybook/comparison/pyrex/filter.pyx @@ -0,0 +1,44 @@ +# -*- Mode: Python -*- Not really, but close enough + +cimport c_numpy +from c_numpy cimport import_array, ndarray, npy_intp,\ + NPY_DOUBLE, NPY_CDOUBLE, NPY_FLOAT, \ + NPY_CFLOAT, NPY_ALIGNED + +#We need to initialize NumPy +import_array() + +def filter(object ao): + cdef ndarray a, b + cdef npy_intp i, j, M, N, S0, S1 + cdef npy_intp r,rm1,rp1,c,cm1,cp1 + + # Require an ALIGNED array (but not necessarily contiguous) + # We will use strides to access the elements. + a = c_numpy.PyArray_FROMANY(ao, NPY_DOUBLE, 2, 2, NPY_ALIGNED) + b = c_numpy.PyArray_ZEROS(a.nd, a.dimensions, a.descr.type_num, 0) + M = a.dimensions[0] + N = a.dimensions[1] + S0 = a.strides[0] + S1 = a.strides[1] + for i from 1 <= i < M-1: + r = i*S0 + rm1 = r-S0 + rp1 = r+S0 + oS = i*N + for j from 1 <= j < N-1: + c = j*S1 + cm1 = c-S1 + cp1 = c+S1 + (<double *>b.data)[oS+j] = \ + (<double *>(a.data+r+c))[0] + \ + ((<double *>(a.data+rm1+c))[0] + \ + (<double *>(a.data+rp1+c))[0] + \ + (<double *>(a.data+r+cm1))[0] + \ + (<double *>(a.data+r+cp1))[0])*0.5 + \ + ((<double *>(a.data+rm1+cm1))[0] + \ + (<double *>(a.data+rp1+cm1))[0] + \ + (<double *>(a.data+rp1+cp1))[0] + \ + (<double *>(a.data+rm1+cp1))[0])*0.25 + return b + diff --git a/doc/numpybook/comparison/pyrex/setup.py b/doc/numpybook/comparison/pyrex/setup.py new file mode 100644 index 000000000..64cf12bc8 --- /dev/null +++ b/doc/numpybook/comparison/pyrex/setup.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +from distutils.core import setup +from distutils.extension import Extension + +import numpy + +# Define a pyrex-based extension module, using the generated sources if pyrex +from Pyrex.Distutils import build_ext +pyx_sources = ['add.pyx'] +cmdclass = {'build_ext': build_ext} + + +pyx_ext = Extension('add', + pyx_sources, + include_dirs = [numpy.get_include()]) + +pyx_ext2 = Extension('blur', + ['blur.pyx'], + include_dirs = [numpy.get_include()]) + + +# Call the routine which does the real work +setup(name = 'add', + description = 'Small example on using Pyrex to write a Numpy extension', + url = 'http://www.scipy.org/Cookbook/Pyrex_and_NumPy', + ext_modules = [pyx_ext, pyx_ext2], + cmdclass = cmdclass, + ) diff --git a/doc/numpybook/comparison/pyrex/timeme b/doc/numpybook/comparison/pyrex/timeme new file mode 100755 index 000000000..2f7cd297f --- /dev/null +++ b/doc/numpybook/comparison/pyrex/timeme @@ -0,0 +1,2 @@ +python2.4 -m timeit -s "import numpy as N; a=N.random.rand(100,200); import filter" "b = filter.filter(a)" + |