[pypy-commit] pypy numpy-multidim: for a good start - support for zeroes

fijal noreply at buildbot.pypy.org
Wed Oct 26 22:14:58 CEST 2011


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: numpy-multidim
Changeset: r48488:159a0cccc4a9
Date: 2011-10-26 22:13 +0200
http://bitbucket.org/pypy/pypy/changeset/159a0cccc4a9/

Log:	for a good start - support for zeroes

diff --git a/pypy/module/micronumpy/compile.py b/pypy/module/micronumpy/compile.py
--- a/pypy/module/micronumpy/compile.py
+++ b/pypy/module/micronumpy/compile.py
@@ -5,7 +5,7 @@
 
 from pypy.interpreter.baseobjspace import InternalSpaceCache, W_Root
 from pypy.module.micronumpy.interp_dtype import W_Float64Dtype
-from pypy.module.micronumpy.interp_numarray import Scalar, SingleDimArray, BaseArray
+from pypy.module.micronumpy.interp_numarray import Scalar, NDimArray, BaseArray
 from pypy.rlib.objectmodel import specialize
 
 
@@ -13,7 +13,7 @@
     pass
 
 def create_array(dtype, size):
-    a = SingleDimArray(size, dtype=dtype)
+    a = NDimArray(size, dtype=dtype)
     for i in range(size):
         dtype.setitem(a.storage, i, dtype.box(float(i % 10)))
     return a
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
@@ -46,7 +46,7 @@
         dtype = space.interp_w(interp_dtype.W_Dtype,
             space.call_function(space.gettypefor(interp_dtype.W_Dtype), w_dtype)
         )
-        arr = SingleDimArray(len(l), dtype=dtype)
+        arr = NDimArray(len(l), [len(l)], dtype=dtype)
         i = 0
         for w_elem in l:
             dtype.setitem_w(space, arr.storage, i, w_elem)
@@ -348,7 +348,7 @@
         i = 0
         signature = self.signature
         result_size = self.find_size()
-        result = SingleDimArray(result_size, self.find_dtype())
+        result = NDimArray(result_size, [result_size], self.find_dtype())
         while i < result_size:
             numpy_driver.jit_merge_point(signature=signature,
                                          result_size=result_size, i=i,
@@ -468,6 +468,7 @@
         raise NotImplementedError
 
     def descr_len(self, space):
+        # XXX find shape first
         return space.wrap(self.find_size())
 
     def calc_index(self, item):
@@ -510,10 +511,11 @@
         return (self.start + item * self.step)
 
 
-class SingleDimArray(BaseArray):
-    def __init__(self, size, dtype):
+class NDimArray(BaseArray):
+    def __init__(self, size, shape, dtype):
         BaseArray.__init__(self)
         self.size = size
+        self.shape = shape
         self.dtype = dtype
         self.storage = dtype.malloc(size)
         self.signature = dtype.signature
@@ -534,7 +536,7 @@
         return self.dtype.getitem(self.storage, i)
 
     def descr_len(self, space):
-        return space.wrap(self.size)
+        return space.wrap(self.shape[0])
 
     def setitem_w(self, space, item, w_value):
         self.invalidated()
@@ -550,12 +552,21 @@
     def __del__(self):
         lltype.free(self.storage, flavor='raw', track_allocation=False)
 
- at unwrap_spec(size=int)
-def zeros(space, size, w_dtype=None):
+def zeros(space, w_size, w_dtype=None):
     dtype = space.interp_w(interp_dtype.W_Dtype,
         space.call_function(space.gettypefor(interp_dtype.W_Dtype), w_dtype)
     )
-    return space.wrap(SingleDimArray(size, dtype=dtype))
+    if space.isinstance_w(w_size, space.w_int):
+        size = space.int_w(w_size)
+        shape = [size]
+    else:
+        size = 1
+        shape = []
+        for w_item in space.fixedview(w_size):
+            item = space.int_w(w_item)
+            size *= item
+            shape.append(item)
+    return space.wrap(NDimArray(size, shape, dtype=dtype))
 
 @unwrap_spec(size=int)
 def ones(space, size, w_dtype=None):
@@ -563,7 +574,7 @@
         space.call_function(space.gettypefor(interp_dtype.W_Dtype), w_dtype)
     )
 
-    arr = SingleDimArray(size, dtype=dtype)
+    arr = NDimArray(size, [size], dtype=dtype)
     one = dtype.adapt_val(1)
     for i in xrange(size):
         arr.dtype.setitem(arr.storage, i, one)
diff --git a/pypy/module/micronumpy/interp_support.py b/pypy/module/micronumpy/interp_support.py
--- a/pypy/module/micronumpy/interp_support.py
+++ b/pypy/module/micronumpy/interp_support.py
@@ -9,7 +9,7 @@
 
 @unwrap_spec(s=str)
 def fromstring(space, s):
-    from pypy.module.micronumpy.interp_numarray import SingleDimArray
+    from pypy.module.micronumpy.interp_numarray import NDimArray
     length = len(s)
 
     if length % FLOAT_SIZE == 0:
@@ -19,7 +19,7 @@
             "string length %d not divisable by %d" % (length, FLOAT_SIZE)))
 
     dtype = space.fromcache(W_Float64Dtype)
-    a = SingleDimArray(number, dtype=dtype)
+    a = NDimArray(number, dtype=dtype)
 
     start = 0
     end = FLOAT_SIZE
@@ -31,4 +31,4 @@
         start += FLOAT_SIZE
         end += FLOAT_SIZE
 
-    return space.wrap(a)
\ No newline at end of file
+    return space.wrap(a)
diff --git a/pypy/module/micronumpy/test/test_base.py b/pypy/module/micronumpy/test/test_base.py
--- a/pypy/module/micronumpy/test/test_base.py
+++ b/pypy/module/micronumpy/test/test_base.py
@@ -1,6 +1,6 @@
 from pypy.conftest import gettestobjspace
 from pypy.module.micronumpy import interp_dtype
-from pypy.module.micronumpy.interp_numarray import SingleDimArray, Scalar
+from pypy.module.micronumpy.interp_numarray import NDimArray, Scalar
 from pypy.module.micronumpy.interp_ufuncs import (find_binop_result_dtype,
         find_unaryop_result_dtype)
 
@@ -13,7 +13,7 @@
     def test_binop_signature(self, space):
         float64_dtype = space.fromcache(interp_dtype.W_Float64Dtype)
 
-        ar = SingleDimArray(10, dtype=float64_dtype)
+        ar = NDimArray(10, dtype=float64_dtype)
         v1 = ar.descr_add(space, ar)
         v2 = ar.descr_add(space, Scalar(float64_dtype, 2.0))
         assert v1.signature is not v2.signature
@@ -22,7 +22,7 @@
         v4 = ar.descr_add(space, ar)
         assert v1.signature is v4.signature
 
-        bool_ar = SingleDimArray(10, dtype=space.fromcache(interp_dtype.W_BoolDtype))
+        bool_ar = NDimArray(10, dtype=space.fromcache(interp_dtype.W_BoolDtype))
         v5 = ar.descr_add(space, bool_ar)
         assert v5.signature is not v1.signature
         assert v5.signature is not v2.signature
@@ -30,7 +30,7 @@
         assert v5.signature is v6.signature
 
     def test_slice_signature(self, space):
-        ar = SingleDimArray(10, dtype=space.fromcache(interp_dtype.W_Float64Dtype))
+        ar = NDimArray(10, dtype=space.fromcache(interp_dtype.W_Float64Dtype))
         v1 = ar.descr_getitem(space, space.wrap(slice(1, 5, 1)))
         v2 = ar.descr_getitem(space, space.wrap(slice(4, 6, 1)))
         assert v1.signature is v2.signature
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
@@ -599,6 +599,11 @@
             for i in xrange(5):
                 assert c[i] == func(b[i], 3)
 
+class AppTestMultiDim(BaseNumpyAppTest):
+    def test_init(self):
+        import numpy
+        a = numpy.zeros((2, 2))
+        assert len(a) == 2
 
 class AppTestSupport(object):
     def setup_class(cls):


More information about the pypy-commit mailing list