1 | initial version |
Here are several non-answers, in random order of craziness and/or workingness (which isn't a word, but you know what I mean.) All of them are dangerous and/or will break in many important cases. I'm posting them more as possible directions to follow up than as anything that anyone would actually use.
(1) If you're willing to live with a DeprecationWarning, the following should do most of what you want:
class D(object):
def __getitem__(self, *args):
args = flatten(args)
def dfun(f):
ops = f.operands()
dv = [f] + [ops[i] for i in args]
return diff(*dv)
return dfun
D = D()
After which the D operator kind of works:
sage: f = function('f', var('t'), var("x"), var("y"))
sage: expand((diff(f,x,y)+1)**2)
D[1, 2](f)(t, x, y)^2 + 2*D[1, 2](f)(t, x, y) + 1
sage: D[1,2](f)(t, x, y)^2 + 2*D[1,2](f)(t, x, y) + 1
D[1, 2](f)(t, x, y)^2 + 2*D[1, 2](f)(t, x, y) + 1
sage:
sage: old_form = expand((diff(f,x,y)+1)**2)
sage: new_form = D[1,2](f)(t, x, y)^2 + 2*D[1,2](f)(t, x, y) + 1
sage: bool(old_form == new_form)
True
(2) You could simply define a print function to make sure that you get the output in a form you can re-enter (i.e. one with diffs). Here I'll cheat a little and use the existing maxima printing:
def dpr(obj):
print repr(obj._maxima_()).replace("'","")
sage: z = expand((diff(f,x,y)+1)**2)
sage: z
D[1, 2](f)(t, x, y)^2 + 2*D[1, 2](f)(t, x, y) + 1
sage: dpr(z)
(diff(f(t,x,y),x,1,y,1))^2+2*diff(f(t,x,y),x,1,y,1)+1
sage: bool((diff(f(t,x,y),x,1,y,1))^2+2*diff(f(t,x,y),x,1,y,1)+1 == z)
True
(3) You can play games with FDerivativeOperator's repr, but I couldn't get this to work because of the way Expressions print. Namely:
sage: f = function("f", var("t"))
sage: diff(f,t)
D[0](f)(t)
sage: diff(f,t).operator()
D[0](f)
sage:
sage: sage.symbolic.operators.FDerivativeOperator.__repr__ = lambda *args: 'fred'
sage: diff(f,t)
D[0](f)(t)
sage: diff(f,t).operator()
fred
I can change the name that the operator itself has, but that doesn't seem to be used along the Expression printing path. I always get lost in the code at this point, so this is kind of a wash. But I feel like something along these lines should work, even though it doesn't seem to.
If I actually needed this to work I would probably use a variant of (2). I can't help but wonder if there isn't a better way to accomplish your goal, though.
2 | No.2 Revision |
Here are several non-answers, in random order of craziness and/or workingness (which isn't a word, but you know what I mean.) All of them are dangerous and/or will break in many important cases. I'm posting them more as possible directions to follow up than as anything that anyone would actually use.
(1) If you're willing to live with a DeprecationWarning, the following should do most of what you want:
class D(object):
def __getitem__(self, *args):
args = flatten(args)
def dfun(f):
ops = f.operands()
dv = [f] + [ops[i] for i in args]
return diff(*dv)
return dfun
D = D()
After which the D operator kind of works:
sage: f = function('f', var('t'), var("x"), var("y"))
sage: expand((diff(f,x,y)+1)**2)
D[1, 2](f)(t, x, y)^2 + 2*D[1, 2](f)(t, x, y) + 1
sage: D[1,2](f)(t, x, y)^2 + 2*D[1,2](f)(t, x, y) + 1
D[1, 2](f)(t, x, y)^2 + 2*D[1, 2](f)(t, x, y) + 1
sage:
sage: old_form = expand((diff(f,x,y)+1)**2)
sage: new_form = D[1,2](f)(t, x, y)^2 + 2*D[1,2](f)(t, x, y) + 1
sage: bool(old_form == new_form)
True
(2) You could simply define a print function to make sure that you get the output in a form you can re-enter (i.e. one with diffs). Here I'll cheat a little and use the existing maxima printing:
def dpr(obj):
print repr(obj._maxima_()).replace("'","")
sage: z = expand((diff(f,x,y)+1)**2)
sage: z
D[1, 2](f)(t, x, y)^2 + 2*D[1, 2](f)(t, x, y) + 1
sage: dpr(z)
(diff(f(t,x,y),x,1,y,1))^2+2*diff(f(t,x,y),x,1,y,1)+1
sage: bool((diff(f(t,x,y),x,1,y,1))^2+2*diff(f(t,x,y),x,1,y,1)+1 == z)
True
(3) You can play games with FDerivativeOperator's repr, but I couldn't get this to work because of the way Expressions print. Namely:
sage: f = function("f", var("t"))
sage: diff(f,t)
D[0](f)(t)
sage: diff(f,t).operator()
D[0](f)
sage:
sage: sage.symbolic.operators.FDerivativeOperator.__repr__ = lambda *args: 'fred'
sage: diff(f,t)
D[0](f)(t)
sage: diff(f,t).operator()
fred
I can change the name that the operator itself has, but that doesn't seem to be used along the Expression printing path. I always get lost in the code at this point, so this is kind of a wash. But I feel like something along these lines should work, even though it doesn't seem to.
If I actually needed to do something like this to work I would probably use a variant of (2). I can't help but wonder if there isn't a better way to accomplish your goal, though.