Repository: numpy/numpy
File: numpy/lib/tests/test_function_base.py (Python)

Code before:
        assert_equal(np.interp(0.5, [     0,      1], sc([np.nan,     10])), sc(np.nan))
        assert_equal(np.interp(0.5, [     0,      1], sc([     0, np.nan])), sc(np.nan))

    def test_non_finite_inf(self, sc):
        """ Test that interp between opposite infs gives nan """
        inf = np.inf
        nan = np.nan
        assert_equal(np.interp(0.5, [-inf, +inf], sc([      0,      10])), sc(nan))
        assert_equal(np.interp(0.5, [      0,       1], sc([-inf, +inf])), sc(nan))
        assert_equal(np.interp(0.5, [      0,       1], sc([+inf, -inf])), sc(nan))

        # unless the y values are equal
        assert_equal(np.interp(0.5, [-np.inf, +np.inf], sc([     10,      10])), sc(10))

    def test_non_finite_half_inf_xf(self, sc):
        """ Test that interp where both axes have a bound at inf gives nan """
        inf = np.inf
        nan = np.nan
        assert_equal(np.interp(0.5, [-inf,       1], sc([-inf,      10])), sc(nan))
        assert_equal(np.interp(0.5, [-inf,       1], sc([+inf,      10])), sc(nan))
        assert_equal(np.interp(0.5, [-inf,       1], sc([      0, -inf])), sc(nan))
        assert_equal(np.interp(0.5, [-inf,       1], sc([      0, +inf])), sc(nan))
        assert_equal(np.interp(0.5, [      0, +inf], sc([-inf,      10])), sc(nan))
        assert_equal(np.interp(0.5, [      0, +inf], sc([+inf,      10])), sc(nan))
        assert_equal(np.interp(0.5, [      0, +inf], sc([      0, -inf])), sc(nan))
        assert_equal(np.interp(0.5, [      0, +inf], sc([      0, +inf])), sc(nan))

    def test_non_finite_half_inf_x(self, sc):
        """ Test interp where the x axis has a bound at inf """
        assert_equal(np.interp(0.5, [-np.inf, -np.inf], sc([0, 10])), sc(10))
        assert_equal(np.interp(0.5, [-np.inf, 1      ], sc([0, 10])), sc(10))  # noqa: E202
        assert_equal(np.interp(0.5, [      0, +np.inf], sc([0, 10])), sc(0))
        assert_equal(np.interp(0.5, [+np.inf, +np.inf], sc([0, 10])), sc(0))

    def test_non_finite_half_inf_f(self, sc):
        """ Test interp where the f axis has a bound at inf """
        assert_equal(np.interp(0.5, [0, 1], sc([      0, -np.inf])), sc(-np.inf))
        assert_equal(np.interp(0.5, [0, 1], sc([      0, +np.inf])), sc(+np.inf))
        assert_equal(np.interp(0.5, [0, 1], sc([-np.inf,      10])), sc(-np.inf))
        assert_equal(np.interp(0.5, [0, 1], sc([+np.inf,      10])), sc(+np.inf))
        assert_equal(np.interp(0.5, [0, 1], sc([-np.inf, -np.inf])), sc(-np.inf))
        assert_equal(np.interp(0.5, [0, 1], sc([+np.inf, +np.inf])), sc(+np.inf))

    def test_complex_interp(self):
        # test complex interpolation
        x = np.linspace(0, 1, 5)
        y = np.linspace(0, 1, 5) + (1 + np.linspace(0, 1, 5)) * 1.0j
        x0 = 0.3
        y0 = x0 + (1 + x0) * 1.0j
        assert_almost_equal(np.interp(x0, x, y), y0)
        # test complex left and right

Review comment (suggestion):
```suggestion
        assert_equal(np.interp(0.5, [-inf,    1], sc([-inf,   10])), sc(nan))
        assert_equal(np.interp(0.5, [-inf,    1], sc([+inf,   10])), sc(nan))
        assert_equal(np.interp(0.5, [-inf,    1], sc([   0, -inf])), sc(nan))
        assert_equal(np.interp(0.5, [-inf,    1], sc([   0, +inf])), sc(nan))
        assert_equal(np.interp(0.5, [   0, +inf], sc([-inf,   10])), sc(nan))
        assert_equal(np.interp(0.5, [   0, +inf], sc([+inf,   10])), sc(nan))
        assert_equal(np.interp(0.5, [   0, +inf], sc([   0, -inf])), sc(nan))
        assert_equal(np.interp(0.5, [   0, +inf], sc([   0, +inf])), sc(nan))
```

Code after:
        assert_equal(np.interp(0.5, [     0,      1], sc([np.nan,     10])), sc(np.nan))
        assert_equal(np.interp(0.5, [     0,      1], sc([     0, np.nan])), sc(np.nan))

    def test_non_finite_inf(self, sc):
        """ Test that interp between opposite infs gives nan """
        inf = np.inf
        nan = np.nan
        assert_equal(np.interp(0.5, [-inf, +inf], sc([   0,   10])), sc(nan))
        assert_equal(np.interp(0.5, [   0,    1], sc([-inf, +inf])), sc(nan))
        assert_equal(np.interp(0.5, [   0,    1], sc([+inf, -inf])), sc(nan))

        # unless the y values are equal
        assert_equal(np.interp(0.5, [-np.inf, +np.inf], sc([     10,      10])), sc(10))

    def test_non_finite_half_inf_xf(self, sc):
        """ Test that interp where both axes have a bound at inf gives nan """
        inf = np.inf
        nan = np.nan
        assert_equal(np.interp(0.5, [-inf,    1], sc([-inf,   10])), sc(nan))
        assert_equal(np.interp(0.5, [-inf,    1], sc([+inf,   10])), sc(nan))
        assert_equal(np.interp(0.5, [-inf,    1], sc([   0, -inf])), sc(nan))
        assert_equal(np.interp(0.5, [-inf,    1], sc([   0, +inf])), sc(nan))
        assert_equal(np.interp(0.5, [   0, +inf], sc([-inf,   10])), sc(nan))
        assert_equal(np.interp(0.5, [   0, +inf], sc([+inf,   10])), sc(nan))
        assert_equal(np.interp(0.5, [   0, +inf], sc([   0, -inf])), sc(nan))
        assert_equal(np.interp(0.5, [   0, +inf], sc([   0, +inf])), sc(nan))

    def test_non_finite_half_inf_x(self, sc):
        """ Test interp where the x axis has a bound at inf """
        assert_equal(np.interp(0.5, [-np.inf, -np.inf], sc([0, 10])), sc(10))
        assert_equal(np.interp(0.5, [-np.inf, 1      ], sc([0, 10])), sc(10))  # noqa: E202
        assert_equal(np.interp(0.5, [      0, +np.inf], sc([0, 10])), sc(0))
        assert_equal(np.interp(0.5, [+np.inf, +np.inf], sc([0, 10])), sc(0))

    def test_non_finite_half_inf_f(self, sc):
        """ Test interp where the f axis has a bound at inf """
        assert_equal(np.interp(0.5, [0, 1], sc([      0, -np.inf])), sc(-np.inf))
        assert_equal(np.interp(0.5, [0, 1], sc([      0, +np.inf])), sc(+np.inf))
        assert_equal(np.interp(0.5, [0, 1], sc([-np.inf,      10])), sc(-np.inf))
        assert_equal(np.interp(0.5, [0, 1], sc([+np.inf,      10])), sc(+np.inf))
        assert_equal(np.interp(0.5, [0, 1], sc([-np.inf, -np.inf])), sc(-np.inf))
        assert_equal(np.interp(0.5, [0, 1], sc([+np.inf, +np.inf])), sc(+np.inf))

    def test_complex_interp(self):
        # test complex interpolation
        x = np.linspace(0, 1, 5)
        y = np.linspace(0, 1, 5) + (1 + np.linspace(0, 1, 5)) * 1.0j
        x0 = 0.3
        y0 = x0 + (1 + x0) * 1.0j
        assert_almost_equal(np.interp(x0, x, y), y0)
        # test complex left and right