Skip to content
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

Add xp.trapz #4129

Open
ShreyasKhandekar opened this issue Feb 28, 2025 · 0 comments · May be fixed by #4127
Open

Add xp.trapz #4129

ShreyasKhandekar opened this issue Feb 28, 2025 · 0 comments · May be fixed by #4127
Labels
enhancement New feature or request

Comments

@ShreyasKhandekar
Copy link
Contributor

Feature Description
I propose adding a trapz or trapezoid function to our data science library for numerical integration using the trapezoidal rule. NumPy already provides this functionality with numpy.trapz, and it would be beneficial for users to have a native implementation within our library. This function is used for approximating the integral of discrete data points, which is a common task in data analysis, signal processing, and scientific computing. We came across the need for this function while testing Arkouda in a potential new use case.

Adding this to the array_api would be easiest since we can use the existing array_api functionality to build up the trapz implementation just like NumPy using xp.diff and vector operations.

Example Behavior
Expected behavior of the function:

import arkouda as ak
import arkouda.array_api as xp

y = y = xp.asarray(ak.array([1, 2, 3]))
x = xp.asarray(ak.array([4, 6, 8]))

# Expected output similar to NumPy's trapezoidal integration
print(xp.trapz(y)) # Expected: 4.0
print(xp.trapz(y, dx=2.0)) # Expected: 8.0
print(xp.trapz(y, x))  # Expected: 8.0

Describe Workarounds
Users could make up their own trapz by using the basic building blocks like xp.diff and slicing and multiplication. Or they can call out to numpy.trapz

Additional context
NumPy’s implementation of trapz or trapezoid is widely used and well-documented:
https://numpy.org/doc/stable/reference/generated/numpy.trapezoid.html
https://numpy.org/doc/1.26/reference/generated/numpy.trapz.html#numpy.trapz

Although trapz has been deprecated since Numpy 2.0.0 and replaced with trapezoid, Arkouda still uses Numpu 1.26 so we should have both overloads of the function available.

@ShreyasKhandekar ShreyasKhandekar added the enhancement New feature or request label Feb 28, 2025
@ShreyasKhandekar ShreyasKhandekar linked a pull request Feb 28, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant