Repository: scipy/scipy
File: scipy/integrate/tests/test_quadrature.py (Python)

Code before:
# mypy: disable-error-code="attr-defined"
import pytest
import numpy as np
from numpy.testing import assert_equal, assert_almost_equal, assert_allclose
from hypothesis import given
import hypothesis.strategies as st
import hypothesis.extra.numpy as hyp_num

from scipy.integrate import (romb, newton_cotes,
                             cumulative_trapezoid, trapezoid,
                             quad, simpson, fixed_quad,
                             qmc_quad, cumulative_simpson)
from scipy.integrate._quadrature import _cumulative_simpson_unequal_intervals

from scipy import stats, special, integrate
from scipy.conftest import skip_xp_invalid_arg
from scipy._lib._array_api import make_xp_test_case, xp_default_dtype, is_numpy
from scipy._lib._array_api_no_0d import xp_assert_close, xp_assert_equal
from scipy._lib import array_api_extra as xpx

skip_xp_backends = pytest.mark.skip_xp_backends

@make_xp_test_case(fixed_quad)
class TestFixedQuad:
    def test_scalar(self):
        n = 4
        expected = 1/(2*n)
        got, _ = fixed_quad(lambda x: x**(2*n - 1), 0, 1, n=n)
        # quadrature exact for this input
        xp_assert_close(got, np.asarray(expected), rtol=1e-12)

    def test_0d(self, xp):
        n = 4
        expected = 1/(2*n)
        got, _ = fixed_quad(lambda x: x**(2*n - 1), xp.asarray(0.), xp.asarray(1.), n=n)
        # quadrature exact for this input
        xp_assert_close(got, xp.asarray(expected), rtol=1e-12)

    @pytest.mark.parametrize('dtype', [None, 'float32', 'float64'])
    def test_vector(self, dtype, xp):
        dtype = dtype if dtype is None else getattr(xp, dtype)
        n = 4
        p = xp.arange(1., 2*n, dtype=dtype)
        a, b = xp.asarray(0., dtype=dtype), xp.asarray(1., dtype=dtype)

Review comment (suggestion):
```suggestion
from scipy._external import array_api_extra as xpx
```

Code after:
# mypy: disable-error-code="attr-defined"
import pytest
import numpy as np
from numpy.testing import assert_equal, assert_almost_equal, assert_allclose
from hypothesis import given
import hypothesis.strategies as st
import hypothesis.extra.numpy as hyp_num

from scipy.integrate import (romb, newton_cotes,
                             cumulative_trapezoid, trapezoid,
                             quad, simpson, fixed_quad,
                             qmc_quad, cumulative_simpson)
from scipy.integrate._quadrature import _cumulative_simpson_unequal_intervals

from scipy import stats, special, integrate
from scipy.conftest import skip_xp_invalid_arg
from scipy._lib._array_api import make_xp_test_case, xp_default_dtype, is_numpy
from scipy._lib._array_api_no_0d import xp_assert_close, xp_assert_equal
from scipy._external import array_api_extra as xpx

skip_xp_backends = pytest.mark.skip_xp_backends

@make_xp_test_case(fixed_quad)
class TestFixedQuad:
    def test_scalar(self):
        n = 4
        expected = 1/(2*n)
        got, _ = fixed_quad(lambda x: x**(2*n - 1), 0, 1, n=n)
        # quadrature exact for this input
        xp_assert_close(got, np.asarray(expected), rtol=1e-12)

    def test_0d(self, xp):
        n = 4
        expected = 1/(2*n)
        got, _ = fixed_quad(lambda x: x**(2*n - 1), xp.asarray(0.), xp.asarray(1.), n=n)
        # quadrature exact for this input
        xp_assert_close(got, xp.asarray(expected), rtol=1e-12)

    @pytest.mark.parametrize('dtype', [None, 'float32', 'float64'])
    def test_vector(self, dtype, xp):
        dtype = dtype if dtype is None else getattr(xp, dtype)
        n = 4
        p = xp.arange(1., 2*n, dtype=dtype)
        a, b = xp.asarray(0., dtype=dtype), xp.asarray(1., dtype=dtype)