[pypy-svn] r70335 - pypy/branch/micronumpy/pypy/module/micronumpy

electronicru at codespeak.net electronicru at codespeak.net
Tue Dec 29 11:33:09 CET 2009


Author: electronicru
Date: Tue Dec 29 11:33:09 2009
New Revision: 70335

Modified:
   pypy/branch/micronumpy/pypy/module/micronumpy/mdarray.py
   pypy/branch/micronumpy/pypy/module/micronumpy/ndarray.py
   pypy/branch/micronumpy/pypy/module/micronumpy/sdarray.py
   pypy/branch/micronumpy/pypy/module/micronumpy/ufunc.py
Log:
I have modified some issues, but I have no time to implement slices fully.
Going out for 20 or more days.
Happy New Year!




Modified: pypy/branch/micronumpy/pypy/module/micronumpy/mdarray.py
==============================================================================
--- pypy/branch/micronumpy/pypy/module/micronumpy/mdarray.py	(original)
+++ pypy/branch/micronumpy/pypy/module/micronumpy/mdarray.py	Tue Dec 29 11:33:09 2009
@@ -25,45 +25,123 @@
         current *= d
     return pos
 
-def create_mdarray(data_type, unwrap, coerce):
-    class MultiDimArray(BaseNumArray):
-        def __init__(self, space, shape):
-            self.shape = shape
-            self.space = space
-            size = 1
-            for dimension in shape:
-                size *= dimension
-            self.storage = [data_type(0.0)] * size
-            make_sure_not_resized(self.storage)
-
-        def _unpack_indexes(self, space, w_index):
-            indexes = [space.int_w(w_i) for w_i in space.fixedview(w_index)]
-            if len(indexes) != len(self.shape):
-                raise OperationError(space.w_IndexError, space.wrap(
-                    'Wrong index'))
-            return indexes
-
-        def getitem(self, w_index):
-            space = self.space
-            indexes = self._unpack_indexes(space, w_index)
-            pos = compute_pos(space, indexes, self.shape)
-            return space.wrap(self.storage[pos])
-
-        def setitem(self, w_index, w_value):
-            space = self.space
-            indexes = self._unpack_indexes(space, w_index)
-            pos = compute_pos(space, indexes, self.shape)
-            self.storage[pos] = coerce(space, w_value)
-            return space.w_None #XXX: necessary?
-
-        def len(self):
-            space = self.space
-            return space.wrap(self.shape[0])
-    return MultiDimArray
+def compute_slices(space, slices, dim):
+    strides = []
+    i = 1
+    for d in dim:
+        strides.append(i)
+        i *= d
+    strides.reverse()
+    length = i
+    shape = []
+    sliceout = []
+    for v in space.unpackiterable(slices):
+        sliceoutnew=[] #FIXME: Not RPYthon - there are no slices.
+        if space.is_true(space.isinstance(v, space.w_slice)):
+            sl=space.unwrap(v)
+            if sl.step<0:
+                reverse=True
+                sl=slice(sl.stop, sl.start, -sl.step)
+            else:
+                reverse=False
+            stride=strides.pop(0)
+            if sl.step == 1:
+                newsl = [slice(stride*sl.start, stride*sl.stop)]
+            else:
+                newsl = []
+                for i in range(sl.start, sl.stop, sl.step):
+                    newsl.append(slice(stride*i, stride*(i+1)))
+
+            if reverse:
+                newsl.reverse()
+
+            shape.append((sl.stop-sl.start)//sl.step)
+
+            #here multiple old slices x new slices.
+            for sl in sliceout:
+                for sl2 in newsl:
+                    pass #I have no time
+
+        else:
+            #extract item from slices, without appending to shape
+
+        sliceout = sliceoutnew
+
+    return shape, sliceout
+
+#Was undetectable
+class MultiDimArrayAbstract(BaseNumArray):
+
+    def dtype(self, data):
+        return self.__class__.data_type(data)
+
+    def unwrap(self, w_data): #XXX: NOT USED
+        return self.__class__.data_w(w_data)
+
+    def coerce(self, data):
+        return self.__class__.data_coerce(data)
+
+    def __init__(self, space, shape):
+        self.shape = shape
+        self.space = space
+        size = 1
+        for dimension in shape:
+            size *= dimension
+        self.storage = [self.dtype(0.0)] * size
+        make_sure_not_resized(self.storage)
+
+    def _unpack_indexes(self, space, w_index):
+        indexes = [space.int_w(w_i) for w_i in space.fixedview(w_index)]
+        if len(indexes) != len(self.shape):
+            raise OperationError(space.w_IndexError, space.wrap(
+                'Wrong index'))
+        return indexes
+
+    def getitem(self, w_index):
+        space = self.space
+        indexes = self._unpack_indexes(space, w_index)
+        pos = compute_pos(space, indexes, self.shape)
+        return space.wrap(self.storage[pos])
+
+    def setitem(self, w_index, w_value):
+        space = self.space
+        indexes = self._unpack_indexes(space, w_index)
+        pos = compute_pos(space, indexes, self.shape)
+        self.storage[pos] = self.coerce(space, w_value)
+        return space.w_None #XXX: necessary?
+
+    def len(self):
+        space = self.space
+        return space.wrap(self.shape[0])
+
+    def load_iterable(self, space, w_values):
+        self._load_iter(space, w_values, 0)
+
+    def _load_iter(self, space, w_values, start): #TODO: shape check
+        vals=space.unpackiterable(w_values)
+        if space.is_true(space.isinstance(vals[0], space.w_tuple) or space.is_true(space.isinstance(vals[0], space.w_list):
+            idx=start
+            for v in vals:
+                add=self._load_iter(space, v, idx)
+                idx+=add
+            return idx
+        else:
+            idx=start
+            for v in vals:
+                self.storage[idx]=self.unwrap(val)
+                idx+=1
+            return idx
+                
+
+mdarraytype=MultiDimArrayAbstract
+
+class MultiDimIntArray(MultiDimArrayAbstact):
+    data_type, data_w, data_coerce = int, unwrap_int, coerce_int
 
-MultiDimIntArray = create_mdarray(int, unwrap_int, coerce_int)
 MultiDimArray = MultiDimIntArray #XXX: compatibility
-MultiDimFloatArray = create_mdarray(float, unwrap_float, coerce_float)
+
+class MultiDimFloatArray(MultiDimArrayAbstact):
+    data_type, data_w, data_coerce = float, unwrap_float, coerce_float
 
 class ResultFactory(object):
     def __init__(self, space):

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	Tue Dec 29 11:33:09 2009
@@ -50,7 +50,7 @@
         self.space = space
         if w_dtype == space.w_None:
             #TODO: infer type from w_values (better than this)
-            w_dtype = space.type(space.fixedview(w_values)[0]) #FIXME: Allocates an entire array and throws it away!
+            w_dtype = space.type(space.getitem(w_values, space.wrap(0))) #FIXED
 
         self.dtype = w_dtype
         if w_shape == space.w_None:
@@ -64,7 +64,7 @@
                 length = shape_w[0]
                 self.array = sdresult(space, w_dtype)(space, length)
             else:
-                self.array = mdresult(space, w_dtype)(space, unpack_shape(space, w_shape))
+                self.array = mdresult(space, w_dtype)(space, shape_w) #w_shape still may be w_None 
         except KeyError, e:
             raise OperationError(space.w_NotImplementedError,
                     space.wrap("Haven't implemented generic array yet!"))

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	Tue Dec 29 11:33:09 2009
@@ -13,87 +13,104 @@
 from pypy.module.micronumpy.dtype import unwrap_float32, coerce_float32, float32
 
 def create_sdarray(data_type, unwrap, coerce):
-    class NumArray(BaseNumArray):
-        def __init__(self, space, length):
-            self.shape = (1,)
-            self.length = length
-            self.space = space
-            self.storage = [data_type(0.0)] * length
-            make_sure_not_resized(self.storage)
-
-        mul = mul_operation()
-        div = div_operation()
-        add = add_operation()
-        sub = sub_operation()
-        copy = copy_operation()
-
-        def create_scalar_op(f):
-            def scalar_operation(self, space, source, w_x):
-                space = self.space
-                x = self.coerce(space, w_x)
-                for i in range(source.length):
-                    self.storage[i] = f(source.storage[i], x)
-            return scalar_operation
+class NumArrayAbstract(BaseNumArray):
+    
+    def dtype(self, data):
+        return self.__class__.data_type(data)
+
+    def unwrap(self, w_data):
+        return self.__class__.data_w(w_data)
+
+    def coerce(self, data):
+        return self.__class__.data_coerce(data)
+
+    def __init__(self, space, length):
+        self.shape = (length,) #As in numpy
+        self.length = length
+        self.space = space
+        self.storage = [self.dtype(0.0)] * length
+        make_sure_not_resized(self.storage)
+
+    mul = mul_operation()
+    div = div_operation()
+    add = add_operation()
+    sub = sub_operation()
+    copy = copy_operation()
+
+    def create_scalar_op(f):
+        def scalar_operation(self, space, source, w_x):
+            space = self.space
+            x = self.coerce(space, w_x)
+            for i in range(source.length):
+                self.storage[i] = f(source.storage[i], x)
+        return scalar_operation
 
-        mul_scalar = create_scalar_op(mul)
+    mul_scalar = create_scalar_op(mul)
 #        div_scalar = create_scalar_op(div)
 #        add_scalar = create_scalar_op(add)
 #        sub_scalar = create_scalar_op(sub)
 
-        def create_fixedview_op(f):
-            def fixedview_operation(self, w_xs):
-                space = self.space
-                try:
-                    xs = space.fixedview(w_xs, len(self.storage))
-                except UnpackValueError, e:
-                    # w_xs is of the wrong size
-                    raise OperationError(space.w_ValueError,
-                                         space.wrap("shape mismatch: objects cannot be broadcast to the same shape"))
-
-                i = 0
-                for w_x in xs:
-                    self.storage[i] = f(source.storage[i], self.coerce(w_x)) #TODO: probably shouldn't coerce
-                    i += 1
-                return result
-            return fixedview_operation
-
-        copy_iterable = create_fixedview_op(copy)
+    def create_fixedview_op(f):
+        def fixedview_operation(self, w_xs):
+            space = self.space
+            try:
+                xs = space.fixedview(w_xs, len(self.storage))
+            except UnpackValueError, e:
+                # w_xs is of the wrong size
+                raise OperationError(space.w_ValueError,
+                                     space.wrap("shape mismatch: objects cannot be broadcast to the same shape"))
 
-        def load_iterable(self, space, w_values): #FIXME: less than ideal
             i = 0
-            for x in space.fixedview(w_values, self.length):
-                self.storage[i] = unwrap(space, x)
+            for w_x in xs:
+                self.storage[i] = f(source.storage[i], self.coerce(w_x)) #TODO: probably shouldn't coerce
                 i += 1
+            return result
+        return fixedview_operation
 
-        def getitem(self, w_index):
-            space = self.space
-            index = space.int_w(w_index)
-            try:
-                return space.wrap(self.storage[index])
-            except IndexError:
-                raise OperationError(space.w_IndexError,
-                                     space.wrap("list index out of range"))
+    copy_iterable = create_fixedview_op(copy)
 
-        def setitem(self, w_index, w_value):
-            space = self.space
-            index = space.int_w(w_index)
-            try:
-                self.storage[index] = coerce(space, w_value)
-            except IndexError:
-                raise OperationError(space.w_IndexError,
-                                     space.wrap("list index out of range"))
-            return space.w_None
+    def load_iterable(self, space, w_values): #FIXME: less than ideal
+        i = 0
+        for x in space.fixedview(w_values, self.length):
+            self.storage[i] = self.unwrap(space, x)
+            i += 1
+
+    def getitem(self, w_index):
+        space = self.space
+        index = space.int_w(w_index)
+        try:
+            return space.wrap(self.storage[index])
+        except IndexError:
+            raise OperationError(space.w_IndexError,
+                                 space.wrap("list index out of range"))
+
+    def setitem(self, w_index, w_value):
+        space = self.space
+        index = space.int_w(w_index)
+        try:
+            self.storage[index] = self.coerce(space, w_value)
+        except IndexError:
+            raise OperationError(space.w_IndexError,
+                                 space.wrap("list index out of range"))
+        return space.w_None
+
+    def len(self):
+        space = self.space
+        return space.wrap(len(self.storage))
+
+sdarraytype=NumArrayAbstact
+
+class IntArray(NumArrayAbstact):
+    data_type, data_w, data_coerce = int, unwrap_int, coerce_int
 
-        def len(self):
-            space = self.space
-            return space.wrap(len(self.storage))
+NumArray = IntArray #XXX: compatibility
+
+class FloatArray(NumArrayAbstact):
+    data_type, data_w, data_coerce = float, unwrap_float, coerce_float
 
-    return NumArray
+class Float32Array(NumArrayAbstract):
+    data_type, data_w, data_coerce = float32, unwrap_float32, coerce_float32
 
-IntArray = create_sdarray(int, unwrap_int, coerce_int)
-NumArray = IntArray # FIXME: compatibility for now
-FloatArray = create_sdarray(float, unwrap_float, coerce_float)
-Float32Array = create_sdarray(float32, unwrap_float32, coerce_float32)
 GenericArray = None
 
 class ResultFactory(object):

Modified: pypy/branch/micronumpy/pypy/module/micronumpy/ufunc.py
==============================================================================
--- pypy/branch/micronumpy/pypy/module/micronumpy/ufunc.py	(original)
+++ pypy/branch/micronumpy/pypy/module/micronumpy/ufunc.py	Tue Dec 29 11:33:09 2009
@@ -1,11 +1,16 @@
 from pypy.module.micronumpy.ndarray import array, zeros, ndarray
+from pypy.module.micronumpy.sdarray import sdarrytype
+from pypy.module.micronumpy.mdarray import mdarrytype
 from pypy.interpreter.baseobjspace import ObjSpace, W_Root
 from pypy.interpreter.error import OperationError
 
-def minimum(space, w_a, w_b):
+def _assert_both(space, w_a, w_b):
     if not isinstance(w_a, ndarray) or not isinstance(w_b, ndarray):
         raise OperationError(space.w_TypeError,
                              space.wrap("expecting ndarray object"))
+
+def minimum(space, w_a, w_b):
+    _assert_both(w_a, w_b)
     if w_a.array.length != w_b.array.length:
         raise OperationError(space.w_ValueError,
                              space.wrap("minimum of arrays of different length"))
@@ -19,3 +24,15 @@
             res.array.storage[i] = two
     return space.wrap(res)
 minimum.unwrap_spec = [ObjSpace, W_Root, W_Root]
+
+def dot(space, w_a, w_b):
+    _assert_both(w_a, w_b)
+    if len(w_b.array.shape)==1:
+        w_b_new=zeros(space, space.newtuple([space.wrap(1), space.wrap(w_b.array.shape[0])]))
+        for idx, value in enumerate(w_b.array.storage):
+            w_b_new.array.storage[idx]=value
+        w_b=w_b_new
+    
+    #waiting for slice.
+
+



More information about the Pypy-commit mailing list