[pypy-svn] pypy numpy-exp: implemented division in numpy, thanks bretp

alex_gaynor commits-noreply at bitbucket.org
Thu May 5 00:39:48 CEST 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: numpy-exp
Changeset: r43897:f87795f00a56
Date: 2011-05-04 18:38 -0400
http://bitbucket.org/pypy/pypy/changeset/f87795f00a56/

Log:	implemented division in numpy, thanks bretp

diff --git a/pypy/module/micronumpy/numarray.py b/pypy/module/micronumpy/numarray.py
--- a/pypy/module/micronumpy/numarray.py
+++ b/pypy/module/micronumpy/numarray.py
@@ -132,6 +132,10 @@
                 a = frame.popvalue()
                 b = frame.popvalue()
                 frame.pushvalue(a * b)
+            elif opcode == 'd':
+                a = frame.popvalue()
+                b = frame.popvalue()
+                frame.pushvalue(a / b)
             else:
                 raise NotImplementedError(
                     "Can't handle bytecode instruction %s" % opcode)
@@ -165,8 +169,9 @@
         return func_with_new_name(impl, "binop_%s_impl" % bytecode)
 
     descr_add = _binop_impl("a")
+    descr_sub = _binop_impl("s")
     descr_mul = _binop_impl("m")
-    descr_sub = _binop_impl("s")
+    descr_div = _binop_impl("d")
 
     def compile(self):
         raise NotImplementedError("abstract base class")
@@ -203,6 +208,7 @@
     __add__ = interp2app(BaseArray.descr_add),
     __sub__ = interp2app(BaseArray.descr_sub),
     __mul__ = interp2app(BaseArray.descr_mul),
+    __div__ = interp2app(BaseArray.descr_div),
 )
 
 class SingleDimArray(BaseArray):
@@ -263,5 +269,6 @@
     __add__ = interp2app(BaseArray.descr_add),
     __sub__ = interp2app(BaseArray.descr_sub),
     __mul__ = interp2app(BaseArray.descr_mul),
+    __div__ = interp2app(BaseArray.descr_div),
     force = interp2app(SingleDimArray.force),
 )
\ No newline at end of file
diff --git a/pypy/module/micronumpy/test/test_numpy.py b/pypy/module/micronumpy/test/test_numpy.py
--- a/pypy/module/micronumpy/test/test_numpy.py
+++ b/pypy/module/micronumpy/test/test_numpy.py
@@ -82,6 +82,28 @@
         for i in range(5):
             assert b[i] == i * 5
 
+    def test_div(self):
+        from numpy import array
+        a = array(range(1, 6))
+        b = (a / a).force()
+        for i in range(5):
+            assert b[i] == 1
+
+    def test_div_other(self):
+        from numpy import array
+        a = array(range(5))
+        b = array([2, 2, 2, 2, 2])
+        c = (a / b).force()
+        for i in range(5):
+            assert c[i] == i / 2.0
+
+    def test_div_constant(self):
+        from numpy import array
+        a = array(range(5))
+        b = (a / 5.0).force()
+        for i in range(5):
+            assert b[i] == i / 5.0
+
 class AppTestNumpy(object):
     def setup_class(cls):
         py.test.skip("unimplemented")


More information about the Pypy-commit mailing list