why is symbolic comparison so slow?
I tried to write a little differentiatior:
from operator import add, mul, pow
def mydiff(s,x=x):
if x not in SR(s).variables():
return 0
elif s == x:
return 1
elif s == log(x):
return 1/x
elif s == sin(x):
return cos(x)
elif s == cos(x):
return -sin(x)
elif s == tan(x):
return 1+tan(x)^2
elif s == arcsin(x):
return 1/sqrt(1-x^2)
elif s == arctan(x):
return 1/(1+x^2)
op = s.operator()
ops = s.operands()
if op == pow and ops[0]==x and ops[1]._is_numeric():
return ops[1]*ops[0]^(ops[1]-1)
elif op == add:
return mydiff(ops[0]) + mydiff(reduce(op,ops[1:]))
elif op == mul:
f = ops[0]; g = reduce(mul,ops[1:])
return g*mydiff(f) + f*mydiff(g)
elif len(ops)==1:
return mydiff(ops[0],x)*mydiff(op(x),x).subs(x=ops[0]))
one my one year old macbook i need to wait more than 17 second to get the following answer
%time mydiff(sum(k*x^k,k,0,10))
CPU times: user 12.39 s, sys: 0.78 s, total: 13.17 s
Wall time: 17.49 s
100*x^9 + 81*x^8 + 64*x^7 + 49*x^6 + 36*x^5 + 25*x^4 + 16*x^3 + 9*x^2 + 4*x + 1
why does it _that_ long?