1. Pypy
  2. Untitled project
  3. pypy

Commits

Andrews Medina  committed a73ce1b

implemented put and array.put

  • Participants
  • Parent commits 7bf8c50
  • Branches ndarray-ptp

Comments (0)

Files changed (4)

File pypy/module/micronumpy/__init__.py

View file
     appleveldefs = {}
     interpleveldefs = {
         'choose': 'interp_arrayops.choose',
+        'put': 'interp_arrayops.put',
         'repeat': 'interp_arrayops.repeat',
     }
     submodules = {

File pypy/module/micronumpy/interp_arrayops.py

View file
     loop.choose(space, arr, choices, shape, dtype, out, MODES[mode])
     return out
 
+
+@unwrap_spec(mode=str)
+def put(space, w_arr, w_indices, w_values, mode='raise'):
+    from pypy.module.micronumpy import constants
+    from pypy.module.micronumpy.support import int_w
+    arr = convert_to_array(space, w_arr)
+    if mode not in constants.MODES:
+        raise OperationError(space.w_ValueError,
+                             space.wrap("mode %s not known" % (mode,)))
+    indices = convert_to_array(space, w_indices)
+    values = convert_to_array(space, w_values)
+    if not indices:
+        raise OperationError(space.w_ValueError,
+                             space.wrap("indice list cannot be empty"))
+    if not values:
+        raise OperationError(space.w_ValueError,
+                             space.wrap("value list cannot be empty"))
+    dtype = arr.get_dtype()
+    val_iter = values.create_iter()
+    ind_iter = indices.create_iter()
+    while not ind_iter.done():
+        index = int_w(space, ind_iter.getitem())
+        if index < 0 or index >= arr.get_size():
+            if constants.MODES[mode] == constants.MODE_RAISE:
+                raise OperationError(space.w_ValueError, space.wrap(
+                    "invalid entry in choice array"))
+            elif constants.MODES[mode] == constants.MODE_WRAP:
+                index = index % arr.get_size()
+            else:
+                assert constants.MODES[mode] == constants.MODE_CLIP
+                if index < 0:
+                    index = 0
+                else:
+                    index = arr.get_size() - 1
+        arr.setitem(space, [index], val_iter.getitem().convert_to(dtype))
+        ind_iter.next()
+        val_iter.next()
+
+
 def diagonal(space, arr, offset, axis1, axis2):
     shape = arr.get_shape()
     shapelen = len(shape)

File pypy/module/micronumpy/interp_numarray.py

View file
         raise OperationError(space.w_NotImplementedError, space.wrap(
             "ptp (peak to peak) not implemented yet"))
 
-    def descr_put(self, space, w_indices, w_values, w_mode='raise'):
-        raise OperationError(space.w_NotImplementedError, space.wrap(
-            "put not implemented yet"))
+    @unwrap_spec(mode=str)
+    def descr_put(self, space, w_indices, w_values, mode='raise'):
+        from pypy.module.micronumpy.interp_arrayops import put
+        put(space, self, w_indices, w_values, mode)
 
     def descr_resize(self, space, w_new_shape, w_refcheck=True):
         raise OperationError(space.w_NotImplementedError, space.wrap(
     prod = interp2app(W_NDimArray.descr_prod),
     max = interp2app(W_NDimArray.descr_max),
     min = interp2app(W_NDimArray.descr_min),
+    put = interp2app(W_NDimArray.descr_put),
     argmax = interp2app(W_NDimArray.descr_argmax),
     argmin = interp2app(W_NDimArray.descr_argmin),
     all = interp2app(W_NDimArray.descr_all),

File pypy/module/micronumpy/test/test_arrayops.py

View file
         x = array([0, 0, 0], dtype='i2')
         r = array([2, 1, 0]).choose([a, b, c], out=x)
         assert r.dtype == 'i2'
+
+    def test_put_basic(self):
+        from numpypy import arange, array
+        a = arange(5)
+        a.put([0,2], [-44, -55])
+        assert (a == array([-44, 1, -55, 3, 4])).all()
+
+    def test_put_modes(self):
+        from numpypy import array, arange
+        a = arange(5)
+        a.put(22, -5, mode='clip')
+        assert (a == array([0, 1, 2, 3, -5])).all()
+        a = arange(5)
+        a.put(22, -5, mode='wrap')
+        assert (a == array([0, 1, -5, 3, 4])).all()
+        raises(ValueError, "arange(5).put(22, -5, mode='raise')")
+        raises(ValueError, "arange(5).put(22, -5, mode='wrongmode')")