[pypy-svn] r46412 - in pypy/dist/pypy/rpython/numpy: . test

simonb at codespeak.net simonb at codespeak.net
Sat Sep 8 03:53:55 CEST 2007


Author: simonb
Date: Sat Sep  8 03:53:54 2007
New Revision: 46412

Modified:
   pypy/dist/pypy/rpython/numpy/aarray.py
   pypy/dist/pypy/rpython/numpy/rarray.py
   pypy/dist/pypy/rpython/numpy/test/test_array.py
Log:
hacking on arithmetic. broke it.

Modified: pypy/dist/pypy/rpython/numpy/aarray.py
==============================================================================
--- pypy/dist/pypy/rpython/numpy/aarray.py	(original)
+++ pypy/dist/pypy/rpython/numpy/aarray.py	Sat Sep  8 03:53:54 2007
@@ -9,6 +9,23 @@
 
 import numpy
 
+numpy_typedict = {
+    (SomeInteger, rffi.r_signedchar) : 'b', 
+    (SomeInteger, rffi.r_short) : 'h', 
+    (SomeInteger, rffi.r_int) : 'i', 
+    (SomeInteger, rffi.r_long) : 'l', 
+    (SomeInteger, int) : 'l', 
+    (SomeInteger, rffi.r_longlong) : 'q', 
+    (SomeInteger, rffi.r_uchar) : 'B', 
+    (SomeInteger, rffi.r_ushort) : 'H', 
+    (SomeInteger, rffi.r_uint) : 'I', 
+    (SomeInteger, rffi.r_ulong) : 'L', 
+    (SomeInteger, rffi.r_ulonglong) : 'Q', 
+    #(SomeFloat, float) : 'f', 
+    (SomeFloat, float) : 'd', 
+}
+valid_typecodes='bhilqBHILQd'
+
 class SomeArray(SomeObject):
     """Stands for an object from the numpy module."""
     typecode_to_item = {
@@ -25,12 +42,16 @@
         #'f' : SomeFloat(), # XX single precision float XX
         'd' : SomeFloat(),
     }
+
     def __init__(self, typecode, ndim=1):
         if not typecode in self.typecode_to_item:
             raise AnnotatorError("bad typecode: %r"%typecode)
         self.typecode = typecode
         self.ndim = ndim
 
+    def get_one_dim(self):
+        return SomeArray(self.typecode)
+
     def can_be_none(self):
         return True
 
@@ -76,11 +97,34 @@
                     break
         if typecode is None:
             raise AnnotatorError()
+        ndim = max(s_arr1.ndim, s_arr2.ndim)
         return SomeArray(typecode, s_arr1.ndim)
 
     # union ?
     sub = mul = div = add
 
+class __extend__(pairtype(SomeArray, SomeFloat)):
+    def add((s_arr, s_flt)):
+        item = s_arr.get_item_type()
+        typecode = None
+        if float in (item.knowntype, s_flt.knowntype):
+            typecode = 'd'
+        else:
+            item_knowntype = rarithmetic.compute_restype(item.knowntype, s_flt.knowntype)
+            for typecode, s_item in SomeArray.typecode_to_item.items():
+                if s_item.knowntype == item_knowntype:
+                    break
+        if typecode is None:
+            raise AnnotatorError()
+        return SomeArray(typecode, s_arr.ndim)
+    # union ?
+    sub = mul = div = add
+
+class __extend__(pairtype(SomeFloat, SomeArray)):
+    def add((s_flt, s_arr)):
+        return pair(s_arr, s_flt).add()
+    # union ?
+    sub = mul = div = add
 
 class __extend__(pairtype(SomeArray, SomeTuple)):
     def get_leftover_dim((s_array, s_index)):
@@ -133,23 +177,6 @@
         s_tuple = SomeTuple([s_index])
         return pair(s_array, s_tuple).getitem()
 
-numpy_typedict = {
-    (SomeInteger, rffi.r_signedchar) : 'b', 
-    (SomeInteger, rffi.r_short) : 'h', 
-    (SomeInteger, rffi.r_int) : 'i', 
-    (SomeInteger, rffi.r_long) : 'l', 
-    (SomeInteger, int) : 'l', 
-    (SomeInteger, rffi.r_longlong) : 'q', 
-    (SomeInteger, rffi.r_uchar) : 'B', 
-    (SomeInteger, rffi.r_ushort) : 'H', 
-    (SomeInteger, rffi.r_uint) : 'I', 
-    (SomeInteger, rffi.r_ulong) : 'L', 
-    (SomeInteger, rffi.r_ulonglong) : 'Q', 
-    #(SomeFloat, float) : 'f', 
-    (SomeFloat, float) : 'd', 
-}
-valid_typecodes='bhilqBHILQfd'
-
 class ArrayCallEntry(ExtRegistryEntry):
     _about_ = numpy.array
 

Modified: pypy/dist/pypy/rpython/numpy/rarray.py
==============================================================================
--- pypy/dist/pypy/rpython/numpy/rarray.py	(original)
+++ pypy/dist/pypy/rpython/numpy/rarray.py	Sat Sep  8 03:53:54 2007
@@ -18,6 +18,7 @@
 from pypy.rlib.unroll import unrolling_iterable
 from pypy.annotation import listdef
 from pypy.rpython.memory.lltypelayout import sizeof
+from pypy.rlib.objectmodel import debug_assert
 
 def gen_build_from_shape(ndim, zero=False):
     unrolling_dims = unrolling_iterable(reversed(range(ndim)))
@@ -104,8 +105,8 @@
         # Suffix of ao.shape must match target_ao.shape
         # (suffix starts at the first non-1 entry in ao.shape.)
         # ao.shape must be no longer than target_ao.shape.
-        assert ao.ndim <= ndim
-        assert target_ao.ndim == ndim
+        debug_assert(ao.ndim <= ndim, "ao.ndim <= ndim")
+        debug_assert(target_ao.ndim == ndim, "target_ao.ndim == ndim")
         # XX check suffix condition here... ?
         broadcast = ao.ndim < ndim
         i = 0
@@ -131,7 +132,7 @@
 
     def ll_iter_broadcast_to_shape(ITER, ao, target_ao, iter_reset):
         "iterate over <ao> but broadcast to the shape of <target_ao>"
-        assert target_ao.ndim == ndim
+        debug_assert(target_ao.ndim == ndim, "target_ao.ndim == ndim")
         delta = j = ndim - ao.ndim
         shape = target_ao.shape
         for i in range(ao.ndim):
@@ -183,7 +184,7 @@
 def ll_array_set(it0, it1, iter_next):
     if it0.size == 0:
         return # empty LHS..
-    assert it0.size == it1.size
+    debug_assert(it0.size == it1.size, "it0.size == it1.size")
     while it0.index < it0.size:
         it0.dataptr[0] = it1.dataptr[0]
         iter_next(it0)
@@ -194,6 +195,15 @@
         it.dataptr[0] = value
         iter_next(it)
 
+def ll_array_add(it0, it1, it2, iter_next):
+    debug_assert(it0.size == it1.size, "it0.size == it1.size")
+    debug_assert(it1.size == it2.size, "it0.size == it1.size")
+    while it0.index < it0.size:
+        it0.dataptr[0] = it1.dataptr[0] + it2.dataptr[0]
+        iter_next(it0)
+        iter_next(it1)
+        iter_next(it2)
+
 def dim_of_ITER(ITER):
     return ITER.TO.coordinates.length
 
@@ -303,9 +313,36 @@
 
 class __extend__(pairtype(ArrayRepr, ArrayRepr)):
     def rtype_add((r_array1, r_array2), hop):
-        v_arr1, v_arr2 = hop.inputargs(r_array1, r_array2)
-        cARRAY = hop.inputconst(Void, hop.r_result.ARRAY.TO)
-        return hop.gendirectcall(ll_add, cARRAY, v_arr1, v_arr2)
+        v_array1, v_array2 = hop.inputargs(r_array1, r_array2)
+        r_array0 = hop.r_result
+        cARRAY = hop.inputconst(Void, r_array0.ARRAY.TO)
+        # We build a contiguous "result" array
+        # from the largest of the two args:
+        v_bigarray = hop.gendirectcall(ll_find_largest, cARRAY, v_array1, v_array2)
+        v_array0 = hop.gendirectcall(ll_build_like, cARRAY, v_bigarray)
+        iter_new, iter_reset, iter_broadcast, iter_next = gen_iter_funcs(r_array0.ndim)
+        cITER = hop.inputconst(Void, r_array0.ITER.TO)
+        creset = hop.inputconst(Void, iter_reset)
+        cbroadcast = hop.inputconst(Void, iter_broadcast)
+        cnext = hop.inputconst(Void, iter_next)
+        v_it0 = hop.gendirectcall(iter_new, cITER, v_array0, v_bigarray, creset, cbroadcast)
+        v_it1 = hop.gendirectcall(iter_new, cITER, v_array1, v_bigarray, creset, cbroadcast)
+        v_it2 = hop.gendirectcall(iter_new, cITER, v_array2, v_bigarray, creset, cbroadcast)
+        return hop.gendirectcall(ll_array_add, v_it0, v_it1, v_it2, cnext)
+
+class __extend__(pairtype(ArrayRepr, Repr)):
+    def rtype_add((r_array, r_ob), hop):
+        assert 0
+        v_ob = hop.inputarg(r_ob, 1)
+        if isinstance(r_ob.lowleveltype, Primitive):
+            r_item, v_item = convert_scalar_to_array(r_array, v_ob, hop.llops)
+
+class __extend__(pairtype(Repr, ArrayRepr)):
+    def rtype_add((r_ob, r_array), hop):
+        # XX ach! how to get this to work ??
+        assert 0
+        return pair(r_array, r_ob).rtype_add(hop)
+
 
         
 def gen_getset_item(ndim):
@@ -377,7 +414,7 @@
             array.strides[tgt_i] = ao.strides[src_i]
             tgt_i += 1
             src_i += 1
-        assert tgt_i == ndim
+        debug_assert(tgt_i == ndim, "tgt_i == ndim")
         array.dataptr = dataptr
         array.data = ao.data # keep a ref
         return array
@@ -405,15 +442,17 @@
     #v_array = llops.gendirectcall(ll_build_alias_to_list, cARRAY, v_list) # nice idea...
     return v_array
 
-def convert_scalar_to_array(ITEM, v_item, llops):
+def convert_scalar_to_array(r_array, v_item, llops):
     # x -> array([x])
+    s_array = r_array.s_array.get_one_dim()
+    r_array = llops.rtyper.getrepr(s_array)
     from pypy.rpython.rmodel import inputconst
-    ARRAY, _ = ArrayRepr.make_types(1, ITEM)
-    cARRAY = inputconst(Void, ARRAY.TO)
-    cITEM = inputconst(Void, ITEM)
-    v_casted = llops.genop("cast_primitive", [v_item], ITEM)
+#    ARRAY, _ = ArrayRepr.make_types(1, ITEM)
+    cARRAY = inputconst(Void, r_array.ARRAY.TO)
+    cITEM = inputconst(Void, r_array.ITEM)
+    v_casted = llops.genop("cast_primitive", [v_item], r_array.ITEM)
     v_array = llops.gendirectcall(ll_build_from_scalar, cARRAY, v_casted)
-    return v_array
+    return r_array, v_array
 
 class __extend__(pairtype(ArrayRepr, Repr)):
     def rtype_getitem((r_array, r_key), hop):
@@ -476,7 +515,7 @@
                 source_ndim = r_item.ndim
             elif isinstance(r_item.lowleveltype, Primitive):
                 # "broadcast" a scalar
-                v_item = convert_scalar_to_array(r_view.ITEM, v_item, hop.llops)
+                r_item, v_item = convert_scalar_to_array(r_view, v_item, hop.llops)
                 source_ndim = 1
             elif isinstance(r_item, AbstractBaseListRepr):
                 v_item = convert_list_to_array(r_item, v_item, hop.llops)
@@ -487,7 +526,6 @@
             v_it1 = hop.gendirectcall(iter_new, cITER, v_item, v_view, creset, cbroadcast)
             return hop.gendirectcall(ll_array_set, v_it0, v_it1, cnext) 
 
-
 class __extend__(pairtype(ArrayRepr, ArrayRepr)):
     def convert_from_to((r_array0, r_array1), v, llops):
         assert 0
@@ -564,6 +602,29 @@
     array.dataptr = ao.dataptr
     return array
 
+def ll_find_largest(ARRAY, array0, array1):
+    sz0 = ll_mul_list(array0.shape, array0.ndim)
+    sz1 = ll_mul_list(array1.shape, array1.ndim)
+    # XXX 
+    if sz0 > sz1:
+        return array0
+    return array1
+
+def ll_build_like(ARRAY, ao):
+    array = ll_allocate(ARRAY, ao.ndim)
+    sz = ll_mul_list(ao.shape)
+    array.data = malloc(ARRAY.data.TO, sz)
+    array.dataptr = array.data
+    itemsize = 1
+    i = ao.ndim - 1
+    while i >= 0:
+        size = ao.shape[i]
+        array.shape[i] = size
+        array.strides[i] = itemsize
+        itemsize *= size
+        i -= 1
+    return array
+
 def ll_setitem1(array, index, item):
     array.data[index] = item
 

Modified: pypy/dist/pypy/rpython/numpy/test/test_array.py
==============================================================================
--- pypy/dist/pypy/rpython/numpy/test/test_array.py	(original)
+++ pypy/dist/pypy/rpython/numpy/test/test_array.py	Sat Sep  8 03:53:54 2007
@@ -34,6 +34,7 @@
     return my_array[0]
 
 class Test_annotation:
+
     def test_annotate_array_access_int(self):
         t = TranslationContext()
         a = t.buildannotator()
@@ -105,6 +106,7 @@
         assert s.ndim == 2
 
     def test_annotate_array_add(self):
+        py.test.skip("broken ATM")
         def f():
             a1 = numpy.array([1,2])
             a2 = numpy.array([6,9])
@@ -116,6 +118,7 @@
         assert s.typecode == 'i'
 
     def test_annotate_array_add_coerce(self):
+        py.test.skip("broken ATM")
         def f():
             a1 = numpy.array([1,2])
             a2 = numpy.array([6.,9.])
@@ -126,6 +129,18 @@
         s = a.build_types(f, [])
         assert s.typecode == 'd'
 
+    def test_annotate_array_add_2(self):
+        py.test.skip("broken ATM")
+        def f():
+            a = numpy.array([1,2])
+            a = a + 3
+            return a
+
+        t = TranslationContext()
+        a = t.buildannotator()
+        s = a.build_types(f, [])
+        assert s.typecode == 'i'
+
     def test_annotate_array_dtype(self):
         def f():
             a1 = numpy.array([1,2], dtype='d')
@@ -266,6 +281,7 @@
         assert res == 45
 
     def test_specialize_array_add(self):
+        py.test.skip("broken ATM")
         def f():
             a1 = numpy.array([1.,2.])
             a2 = numpy.array([6,9])
@@ -275,6 +291,14 @@
         assert res.data[0] == 7
         assert res.data[1] == 11
 
+        def f():
+            a1 = numpy.array([1,2])
+            return a1 + 2
+
+        res = interpret(f, [])
+        assert res.data[0] == 3
+        assert res.data[1] == 4
+
     def test_specialize_array_attr(self):
         def f():
             a = numpy.array([1,2])



More information about the Pypy-commit mailing list