Ask Your Question

Revision history [back]

This is more like a comment to Mike's answer, but it's rather long and I don't see how to add code to comments, so I'll put it here.

A workaround to check if an expression is trivially equal to another is to use match().

sage: tan(x).match(tan(x)) is not None
True
sage: tan(x).match(log(x)) is not None
False

This is very fast:

sage: sage: %timeit bool(sin(x) == sin(x))
625 loops, best of 3: 19.8 µs per loop
sage: sage: %timeit bool(sin(x) == log(x))
5 loops, best of 3: 639 ms per loop
sage: sage: %timeit bool(sin(x).match(log(x)) is not None)
625 loops, best of 3: 8.3 µs per loop
sage: sage: %timeit bool(sin(x).match(sin(x)) is not None)
625 loops, best of 3: 10.9 µs per loop

Here is the mydiff() function using this hack (and removing the reduce() call for sums):

from operator import add, mul, pow
def mydiff2(s,x=x):
    if not s.has(x):
        return 0
    op = s.operator()
    ops = s.operands()
    if s.match(x) is not None:
        return 1
    elif op is log and ops[0].match(x) is not None:
        return 1/x
    elif op is sin and ops[0].match(x) is not None:
        return cos(x)
    elif op is cos and ops[0].match(x) is not None:
        return -sin(x)
    elif op is tan and ops[0].match(x) is not None:
        return 1+tan(x)**2
    elif op is arcsin and ops[0].match(x) is not None:
        return 1/sqrt(1-x**2)
    elif op is arctan and ops[0].match(x) is not None:
        return 1/(1+x**2)
    elif op is pow and (ops[0].match(x) is not None) and ops[1]._is_numeric():
        return ops[1]*ops[0]**(ops[1]-1)
    elif op is add:
        return sum(map(mydiff2, ops))
    elif op is mul:
        f = ops[0]; g = reduce(mul,ops[1:])
        return g*mydiff2(f) + f*mydiff2(g)

Now I get:

sage: var('k')
k
sage: inp = sum(k*x^k,k,0,10)
sage: inp
10*x^10 + 9*x^9 + 8*x^8 + 7*x^7 + 6*x^6 + 5*x^5 + 4*x^4 + 3*x^3 + 2*x^2 + x
sage: load ../symbolic_eq2.py
sage: %time res2 = mydiff2(inp)
CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
Wall time: 0.00 s
sage: res2
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

Note that speeding up symbolic comparison is #6799 on trac.