[pypy-commit] pypy default: (mikefc) implementation of var and std
fijal
noreply at buildbot.pypy.org
Sun Jan 8 11:56:36 CET 2012
Author: Maciej Fijalkowski <fijall at gmail.com>
Branch:
Changeset: r51130:da8d76b03c38
Date: 2012-01-08 12:56 +0200
http://bitbucket.org/pypy/pypy/changeset/da8d76b03c38/
Log: (mikefc) implementation of var and std
diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -563,6 +563,18 @@
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(
@@ -1204,6 +1216,8 @@
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),
diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -978,6 +978,20 @@
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):
More information about the pypy-commit
mailing list