[pypy-svn] r70378 - in pypy/branch/micronumpy/pypy/module/micronumpy: . test

dan at codespeak.net dan at codespeak.net
Sat Jan 2 13:19:50 CET 2010


Author: dan
Date: Sat Jan  2 13:19:49 2010
New Revision: 70378

Modified:
   pypy/branch/micronumpy/pypy/module/micronumpy/__init__.py
   pypy/branch/micronumpy/pypy/module/micronumpy/array.py
   pypy/branch/micronumpy/pypy/module/micronumpy/dtype.py
   pypy/branch/micronumpy/pypy/module/micronumpy/ndarray.py
   pypy/branch/micronumpy/pypy/module/micronumpy/sdarray.py
   pypy/branch/micronumpy/pypy/module/micronumpy/test/test_numpy.py
Log:
Generalized multiplication to all arithmetic operations with single dimensional array and scalar operators.

Modified: pypy/branch/micronumpy/pypy/module/micronumpy/__init__.py
==============================================================================
--- pypy/branch/micronumpy/pypy/module/micronumpy/__init__.py	(original)
+++ pypy/branch/micronumpy/pypy/module/micronumpy/__init__.py	Sat Jan  2 13:19:49 2010
@@ -4,12 +4,11 @@
 class Module(MixedModule):
 
     applevel_name = 'numpy'
-    appleveldefs = {
-        #'array' : 'app_numarray.array',
-        }
+    appleveldefs = {}
     
     interpleveldefs = {
         'array'    : 'ndarray.array',
+        'ndarray'  : 'ndarray.ndarray',
         'zeros'    : 'ndarray.zeros',
         'minimum'  : 'ufunc.minimum',
         }

Modified: pypy/branch/micronumpy/pypy/module/micronumpy/array.py
==============================================================================
--- pypy/branch/micronumpy/pypy/module/micronumpy/array.py	(original)
+++ pypy/branch/micronumpy/pypy/module/micronumpy/array.py	Sat Jan  2 13:19:49 2010
@@ -17,7 +17,7 @@
     return div
 
 def add_operation():
-    def add(x, y): return x * y
+    def add(x, y): return x + y
     return add
 
 def sub_operation():

Modified: pypy/branch/micronumpy/pypy/module/micronumpy/dtype.py
==============================================================================
--- pypy/branch/micronumpy/pypy/module/micronumpy/dtype.py	(original)
+++ pypy/branch/micronumpy/pypy/module/micronumpy/dtype.py	Sat Jan  2 13:19:49 2010
@@ -8,7 +8,7 @@
 def coerce_float(space, w_x):
     return unwrap_float(space, space.float(w_x))
 
-from rlib.rarithmetic import r_singlefloat as float32
+from pypy.rlib.rarithmetic import r_singlefloat as float32
 def unwrap_float32(space, w_x):
     return float32(space.float_w(w_x))
 def coerce_float32(space, w_x):

Modified: pypy/branch/micronumpy/pypy/module/micronumpy/ndarray.py
==============================================================================
--- pypy/branch/micronumpy/pypy/module/micronumpy/ndarray.py	(original)
+++ pypy/branch/micronumpy/pypy/module/micronumpy/ndarray.py	Sat Jan  2 13:19:49 2010
@@ -19,6 +19,18 @@
             }
     return types[w_types]
 
+def mul_scalar(result, source, w_x):     result.mul_scalar(source, w_x)
+def mul_fixedview(result, source, w_xs): result.mul_fixedview(source, w_xs)
+
+def div_scalar(result, source, w_x):     result.div_scalar(source, w_x)
+def div_fixedview(result, source, w_xs): result.div_fixedview(source, w_xs)
+
+def add_scalar(result, source, w_x):     result.add_scalar(source, w_x)
+def add_fixedview(result, source, w_xs): result.add_fixedview(source, w_xs)
+
+def sub_scalar(result, source, w_x):     result.sub_scalar(source, w_x)
+def sub_fixedview(result, source, w_xs): result.sub_fixedview(source, w_xs)
+
 def unpack_shape(space, w_shape):
     if space.is_true(space.isinstance(w_shape, space.w_int)):
         return [space.int_w(w_shape)]
@@ -54,7 +66,6 @@
     return [space.int_w(space.len(w_values))] #TODO: handle multi-dimensional arrays...
 
 class ndarray(Wrappable):
-    #FIXME: blows up (NoneNotWrapped != None) when not called by applevel?
     def __init__(self, space, w_values, w_shape=NoneNotWrapped, w_dtype=NoneNotWrapped):
         self.array = None
         self.space = space
@@ -98,22 +109,30 @@
             if e.match(space, space.w_TypeError): pass
             else: raise
 
-    def descr_mul(self, w_x):
-        space = self.space
-        if space.type(w_x) in (space.w_list, space.w_tuple):
-            #xs = space.fixedview(w_x)
-            raise OperationError(space.w_NotImplementedError,
-                                 space.wrap("Haven't implemented array * iterable yet!"))
-        else:
-            result_t = result_type(space, (space.type(w_x), self.dtype))
-            result_array = sdresult(space, result_t)(space, self.array.length) #FIXME: support multi-dimensional array!
-            result_array.mul_scalar(self.array, w_x) #TODO: reverse so that result_array = self.array.mul_scalar(w_x)
-
-            result = ndarray(space, space.w_None, None, None)
-            result.array = result_array
-            w_result = space.wrap(result)
-        return w_result
-    descr_mul.unwrap_spec = ['self', W_Root]
+    def create_math_operation(f):
+        def math_operation(self, w_x):
+            space = self.space
+            if space.type(w_x) in (space.w_list, space.w_tuple):
+                raise OperationError(space.w_NotImplementedError,
+                                     space.wrap("Haven't implemented array * iterable yet!"))
+            else:
+                result_t = result_type(space, (space.type(w_x), self.dtype))
+                result_array = sdresult(space, result_t)(space, self.array.length) #FIXME: support multi-dimensional array!
+                #result_array.mul_scalar(self.array, w_x) #TODO: reverse so that result_array = self.array.mul_scalar(w_x)
+                f(result_array, self.array, w_x) #TODO: can i use member function pointers?
+
+                result = ndarray(space, space.w_None, None, None)
+                result.array = result_array
+                w_result = space.wrap(result)
+            return w_result
+        math_operation.unwrap_spec = ['self', W_Root]
+        return math_operation
+
+    # Math Operations
+    descr_mul = create_math_operation(mul_scalar)
+    descr_div = create_math_operation(div_scalar)
+    descr_add = create_math_operation(add_scalar)
+    descr_sub = create_math_operation(sub_scalar)
 
     def descr_iter(self):
         space = self.space
@@ -149,6 +168,9 @@
     __new__ = interp2app(descr_new),
     __iter__ = interp2app(ndarray.descr_iter),
     __mul__ = interp2app(ndarray.descr_mul),
+    __div__ = interp2app(ndarray.descr_div),
+    __add__ = interp2app(ndarray.descr_add),
+    __sub__ = interp2app(ndarray.descr_sub),
     __getitem__ = interp2app(ndarray.descr_getitem),
     __setitem__ = interp2app(ndarray.descr_setitem),
     __len__     = interp2app(ndarray.descr_len),

Modified: pypy/branch/micronumpy/pypy/module/micronumpy/sdarray.py
==============================================================================
--- pypy/branch/micronumpy/pypy/module/micronumpy/sdarray.py	(original)
+++ pypy/branch/micronumpy/pypy/module/micronumpy/sdarray.py	Sat Jan  2 13:19:49 2010
@@ -36,14 +36,15 @@
                 space = self.space
                 x = coerce(space, w_x)
                 for i in range(source.length):
-                    self.storage[i] = f(source.storage[i], x)
+                    self.storage[i] = f(data_type(source.storage[i]), x)
             return scalar_operation
 
         mul_scalar = create_scalar_op(mul)
-#        div_scalar = create_scalar_op(div)
-#        add_scalar = create_scalar_op(add)
-#        sub_scalar = create_scalar_op(sub)
+        div_scalar = create_scalar_op(div)
+        add_scalar = create_scalar_op(add)
+        sub_scalar = create_scalar_op(sub)
 
+        #TODO: wrap up fixedview and scalar together
         def create_fixedview_op(f):
             def fixedview_operation(self, w_xs):
                 space = self.space

Modified: pypy/branch/micronumpy/pypy/module/micronumpy/test/test_numpy.py
==============================================================================
--- pypy/branch/micronumpy/pypy/module/micronumpy/test/test_numpy.py	(original)
+++ pypy/branch/micronumpy/pypy/module/micronumpy/test/test_numpy.py	Sat Jan  2 13:19:49 2010
@@ -22,16 +22,26 @@
             assert compare(ar, data)
         return array_type_test
         """)
+        cls.w_array_scalar_op_test = cls.space.appexec([cls.w_compare],
+        """(compare):
+        def array_scalar_op_test(self, data_type, f, value, length):
+            compare = self.compare
+            from numpy import array
+            data = [data_type(x) for x in range(length)]
+            ar = array(data)
+            assert compare(f(ar, value), [f(x, value) for x in data])
+        return array_scalar_op_test
+        """)
 
     def test_int_array(self): self.array_type_test(int)
     def test_float_array(self): self.array_type_test(float)
 
-    def test_array_mul(self):
-        compare = self.compare
-        from numpy import array
-        data = range(4)
-        ar = array(data)
-        assert compare(ar * 4, [x * 4 for x in data])
+    def test_sdarray_operators(self):
+        from operator import mul, div, add, sub
+        self.array_scalar_op_test(self, float, mul, 2.0, 16)
+        self.array_scalar_op_test(self, float, div, 2.0, 16)
+        self.array_scalar_op_test(self, float, add, 2.0, 16)
+        self.array_scalar_op_test(self, float, sub, 2.0, 16)
 
     def test_iterable_construction(self):
         compare = self.compare



More information about the Pypy-commit mailing list