[pypy-commit] pypy default: merge ufunc-outer which implements numpypy.ufunc.outer

mattip pypy.commits at gmail.com
Wed May 11 05:31:27 EDT 2016


Author: Matti Picus <matti.picus at gmail.com>
Branch: 
Changeset: r84371:0c3ac8d64955
Date: 2016-05-11 12:29 +0300
http://bitbucket.org/pypy/pypy/changeset/0c3ac8d64955/

Log:	merge ufunc-outer which implements numpypy.ufunc.outer

diff --git a/pypy/module/micronumpy/ndarray.py b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -443,7 +443,7 @@
                         'array does not have imaginary part to set')
         self.implementation.set_imag(space, self, w_value)
 
-    def reshape(self, space, w_shape, order):
+    def reshape(self, space, w_shape, order=NPY.ANYORDER):
         new_shape = get_shape_from_iterable(space, self.get_size(), w_shape)
         new_impl = self.implementation.reshape(self, new_shape, order)
         if new_impl is not None:
diff --git a/pypy/module/micronumpy/test/test_ufuncs.py b/pypy/module/micronumpy/test/test_ufuncs.py
--- a/pypy/module/micronumpy/test/test_ufuncs.py
+++ b/pypy/module/micronumpy/test/test_ufuncs.py
@@ -1480,7 +1480,21 @@
 
     def test_outer(self):
         import numpy as np
-        from numpy import absolute
+        c = np.multiply.outer([1, 2, 3], [4, 5, 6])
+        assert c.shape == (3, 3)
+        assert (c ==[[ 4,  5,  6],
+                     [ 8, 10, 12],
+                     [12, 15, 18]]).all()
+        A = np.array([[1, 2, 3], [4, 5, 6]])
+        B = np.array([[1, 2, 3, 4]])
+        c = np.multiply.outer(A, B)
+        assert c.shape == (2, 3, 1, 4)
+        assert (c == [[[[ 1,  2,  3,  4]],
+                       [[ 2,  4,  6,  8]],
+                       [[ 3,  6,  9, 12]]],
+                      [[[ 4,  8, 12, 16]],
+                       [[ 5, 10, 15, 20]],
+                       [[ 6, 12, 18, 24]]]]).all()
         exc = raises(ValueError, np.absolute.outer, [-1, -2])
         assert exc.value[0] == 'outer product only supported for binary functions'
 
diff --git a/pypy/module/micronumpy/ufuncs.py b/pypy/module/micronumpy/ufuncs.py
--- a/pypy/module/micronumpy/ufuncs.py
+++ b/pypy/module/micronumpy/ufuncs.py
@@ -363,12 +363,18 @@
                 out = space.call_method(obj, '__array_wrap__', out, space.w_None)
             return out
 
-    def descr_outer(self, space, __args__):
-        return self._outer(space, __args__)
-
-    def _outer(self, space, __args__):
-        raise oefmt(space.w_ValueError,
+    def descr_outer(self, space, args_w):
+        if self.nin != 2:
+            raise oefmt(space.w_ValueError,
                     "outer product only supported for binary functions")
+        if len(args_w) != 2:
+            raise oefmt(space.w_ValueError,
+                    "exactly two arguments expected")
+        args = [convert_to_array(space, w_obj) for w_obj in args_w]
+        w_outshape = [space.wrap(i) for i in args[0].get_shape() + [1]*args[1].ndims()]
+        args0 = args[0].reshape(space, space.newtuple(w_outshape))
+        return self.descr_call(space, Arguments.frompacked(space, 
+                                                        space.newlist([args0, args[1]])))
 
     def parse_kwargs(self, space, kwds_w):
         w_casting = kwds_w.pop('casting', None)


More information about the pypy-commit mailing list