Author: Alex Gaynor <alex.gay...@gmail.com> Branch: Changeset: r44976:117991cc4de7 Date: 2011-06-16 14:30 -0700 http://bitbucket.org/pypy/pypy/changeset/117991cc4de7/
Log: Added numpy.array.mean and numpy.mean. diff --git a/pypy/module/micronumpy/__init__.py b/pypy/module/micronumpy/__init__.py --- a/pypy/module/micronumpy/__init__.py +++ b/pypy/module/micronumpy/__init__.py @@ -21,4 +21,6 @@ 'sign': 'interp_ufuncs.sign', } - appleveldefs = {} + appleveldefs = { + 'mean': 'app_numpy.mean', + } diff --git a/pypy/module/micronumpy/app_numpy.py b/pypy/module/micronumpy/app_numpy.py new file mode 100644 --- /dev/null +++ b/pypy/module/micronumpy/app_numpy.py @@ -0,0 +1,7 @@ +import numpy + + +def mean(a): + if not hasattr(a, "mean"): + a = numpy.array(a) + return a.mean() \ No newline at end of file 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 @@ -99,6 +99,14 @@ self.invalidated() return self.get_concrete().descr_setitem(space, item, value) + def descr_mean(self, space): + s = 0 + concrete = self.get_concrete() + for i in xrange(concrete.size): + s += concrete.getitem(i) + return space.wrap(s / concrete.size) + + class FloatWrapper(BaseArray): """ Intermediate class representing a float literal. @@ -324,4 +332,6 @@ __sub__ = interp2app(BaseArray.descr_sub), __mul__ = interp2app(BaseArray.descr_mul), __div__ = interp2app(BaseArray.descr_div), -) + + mean = interp2app(BaseArray.descr_mean), +) \ No newline at end of file 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 @@ -165,7 +165,7 @@ assert len(s) == 4 for i in range(4): assert s[i] == a[2*i+1] - + def test_slice_update(self): from numpy import array a = array(range(5)) @@ -177,17 +177,24 @@ def test_slice_invaidate(self): - # check that slice shares invalidation list with + # check that slice shares invalidation list with from numpy import array a = array(range(5)) s = a[0:2] b = array([10,11]) c = s + b - a[0]=100 + a[0] = 100 assert c[0] == 10 assert c[1] == 12 d = s + b - a[1]=101 + a[1] = 101 assert d[0] == 110 assert d[1] == 12 + def test_mean(self): + from numpy import array, mean + a = array(range(5)) + assert a.mean() == 2.0 + assert mean(a) == 2.0 + assert mean(range(5)) == 2.0 + assert a[:4].mean() == 1.5 \ No newline at end of file _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit