[Numpy-svn] r8571 - in branches/1.5.x/numpy/core: src/umath tests

numpy-svn at scipy.org numpy-svn at scipy.org
Sat Jul 31 09:17:12 EDT 2010


Author: ptvirtan
Date: 2010-07-31 08:17:11 -0500 (Sat, 31 Jul 2010)
New Revision: 8571

Modified:
   branches/1.5.x/numpy/core/src/umath/funcs.inc.src
   branches/1.5.x/numpy/core/tests/test_umath.py
Log:
BUG: (backport r8569) core/umath: fix powers of complex 0 (#1271)

Modified: branches/1.5.x/numpy/core/src/umath/funcs.inc.src
===================================================================
--- branches/1.5.x/numpy/core/src/umath/funcs.inc.src	2010-07-31 13:16:55 UTC (rev 8570)
+++ branches/1.5.x/numpy/core/src/umath/funcs.inc.src	2010-07-31 13:17:11 UTC (rev 8571)
@@ -238,7 +238,22 @@
         return;
     }
     if (ar == 0. && ai == 0.) {
-        *r = npy_cpack at c@(0., 0.);
+        if (br > 0 && bi == 0) {
+            *r = npy_cpack at c@(0., 0.);
+        }
+        else {
+            /* NB: there are four complex zeros; c0 = (+-0, +-0), so that unlike
+             *     for reals, c0**p, with `p` negative is in general
+             *     ill-defined.
+             *
+             *     c0**z with z complex is also ill-defined.
+             */
+            *r = npy_cpack at c@(NPY_NAN, NPY_NAN);
+
+            /* Raise invalid */
+            ar = NPY_INFINITY;
+            ar = ar - ar;
+        }
         return;
     }
     if (bi == 0 && (n=(intp)br) == br) {

Modified: branches/1.5.x/numpy/core/tests/test_umath.py
===================================================================
--- branches/1.5.x/numpy/core/tests/test_umath.py	2010-07-31 13:16:55 UTC (rev 8570)
+++ branches/1.5.x/numpy/core/tests/test_umath.py	2010-07-31 13:17:11 UTC (rev 8571)
@@ -86,7 +86,32 @@
             finally:
                 np.seterr(**err)
 
+    def test_power_zero(self):
+        # ticket #1271
+        zero = np.array([0j])
+        one = np.array([1+0j])
+        cinf = np.array([complex(np.inf, 0)])
+        cnan = np.array([complex(np.nan, np.nan)])
 
+        def assert_complex_equal(x, y):
+            x, y = np.asarray(x), np.asarray(y)
+            assert_array_equal(x.real, y.real)
+            assert_array_equal(x.imag, y.imag)
+
+        # positive powers
+        for p in [0.33, 0.5, 1, 1.5, 2, 3, 4, 5, 6.6]:
+            assert_complex_equal(np.power(zero, p), zero)
+
+        # zero power
+        assert_complex_equal(np.power(zero, 0), one)
+        assert_complex_equal(np.power(zero, 0+1j), cnan)
+
+        # negative power
+        for p in [0.33, 0.5, 1, 1.5, 2, 3, 4, 5, 6.6]:
+            assert_complex_equal(np.power(zero, -p), cnan)
+        assert_complex_equal(np.power(zero, -1+0.2j), cnan)
+
+
 class TestLog2(TestCase):
     def test_log2_values(self) :
         x = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]




More information about the Numpy-svn mailing list