ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 24 May 2011 22:09:53 +0200Defining new operatorshttps://ask.sagemath.org/question/8128/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!Tue, 24 May 2011 20:13:35 +0200https://ask.sagemath.org/question/8128/defining-new-operators/Answer by cswiercz for <p>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.</p>
<p>Thanks!</p>
https://ask.sagemath.org/question/8128/defining-new-operators/?answer=12377#post-id-12377The 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.Tue, 24 May 2011 21:31:59 +0200https://ask.sagemath.org/question/8128/defining-new-operators/?answer=12377#post-id-12377Comment by cswiercz for <p>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.</p>
<p>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 <code>__mul__()</code> method.</p>
<pre><code>class _dx:
def __mul__(self, other):
return other.derivative()
</code></pre>
<p>(A tiny class!) Of course, this operator will only work on objects that have a <code>.derivative()</code> method. After defining this class we can use it in Sage as follows:</p>
<pre><code>sage: dx = _dx()
sage: var('x')
sage: f = sin(x) + x^2
sage: dx*f
cos(x) + 2*x
</code></pre>
<p>My guess is that there's a way to make a Singleton / class object sort of thing where you don't need the line "<code>dx = _dx()</code>". Unfortunately, I'm not sure how to go about doing that.</p>
https://ask.sagemath.org/question/8128/defining-new-operators/?comment=21677#post-id-21677No problem. I just thought of a more elegant approach where you could overwrite the __lmul__ and __rmul__ methods separately (left and right multiplication) so you could define "L = 2*dx" as a new operator once "dx" is defined as above. If you like I could write up an example, depending on your needs. A full list of overwritable class methods can be found here: http://docs.python.org/reference/datamodel.htmlTue, 24 May 2011 22:09:53 +0200https://ask.sagemath.org/question/8128/defining-new-operators/?comment=21677#post-id-21677Comment by cory tobin for <p>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.</p>
<p>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 <code>__mul__()</code> method.</p>
<pre><code>class _dx:
def __mul__(self, other):
return other.derivative()
</code></pre>
<p>(A tiny class!) Of course, this operator will only work on objects that have a <code>.derivative()</code> method. After defining this class we can use it in Sage as follows:</p>
<pre><code>sage: dx = _dx()
sage: var('x')
sage: f = sin(x) + x^2
sage: dx*f
cos(x) + 2*x
</code></pre>
<p>My guess is that there's a way to make a Singleton / class object sort of thing where you don't need the line "<code>dx = _dx()</code>". Unfortunately, I'm not sure how to go about doing that.</p>
https://ask.sagemath.org/question/8128/defining-new-operators/?comment=21678#post-id-21678Thanks! That should suffice for my purposes.Tue, 24 May 2011 21:52:58 +0200https://ask.sagemath.org/question/8128/defining-new-operators/?comment=21678#post-id-21678