2

I am sorry if this question is incomplete or hard to understand, I am trying to figure it out myself ! I have the following C code, inside python-both languages that I am not very good at.

The following C code copies numpy array I think. Its not working when I use it, it uses the SWITCH statement and prints the "default" answer. Can someone help in this?

Is it the version of python? It seems to work in old versions of python.

  static PyObject *llpy_acopy(PyObject *unused, PyObject *args)
  {
  PyObject *afrom = PyTuple_GET_ITEM(args, 0);
  PyObject *ato = PyTuple_GET_ITEM(args, 1);
  PyArrayObject *tmp = (PyArrayObject *)afrom;
  int nd = tmp->nd;
  int n = 1;
  int i;
  for (i=0; i<nd; i++)
    n *= tmp->dimensions[i];

  switch(tmp->descr->type_num) {
  case PyArray_FLOAT:
      memcpy(((PyArrayObject *)ato)->data, tmp->data,
              (size_t)(n * sizeof(float)));
      break;
  case PyArray_INT:
      memcpy(IDATA(ato), IDATA(afrom), 
           (size_t )(n  * sizeof(int)));
      break;
   case PyArray_CHAR:
      memcpy(CDATA(ato), CDATA(afrom), 
             (size_t )(n * sizeof(char)));
      break;
   default:
      PyErr_SetString(PyExc_TypeError, "Cannot copy array now");
      return NULL;
    }
   Py_INCREF(Py_None);
  return Py_None;
      }

The two arrays being copied from one to the other are -that is the first array is copied into the second, replacing the existing values.

[[ 1.          0.          0.        ]
[ 1.51999998  1.42799997  0.        ]
[ 0.73699999  2.37800002  0.        ]
  ..., 
[-7.55183792 -9.31523228 -0.19252452]
[-6.7193203  -8.40346909  0.58392692]
[-8.47850037 -9.02481842 -0.46105781]]


[[ 1.          0.          0.        ]
 [ 1.51999998  1.42799997  0.        ]
 [ 0.73699999  2.37800002  0.        ]
  ..., 
 [-5.86299992 -9.36299992 -5.0999999 ]
 [-5.61800003 -7.94799995 -4.84499979]
 [-6.12400007 -9.65600014 -6.02799988]]
Joel Vroom
  • 1,611
  • 1
  • 16
  • 30
Steve Grafton
  • 1,821
  • 4
  • 17
  • 18
  • Have you tried debugging so that you know what value "tmp->descr->type_num" returns? – dckuehn Oct 06 '13 at 01:45
  • 1
    This whole thing doesn't make much sense and oversimplifies general numpy arrays. You should use the build-in numpy C-api which has functions like `PyArray_CopyInto` available. – seberg Oct 06 '13 at 08:54
  • 2
    As to why it goes to the error path. My best guess is you may be hit by different integer types or double precision numbers? – seberg Oct 06 '13 at 08:58

1 Answers1

0

The expression tmp->descr->type_num will return an integer (eg. double precision results in type_num=12). But I don't know about PyArray_INT, PyArray_FLOAT, PyArray_CHAR, etc. From a Google search, these look like types defined in the old numeric_numarray.h header. I think what you want to do is use the data-type checking listed here. See PyTypeNum_ISFLOAT(num), PyTypeNum_ISINTEGER(num), etc.

Joel Vroom
  • 1,611
  • 1
  • 16
  • 30
  • 1
    you are right Joel, thank you very much. The problem was the package I had been given was built by several people over a long period of time and they had MIXED numpy and Numeric definitions. I switched from using Numeric array types to Numpy arrays and the problem seems to have sorted itself out. You can see more in http://stackoverflow.com/questions/19236121/equivalent-of-using-include-numeric-arrayobject-h-in-numpy/19236834#19236834 – Steve Grafton Oct 08 '13 at 12:10