File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -499,6 +499,15 @@ def cumsum(self, dim):
499499 def cumprod (self , dim ):
500500 return px .reduction .cumprod (self , dim )
501501
502+ def diff (self , dim , n = 1 ):
503+ """Compute the n-th discrete difference along the given dimension."""
504+ slice1 = {dim : slice (1 , None )}
505+ slice2 = {dim : slice (None , - 1 )}
506+ x = self
507+ for _ in range (n ):
508+ x = x [slice1 ] - x [slice2 ]
509+ return x
510+
502511
503512class XTensorConstantSignature (tuple ):
504513 def __eq__ (self , other ):
Original file line number Diff line number Diff line change 11import numpy as np
22import pytest
33from xarray import DataArray
4+ from xtensor .util import xr_arange_like
45
56from pytensor .xtensor import xtensor
67from tests .xtensor .util import xr_assert_allclose , xr_function
@@ -40,3 +41,22 @@ def test_basic_indexing(labeled, indices):
4041 res = fn (x_test )
4142 expected_res = x_test [indices ]
4243 xr_assert_allclose (res , expected_res )
44+
45+
46+ @pytest .mark .parametrize ("n" , ["implicit" , 1 , 2 ])
47+ @pytest .mark .parametrize ("dim" , ["a" , "b" ])
48+ def test_diff (dim , n ):
49+ x = xtensor (dims = ("a" , "b" ), shape = (7 , 11 ))
50+ if n == "implicit" :
51+ out = x .diff (dim )
52+ else :
53+ out = x .diff (dim , n = n )
54+
55+ fn = xr_function ([x ], out )
56+ x_test = xr_arange_like (x )
57+ res = fn (x_test )
58+ if n == "implicit" :
59+ expected_res = x_test .diff (dim )
60+ else :
61+ expected_res = x_test .diff (dim , n = n )
62+ xr_assert_allclose (res , expected_res )
You can’t perform that action at this time.
0 commit comments