1 | initial version |
The following 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):
# Single input
if x.is_symbol():
if x.is_integer():
return 0
else:
pass
elif (-1*(x)).is_symbol():
if x.is_integer():
return 0
else:
return -dd(-x)
# Power in input with plus (a1^c1)
elif x.operator() == operator.pow:
#print('pow1')
try:
a, b = x.operands()
if a.is_integer():
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():
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:
#print('pow2')
try:
a, b = (-1*x).operands()
if a.is_integer():
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():
return -a*(b^(a-1))*dd(b)
else:
return -b*(a^(b-1))*dd(a)
# Summation in input
elif x.operator() == add_vararg:
#print('add')
oplist = x.operands()
return sum(dd(op) for op in oplist)
# Multiplication in input
elif x.operator() == mul_vararg:
#print('mul')
try:
a, b = x.operands()
if a.is_integer():
return a*dd(b)
elif a.is_integer():
return b*dd(a)
else:
return a*dd(b) + b*dd(a)
except:
a, b, c = x.operands()
if a.is_integer():
return -a*dd(b)
elif a.is_integer():
return -b*dd(a)
else:
return -a*dd(b) - b*dd(a)
##################################################################
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')
eq1=a1*a2+c1*a3-a2*a4
eq2=a1^c1
eq3=-a1^c1
eq4=1/a1
show(dd(eq1))
show(dd(eq2))
show(dd(eq3))
show(dd(eq4))
2 | No.2 Revision |
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):
# Single input
if x.is_symbol():
if x.is_integer():
return 0
else:
pass
elif (-1*(x)).is_symbol():
if x.is_integer():
return 0
else:
return -dd(-x)
# Power in input with plus (a1^c1)
elif x.operator() == operator.pow:
#print('pow1')
try:
a, b = x.operands()
if a.is_integer():
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():
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:
#print('pow2')
try:
a, b = (-1*x).operands()
if a.is_integer():
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():
return -a*(b^(a-1))*dd(b)
else:
return -b*(a^(b-1))*dd(a)
# Summation in input
elif x.operator() == add_vararg:
#print('add')
oplist = x.operands()
return sum(dd(op) for op in oplist)
# Multiplication in input
elif x.operator() == mul_vararg:
#print('mul')
try:
a, b = x.operands()
if a.is_integer():
return a*dd(b)
elif a.is_integer():
return b*dd(a)
else:
return a*dd(b) + b*dd(a)
except:
a, b, c = x.operands()
if a.is_integer():
return -a*dd(b)
elif a.is_integer():
return -b*dd(a)
else:
return -a*dd(b) - b*dd(a)
##################################################################
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')
eq1=a1*a2+c1*a3-a2*a4
eq2=a1^c1
eq3=-a1^c1
eq4=1/a1
show(dd(eq1))
show(dd(eq2))
show(dd(eq3))
show(dd(eq4))
3 | No.3 Revision |
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_symbol():
if x.is_integer():
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():
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:
#print('pow1')
try:
a, b = x.operands()
if a.is_integer():
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():
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:
#print('pow2')
try:
a, b = (-1*x).operands()
if a.is_integer():
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():
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:
#print('add')
oplist = x.operands()
return sum(dd(op) for op in oplist)
# Multiplication in input
elif x.operator() == mul_vararg:
#print('mul')
try:
a, b = x.operands()
ops=x.operands()
result=0
for i in range(len(ops)):
prod=1
for j in range(len(ops)):
if a.is_integer():
return a*dd(b)
elif a.is_integer():
return b*dd(a)
else:
return a*dd(b) + b*dd(a)
except:
a, b, c = x.operands()
if a.is_integer():
return -a*dd(b)
elif a.is_integer():
return -b*dd(a)
else:
return -a*dd(b) - b*dd(a)
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')
eq1=a1*a2+c1*a3-a2*a4
eq2=a1^c1
eq3=-a1^c1
eq4=1/a1
show(dd(eq1))
show(dd(eq2))
show(dd(eq3))
show(dd(eq4))
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))