Hello NumPy developers,

I am writing to propose an enhancement to numpy.gradient to support periodic 
boundary conditions natively. While numpy.gradient is a foundational tool for 
numerical differentiation, it currently lacks a mechanism to handle periodic 
domains without requiring users to manually pad their arrays (e.g., via 
np.pad(mode='wrap')).

I believe adding a periodic parameter (potentially a boolean or a tuple for 
multi-axis support) would be a significant addition for the following reasons:

1. Elimination of Boundary Noise: In physical simulations (CFD, acoustics, 
orbital mechanics), the current edge_order=1 or 2 logic introduces artificial 
numerical dissipation or "noise" at the boundaries because it drops to 
one-sided differences. For periodic systems, every point is an interior point, 
and a central difference should be maintained throughout.

2. Memory Efficiency: For large-scale datasets, manual padding creates 
unnecessary memory copies. Native support would allow us to compute the 
gradient at the edges using index wrapping, improving performance and reducing 
the memory footprint.

3. Consistency with the SciPy Stack: Other core tools like scipy.ndimage (via 
mode='wrap') and numpy.fft inherently assume periodicity. Adding this to 
numpy.gradient fills a logical gap, providing a consistent experience for users 
moving between spatial and frequency domain analysis.

4. Simplified Implementation: Paradoxically, for periodic axes, the internal 
logic for non-uniform spacing becomes simpler. We can avoid the complex 
second-order boundary coefficients (the a,b,c Lagrange terms for edges) and 
apply the interior central difference stencil globally by wrapping the 
coordinate distances and array values.

Implementation Plan: I am envisioning an API such as np.gradient(f, ..., 
periodic=[True, False, True]). This would default to False to preserve backward 
compatibility.

I have a background in numerical methods and am an active user of the library. 
If the steering council and moderators find this feature to be a valuable 
addition to the NumPy core, I am prepared to draft a NEP (if required) and 
submit a Pull Request with a full implementation, including unit tests for both 
uniform and non-uniform grids.

I would love to hear your thoughts on whether this fits within the current 
roadmap for the library.

Best regards,
Renato Miotto
https://github.com/rfmiotto
_______________________________________________
NumPy-Discussion mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/numpy-discussion.python.org
Member address: [email protected]

Reply via email to