1. Takafumi Arakaki
  2. Numpy Toy Box

Commits

Takafumi Arakaki  committed 3373cc2

ascarray for PyArray_AsCArray sample code

  • Participants
  • Parent commits 6fa7987
  • Branches default

Comments (0)

Files changed (3)

File ascarray/ascarraymodule.c

View file
+#include <Python.h>
+#include "structmember.h"
+#include <numpy/arrayobject.h>
+
+static void print_indent(int inum)
+{
+  int i;
+  for (i = 0; i < inum; ++i){
+    printf("  ");
+  }
+}
+
+static void print_array_d1d(double *a1d, int dim1, int inum)
+{
+  int i;
+  print_indent(inum);
+  printf("[%d]", dim1);
+  for (i = 0; i < dim1; ++i){
+    printf(" %.2f", a1d[i]);
+  }
+  printf("\n");
+}
+
+static void print_array_d2d(double **a2d, int dim1, int dim2, int inum)
+{
+  int i;
+  print_indent(inum);
+  printf("[%d]\n", dim1);
+  for (i = 0; i < dim1; ++i){
+    print_array_d1d(a2d[i], dim2, inum+1);
+  }
+}
+
+static void
+print_array_d3d(double ***a3d, int dim1, int dim2, int dim3, int inum)
+{
+  int i;
+  print_indent(inum);
+  printf("[%d]\n", dim1);
+  for (i = 0; i < dim1; ++i){
+    print_array_d2d(a3d[i], dim2, dim3, inum+1);
+  }
+}
+
+static PyObject *
+print_array(PyObject *dummy, PyObject *args)
+{
+  int nd;
+  PyObject *o1;
+  npy_intp dims[3];
+  PyArray_Descr *descr;
+
+  if (PyArg_ParseTuple(args, "O:print_array", &o1) < 0) {
+    PyErr_SetString( PyExc_TypeError, "bad arguments");
+    return NULL;
+  }
+
+  nd = PyArray_NDIM(o1);
+  if (nd > 3){
+    PyErr_SetString( PyExc_TypeError,
+		     "number of dimensions should be 1, 2 or 3" );
+    return NULL;
+  }
+
+  switch(nd){
+  case 1:
+    if (PyArray_ISFLOAT(o1)){
+      double *ptr;
+      descr = PyArray_DescrFromType(NPY_DOUBLE);
+      Py_INCREF(descr);
+      PyArray_AsCArray(&o1, (void *)&ptr, dims, nd, descr);
+      Py_DECREF(descr);
+      print_array_d1d(ptr, dims[0], 0);
+      PyArray_Free(o1, (void *)ptr);
+    }else if (PyArray_ISINTEGER(o1)){
+      return NULL;
+    }else{
+      return NULL;
+    }
+    break;
+  case 2:
+    if (PyArray_ISFLOAT(o1)){
+      double **ptr;
+      descr = PyArray_DescrFromType(NPY_DOUBLE);
+      Py_INCREF(descr);
+      PyArray_AsCArray(&o1, (void *)&ptr, dims, nd, descr);
+      Py_DECREF(descr);
+      print_array_d2d(ptr, dims[0], dims[1], 0);
+      PyArray_Free(o1, (void *)ptr);
+    }else if (PyArray_ISINTEGER(o1)){
+      return NULL;
+    }else{
+      return NULL;
+    }
+    break;
+  case 3:
+    if (PyArray_ISFLOAT(o1)){
+      double ***ptr;
+      PyObject *tmp=o1;
+      descr = PyArray_DescrFromType(NPY_DOUBLE);
+      Py_INCREF(tmp);
+      Py_INCREF(descr);
+      PyArray_AsCArray(&o1, (void *)&ptr, dims, nd, descr);
+      print_array_d3d(ptr, dims[0], dims[1], dims[2], 0);
+      PyArray_Free(o1, (void *)ptr);
+      Py_DECREF(descr);
+      Py_DECREF(tmp);
+    }else if (PyArray_ISINTEGER(o1)){
+      return NULL;
+    }else{
+      return NULL;
+    }
+    break;
+  }
+
+  return Py_BuildValue("");  /* return None */
+}
+
+static PyMethodDef module_methods[] = {
+  {"print_array", (PyCFunction)print_array, METH_VARARGS, ""},
+  {NULL}  /* Sentinel */
+};
+
+#ifndef PyMODINIT_FUNC	/* declarations for DLL import/export */
+#define PyMODINIT_FUNC void
+#endif
+PyMODINIT_FUNC
+initascarray(void)
+{
+  (void) Py_InitModule("ascarray", module_methods);
+  import_array();   /* required NumPy initialization */
+}

File ascarray/setup.py

View file
+from numpy.distutils.core import setup, Extension
+
+module1 = Extension(
+    'ascarray',
+    sources = ['ascarraymodule.c'],
+    extra_compile_args = ["-fPIC", "-g", "-traceback"],
+)
+
+setup( name = 'my',
+       ext_modules = [module1],
+)
+
+# compile: python setup.py build_ext -i -f

File ascarray/use_aca.py

View file
+import numpy
+from ascarray import *
+
+iac1d = numpy.array([1,2,3], dtype=numpy.int, order='C')
+iaf1d = numpy.array([1,2,3], dtype=numpy.int, order='F')
+fac1d = numpy.array([1,2,3], dtype=numpy.float, order='C')
+faf1d = numpy.array([1,2,3], dtype=numpy.float, order='F')
+fac2d = numpy.array([[1,2,3],[1,2,3],[1,2,3]], dtype=numpy.float)
+faf2d = numpy.array([[1,2,3],[1,2,3],[1,2,3]], dtype=numpy.float, order='F')
+
+fa_3d = numpy.arange(2**3, dtype=numpy.float).reshape([2]*3)
+fac3d = fa_3d.copy('C')
+faf3d = fa_3d.copy('F')
+
+fa_4d = numpy.arange(2**4, dtype=numpy.float).reshape([2]*4)
+fac4d = fa_4d.copy('C')
+faf4d = fa_4d.copy('F')
+
+print
+print 'print_array'
+#print_array(iac1d)
+#print_array(iaf1d)
+print_array(fac1d)
+print_array(faf1d)
+print_array(fac2d)
+print_array(faf2d)
+print_array(fac3d)
+## print_array(faf3d)
+## print_array(fac4d)
+## print_array(faf4d)