[pypy-commit] pypy numpypy-complex2: add failing test for pow ( ** ) for interpreter,

mattip noreply at buildbot.pypy.org
Tue Oct 2 18:25:13 CEST 2012


Author: mattip <matti.picus at gmail.com>
Branch: numpypy-complex2
Changeset: r57744:bfe960db6801
Date: 2012-10-02 06:07 +0200
http://bitbucket.org/pypy/pypy/changeset/bfe960db6801/

Log:	add failing test for pow ( ** ) for interpreter, start to fix for
	interpreter, rcomplex, and numpypy disallow arctan2 for complex

diff --git a/pypy/interpreter/astcompiler/test/test_compiler.py b/pypy/interpreter/astcompiler/test/test_compiler.py
--- a/pypy/interpreter/astcompiler/test/test_compiler.py
+++ b/pypy/interpreter/astcompiler/test/test_compiler.py
@@ -153,6 +153,11 @@
             yield self.simple_test, "x = 17 %s 5" % operator, "x", expected
             expected = eval("0 %s 11" % operator)
             yield self.simple_test, "x = 0 %s 11" % operator, "x", expected
+    
+    def test_pow(self):
+        expected = eval("complex(float('inf'), 0.) ** complex(10., 3.0)")
+        yield self.simple_test, \
+           "x = complex(float('inf'), 0.) ** complex(10., 3.0)", "x",  expected
 
     def test_compare(self):
         yield self.st, "x = 2; y = 5; y; h = 1 < x >= 3 < x", "h", False
diff --git a/pypy/module/micronumpy/interp_ufuncs.py b/pypy/module/micronumpy/interp_ufuncs.py
--- a/pypy/module/micronumpy/interp_ufuncs.py
+++ b/pypy/module/micronumpy/interp_ufuncs.py
@@ -547,7 +547,8 @@
                                  "allow_complex": False}),
             ("fmax", "fmax", 2, {"promote_to_float": True}),
             ("fmin", "fmin", 2, {"promote_to_float": True}),
-            ("fmod", "fmod", 2, {"promote_to_float": True, 'allow_complex': False}),
+            ("fmod", "fmod", 2, {"promote_to_float": True, 
+                                 'allow_complex': False}),
             ("floor", "floor", 1, {"promote_to_float": True,
                                    "allow_complex": False}),
             ("ceil", "ceil", 1, {"promote_to_float": True,
@@ -567,7 +568,8 @@
             ("arcsin", "arcsin", 1, {"promote_to_float": True}),
             ("arccos", "arccos", 1, {"promote_to_float": True}),
             ("arctan", "arctan", 1, {"promote_to_float": True}),
-            ("arctan2", "arctan2", 2, {"promote_to_float": True}),
+            ("arctan2", "arctan2", 2, {"promote_to_float": True,
+                                       "allow_complex": False}),
             ("sinh", "sinh", 1, {"promote_to_float": True}),
             ("cosh", "cosh", 1, {"promote_to_float": True}),
             ("tanh", "tanh", 1, {"promote_to_float": True}),
diff --git a/pypy/module/micronumpy/test/test_complex.py b/pypy/module/micronumpy/test/test_complex.py
--- a/pypy/module/micronumpy/test/test_complex.py
+++ b/pypy/module/micronumpy/test/test_complex.py
@@ -263,7 +263,7 @@
     def test_not_complex(self):
         from _numpypy import (radians, deg2rad, degrees, rad2deg,
                   isneginf, isposinf, logaddexp, logaddexp2, fmod,
-                  max, min)
+                  arctan2)
         raises(TypeError, radians, complex(90,90))
         raises(TypeError, deg2rad, complex(90,90))
         raises(TypeError, degrees, complex(90,90))
@@ -272,6 +272,7 @@
         raises(TypeError, isposinf, complex(1, 1))
         raises(TypeError, logaddexp, complex(1, 1), complex(3, 3))
         raises(TypeError, logaddexp2, complex(1, 1), complex(3, 3))
+        raises(TypeError, arctan2, complex(1, 1), complex(3, 3))
         raises (TypeError, fmod, complex(90,90), 3) 
 
     def test_isnan_isinf(self):
@@ -291,6 +292,7 @@
         ninf = -float('inf')
         nan = float('nan')
         cmpl = complex
+        from math import copysign
         from _numpypy import power, array, complex128, complex64
         for c,rel_err in ((complex128, 5e-323), (complex64, 1e-7)):
             a = array([cmpl(-5., 0), cmpl(-5., -5.), cmpl(-5., 5.),
@@ -304,17 +306,24 @@
                      ], dtype=c)
             got_err = False
             for p in (3, -1, 10000, 2.3, -10000, 10+3j):
-                b = self.c_pow(a, p)
+                b = power(a, p)
                 for i in range(len(a)):
-                    r = a[i]**p
+                    print a[i],p,p.real,p.imag
+                    try:
+                        r = self.c_pow((float(a[i].real), float(a[i].imag)), 
+                                (float(p.real), float(p.imag)))
+                    except ZeroDivisionError:
+                        r = (nan, nan)
+                    except OverflowError:
+                        r = (inf, -copysign(inf, a[i].imag))
                     msg = 'result of %r(%r)**%r got %r expected %r\n ' % \
                             (c,a[i], p, b[i], r)
                     try:        
-                        t1 = float(r.real)        
+                        t1 = float(r[0])        
                         t2 = float(b[i].real)        
                         self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg)
-                        t1 = float(r.imag)        
-                        t2 = float(b[i].imag)        
+                        t1 = float(r[1])        
+                        t2 = float(b[i].imag)
                         self.rAlmostEqual(t1, t2, rel_err=rel_err, msg=msg)
                     except AssertionError as e:
                         print e.message
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
@@ -1156,14 +1156,16 @@
 
     @complex_binary_op
     def pow(self, v1, v2):
-        if not isfinite(v1[0]) or not isfinite(v1[1]):
-            return rfloat.NAN, rfloat.NAN
+        if v1[1] == 0 and v2[1] == 0 and v1[0] > 0:
+            return math.pow(v1[0], v2[0]), 0
+        #if not isfinite(v1[0]) or not isfinite(v1[1]):
+        #    return rfloat.NAN, rfloat.NAN
         try:
             return rcomplex.c_pow(v1, v2)
         except ZeroDivisionError:
             return rfloat.NAN, rfloat.NAN
         except OverflowError:
-            return rfloat.INFINITY, rfloat.INFINITY
+            return rfloat.INFINITY, -math.copysign(rfloat.INFINITY, v1[1])
 
 
     #complex copysign does not exist in numpy
@@ -1332,9 +1334,9 @@
             return rfloat.NAN, math.copysign(rfloat.INFINITY, v[1])
         return rcomplex.c_atan(*v)
 
-    @complex_binary_op
-    def arctan2(self, v1, v2):
-        return rcomplex.c_atan2(v1, v2)
+    #@complex_binary_op
+    #def arctan2(self, v1, v2):
+    #    return rcomplex.c_atan2(v1, v2)
 
     @complex_unary_op
     def sinh(self, v):
diff --git a/pypy/objspace/std/complexobject.py b/pypy/objspace/std/complexobject.py
--- a/pypy/objspace/std/complexobject.py
+++ b/pypy/objspace/std/complexobject.py
@@ -229,6 +229,8 @@
         raise OperationError(space.w_ZeroDivisionError, space.wrap("0.0 to a negative or complex power"))
     except OverflowError:
         raise OperationError(space.w_OverflowError, space.wrap("complex exponentiation"))
+    except ValueError as e:
+        raise OperationError(space.w_ValueError, space.wrap(e.message))
     return w_p
 
 def neg__Complex(space, w_complex):
diff --git a/pypy/rlib/rcomplex.py b/pypy/rlib/rcomplex.py
--- a/pypy/rlib/rcomplex.py
+++ b/pypy/rlib/rcomplex.py
@@ -67,7 +67,7 @@
 
 def c_pow(x, y):
     (r1, i1), (r2, i2) = x, y
-    if i1 == 0 and i2 == 0 and r1>=0:
+    if i1 == 0 and i2 == 0 and r1 > 0:
         rr = math.pow(r1, r2)
         ir = 0.
     elif r2 == 0.0 and i2 == 0.0:


More information about the pypy-commit mailing list