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

simonb at codespeak.net simonb at codespeak.net
Mon Aug 27 20:29:39 CEST 2007


Author: simonb
Date: Mon Aug 27 20:29:37 2007
New Revision: 46058

Modified:
   pypy/dist/pypy/rpython/numpy/aarray.py
   pypy/dist/pypy/rpython/numpy/test/test_array.py
Log:
hacking on numpy, implement some coercion, ran into problem with SomeExternalObject method annotation

Modified: pypy/dist/pypy/rpython/numpy/aarray.py
==============================================================================
--- pypy/dist/pypy/rpython/numpy/aarray.py	(original)
+++ pypy/dist/pypy/rpython/numpy/aarray.py	Mon Aug 27 20:29:37 2007
@@ -1,10 +1,10 @@
 from pypy.rpython.extregistry import ExtRegistryEntry
 from pypy.annotation.pairtype import pairtype
 from pypy.annotation.model import SomeExternalObject, SomeList, SomeImpossibleValue
-from pypy.annotation.model import SomeInteger, SomeFloat, SomeString, SomeChar
-from pypy.annotation.listdef import ListDef
+from pypy.annotation.model import SomeObject, SomeInteger, SomeFloat, SomeString, SomeChar
 from pypy.tool.error import AnnotatorError
 from pypy.rpython.lltypesystem import rffi
+from pypy.rlib import rarithmetic
 
 import numpy
 
@@ -21,13 +21,13 @@
         'I' : SomeInteger(knowntype=rffi.r_uint),
         'L' : SomeInteger(knowntype=rffi.r_ulong),
         'Q' : SomeInteger(knowntype=rffi.r_ulonglong),
-        'f' : SomeFloat(), # XX single precision float XX
+        #'f' : SomeFloat(), # XX single precision float XX
         'd' : SomeFloat(),
     }
-    def __init__(self, knowntype, typecode):
-        self.knowntype = knowntype
+    def __init__(self, knowntype, typecode, rank=1):
+        self.knowntype = knowntype # == numpy.ndarray (do we need this for anything?)
         self.typecode = typecode
-        self.rank = 1
+        self.rank = rank
 
     def can_be_none(self):
         return True
@@ -47,9 +47,24 @@
         return self.typecode_to_item[self.typecode]
 
 class __extend__(pairtype(SomeArray, SomeArray)):
-    def add((s_arr1,s_arr2)):
-        # TODO: coerce the array types
-        return SomeArray(s_arr1.knowntype, s_arr1.typecode)
+
+    def union((s_arr1, s_arr2)):
+        item1 = s_arr1.get_item_type()
+        item2 = s_arr2.get_item_type()
+        typecode = None
+        if float in (item1.knowntype, item2.knowntype):
+            typecode = 'd'
+        else:
+            item_knowntype = rarithmetic.compute_restype(item1.knowntype, item2.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(s_arr1.knowntype, typecode)
+
+    add = sub = mul = div = truediv = union
+
 
 class __extend__(pairtype(SomeArray, SomeInteger)):
     def setitem((s_cto, s_index), s_value):
@@ -71,7 +86,7 @@
     (SomeInteger, rffi.r_uint) : 'I', 
     (SomeInteger, rffi.r_ulong) : 'L', 
     (SomeInteger, rffi.r_ulonglong) : 'Q', 
-    (SomeFloat, float) : 'f', 
+    #(SomeFloat, float) : 'f', 
     (SomeFloat, float) : 'd', 
 }
 valid_typecodes='bhilqBHILQfd'
@@ -87,7 +102,7 @@
         # First guess type from input list
         listitem = arg_list.listdef.listitem
         key = type(listitem.s_value), listitem.s_value.knowntype
-        typecode = numpy_typedict.get( key, None )
+        typecode = numpy_typedict.get(key, None)
 
         # now see if the dtype arg over-rides the typecode
         dtype = None
@@ -119,6 +134,11 @@
         from pypy.rpython.numpy.rarray import ArrayRepr
         return ArrayRepr(rtyper, s_array)
 
+    def get_field_annotation(self, knowntype, fieldname):
+        if fieldname in ('transpose',):
+            # XX knowntype is not enough to learn annotation from XX
+            return SomeArray()
+
 
 
 # Importing for side effect of registering types with extregistry

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	Mon Aug 27 20:29:37 2007
@@ -13,6 +13,7 @@
 from pypy.rpython.lltypesystem import rffi
 from pypy.rpython.rint import IntegerRepr
 from pypy.rpython.numpy.rarray import ArrayRepr
+from pypy.rpython.numpy.aarray import SomeArray
 
 import numpy
 
@@ -24,13 +25,13 @@
     return my_array[0]
 
 class Test_annotation:
-    def test_annotate_array_access_int(self):
+    def test_array_access_int(self):
         t = TranslationContext()
         a = t.buildannotator()
         s = a.build_types(access_array, [int])
         assert s.knowntype == rffi.r_int
 
-    def test_annotate_array_access_float(self):
+    def test_array_access_float(self):
         t = TranslationContext()
         a = t.buildannotator()
         s = a.build_types(access_array, [float])
@@ -39,9 +40,9 @@
         if conftest.option.view:
             t.view()
 
-    def test_annotate_array_access_bytype(self):
+    def test_array_access_bytype(self):
         def access_array_bytype(dummy):
-            my_array = numpy.array([1],'f')
+            my_array = numpy.array([1],'d')
             return my_array[0]
 
         t = TranslationContext()
@@ -52,7 +53,7 @@
         if conftest.option.view:
             t.view()
 
-    def test_annotate_array_access_variable(self):
+    def test_array_access_variable(self):
         def access_with_variable():
             my_array = numpy.array(range(10))
             my_array[2] = 2
@@ -67,6 +68,40 @@
         s = a.build_types(access_with_variable, [])
         assert s.knowntype == rffi.r_int
 
+    def test_array_add(self):
+        def f():
+            a1 = numpy.array([1,2])
+            a2 = numpy.array([6,9])
+            return a1 + a2
+
+        t = TranslationContext()
+        a = t.buildannotator()
+        s = a.build_types(f, [])
+        assert s.typecode == 'i'
+
+    def test_array_add_coerce(self):
+        def f():
+            a1 = numpy.array([1,2])
+            a2 = numpy.array([6.,9.])
+            return a1 + a2
+
+        t = TranslationContext()
+        a = t.buildannotator()
+        s = a.build_types(f, [])
+        assert s.typecode == 'd'
+
+    def test_array_method(self):
+        def f():
+            a1 = numpy.array([1,2])
+            return a1.transpose()
+
+        t = TranslationContext()
+        a = t.buildannotator()
+        s = a.build_types(f, [])
+        assert type(s) == SomeArray
+
+
+
 class Test_specialization:
     def test_specialize_array_create(self):
         def create_array():



More information about the Pypy-commit mailing list