Source

Numpy Toy Box / accesscarray / accesscarraymodule.c

Full commit
#include <Python.h>
#include "structmember.h"
#include <numpy/arrayobject.h>

static PyObject *
print_a1(PyObject *dummy, PyObject *args)
{
  int i,j,k, nd;
  PyObject *o1;

  if (PyArg_ParseTuple(args, "O:print_a1", &o1) < 0) {
    PyErr_SetString( PyExc_TypeError,  "bad arguments");
    return NULL;
  }

  nd = PyArray_NDIM(o1);

  switch(nd){
  case 1:
    printf("[%d] ", (int)PyArray_DIMS(o1)[0]);
    for (i = 0; i < PyArray_DIMS(o1)[0]; ++i){
      /* printf("%.2f ", ((double *)c1)[i]); */
    }
    break;
  case 2:
    printf("[%d] ", (int)PyArray_DIMS(o1)[0]);
    for (i = 0; i < PyArray_DIMS(o1)[0]; ++i){
      printf("\n  [%d] ", (int)PyArray_DIMS(o1)[1]);
      for (j = 0; j < PyArray_DIMS(o1)[1]; ++j){
	/* printf("%.2f ", ((double **)c1)[i][j]); */
	/* printf("%.2f ", ((double **)PyArray_DATA(o1))[i][j]); */
	printf("%.2f ", ((double *)PyArray_DATA(o1))[i*PyArray_DIMS(o1)[1]+j]);
      }
    }
    break;
  case 3:
    printf("[%d] ", (int)PyArray_DIMS(o1)[0]);
    for (i = 0; i < PyArray_DIMS(o1)[0]; ++i){
      printf("\n  [%d] ", (int)PyArray_DIMS(o1)[1]);
      for (j = 0; j < PyArray_DIMS(o1)[1]; ++j){
	printf("\n    [%d] ", (int)PyArray_DIMS(o1)[2]);
	for (k = 0; k < PyArray_DIMS(o1)[2]; ++k){
	  /* printf("%.2f ", ((double ***)c1)[i][j][k]); */
	  printf("%.2f ", ((double *)PyArray_DATA(o1))[i*PyArray_DIMS(o1)[1]*PyArray_DIMS(o1)[2]+j*PyArray_DIMS(o1)[2]+k]);
	}
      }
    }
    break;
  }
  printf("\n");

  /* printf("if ( ((PyArrayObject *)o1)->data == c1): "); */
  /* if ( ((PyArrayObject *)o1)->data == c1){ */
  /*   printf("True\n"); */
  /* }else{ */
  /*   printf("False\n"); */
  /* } */

  /* if (PyArray_Free(o1, (void *)c1) < 0){ */
  /*   PyErr_SetString( PyExc_TypeError,  "PyArray_Free fail"); */
  /*   return NULL; */
  /* } */

  return Py_BuildValue("");  /* return None */
}


static PyObject *
print_a2(PyObject *dummy, PyObject *args)
{
  int i,j,k, nd;
  PyObject *o1;
  char *c1;

  if (PyArg_ParseTuple(args, "O:print_a1", &o1) < 0) {
    PyErr_SetString( PyExc_TypeError,  "bad arguments");
    return NULL;
  }

  nd = PyArray_NDIM(o1);
  c1 = PyArray_BYTES(o1);

  switch(nd){
  case 1:
    {
      int n0 = PyArray_DIM(o1,0);
      printf("[%d] ", n0);
      for (i = 0; i < n0; ++i){
	printf("%.2f ", ((double *)c1)[i]);
      }
    }
    break;
  case 2:
    {
      int n0 = PyArray_DIM(o1,0);
      int n1 = PyArray_DIM(o1,1);
      printf("[%d] ", n0);
      for (i = 0; i < n0; ++i){
	printf("\n  [%d] ", n1);
	for (j = 0; j < n1; ++j){
	  printf("%.2f ", ((double *)c1)[i*n0+j]);
	}
      }
    }
    break;
  case 3:
    {
      int n0 = PyArray_DIM(o1,0);
      int n1 = PyArray_DIM(o1,1);
      int n2 = PyArray_DIM(o1,2);
      printf("[%d] ", n0);
      for (i = 0; i < n0; ++i){
	printf("\n  [%d] ", n1);
	for (j = 0; j < n1; ++j){
	  printf("\n    [%d] ", n2);
	  for (k = 0; k < n2; ++k){
	    printf("%.2f ", ((double *)c1)[i*n0*n1+j*n1+k]);
	  }
	}
      }
    }
    break;
  }
  printf("\n");

  return Py_BuildValue("");  /* return None */
}


static PyObject *
check_pointer(PyObject *dummy, PyObject *args)
{
  int nd;
  PyObject *obj, *rtn;
  char *data;
  void *elem;
  int index;
  int num_elem=0, num_missmatch=0;

  if (PyArg_ParseTuple(args, "O:check_pointer", &obj) < 0) {
    PyErr_SetString( PyExc_TypeError,  "bad arguments");
    return NULL;
  }

  rtn = PyArray_SimpleNew(PyArray_NDIM(obj), PyArray_DIMS(obj), NPY_INT);
  if (rtn == NULL){
    return NULL;
  }

  nd = PyArray_NDIM(obj);
  data = PyArray_BYTES(obj);

  PySys_WriteStdout("Check elements of %d-dim array: ", nd);
  switch(nd){
  case 1:
    {
      int n0 = PyArray_DIM(obj,0);
      int i;

      for (i = 0; i < n0; ++i){
	index = i;

	if (PyArray_ISFLOAT(obj)){
	  elem = (void *)&((double *)data)[index];
	}else if (PyArray_ISINTEGER(obj)){
	  elem = (void *)&((int *)data)[index];
	}else{
	  PySys_WriteStdout("Unknown data type\n");
	  break;
	}
	
	if (PyArray_GETPTR1(obj, i) == elem){
	  PySys_WriteStdout(".");
	  *(int *)PyArray_GETPTR1(rtn,i) = 0;
	}else{
	  num_missmatch++;
	  PySys_WriteStdout("!");
	  *(int *)PyArray_GETPTR1(rtn,i) = 1;
	}
	num_elem++;
      }
    }
    break;
  case 2:
    {
      int n0 = PyArray_DIM(obj,0);
      int n1 = PyArray_DIM(obj,1);
      int i,j;

      for (i = 0; i < n0; ++i){
	for (j = 0; j < n1; ++j){
	  index = i*n1 + j;

	  if (PyArray_ISFLOAT(obj)){
	    elem = (void *)&((double *)data)[index];
	  }else if (PyArray_ISINTEGER(obj)){
	    elem = (void *)&((int *)data)[index];
	  }else{
	    PySys_WriteStdout("Unknown data type\n");
	    break;
	  }

	  if (PyArray_GETPTR2(obj,i,j) == elem){
	    PySys_WriteStdout(".");
	    *(int *)PyArray_GETPTR2(rtn,i,j) = 0;
	  }else{
	    num_missmatch++;
	    PySys_WriteStdout("!");
	    *(int *)PyArray_GETPTR2(rtn,i,j) = 1;
	  }
	  num_elem++;
	}
      }
    }
    break;
  case 3:
    {
      int n0 = PyArray_DIM(obj,0);
      int n1 = PyArray_DIM(obj,1);
      int n2 = PyArray_DIM(obj,2);
      int i,j,k;

      for (i = 0; i < n0; ++i){
	for (j = 0; j < n1; ++j){
	  for (k = 0; k < n2; ++k){
	    index = i*n1*n2 + j*n2 + k;

	    if (PyArray_ISFLOAT(obj)){
	      elem = (void *)&((double *)data)[index];
	    }else if (PyArray_ISINTEGER(obj)){
	      elem = (void *)&((int *)data)[index];
	    }else{
	      PySys_WriteStdout("Unknown data type\n");
	      break;
	    }

	    if (PyArray_GETPTR3(obj,i,j,k) == elem){
	      PySys_WriteStdout(".");
	      *(int *)PyArray_GETPTR3(rtn,i,j,k) = 0;
	    }else{
	      num_missmatch++;
	      PySys_WriteStdout("!");
	      *(int *)PyArray_GETPTR3(rtn,i,j,k) = 1;
	    }
	    num_elem++;
	  }
	}
      }
    }
    break;
  case 4:
    {
      int n0 = PyArray_DIM(obj,0);
      int n1 = PyArray_DIM(obj,1);
      int n2 = PyArray_DIM(obj,2);
      int n3 = PyArray_DIM(obj,3);
      int i,j,k,l;

      for (i = 0; i < n0; ++i){
	for (j = 0; j < n1; ++j){
	  for (k = 0; k < n2; ++k){
	    for (l = 0; l < n3; ++l){
	      index = i*n1*n2*n3 + j*n2*n3 + k*n3 + l;

	      if (PyArray_ISFLOAT(obj)){
		elem = (void *)&((double *)data)[index];
	      }else if (PyArray_ISINTEGER(obj)){
		elem = (void *)&((int *)data)[index];
	      }else{
		PySys_WriteStdout("Unknown data type\n");
		break;
	      }

	      if (PyArray_GETPTR4(obj,i,j,k,l) == elem){
		PySys_WriteStdout(".");
		*(int *)PyArray_GETPTR4(rtn,i,j,k,l) = 0;
	      }else{
		num_missmatch++;
		PySys_WriteStdout("!");
		*(int *)PyArray_GETPTR4(rtn,i,j,k,l) = 1;
	      }
	      num_elem++;
	    }
	  }
	}
      }
    }
    break;
  }
  
  PySys_WriteStdout("\n");
  PySys_WriteStdout("Missmatch: %.2f%% (%d/%d) \n",
		    100.0*num_missmatch/num_elem, num_missmatch, num_elem);

  return rtn;
}


static PyObject *
ftimes21(PyObject *dummy, PyObject *args)
{
  PyObject *o0, *o1, *o2;
  double *d0, *d1, *d2;
  int n0, n1, i, j;

  if (PyArg_ParseTuple(args, "OO:ftimes21", &o0, &o1, &o2) < 0) {
    PyErr_SetString( PyExc_TypeError, "bad arguments");
    return NULL;
  }
  if (PyArray_NDIM(o0) != 2) {
    PyErr_SetString( PyExc_ValueError, "bad ndim");
    return NULL;
  }
  if (PyArray_NDIM(o1) != 1) {
    PyErr_SetString( PyExc_ValueError, "bad ndim");
    return NULL;
  }
  if (PyArray_NDIM(o2) != 1) {
    PyErr_SetString( PyExc_ValueError, "bad ndim");
    return NULL;
  }
  n0 = PyArray_DIM(o0, 0);
  n1 = PyArray_DIM(o0, 1);
  if (PyArray_DIM(o1, 0) != n1) {
    PyErr_SetString( PyExc_ValueError, "bad shape");
    return NULL;
  }
  if (PyArray_DIM(o2, 0) != n0) {
    PyErr_SetString( PyExc_ValueError, "bad shape");
    return NULL;
  }
  
  d0 = (double *)PyArray_BYTES(o0);
  d1 = (double *)PyArray_BYTES(o1);
  d2 = (double *)PyArray_BYTES(o2);

  for (i = 0; i < n0; ++i) {
    d2[i] = 0;
    for (j = 0; j < n1; ++j) {
      d2[i] += d0[i*n1 + j] * d1[j];
    }
  }

  return Py_BuildValue("");  /* return None */
}


static PyMethodDef module_methods[] = {
  {"print_a1", (PyCFunction)print_a1, METH_VARARGS, ""},
  {"print_a2", (PyCFunction)print_a2, METH_VARARGS, ""},
  {"check_pointer", (PyCFunction)check_pointer, METH_VARARGS, ""},
  {"ftimes21", (PyCFunction)ftimes21, METH_VARARGS, ""},
  {NULL}  /* Sentinel */
};

#ifndef PyMODINIT_FUNC	/* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif
PyMODINIT_FUNC
initaccesscarray(void)
{
  (void) Py_InitModule("accesscarray", module_methods);
  import_array();   /* required NumPy initialization */
}