1. Pypy
  2. Untitled project
  3. pypy

Commits

Maciej Fijalkowski  committed da8d76b

(mikefc) implementation of var and std

  • Participants
  • Parent commits 7bb8b38
  • Branches default

Comments (0)

Files changed (2)

File pypy/module/micronumpy/interp_numarray.py

View file
     def descr_mean(self, space):
         return space.div(self.descr_sum(space), space.wrap(self.size))
 
+    def descr_var(self, space):
+        ''' var = mean( (values - mean(values))**2 ) '''
+        w_res = self.descr_sub(space, self.descr_mean(space))
+        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)
+
+    def descr_std(self, space):
+        ''' std(v) = sqrt(var(v)) '''
+        return interp_ufuncs.get(space).sqrt.call(space, [self.descr_var(space)] )
+
     def descr_nonzero(self, space):
         if self.size > 1:
             raise OperationError(space.w_ValueError, space.wrap(
     all = interp2app(BaseArray.descr_all),
     any = interp2app(BaseArray.descr_any),
     dot = interp2app(BaseArray.descr_dot),
+    var = interp2app(BaseArray.descr_var),
+    std = interp2app(BaseArray.descr_std),
 
     copy = interp2app(BaseArray.descr_copy),
     reshape = interp2app(BaseArray.descr_reshape),

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

View file
         assert a[:, 0].tolist() == [17.1, 40.3]
         assert a[0].tolist() == [17.1, 27.2]
 
+    def test_var(self):
+        from _numpypy import array
+        a = array(range(10))
+        assert a.var() == 8.25
+        a = array([5.0])
+        assert a.var() == 0.0
+
+    def test_std(self):
+        from _numpypy import array
+        a = array(range(10))
+        assert a.std() == 2.8722813232690143
+        a = array([5.0])
+        assert a.std() == 0.0
+
 
 class AppTestMultiDim(BaseNumpyAppTest):
     def test_init(self):