Author: mattip <matti.pi...@gmail.com>
Branch: numpypy-complex2
Changeset: r57256:0639de218850
Date: 2012-09-09 23:13 +0300
http://bitbucket.org/pypy/pypy/changeset/0639de218850/

Log:    add tests for fmax, discover problem with converting complex to
        float and raise TypeError

diff --git a/pypy/module/micronumpy/interp_boxes.py 
b/pypy/module/micronumpy/interp_boxes.py
--- a/pypy/module/micronumpy/interp_boxes.py
+++ b/pypy/module/micronumpy/interp_boxes.py
@@ -42,6 +42,9 @@
         self.imag = imag
 
     def convert_to(self, dtype):
+        from pypy.module.micronumpy.types import ComplexFloating
+        if not isinstance(dtype.itemtype, ComplexFloating):
+            raise TypeError('cannot convert %r to complex' % dtype)
         return dtype.box_complex(self.real, self.imag)
 
     def convert_real_to(self, dtype):
@@ -74,7 +77,12 @@
         return space.wrap(box.value)
 
     def descr_float(self, space):
-        box = self.convert_to(W_Float64Box._get_dtype(space))
+        try:
+            box = self.convert_to(W_Float64Box._get_dtype(space))
+        except TypeError:
+            raise OperationError(space.w_TypeError,
+                                 space.wrap("Cannot convert %s to float" % 
self._get_dtype(space).name))
+
         assert isinstance(box, W_Float64Box)
         return space.wrap(box.value)
 
diff --git a/pypy/module/micronumpy/test/test_ufuncs.py 
b/pypy/module/micronumpy/test/test_ufuncs.py
--- a/pypy/module/micronumpy/test/test_ufuncs.py
+++ b/pypy/module/micronumpy/test/test_ufuncs.py
@@ -200,7 +200,7 @@
         assert isnan(fabs(float('nan')))
 
     def test_fmax(self):
-        from _numpypy import fmax
+        from _numpypy import fmax, array
         import math
 
         nnan, nan, inf, ninf = float('-nan'), float('nan'), float('inf'), 
float('-inf')
@@ -209,8 +209,8 @@
         assert (fmax(a, [ninf]*5) == a).all()
         assert (fmax(a, [inf]*5) == [inf]*5).all()
         assert (fmax(a, [1]*5) == [1, 1, 1, 5, inf]).all()
-        assert math.isnan(fmax(nan, 0))
-        assert math.isnan(fmax(0, nan))
+        assert fmax(nan, 0) == 0
+        assert fmax(0, nan) == 0
         assert math.isnan(fmax(nan, nan))
         # The numpy docs specify that the FIRST NaN should be used if both are 
NaN
         # Since comparisons with nnan and nan all return false,
@@ -218,6 +218,30 @@
         # on Microsoft win32
         assert math.copysign(1., fmax(nnan, nan)) == math.copysign(1., nnan)
 
+        a = array((complex(ninf, 10), complex(10, ninf), 
+                   complex( inf, 10), complex(10,  inf),
+                   5+5j, 5-5j, -5+5j, -5-5j,
+                   0+5j, 0-5j, 5, -5,
+                   complex(nan, 0), complex(0, nan)), dtype = complex)
+        b = [ninf]*a.size
+        res = [a[0 ], a[1 ], a[2 ], a[3 ], 
+               a[4 ], a[5 ], a[6 ], a[7 ],
+               a[8 ], a[9 ], a[10], a[11],
+               b[12], b[13]]
+        assert (fmax(a, b) == res).all()
+        b = [inf]*a.size
+        res = [b[0 ], b[1 ], a[2 ], b[3 ], 
+               b[4 ], b[5 ], b[6 ], b[7 ],
+               b[8 ], b[9 ], b[10], b[11],
+               b[12], b[13]]
+        assert (fmax(a, b) == res).all()
+        b = [0]*a.size
+        res = [b[0 ], a[1 ], a[2 ], a[3 ], 
+               a[4 ], a[5 ], b[6 ], b[7 ],
+               a[8 ], b[9 ], a[10], b[11],
+               b[12], b[13]]
+        assert (fmax(a, b) == res).all()
+
 
     def test_fmin(self):
         from _numpypy import fmin
@@ -308,6 +332,7 @@
             [False, False, False, False, False]).all()
         assert (signbit([-0, -0.0, -1, -1.0, float('-inf')]) ==
             [False,  True,  True,  True,  True]).all()
+
         skip('sign of nan is non-determinant')
         assert (signbit([float('nan'), float('-nan'), -float('nan')]) ==
             [False, True, True]).all()    
@@ -1042,7 +1067,7 @@
             assert repr(abs(complex(float('nan'), float('nan')))) == 'nan'
 
         assert False, 'untested: ' + \
-                     'signbit, fabs, fmax, fmin, floor, ceil, trunc, ' + \
+                     'fabs, fmax, fmin, floor, ceil, trunc, ' + \
                      'exp2, expm1, isnan, isinf, isneginf, isposinf, ' + \
                      'isfinite, radians, degrees, log2, log1p, ' + \
                      'logaddexp, npy_log2_1p, logaddexp2'
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -647,9 +647,9 @@
 
     @simple_binary_op
     def fmax(self, v1, v2):
-        if math.isnan(v1):
+        if math.isnan(v2):
             return v1
-        elif math.isnan(v2):
+        elif math.isnan(v1):
             return v2
         return max(v1, v2)
 
@@ -1147,29 +1147,23 @@
             return 1,0
         return -1,0
 
-    @raw_unary_op
-    def signbit(self, v):
-        return rfloat.copysign(1.0, v) < 0.0
+    def fmax(self, v1, v2):
+        if self.isnan(v2):
+            return v1
+        elif self.isnan(v1):
+            return v2
+        if v1 >= v2:
+            return v1
+        return v2
 
-    @complex_unary_op
-    def fabs(self, v):
-        return rcomplex.abs(*v)
-
-    @simple_binary_op
-    def fmax(self, v1, v2):
-        if math.isnan(v1):
+    def fmin(self, v1, v2):
+        if math.isnan(v2):
             return v1
-        elif math.isnan(v2):
+        elif math.isnan(v1):
             return v2
-        return max(v1, v2)
-
-    @simple_binary_op
-    def fmin(self, v1, v2):
-        if math.isnan(v1):
+        if v1 <= v2:
             return v1
-        elif math.isnan(v2):
-            return v2
-        return min(v1, v2)
+        return v2
 
     #@simple_binary_op
     #def fmod(self, v1, v2):
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to