Ask Your Question

Does a subtraction symbolic expression actually exist?

asked 2012-12-18 15:28:15 -0600

rmp251 gravatar image

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>
edit retag flag offensive close merge delete


Apparently we don't use this, though ginac does seem to have a `sub` subtraction thing. Interesting.

kcrisman gravatar imagekcrisman ( 2012-12-18 15:45:09 -0600 )edit

subtraction is not a nice operator; it's not associative!

benjaminfjones gravatar imagebenjaminfjones ( 2012-12-18 17:40:46 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2012-12-18 22:06:16 -0600

We 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.

The patch attached to ticket #13738 wraps some internal GiNaC functions to view the expression tree.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-12-18 15:28:15 -0600

Seen: 228 times

Last updated: Dec 18 '12