[pypy-commit] pypy numpy-refactor: start refactoring numpy into something massively simpler

fijal noreply at buildbot.pypy.org
Thu Aug 30 12:21:43 CEST 2012


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: numpy-refactor
Changeset: r56928:334e6bc9078d
Date: 2012-08-30 12:21 +0200
http://bitbucket.org/pypy/pypy/changeset/334e6bc9078d/

Log:	start refactoring numpy into something massively simpler

diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/+interp_numarray.py
rename from pypy/module/micronumpy/interp_numarray.py
rename to pypy/module/micronumpy/+interp_numarray.py
diff --git a/pypy/module/micronumpy/__init__.py b/pypy/module/micronumpy/__init__.py
--- a/pypy/module/micronumpy/__init__.py
+++ b/pypy/module/micronumpy/__init__.py
@@ -27,7 +27,7 @@
         'ones': 'interp_numarray.ones',
         'dot': 'interp_numarray.dot',
         'fromstring': 'interp_support.fromstring',
-        'flatiter': 'interp_numarray.W_FlatIterator',
+        'flatiter': 'interp_flatiter.W_FlatIterator',
         'isna': 'interp_numarray.isna',
         'concatenate': 'interp_numarray.concatenate',
         'repeat': 'interp_numarray.repeat',
diff --git a/pypy/module/micronumpy/arrayimpl/__init__.py b/pypy/module/micronumpy/arrayimpl/__init__.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/micronumpy/arrayimpl/__init__.py
@@ -0,0 +1,8 @@
+
+from pypy.module.micronumpy.arrayimpl import scalar, concrete
+
+def create_implementation(shape, dtype):
+    if not shape:
+        return scalar.Scalar(dtype)
+    else:
+        return concrete.ConcreteArray(shape, dtype)
diff --git a/pypy/module/micronumpy/arrayimpl/base.py b/pypy/module/micronumpy/arrayimpl/base.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/micronumpy/arrayimpl/base.py
@@ -0,0 +1,3 @@
+
+class BaseArrayImplementation(object):
+    pass
diff --git a/pypy/module/micronumpy/arrayimpl/concrete.py b/pypy/module/micronumpy/arrayimpl/concrete.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/micronumpy/arrayimpl/concrete.py
@@ -0,0 +1,9 @@
+
+from pypy.module.micronumpy.arrayimpl import base
+
+class ConcreteArray(base.BaseArrayImplementation):
+    def __init__(self, shape, dtype):
+        self.shape = shape
+
+    def get_shape(self):
+        return self.shape
diff --git a/pypy/module/micronumpy/arrayimpl/scalar.py b/pypy/module/micronumpy/arrayimpl/scalar.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/micronumpy/arrayimpl/scalar.py
@@ -0,0 +1,9 @@
+
+from pypy.module.micronumpy.arrayimpl import base
+
+class Scalar(base.BaseArrayImplementation):
+    def __init__(self, dtype):
+        pass
+
+    def get_shape(self):
+        return []
diff --git a/pypy/module/micronumpy/interp_flatiter.py b/pypy/module/micronumpy/interp_flatiter.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/micronumpy/interp_flatiter.py
@@ -0,0 +1,5 @@
+
+from pypy.interpreter.baseobjspace import Wrappable
+
+class W_FlatIterator(Wrappable):
+    pass
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,9 +1,7 @@
 from pypy.conftest import gettestobjspace
 from pypy.module.micronumpy.interp_dtype import get_dtype_cache
-from pypy.module.micronumpy.interp_numarray import W_NDimArray, Scalar
 from pypy.module.micronumpy.interp_ufuncs import (find_binop_result_dtype,
         find_unaryop_result_dtype)
-from pypy.module.micronumpy.interp_boxes import W_Float64Box
 from pypy.module.micronumpy.interp_dtype import nonnative_byteorder_prefix,\
      byteorder_prefix
 from pypy.conftest import option
@@ -21,59 +19,6 @@
         cls.w_non_native_prefix = cls.space.wrap(nonnative_byteorder_prefix)
         cls.w_native_prefix = cls.space.wrap(byteorder_prefix)
 
-class TestSignature(object):
-    def test_binop_signature(self, space):
-        float64_dtype = get_dtype_cache(space).w_float64dtype
-        bool_dtype = get_dtype_cache(space).w_booldtype
-
-        ar = W_NDimArray([10], dtype=float64_dtype)
-        ar2 = W_NDimArray([10], dtype=float64_dtype)
-        v1 = ar.descr_add(space, ar)
-        v2 = ar.descr_add(space, Scalar(float64_dtype, W_Float64Box(2.0)))
-        sig1 = v1.find_sig()
-        sig2 = v2.find_sig()
-        assert v1 is not v2
-        assert sig1.left.iter_no == sig1.right.iter_no
-        assert sig2.left.iter_no != sig2.right.iter_no
-        assert sig1.left.array_no == sig1.right.array_no
-        sig1b = ar2.descr_add(space, ar).find_sig()
-        assert sig1b.left.array_no != sig1b.right.array_no
-        assert sig1b is not sig1
-        v3 = ar.descr_add(space, Scalar(float64_dtype, W_Float64Box(1.0)))
-        sig3 = v3.find_sig()
-        assert sig2 is sig3
-        v4 = ar.descr_add(space, ar)
-        assert v1.find_sig() is v4.find_sig()
-
-        bool_ar = W_NDimArray([10], dtype=bool_dtype)
-        v5 = ar.descr_add(space, bool_ar)
-        assert v5.find_sig() is not v1.find_sig()
-        assert v5.find_sig() is not v2.find_sig()
-        v6 = ar.descr_add(space, bool_ar)
-        assert v5.find_sig() is v6.find_sig()
-        v7 = v6.descr_add(space, v6)
-        sig7 = v7.find_sig()
-        assert sig7.left.left.iter_no == sig7.right.left.iter_no
-        assert sig7.left.left.iter_no != sig7.right.right.iter_no
-        assert sig7.left.right.iter_no == sig7.right.right.iter_no
-        v1.forced_result = ar
-        assert v1.find_sig() is not sig1
-
-    def test_slice_signature(self, space):
-        float64_dtype = get_dtype_cache(space).w_float64dtype
-
-        ar = W_NDimArray([10], dtype=float64_dtype)
-        v1 = ar.descr_getitem(space, space.wrap(slice(1, 3, 1)))
-        v2 = ar.descr_getitem(space, space.wrap(slice(4, 6, 1)))
-        assert v1.find_sig() is v2.find_sig()
-
-        v3 = v2.descr_add(space, v1)
-        v4 = v1.descr_add(space, v2)
-        assert v3.find_sig() is v4.find_sig()
-        v5 = ar.descr_add(space, ar).descr_getitem(space, space.wrap(slice(1, 3, 1)))
-        v6 = ar.descr_add(space, ar).descr_getitem(space, space.wrap(slice(1, 4, 1)))
-        assert v5.find_sig() is v6.find_sig()
-
 class TestUfuncCoerscion(object):
     def test_binops(self, space):
         bool_dtype = get_dtype_cache(space).w_booldtype
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
@@ -5,7 +5,7 @@
 from pypy.interpreter.error import OperationError
 from pypy.module.micronumpy.appbridge import get_appbridge_cache
 from pypy.module.micronumpy.interp_iter import Chunk, Chunks
-from pypy.module.micronumpy.interp_numarray import W_NDimArray, shape_agreement
+from pypy.module.micronumpy.interp_numarray import W_NDimArray
 from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
 
 class MockDtype(object):
@@ -35,17 +35,17 @@
         return self.space.newtuple(args_w)
 
     def test_strides_f(self):
-        a = W_NDimArray([10, 5, 3], MockDtype(), 'F')
+        a = W_NDimArray([10, 5, 3], MockDtype(), order='F')
         assert a.strides == [1, 10, 50]
         assert a.backstrides == [9, 40, 100]
 
     def test_strides_c(self):
-        a = W_NDimArray([10, 5, 3], MockDtype(), 'C')
+        a = W_NDimArray([10, 5, 3], MockDtype(), order='C')
         assert a.strides == [15, 3, 1]
         assert a.backstrides == [135, 12, 2]
 
     def test_create_slice_f(self):
-        a = W_NDimArray([10, 5, 3], MockDtype(), 'F')
+        a = W_NDimArray([10, 5, 3], MockDtype(), order='F')
         s = create_slice(a, [Chunk(3, 0, 0, 1)])
         assert s.start == 3
         assert s.strides == [10, 50]
@@ -63,7 +63,7 @@
         assert s.shape == [10, 3]
 
     def test_create_slice_c(self):
-        a = W_NDimArray([10, 5, 3], MockDtype(), 'C')
+        a = W_NDimArray([10, 5, 3], MockDtype(), order='C')
         s = create_slice(a, [Chunk(3, 0, 0, 1)])
         assert s.start == 45
         assert s.strides == [3, 1]
@@ -83,7 +83,7 @@
         assert s.shape == [10, 3]
 
     def test_slice_of_slice_f(self):
-        a = W_NDimArray([10, 5, 3], MockDtype(), 'F')
+        a = W_NDimArray([10, 5, 3], MockDtype(), order='F')
         s = create_slice(a, [Chunk(5, 0, 0, 1)])
         assert s.start == 5
         s2 = create_slice(s, [Chunk(3, 0, 0, 1)])
@@ -117,7 +117,7 @@
         assert s2.start == 1 * 15 + 2 * 3
 
     def test_negative_step_f(self):
-        a = W_NDimArray([10, 5, 3], MockDtype(), 'F')
+        a = W_NDimArray([10, 5, 3], MockDtype(), order='F')
         s = create_slice(a, [Chunk(9, -1, -2, 5)])
         assert s.start == 9
         assert s.strides == [-2, 10, 50]
@@ -131,7 +131,7 @@
         assert s.backstrides == [-120, 12, 2]
 
     def test_index_of_single_item_f(self):
-        a = W_NDimArray([10, 5, 3], MockDtype(), 'F')
+        a = W_NDimArray([10, 5, 3], MockDtype(), order='F')
         r = a._index_of_single_item(self.space, self.newtuple(1, 2, 2))
         assert r == 1 + 2 * 10 + 2 * 50
         s = create_slice(a, [Chunk(0, 10, 1, 10), Chunk(2, 0, 0, 1)])
@@ -141,7 +141,7 @@
         assert r == a._index_of_single_item(self.space, self.newtuple(1, 2, 1))
 
     def test_index_of_single_item_c(self):
-        a = W_NDimArray([10, 5, 3], MockDtype(), 'C')
+        a = W_NDimArray([10, 5, 3], MockDtype(), order='C')
         r = a._index_of_single_item(self.space, self.newtuple(1, 2, 2))
         assert r == 1 * 3 * 5 + 2 * 3 + 2
         s = create_slice(a, [Chunk(0, 10, 1, 10), Chunk(2, 0, 0, 1)])
@@ -151,6 +151,7 @@
         assert r == a._index_of_single_item(self.space, self.newtuple(1, 2, 1))
 
     def test_shape_agreement(self):
+        from pypy.module.micronumpy.interp_numarray import shape_agreement
         assert shape_agreement(self.space, [3], [3]) == [3]
         assert shape_agreement(self.space, [1, 2, 3], [1, 2, 3]) == [1, 2, 3]
         py.test.raises(OperationError, shape_agreement, self.space, [2], [3])
@@ -251,6 +252,8 @@
         a = ndarray(3, dtype=int)
         assert a.shape == (3,)
         assert a.dtype is dtype(int)
+        a = ndarray([], dtype=float)
+        assert a.shape == ()
 
     def test_ndmin(self):
         from _numpypy import array


More information about the pypy-commit mailing list