The following code seems to do the job. However, it looks very clumsy and I would be very happy to receive comments.
##################################################################
from sage.symbolic.operators import add_vararg, mul_vararg
##################################################################
def efdd(self, x):
# x is a number:
if x in RR or (I*x) in RR:
return 0
# Exponential exp(a)
elif x.operator()==exp:
return dd((log(x)).simplify())*x
# Logarithm log(a)
elif x.operator()==ln:
return (1/exp(x))*dd(exp(x))
# Single symbolic input
elif x.is_symbol():
if x.is_integer() or (I*x).is_integer() or x in RR or (I*x) in RR:
return 0
else:
pass
elif (-1*(x)).is_symbol():
if x.is_integer() or (I*x).is_integer() or x in RR or (I*x) in RR:
return 0
else:
return -dd(-x)
# Power in input with plus (a1^c1)
elif x.operator() == operator.pow:
try:
a, b = x.operands()
if a.is_integer() or a in RR or (I*a) in RR:
return a*(b^(a-1))*dd(b)
else:
return b*(a^(b-1))*dd(a)
except:
a, b, c = x.operands()
if a.is_integer() or (I*a).is_integer() or a in RR or (I*a) in RR:
return a*(b^(a-1))*dd(b)
else:
return b*(a^(b-1))*dd(a)
# Power in input with minus (-a1^c1)
elif (-1*x).operator() == operator.pow:
try:
a, b = (-1*x).operands()
if a.is_integer() or (I*a).is_integer() or a in RR or (I*a) in RR:
return -a*(b^(a-1))*dd(b)
else:
return -b*(a^(b-1))*dd(a)
except:
a, b, c = (-1*x).operands()
if a.is_integer() or (I*a).is_integer() or a in RR or (I*a) in RR:
return -a*(b^(a-1))*dd(b)
else:
return -b*(a^(b-1))*dd(a)
# Summation in input
elif x.operator() == add_vararg:
oplist = x.operands()
return sum(dd(op) for op in oplist)
# Multiplication in input
elif x.operator() == mul_vararg:
ops=x.operands()
result=0
for i in range(len(ops)):
prod=1
for j in range(len(ops)):
if i!=j:
prod=prod*ops[j]
result=result+dd(ops[i])*prod
return result
##################################################################
dd = function("dd",latex_name=r"\Delta",eval_func=efdd)
##################################################################
var('a1,a2,a3,a4')
var('c1,c2,c3')
assume(c1,'integer')
assume(c2,'integer')
assume(c3,'integer')
show("dd(a1+a2+a3)=",dd(a1+a2+a3))
show("dd(-a1+a2-a3)=",dd(-a1+a2-a3))
show("dd(a1*a2)=",dd(a1*a2))
show("dd(a1*a2-a3*a4)=",dd(a1*a2-a3*a4))
show("dd(c1*a1)=",dd(c1*a1))
show("dd(-c1*a2)=",dd(-c1*a2))
show("dd(a1^c1)=",dd(a1^c1))
show("dd(-a1^c1)=",dd(-a1^c1))
show("dd(1/a1)=",dd(1/a1))
show("dd(-1/a1)=",dd(-1/a1))
show("dd(1/(a1^2))=",dd(1/(a1^2)))
show("dd(c1*a1*a2)=",dd(c1*a1*a2))
show("dd(a1*a2*a3*a4)=",dd(a1*a2*a3*a4))
show("dd(c1*a2*a3*a4)=",dd(c1*a2*a3*a4))
Check this out: https://doc.sagemath.org/html/en/refe...