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.Wed, 19 Dec 2012 05:06:16 +0100Does a subtraction symbolic expression actually exist?https://ask.sagemath.org/question/9398/does-a-subtraction-symbolic-expression-actually-exist/Can someone please give me an example of a symbolic expression in sage of the subtraction variety? (by subtraction variety I mean using the subtraction operator)
Precisely: how can I create an object `o` of type `sage.symbolic.expression.Expression` such that `o.operator()` is `operator.sub`? It seems that subtraction expressions are always converted to additions.
For example:
sage: x = var('x')
sage: (x-1).operator()
<built-in function add>
Tue, 18 Dec 2012 22:28:15 +0100https://ask.sagemath.org/question/9398/does-a-subtraction-symbolic-expression-actually-exist/Comment by kcrisman for <p>Can someone please give me an example of a symbolic expression in sage of the subtraction variety? (by subtraction variety I mean using the subtraction operator) </p>
<p>Precisely: how can I create an object <code>o</code> of type <code>sage.symbolic.expression.Expression</code> such that <code>o.operator()</code> is <code>operator.sub</code>? It seems that subtraction expressions are always converted to additions. </p>
<p>For example:</p>
<pre><code>sage: x = var('x')
sage: (x-1).operator()
<built-in function add>
</code></pre>
https://ask.sagemath.org/question/9398/does-a-subtraction-symbolic-expression-actually-exist/?comment=18506#post-id-18506Apparently we don't use this, though ginac does seem to have a `sub` subtraction thing. Interesting.Tue, 18 Dec 2012 22:45:09 +0100https://ask.sagemath.org/question/9398/does-a-subtraction-symbolic-expression-actually-exist/?comment=18506#post-id-18506Comment by benjaminfjones for <p>Can someone please give me an example of a symbolic expression in sage of the subtraction variety? (by subtraction variety I mean using the subtraction operator) </p>
<p>Precisely: how can I create an object <code>o</code> of type <code>sage.symbolic.expression.Expression</code> such that <code>o.operator()</code> is <code>operator.sub</code>? It seems that subtraction expressions are always converted to additions. </p>
<p>For example:</p>
<pre><code>sage: x = var('x')
sage: (x-1).operator()
<built-in function add>
</code></pre>
https://ask.sagemath.org/question/9398/does-a-subtraction-symbolic-expression-actually-exist/?comment=18503#post-id-18503subtraction is not a nice operator; it's not associative!Wed, 19 Dec 2012 00:40:46 +0100https://ask.sagemath.org/question/9398/does-a-subtraction-symbolic-expression-actually-exist/?comment=18503#post-id-18503Answer by burcin for <p>Can someone please give me an example of a symbolic expression in sage of the subtraction variety? (by subtraction variety I mean using the subtraction operator) </p>
<p>Precisely: how can I create an object <code>o</code> of type <code>sage.symbolic.expression.Expression</code> such that <code>o.operator()</code> is <code>operator.sub</code>? It seems that subtraction expressions are always converted to additions. </p>
<p>For example:</p>
<pre><code>sage: x = var('x')
sage: (x-1).operator()
<built-in function add>
</code></pre>
https://ask.sagemath.org/question/9398/does-a-subtraction-symbolic-expression-actually-exist/?answer=14385#post-id-14385We only use `add`, `mul` and `pow` operations in the expression tree. Subtraction is represented as addition with an appropriate coefficient and division is multiplication with an appropriate exponent. This makes it easier to handle expressions since you don't need to worry about handling these cases separately.
For example:
sage: var('x,y')
(x, y)
sage: ex = x+y-1
sage: ex.operator()
<built-in function add>
sage: ex.operands()
[x, y, -1]
You can read more about the internal representation of expressions in [the relevant part of the GiNaC tutorial](http://www.ginac.de/tutorial/Internal-representation-of-products-and-sums.html#Internal-representation-of-products-and-sums).
The patch attached to [ticket #13738](http://trac.sagemath.org/sage_trac/ticket/13738) wraps some internal GiNaC functions to view the expression tree.
Wed, 19 Dec 2012 05:06:16 +0100https://ask.sagemath.org/question/9398/does-a-subtraction-symbolic-expression-actually-exist/?answer=14385#post-id-14385