8000 API: Cleaning `numpy/__init__.py` and main namespace - Part 4 [NEP 52] by mtsokol · Pull Request #24445 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

API: Cleaning numpy/__init__.py and main namespace - Part 4 [NEP 52] #24445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 5, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reintroduce trapz tests
  • Loading branch information
mtsokol committed Sep 5, 2023
commit b438a7e5e42478b1d1e88f75bab714f4525d755d
63 changes: 63 additions & 0 deletions numpy/lib/tests/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2114,6 +2114,69 @@ def test_kaiser(self, dtype: str, M: int) -> None:
assert_almost_equal(np.sum(w, axis=0), 10, 15)


@pytest.mark.filterwarnings('ignore:.*trapz.*:DeprecationWarning')
class TestTrapz:

def test_simple(self):
x = np.arange(-10, 10, .1)
r = trapz(np.exp(-.5 * x ** 2) / np.sqrt(2 * np.pi), dx=0.1)
# check integral of normal equals 1
assert_almost_equal(r, 1, 7)

def test_ndim(self):
x = np.linspace(0, 1, 3)
y = np.linspace(0, 2, 8)
z = np.linspace(0, 3, 13)

wx = np.ones_like(x) * (x[1] - x[0])
wx[0] /= 2
wx[-1] /= 2
wy = np.ones_like(y) * (y[1] - y[0])
wy[0] /= 2
wy[-1] /= 2
wz = np.ones_like(z) * (z[1] - z[0])
wz[0] /= 2
wz[-1] /= 2

q = x[:, None, None] + y[None,:, None] + z[None, None,:]

qx = (q * wx[:, None, None]).sum(axis=0)
qy = (q * wy[None, :, None]).sum(axis=1)
qz = (q * wz[None, None, :]).sum(axis=2)

# n-d `x`
r = trapz(q, x=x[:, None, None], axis=0)
assert_almost_equal(r, qx)
r = trapz(q, x=y[None,:, None], axis=1)
assert_almost_equal(r, qy)
r = trapz(q, x=z[None, None,:], axis=2)
assert_almost_equal(r, qz)

# 1-d `x`
r = trapz(q, x=x, axis=0)
assert_almost_equal(r, qx)
r = trapz(q, x=y, axis=1)
assert_almost_equal(r, qy)
r = trapz(q, x=z, axis=2)
assert_almost_equal(r, qz)

def test_masked(self):
# Testing that masked arrays behave as if the function is 0 where
# masked
x = np.arange(5)
y = x * x
mask = x == 2
ym = np.ma.array(y, mask=mask)
r = 13.0 # sum(0.5 * (0 + 1) * 1.0 + 0.5 * (9 + 16))
assert_almost_equal(trapz(ym, x), r)

xm = np.ma.array(x, mask=mask)
assert_almost_equal(trapz(ym, xm), r)

xm = np.ma.array(x, mask=mask)
assert_almost_equal(trapz(y, xm), r)


class TestSinc:

def test_simple(self):
Expand Down
0