Ask Your Question

cory tobin's profile - activity

2017-12-20 06:51:27 +0100 received badge  Famous Question (source)
2017-03-21 16:42:29 +0100 received badge  Notable Question (source)
2014-11-30 18:55:00 +0100 received badge  Popular Question (source)
2011-05-25 02:45:00 +0100 received badge  Nice Question (source)
2011-05-25 01:35:03 +0100 received badge  Student (source)
2011-05-24 21:53:01 +0100 marked best answer Defining new operators

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.

2011-05-24 21:53:01 +0100 received badge  Scholar (source)
2011-05-24 21:52:58 +0100 commented answer Defining new operators

Thanks! That should suffice for my purposes.

2011-05-24 21:42:23 +0100 received badge  Supporter (source)
2011-05-24 20:13:35 +0100 asked a question Defining new operators

Is there a way to define new operators in sage? I'm familiar with working in Mathematica which allows you to create new operators (and palettes to easily access the operators) but I couldn't find any similar feature mentioned in the sage docs. I know Python doesn't allow new operators but I figured maybe the sage preparser fixes that.

Thanks!