File tree 2 files changed +29
-0
lines changed 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):
499
499
def cumprod (self , dim ):
500
500
return px .reduction .cumprod (self , dim )
501
501
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
+
502
511
503
512
class XTensorConstantSignature (tuple ):
504
513
def __eq__ (self , other ):
Original file line number Diff line number Diff line change 1
1
import numpy as np
2
2
import pytest
3
3
from xarray import DataArray
4
+ from xtensor .util import xr_arange_like
4
5
5
6
from pytensor .xtensor import xtensor
6
7
from tests .xtensor .util import xr_assert_allclose , xr_function
@@ -40,3 +41,22 @@ def test_basic_indexing(labeled, indices):
40
41
res = fn (x_test )
41
42
expected_res = x_test [indices ]
42
43
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