Skip to content

Commit

Permalink
add period argument to diff
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdsharpe committed Dec 30, 2023
1 parent 1a0928d commit b7e0ae1
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion aerosandbox/numpy/calculus.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import numpy as _onp
import casadi as _cas
from aerosandbox.numpy.determine_type import is_casadi_type
from aerosandbox.numpy.conditionals import where as _where
from aerosandbox.numpy.arithmetic_dyadic import mod as _mod


def diff(a, n=1, axis=-1):
def diff(a, n=1, axis=-1, period=None):
"""
Calculate the n-th discrete difference along the given axis.
See syntax here: https://numpy.org/doc/stable/reference/generated/numpy.diff.html
"""
if period is not None:
original_result = diff(a, n=n, axis=axis)
remainder = _mod(original_result, period)
return _where(
remainder > period / 2,
remainder - period,
remainder
)

if not is_casadi_type(a):
return _onp.diff(a, n=n, axis=axis)

Expand Down Expand Up @@ -42,3 +53,10 @@ def trapz(x, modify_endpoints=False): # TODO unify with NumPy trapz, this is di
integral[-1] = integral[-1] + x[-1] * 0.5

return integral


if __name__ == '__main__':
import numpy as np

a = np.linspace(-500, 500, 21) % 360 - 180
print(diff(a, period=360))

0 comments on commit b7e0ae1

Please sign in to comment.