Skip to content

Commit 210afac

Browse files
Marius GerbershagenMariusGerbershagen
authored andcommitted
symbolics: add derivative operator
This allows for also using the syntax which sagemath already uses in the output of symbolic derivatives of functions like `D[0,1](f)(x+y,x-y)` in user input.
1 parent 05329f6 commit 210afac

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

src/sage/symbolic/all.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@
1818
from .units import units
1919

2020
π = pi
21+
22+
from .operators import D

src/sage/symbolic/operators.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,65 @@ def parameter_set(self):
194194
[0, 1]
195195
"""
196196
return self._parameter_set
197+
198+
class DerivativeOperator():
199+
"""
200+
Derivative operator.
201+
202+
Acting with this operator onto a function gives a new operator (of
203+
type :class:`FDerivativeOperator`) representing the function
204+
differentiated with respect to one or multiple of its arguments.
205+
206+
This operator takes a list of indices specifying the position of
207+
the arguments to differentiate. For example, D[0, 0, 1] is an
208+
operator that differentiates a function twice with respect to its
209+
first argument and once with respect to its second argument.
210+
211+
EXAMPLES::
212+
213+
sage: x, y = var('x,y'); f = function('f')
214+
sage: D[0](f)(x)
215+
diff(f(x), x)
216+
sage: D[0](f)(x, y)
217+
diff(f(x, y), x)
218+
sage: D[0, 1](f)(x, y)
219+
diff(f(x, y), x, y)
220+
sage: D[0, 1](f)(x, x^2)
221+
D[0, 1](f)(x, x^2)
222+
223+
"""
224+
class DerivativeOperatorWithParameters():
225+
def __init__(self, parameter_set):
226+
self._parameter_set = parameter_set
227+
def __call__(self, function):
228+
return FDerivativeOperator(function, self._parameter_set)
229+
def __repr__(self):
230+
"""
231+
Return the string representation of this derivative operator.
232+
233+
EXAMPLES::
234+
235+
sage: D[0]
236+
D[0]
237+
sage: D[0, 1]
238+
D[0, 1]
239+
"""
240+
return "D[%s]" % (", ".join(map(repr, self._parameter_set)))
241+
242+
def __getitem__(self, args):
243+
"""
244+
TESTS::
245+
246+
The order in which the indices are given should not matter
247+
248+
sage: x, y = var('x,y'); f = function('f')
249+
sage: bool(D[0, 1, 0](f)(x, y) == D[0, 0, 1](f)(x, y))
250+
True
251+
sage: bool(D[1, 0, 0](f)(x, y) == D[0, 0, 1](f)(x, y))
252+
True
253+
"""
254+
if not isinstance(args, tuple):
255+
args = (args,)
256+
return self.DerivativeOperatorWithParameters(args)
257+
258+
D = DerivativeOperator()

0 commit comments

Comments
 (0)