Skip to content

BUG: integrate.trapezoid: fix broadcasting issue #21912

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 6 commits into from
Nov 20, 2024

Conversation

andyfaff
Copy link
Contributor

Closes #21908.

Fixes the 1-D case, and fixes another problem with the n-D case that wasn't picked up by our tests. The latter problem was that it wasn't possible to integrate with an x array that had exactly the same shape as y.

The only selection rule for x is that it needs to be broadcastable to y along the axis of integration.

@andyfaff andyfaff requested a review from lucascolley November 19, 2024 05:34
@andyfaff andyfaff requested a review from steppi as a code owner November 19, 2024 05:34
@github-actions github-actions bot added scipy.integrate defect A clear bug or issue that prevents SciPy from being installed or used as expected labels Nov 19, 2024
Copy link
Member

@lucascolley lucascolley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the fix Andrew! Just need to make the new test generic over namespaces, along the lines of the suggestions I've added.

@lucascolley lucascolley added this to the 1.15.0 milestone Nov 19, 2024
@lucascolley lucascolley changed the title BUG: integrate.trapezoid fix BUG: integrate.trapezoid: fix broadcasting issue Nov 19, 2024
Comment on lines 330 to 345
assert_allclose(trapezoid(y, x=np.array([0, 10., 20.]), axis=0), out0)
# x needs to be broadcastable against y
assert_allclose(
trapezoid(y, x=np.array([0, 10., 20.])[:, None], axis=0),
out0
)
with pytest.raises(Exception):
# x is not broadcastable against y
trapezoid(y, x=np.array([0, 10., 20.])[None, :], axis=0)

out1 = np.array([ 40.5, 130.5, 220.5])
assert_allclose(trapezoid(y, x=x, axis=1), out1)
assert_allclose(
trapezoid(y, x=np.array(np.linspace(0, 9, 10)), axis=1),
out1
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in case I wasn't clear, the rest of the test needs changes too (np -> xp, assert_allclose -> xp_assert_close)

Copy link
Member

@lucascolley lucascolley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@lucascolley lucascolley merged commit 4ffe88d into scipy:main Nov 20, 2024
37 checks passed
@andyfaff andyfaff deleted the trapezoid branch November 20, 2024 00:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.integrate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: integrate.trapezoid: broadcasting failure after #21524
2 participants