33 "SecondDirectionalDerivative" ,
44]
55
6+ from typing import Union
7+
68from pylops import LinearOperator
79from pylops .basicoperators import Diagonal , Gradient , Sum
8- from pylops .utils .typing import DTypeLike , InputDimsLike , NDArray
10+ from pylops .utils .typing import DTypeLike , InputDimsLike , NDArray , Tderivkind
911
1012
1113class FirstDirectionalDerivative (LinearOperator ):
@@ -25,8 +27,9 @@ class FirstDirectionalDerivative(LinearOperator):
2527 v : :obj:`numpy.ndarray`, optional
2628 Single direction (array of size :math:`n_\text{dims}`) or group of directions
2729 (array of size :math:`[n_\text{dims} \times n_{d_0} \times ... \times n_{d_{n_\text{dims}}}]`)
28- sampling : :obj:`tuple`, optional
29- Sampling steps for each direction.
30+ sampling : :obj:`tuple` or :obj:`float`, optional
31+ Sampling steps for each direction. If a single float
32+ is provided, it is used for all directions.
3033 edge : :obj:`bool`, optional
3134 Use reduced order derivative at edges (``True``) or
3235 ignore them (``False``).
@@ -77,9 +80,9 @@ def __init__(
7780 self ,
7881 dims : InputDimsLike ,
7982 v : NDArray ,
80- sampling : int = 1 ,
83+ sampling : Union [ float , InputDimsLike ] = 1.0 ,
8184 edge : bool = False ,
82- kind : str = "centered" ,
85+ kind : Tderivkind = "centered" ,
8386 dtype : DTypeLike = "float64" ,
8487 name : str = "F" ,
8588 ):
@@ -88,7 +91,12 @@ def __init__(
8891 self .kind = kind
8992 self .v = v
9093 Op = self ._calc_first_ddop (
91- dims = dims , sampling = sampling , edge = edge , kind = kind , dtype = dtype , v = v
94+ dims = dims ,
95+ v = v ,
96+ sampling = sampling ,
97+ edge = edge ,
98+ kind = kind ,
99+ dtype = dtype ,
92100 )
93101 super ().__init__ (Op = Op , name = name )
94102
@@ -102,9 +110,9 @@ def _rmatvec(self, x: NDArray) -> NDArray:
102110 def _calc_first_ddop (
103111 dims : InputDimsLike ,
104112 v : NDArray ,
105- sampling : int ,
113+ sampling : Union [ float , InputDimsLike ] ,
106114 edge : bool ,
107- kind : str ,
115+ kind : Tderivkind ,
108116 dtype : DTypeLike ,
109117 ):
110118 Gop = Gradient (dims , sampling = sampling , edge = edge , kind = kind , dtype = dtype )
@@ -133,11 +141,16 @@ class SecondDirectionalDerivative(LinearOperator):
133141 v : :obj:`numpy.ndarray`, optional
134142 Single direction (array of size :math:`n_\text{dims}`) or group of directions
135143 (array of size :math:`[n_\text{dims} \times n_{d_0} \times ... \times n_{d_{n_\text{dims}}}]`)
136- sampling : :obj:`tuple`, optional
137- Sampling steps for each direction.
144+ sampling : :obj:`tuple` or :obj:`float`, optional
145+ Sampling steps for each direction. If a single float
146+ is provided, it is used for all directions.
138147 edge : :obj:`bool`, optional
139148 Use reduced order derivative at edges (``True``) or
140149 ignore them (``False``).
150+ kind : :obj:`str`, optional
151+ .. versionadded:: 2.7.0
152+
153+ Derivative kind (``forward``, ``centered``, or ``backward``).
141154 dtype : :obj:`str`, optional
142155 Type of elements in input array.
143156
@@ -175,17 +188,18 @@ def __init__(
175188 self ,
176189 dims : InputDimsLike ,
177190 v : NDArray ,
178- sampling : int = 1 ,
191+ sampling : Union [ float , InputDimsLike ] = 1.0 ,
179192 edge : bool = False ,
193+ kind : Tderivkind = "centered" ,
180194 dtype : DTypeLike = "float64" ,
181195 name : str = "S" ,
182196 ):
183- self .dims = dims
184- self .v = v
185197 self .sampling = sampling
186198 self .edge = edge
199+ self .kind = kind
200+ self .v = v
187201 Op = self ._calc_second_ddop (
188- dims = dims , v = v , sampling = sampling , edge = edge , dtype = dtype
202+ dims = dims , v = v , sampling = sampling , edge = edge , kind = kind , dtype = dtype
189203 )
190204 super ().__init__ (Op = Op , name = name )
191205
@@ -197,10 +211,15 @@ def _rmatvec(self, x: NDArray) -> NDArray:
197211
198212 @staticmethod
199213 def _calc_second_ddop (
200- dims : InputDimsLike , v : NDArray , sampling : int , edge : bool , dtype : DTypeLike
214+ dims : InputDimsLike ,
215+ v : NDArray ,
216+ sampling : int ,
217+ edge : bool ,
218+ kind : Tderivkind ,
219+ dtype : DTypeLike ,
201220 ):
202221 Dop = FirstDirectionalDerivative (
203- dims = dims , v = v , sampling = sampling , edge = edge , dtype = dtype
222+ dims = dims , v = v , sampling = sampling , edge = edge , kind = kind , dtype = dtype
204223 )
205224 ddop = - Dop .H * Dop
206225 return ddop
0 commit comments