Ask Your Question

Revision history [back]

The following is a pure Python solution to your operator question. I imagine there's a Sage specific solution that provides some speed benefits particularly with operating on Sage objects.

First, establishing some common ground. I assume by an operator you mean something like how $d/dx$ is an operator on differentiable functions. In the sense that if $L=d/dx$ then $L \cdot f = f'$. With this as an example, we start by creating a new Python class and overriding its __mul__() method.

class _dx:
    def __mul__(self, other):
        return other.derivative()

(A tiny class!) Of course, this operator will only work on objects that have a .derivative() method. After defining this class we can use it in Sage as follows:

sage: dx = _dx()
sage: var('x')
sage: f = sin(x) + x^2
sage: dx*f
cos(x) + 2*x

My guess is that there's a way to make a Singleton / class object sort of thing where you don't need the line "dx = _dx()". Unfortunately, I'm not sure how to go about doing that.