Commits

Alex Gaynor committed 36de782

Issue #1007 -- added fill() method to numpypy arrays

Comments (0)

Files changed (2)

pypy/module/micronumpy/interp_numarray.py

     def descr_var(self, space):
         # var = mean((values - mean(values)) ** 2)
         w_res = self.descr_sub(space, self.descr_mean(space, space.w_None))
-        assert isinstance(w_res, BaseArray) 
+        assert isinstance(w_res, BaseArray)
         w_res = w_res.descr_pow(space, space.wrap(2))
         assert isinstance(w_res, BaseArray)
         return w_res.descr_mean(space, space.w_None)
         # std(v) = sqrt(var(v))
         return interp_ufuncs.get(space).sqrt.call(space, [self.descr_var(space)])
 
+    def descr_fill(self, space, w_value):
+        concr = self.get_concrete_or_scalar()
+        concr.fill(space, w_value)
+
     def descr_nonzero(self, space):
         if self.size > 1:
             raise OperationError(space.w_ValueError, space.wrap(
     def copy(self, space):
         return Scalar(self.dtype, self.value)
 
+    def fill(self, space, w_value):
+        self.value = self.dtype.coerce(space, w_value)
+
     def create_sig(self):
         return signature.ScalarSignature(self.dtype)
 
     Intermediate class for performing binary operations.
     """
     _immutable_fields_ = ['left', 'right']
-    
+
     def __init__(self, ufunc, name, shape, calc_dtype, res_dtype, left, right):
         VirtualArray.__init__(self, name, shape, res_dtype)
         self.ufunc = ufunc
     def __init__(self, shape, dtype, left, right):
         Call2.__init__(self, None, 'sliceloop', shape, dtype, dtype, left,
                        right)
-    
+
     def create_sig(self):
         lsig = self.left.create_sig()
         rsig = self.right.create_sig()
     when we'll make AxisReduce lazy
     """
     _immutable_fields_ = ['left', 'right']
-    
+
     def __init__(self, ufunc, name, shape, dtype, left, right, dim):
         Call2.__init__(self, ufunc, name, shape, dtype, dtype,
                        left, right)
         array.setslice(space, self)
         return array
 
+    def fill(self, space, w_value):
+        self.setslice(space, scalar_w(space, self.dtype, w_value))
+
 
 class ViewArray(ConcreteArray):
     def create_sig(self):
     var = interp2app(BaseArray.descr_var),
     std = interp2app(BaseArray.descr_std),
 
+    fill = interp2app(BaseArray.descr_fill),
+
     copy = interp2app(BaseArray.descr_copy),
     reshape = interp2app(BaseArray.descr_reshape),
     tolist = interp2app(BaseArray.descr_tolist),

pypy/module/micronumpy/test/test_numarray.py

         assert isinstance(i['data'][0], int)
         raises(TypeError, getattr, array(3), '__array_interface__')
 
+    def test_fill(self):
+        from _numpypy import array
+
+        a = array([1, 2, 3])
+        a.fill(10)
+        assert (a == [10, 10, 10]).all()
+        a.fill(False)
+        assert (a == [0, 0, 0]).all()
+
+        b = a[:1]
+        b.fill(4)
+        assert (b == [4]).all()
+        assert (a == [4, 0, 0]).all()
+
+        c = b + b
+        c.fill(27)
+        assert (c == [27]).all()
+
+        d = array(10)
+        d.fill(100)
+        assert d == 100
+
 
 class AppTestSupport(BaseNumpyAppTest):
     def setup_class(cls):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.