Repository: numpy/numpy
File: numpy/_core/tests/test_umath.py (Python)

Code before:

    def test_integer_power_of_1(self):
        dtypes = np.typecodes['AllInteger']
        for dt in dtypes:
            arr = np.arange(10, dtype=dt)
            assert_equal(np.power(1, arr), np.ones_like(arr))

    def test_integer_power_of_zero(self):
        dtypes = np.typecodes['AllInteger']
        for dt in dtypes:
            arr = np.arange(1, 10, dtype=dt)
            assert_equal(np.power(0, arr), np.zeros_like(arr))

    def test_integer_to_negative_power(self):
        dtypes = np.typecodes['Integer']
        for dt in dtypes:
            a = np.array([0, 1, 2, 3], dtype=dt)
            b = np.array([0, 1, 2, -3], dtype=dt)
            one = np.array(1, dtype=dt)
            minusone = np.array(-1, dtype=dt)
            assert_raises(ValueError, np.power, a, b)
            assert_raises(ValueError, np.power, a, minusone)
            assert_raises(ValueError, np.power, one, b)
            assert_raises(ValueError, np.power, one, minusone)

    def test_integer_to_negative_power_scalar(self):
        dtypes = np.typecodes['Integer']
        for dt in dtypes:
            sctype = np.dtype(dt).type
            two = sctype(2)
            minusone = sctype(-1)
            with pytest.raises(ValueError):
                np.power(two, minusone)
            with pytest.raises(ValueError):
                np.power(two, minusone)

    def test_float_to_inf_power(self):
        for dt in [np.float32, np.float64]:
            a = np.array([1, 1, 2, 2, -2, -2, np.inf, -np.inf], dt)
            b = np.array([np.inf, -np.inf, np.inf, -np.inf,
                                np.inf, -np.inf, np.inf, -np.inf], dt)
            r = np.array([1, 1, np.inf, 0, np.inf, 0, np.inf, 0], dt)
            assert_equal(np.power(a, b), r)

    def test_power_fast_paths(self):
        # gh-26055
        for dt in [np.float32, np.float64]:
            a = np.array([0, 1.1, 2, 12e12, -10., np.inf, -np.inf], dt)
            expected = np.array([0.0, 1.21, 4., 1.44e+26, 100, np.inf, np.inf])
            result = np.power(a, 2.)
            assert_array_max_ulp(result, expected.astype(dt), maxulp=1)

Review comment (suggestion):
This could use a brief comment to explain why `np.power(two, minusone)` is being called twice.  Seems a bit mysterious without it.

Code after:

    def test_integer_power_of_1(self):
        dtypes = np.typecodes['AllInteger']
        for dt in dtypes:
            arr = np.arange(10, dtype=dt)
            assert_equal(np.power(1, arr), np.ones_like(arr))

    def test_integer_power_of_zero(self):
        dtypes = np.typecodes['AllInteger']
        for dt in dtypes:
            arr = np.arange(1, 10, dtype=dt)
            assert_equal(np.power(0, arr), np.zeros_like(arr))

    def test_integer_to_negative_power(self):
        dtypes = np.typecodes['Integer']
        for dt in dtypes:
            a = np.array([0, 1, 2, 3], dtype=dt)
            b = np.array([0, 1, 2, -3], dtype=dt)
            one = np.array(1, dtype=dt)
            minusone = np.array(-1, dtype=dt)
            assert_raises(ValueError, np.power, a, b)
            assert_raises(ValueError, np.power, a, minusone)
            assert_raises(ValueError, np.power, one, b)
            assert_raises(ValueError, np.power, one, minusone)

    def test_float_to_inf_power(self):
        for dt in [np.float32, np.float64]:
            a = np.array([1, 1, 2, 2, -2, -2, np.inf, -np.inf], dt)
            b = np.array([np.inf, -np.inf, np.inf, -np.inf,
                                np.inf, -np.inf, np.inf, -np.inf], dt)
            r = np.array([1, 1, np.inf, 0, np.inf, 0, np.inf, 0], dt)
            assert_equal(np.power(a, b), r)

    def test_power_fast_paths(self):
        # gh-26055
        for dt in [np.float32, np.float64]:
            a = np.array([0, 1.1, 2, 12e12, -10., np.inf, -np.inf], dt)
            expected = np.array([0.0, 1.21, 4., 1.44e+26, 100, np.inf, np.inf])
            result = np.power(a, 2.)
            assert_array_max_ulp(result, expected.astype(dt), maxulp=1)

            a = np.array([0, 1.1, 2, 12e12], dt)
            expected = np.sqrt(a).astype(dt)
            result = np.power(a, 0.5)
            assert_array_max_ulp(result, expected, maxulp=1)


class TestFloat_power:
    def test_type_conversion(self):
        arg_type = '?bhilBHILefdgFDG'
        res_type = 'ddddddddddddgDDG'