[NumPy-Tickets] [NumPy] #2122: numpy.take gives corrupted result with inplace operation

NumPy Trac numpy-tickets@scipy....
Mon Apr 30 19:27:12 CDT 2012


#2122: numpy.take gives corrupted result with inplace operation
-------------------------+--------------------------------------------------
 Reporter:  rainwoodman  |       Owner:  somebody   
     Type:  defect       |      Status:  new        
 Priority:  high         |   Milestone:  Unscheduled
Component:  numpy.core   |     Version:  1.6.1      
 Keywords:               |  
-------------------------+--------------------------------------------------

Comment(by rainwoodman):

 {{{

 I do agree with you. The only reasonable inplace numpy.take appears to be
 an inplace permute.


 The following is gsl_permute ported to numpy. It is what I am using but
 also too limited
 to be in the library as it can only handle 1D continuous arrays.

 static PyObject * permute(PyObject * self,
     PyObject * args, PyObject * kwds) {
     static char * kwlist[] = {
         "array", "index", NULL
     };
     PyArrayObject * array, * index, *out;
     if(!PyArg_ParseTupleAndKeywords(args, kwds,
         "O!O!", kwlist,
         &PyArray_Type, &array,
         &PyArray_Type, &index)) return NULL;

   size_t n = PyArray_SIZE(index);
   size_t * p = PyArray_DATA(index);
   char * data = PyArray_DATA(array);
   int itemsize = PyArray_ITEMSIZE(array);

   size_t i, k, pk;

   for (i = 0; i < n; i++)
     {
       k = p[i];

       while (k > i)
         k = p[k];

       if (k < i)
         continue ;

       /* Now have k == i, i.e the least in its cycle */

       pk = p[k];

       if (pk == i)
         continue ;

       /* shuffle the elements of the cycle */

       {
         unsigned int a;

         char t[itemsize];

         memmove(t, & data[i * itemsize], itemsize);

         while (pk != i)
           {
             memmove(&data[k * itemsize], & data[pk * itemsize], itemsize);
             k = pk;
             pk = p[k];
           };

         memmove(&data[k * itemsize], t, itemsize);
       }
     }
     Py_INCREF(array);
     return (PyObject*) array;
 }



 }}}

-- 
Ticket URL: <http://projects.scipy.org/numpy/ticket/2122#comment:3>
NumPy <http://projects.scipy.org/numpy>
My example project


More information about the NumPy-Tickets mailing list