1 | initial version |

Perhaps this could help. Define a class MYSR, that inherits from symboic expressions (so that you keep all other operations like multiplication), but owerwrites substraction and addition to avoid cancellations:

```
class MYSR(sage.symbolic.expression.Expression):
def __sub__(self,other):
if self == other:
return self
else:
return super(MYSR,self).__sub__(other)
def __add__(self,other):
if self == -other:
return self
else:
return super(MYSR,self).__add__(other)
U = lambda x: MYSR(SR,x)
w = U(units.power.watt)
m = U(units.length.meter)
n = U(units.force.newton)
```

Then, you can do something like:

```
sage: (w-w)*(n+m)
(meter + newton)*watt
sage: (w+(-w))
watt
```

2 | No.2 Revision |

Perhaps this could help. Define a class MYSR, that inherits from symboic expressions (so that you keep all other operations like multiplication), but owerwrites substraction and addition to avoid cancellations:

```
class MYSR(sage.symbolic.expression.Expression):
def __sub__(self,other):
if self == other:
return self
else:
return super(MYSR,self).__sub__(other)
def __add__(self,other):
if self == -other:
return self
else:
return super(MYSR,self).__add__(other)
U = lambda x: MYSR(SR,x)
```~~w ~~watt = U(units.power.watt)
~~m ~~meter = U(units.length.meter)
~~n ~~newton = U(units.force.newton)

Then, you can do something like:

`sage: `~~(w-w)*(n+m)
~~(watt-watt)*(newton+meter)
(meter + newton)*watt
sage: ~~(w+(-w))
~~(watt+(-watt))
watt

3 | No.3 Revision |

Perhaps this could help. Define a class MYSR, that inherits from symboic expressions (so that you keep all other ~~operations like multiplication), ~~operations), but owerwrites substraction and addition to avoid cancellations:

```
class MYSR(sage.symbolic.expression.Expression):
def __sub__(self,other):
if self == other:
return self
else:
return
```~~super(MYSR,self).__sub__(other)
~~MYSR(SR,super(MYSR,self).__sub__(other))
def __add__(self,other):
if self == -other:
return self
else:
return ~~super(MYSR,self).__add__(other)
~~MYSR(SR,super(MYSR,self).__add__(other))
def __mul__(self,other):
return MYSR(SR,super(MYSR,self).__mul__(other))
def __div__(self,other):
return MYSR(SR,super(MYSR,self).__div__(other))
U = lambda x: MYSR(SR,x)
watt = U(units.power.watt)
meter = U(units.length.meter)
newton = U(units.force.newton)

Then, you can do something like:

```
sage: (watt-watt)*(newton+meter)
(meter + newton)*watt
sage: (watt+(-watt))
watt
```

4 | No.4 Revision |

Perhaps this could help. Define a class MYSR, that inherits from symboic expressions (so that you keep all other operations), but owerwrites substraction and addition to avoid cancellations:

```
class MYSR(sage.symbolic.expression.Expression):
def __sub__(self,other):
if self == other:
return self
else:
return MYSR(SR,super(MYSR,self).__sub__(other))
def __add__(self,other):
if self == -other:
return self
else:
return MYSR(SR,super(MYSR,self).__add__(other))
def __mul__(self,other):
return MYSR(SR,super(MYSR,self).__mul__(other))
def __div__(self,other):
return MYSR(SR,super(MYSR,self).__div__(other))
U = lambda x: MYSR(SR,x)
watt = U(units.power.watt)
meter = U(units.length.meter)
newton = U(units.force.newton)
```

Then, you can do something like:

```
sage: (watt-watt)*(newton+meter) - (watt-watt)*(newton+meter)
(meter + newton)*watt
sage: (watt+(-watt))
watt
```

5 | No.5 Revision |

Perhaps this could help. Define a class MYSR, that inherits from symboic expressions (so that you keep all other operations), but owerwrites substraction and addition to avoid cancellations:

```
class MYSR(sage.symbolic.expression.Expression):
def __sub__(self,other):
if self == other:
return self
else:
return MYSR(SR,super(MYSR,self).__sub__(other))
def __add__(self,other):
if self == -other:
return self
else:
return MYSR(SR,super(MYSR,self).__add__(other))
def __mul__(self,other):
return MYSR(SR,super(MYSR,self).__mul__(other))
def __div__(self,other):
return MYSR(SR,super(MYSR,self).__div__(other))
U = lambda x: MYSR(SR,x)
watt = U(units.power.watt)
meter = U(units.length.meter)
newton = U(units.force.newton)
```

Then, you can do something like:

```
sage: (watt-watt)*(newton+meter) - (watt-watt)*(newton+meter)
(meter + newton)*watt
sage: (watt+(-watt))
watt
```

But use with caution if you use some other operation do happen in your computation:

```
sage: sqrt(watt) - sqrt(watt)
0
sage: type(sqrt(watt))
<type 'sage.symbolic.expression.Expression'>
```

6 | No.6 Revision |

Perhaps this could help. Define a class MYSR, that inherits from symboic expressions (so that you keep all other operations), but owerwrites substraction and addition to avoid cancellations:

```
class MYSR(sage.symbolic.expression.Expression):
def __sub__(self,other):
if self == other:
return self
else:
return MYSR(SR,super(MYSR,self).__sub__(other))
def __add__(self,other):
if self == -other:
return self
else:
return MYSR(SR,super(MYSR,self).__add__(other))
def __mul__(self,other):
return MYSR(SR,super(MYSR,self).__mul__(other))
def __div__(self,other):
return MYSR(SR,super(MYSR,self).__div__(other))
U = lambda x: MYSR(SR,x)
watt = U(units.power.watt)
meter = U(units.length.meter)
newton = U(units.force.newton)
```

Then, you can do something like:

```
sage: (watt-watt)*(newton+meter) - (watt-watt)*(newton+meter)
(meter + newton)*watt
sage: (watt+(-watt))
watt
```

But use with ~~caution ~~caution. You wil have to adapt if you want to use some other operation ~~do happen ~~in your computation:

```
sage: sqrt(watt) - sqrt(watt)
0
sage: type(sqrt(watt))
<type 'sage.symbolic.expression.Expression'>
```

Also, some "external" operations use coercion, that should be defined as well:

```
sage: 2*watt-2*watt
0
sage: watt*2-watt*2
2*watt
sage: type(watt*2)
<class 'sage.all_cmdline.MYSR'>
sage: type(2*watt)
<type 'sage.symbolic.expression.Expression'>
```

7 | No.7 Revision |

```
class MYSR(sage.symbolic.expression.Expression):
def __sub__(self,other):
if self == other:
return self
else:
return MYSR(SR,super(MYSR,self).__sub__(other))
def __add__(self,other):
if self == -other:
return self
else:
return MYSR(SR,super(MYSR,self).__add__(other))
def __mul__(self,other):
return MYSR(SR,super(MYSR,self).__mul__(other))
def __div__(self,other):
return MYSR(SR,super(MYSR,self).__div__(other))
U = lambda x: MYSR(SR,x)
watt = U(units.power.watt)
meter = U(units.length.meter)
newton = U(units.force.newton)
```

Then, you can do something like:

```
sage: (watt-watt)*(newton+meter) - (watt-watt)*(newton+meter)
(meter + newton)*watt
sage: (watt+(-watt))
watt
```

But use with caution. You wil have to adapt if you want to use some other operation in your computation:

```
sage: sqrt(watt) - sqrt(watt)
0
sage: type(sqrt(watt))
<type 'sage.symbolic.expression.Expression'>
```

Also, some "external" operations use coercion, that should be defined as well:

```
sage: 2*watt-2*watt
0
sage: watt*2-watt*2
2*watt
sage: type(watt*2)
<class 'sage.all_cmdline.MYSR'>
sage: type(2*watt)
<type 'sage.symbolic.expression.Expression'>
```

See those pages if you want to deal with such external products :

8 | No.8 Revision |

~~Perhaps this could help. Define ~~You can define a class MYSR, that inherits from symboic expressions (so that you keep all other operations), but owerwrites substraction and addition to avoid cancellations:

```
class MYSR(sage.symbolic.expression.Expression):
def __sub__(self,other):
if self == other:
return self
else:
return MYSR(SR,super(MYSR,self).__sub__(other))
def __add__(self,other):
if self == -other:
return self
else:
return MYSR(SR,super(MYSR,self).__add__(other))
def __mul__(self,other):
return MYSR(SR,super(MYSR,self).__mul__(other))
def __div__(self,other):
return MYSR(SR,super(MYSR,self).__div__(other))
U = lambda x: MYSR(SR,x)
watt = U(units.power.watt)
meter = U(units.length.meter)
newton = U(units.force.newton)
```

Then, you can do something like:

```
sage: (watt-watt)*(newton+meter) - (watt-watt)*(newton+meter)
(meter + newton)*watt
sage: (watt+(-watt))
watt
```

But use with caution. You wil have to adapt if you want to use some other operation in your computation:

```
sage: sqrt(watt) - sqrt(watt)
0
sage: type(sqrt(watt))
<type 'sage.symbolic.expression.Expression'>
```

Also, some "external" operations use coercion, that should be defined as well:

```
sage: 2*watt-2*watt
0
sage: watt*2-watt*2
2*watt
sage: type(watt*2)
<class 'sage.all_cmdline.MYSR'>
sage: type(2*watt)
<type 'sage.symbolic.expression.Expression'>
```

See those pages if you want to deal with such external products :

9 | No.9 Revision |

You can define a class ~~MYSR, ~~MYExp, that inherits from symboic expressions (so that you keep all other operations), but owerwrites substraction and addition to avoid cancellations:

`class `~~MYSR(sage.symbolic.expression.Expression):
~~MYExp(sage.symbolic.expression.Expression):
def __sub__(self,other):
if self == other:
return self
else:
return ~~MYSR(SR,super(MYSR,self).__sub__(other))
~~MYExp(SR, super(MYSR, self).__sub__(other))
def __add__(self,other):
if self == -other:
return self
else:
return ~~MYSR(SR,super(MYSR,self).__add__(other))
~~MYExp(SR,super(MYExp, self).__add__(other))
def __mul__(self,other):
return ~~MYSR(SR,super(MYSR,self).__mul__(other))
~~MYExp(SR, super(MYExp, self).__mul__(other))
def __div__(self,other):
return ~~MYSR(SR,super(MYSR,self).__div__(other))
~~MYExp(SR, super(MYSR, self).__div__(other))
U = lambda x: ~~MYSR(SR,x)
~~MYExp(SR,x)
watt = U(units.power.watt)
meter = U(units.length.meter)
newton = U(units.force.newton)

Then, you can do something like:

```
sage: (watt-watt)*(newton+meter) - (watt-watt)*(newton+meter)
(meter + newton)*watt
sage: (watt+(-watt))
watt
```

But use with caution. You wil have to adapt if you want to use some other operation in your computation:

```
sage: sqrt(watt) - sqrt(watt)
0
sage: type(sqrt(watt))
<type 'sage.symbolic.expression.Expression'>
```

Also, some "external" operations use coercion, that should be defined as well:

```
sage: 2*watt-2*watt
0
sage: watt*2-watt*2
2*watt
sage: type(watt*2)
<class
```~~'sage.all_cmdline.MYSR'>
~~'sage.all_cmdline.MYExp'>
sage: type(2*watt)
<type 'sage.symbolic.expression.Expression'>

See those pages if you want to deal with such external products :

10 | No.10 Revision |

You can define a class MYExp, that inherits from symboic expressions (so that you keep all other operations), but owerwrites substraction and addition to avoid cancellations:

```
class MYExp(sage.symbolic.expression.Expression):
def __sub__(self,other):
if self == other:
return self
else:
return MYExp(SR,
```~~super(MYSR, ~~super(MYExp, self).__sub__(other))
def __add__(self,other):
if self == -other:
return self
else:
return MYExp(SR,super(MYExp, self).__add__(other))
def __mul__(self,other):
return MYExp(SR, super(MYExp, self).__mul__(other))
def __div__(self,other):
return MYExp(SR, ~~super(MYSR, ~~super(MYExp, self).__div__(other))
U = lambda x: MYExp(SR,x)
watt = U(units.power.watt)
meter = U(units.length.meter)
newton = U(units.force.newton)

Then, you can do something like:

```
sage: (watt-watt)*(newton+meter) - (watt-watt)*(newton+meter)
(meter + newton)*watt
sage: (watt+(-watt))
watt
```

```
sage: sqrt(watt) - sqrt(watt)
0
sage: type(sqrt(watt))
<type 'sage.symbolic.expression.Expression'>
```

Also, some "external" operations use coercion, that should be defined as well:

```
sage: 2*watt-2*watt
0
sage: watt*2-watt*2
2*watt
sage: type(watt*2)
<class 'sage.all_cmdline.MYExp'>
sage: type(2*watt)
<type 'sage.symbolic.expression.Expression'>
```

For this, you will have to define a class `MYSR`

at the same level as `SR`

(`Symbolic Ring`

), and define the `parent()`

method of an element of `MYExp`

to be `MYSR`

, and define the coercion in `MYSR`

. See those pages if you want to deal with such external products :

11 | No.11 Revision |

You can define a class MYExp, that inherits from symboic expressions (so that you keep all other operations), but owerwrites substraction and addition to avoid cancellations:

```
class MYExp(sage.symbolic.expression.Expression):
def __sub__(self,other):
return self.__add__(other)
def __add__(self,other):
if self == other:
return self
else:
return
```~~MYExp(SR, super(MYExp, self).__sub__(other))
def __add__(self,other):
if self == -other:
return self
else:
return ~~MYExp(SR,super(MYExp, self).__add__(other))
def __mul__(self,other):
return MYExp(SR, super(MYExp, self).__mul__(other))
def __div__(self,other):
return MYExp(SR, super(MYExp, self).__div__(other))
~~ ~~U = lambda x: MYExp(SR,x)
watt = U(units.power.watt)
meter = U(units.length.meter)
newton = U(units.force.newton)

Then, you can do something like:

```
sage: watt-watt
watt
sage: watt+watt
watt
sage: (watt-watt)*(newton+meter) - (watt-watt)*(newton+meter)
(meter + newton)*watt
```~~sage: (watt+(-watt))
watt
~~

```
sage: sqrt(watt) - sqrt(watt)
0
sage: type(sqrt(watt))
<type 'sage.symbolic.expression.Expression'>
```

Also, some "external" operations use coercion, that should be defined as well:

```
sage: 2*watt-2*watt
0
sage: watt*2-watt*2
2*watt
sage: type(watt*2)
<class 'sage.all_cmdline.MYExp'>
sage: type(2*watt)
<type 'sage.symbolic.expression.Expression'>
```

For this, you will have to define a class `MYSR`

at the same level as `SR`

(`Symbolic Ring`

), and define the `parent()`

method of an element of `MYExp`

to be `MYSR`

, and define the coercion in `MYSR`

. See those pages if you want to deal with such external products :

12 | No.12 Revision |

You can define a class MYExp, that inherits from symboic expressions (so that you keep all other operations), but owerwrites substraction and addition to avoid cancellations:

```
class MYExp(sage.symbolic.expression.Expression):
def __sub__(self,other):
return self.__add__(other)
def __add__(self,other):
if self == other:
return self
else:
return MYExp(SR,super(MYExp, self).__add__(other))
def __mul__(self,other):
return MYExp(SR, super(MYExp, self).__mul__(other))
def __div__(self,other):
return MYExp(SR, super(MYExp, self).__div__(other))
U = lambda x: MYExp(SR,x)
watt = U(units.power.watt)
meter = U(units.length.meter)
newton = U(units.force.newton)
```

Then, you can do something like:

```
sage: watt-watt
watt
sage: watt+watt
watt
sage: (watt-watt)*(newton+meter) - (watt-watt)*(newton+meter)
(meter + newton)*watt
```

```
sage: sqrt(watt) - sqrt(watt)
0
sage: type(sqrt(watt))
<type 'sage.symbolic.expression.Expression'>
sage: watt^2 - watt^2 # __pow__ method is called, not product
0
sage: watt*watt - watt*watt
watt^2
```

Also, some "external" operations use coercion, that should be defined as well:

```
sage: 2*watt-2*watt
0
sage: watt*2-watt*2
2*watt
sage: type(watt*2)
<class 'sage.all_cmdline.MYExp'>
sage: type(2*watt)
<type 'sage.symbolic.expression.Expression'>
```

For this, you will have to define a class `MYSR`

at the same level as `SR`

(`Symbolic Ring`

), and define the `parent()`

method of an element of `MYExp`

to be `MYSR`

, and define the coercion in `MYSR`

. See those pages if you want to deal with such external products :

13 | No.13 Revision |

```
class MYExp(sage.symbolic.expression.Expression):
def __sub__(self,other):
return self.__add__(other)
def __add__(self,other):
if self == other:
return self
else:
return MYExp(SR,super(MYExp, self).__add__(other))
def __mul__(self,other):
return MYExp(SR, super(MYExp, self).__mul__(other))
def __div__(self,other):
return MYExp(SR, super(MYExp, self).__div__(other))
U = lambda x: MYExp(SR,x)
watt = U(units.power.watt)
meter = U(units.length.meter)
newton = U(units.force.newton)
second = U(units.time.second)
joule = U(units.energy.joule)
```

Then, you can do something like:

```
sage: watt-watt
watt
sage: watt+watt
watt
sage: (watt-watt)*(newton+meter) - (watt-watt)*(newton+meter)
(meter + newton)*watt
```

```
sage: sqrt(watt) - sqrt(watt)
0
sage: type(sqrt(watt))
<type 'sage.symbolic.expression.Expression'>
sage: watt^2 - watt^2 # __pow__ method is called, not product
0
sage: watt*watt - watt*watt
watt^2
```

Also, some "external" operations use coercion, that should be defined as well:

```
sage: 2*watt-2*watt
0
sage: watt*2-watt*2
2*watt
sage: type(watt*2)
<class 'sage.all_cmdline.MYExp'>
sage: type(2*watt)
<type 'sage.symbolic.expression.Expression'>
```

`MYSR`

at the same level as `SR`

(`Symbolic Ring`

), and define the `parent()`

method of an element of `MYExp`

to be `MYSR`

, and define the coercion in `MYSR`

. See those pages if you want to deal with such external products :

14 | No.14 Revision |

You can define a class MYExp, that inherits from ~~symboic ~~symbolic expressions (so that you keep all other operations), but owerwrites substraction and addition to avoid cancellations:

```
class MYExp(sage.symbolic.expression.Expression):
def __sub__(self,other):
return self.__add__(other)
def __add__(self,other):
if self == other:
return self
else:
return MYExp(SR,super(MYExp, self).__add__(other))
def __mul__(self,other):
return MYExp(SR, super(MYExp, self).__mul__(other))
def __div__(self,other):
return MYExp(SR, super(MYExp, self).__div__(other))
U = lambda x: MYExp(SR,x)
watt = U(units.power.watt)
meter = U(units.length.meter)
newton = U(units.force.newton)
second = U(units.time.second)
joule = U(units.energy.joule)
```

Then, you can do something like:

```
sage: watt-watt
watt
sage: watt+watt
watt
sage: (watt-watt)*(newton+meter) - (watt-watt)*(newton+meter)
(meter + newton)*watt
```

```
sage: sqrt(watt) - sqrt(watt)
0
sage: type(sqrt(watt))
<type 'sage.symbolic.expression.Expression'>
sage: watt^2 - watt^2 # __pow__ method is called, not product
0
sage: watt*watt - watt*watt
watt^2
```

Also, some "external" operations use coercion, that should be defined as well:

```
sage: 2*watt-2*watt
0
sage: watt*2-watt*2
2*watt
sage: type(watt*2)
<class 'sage.all_cmdline.MYExp'>
sage: type(2*watt)
<type 'sage.symbolic.expression.Expression'>
```

`MYSR`

at the same level as `SR`

(`Symbolic Ring`

), and define the `parent()`

method of an element of `MYExp`

to be `MYSR`

, and define the coercion in `MYSR`

. See those pages if you want to deal with such external products :

15 | No.15 Revision |

You can define a class MYExp, that inherits from symbolic expressions (so that you keep all other operations), but owerwrites substraction and addition to avoid cancellations:

```
class MYExp(sage.symbolic.expression.Expression):
def __sub__(self,other):
return self.__add__(other)
def __add__(self,other):
if self == other:
return self
else:
return MYExp(SR,super(MYExp, self).__add__(other))
def __mul__(self,other):
return MYExp(SR, super(MYExp, self).__mul__(other))
def __div__(self,other):
return MYExp(SR, super(MYExp, self).__div__(other))
U = lambda x: MYExp(SR,x)
watt = U(units.power.watt)
meter = U(units.length.meter)
newton = U(units.force.newton)
second = U(units.time.second)
joule = U(units.energy.joule)
```

Then, you can do something like:

```
sage: watt-watt
watt
sage: watt+watt
watt
sage: (watt-watt)*(newton+meter) - (watt-watt)*(newton+meter)
(meter + newton)*watt
```

```
sage: sqrt(watt) - sqrt(watt)
0
sage: type(sqrt(watt))
<type 'sage.symbolic.expression.Expression'>
sage: watt^2 - watt^2 # __pow__ method is called, not product
0
sage: watt*watt - watt*watt
watt^2
```

Also, some "external" operations use coercion, that should be defined as well:

```
sage: 2*watt-2*watt
0
sage: watt*2-watt*2
2*watt
sage: type(watt*2)
<class 'sage.all_cmdline.MYExp'>
sage: type(2*watt)
<type 'sage.symbolic.expression.Expression'>
```

~~For this, ~~If you want also to deal with such external products, you will have to define a class `MYSR`

at the same level as `SR`

(`Symbolic Ring`

), and define the `parent()`

method of an element of `MYExp`

to be `MYSR`

, and define the coercion in `MYSR`

. See those pages if you want to deal with such external products :

16 | No.16 Revision |

You can define a class MYExp, that inherits from symbolic expressions (so that you keep all other operations), but owerwrites substraction and addition to avoid cancellations:

```
class MYExp(sage.symbolic.expression.Expression):
def __sub__(self,other):
return self.__add__(other)
def __add__(self,other):
if self == other:
return self
else:
return MYExp(SR,super(MYExp, self).__add__(other))
def __mul__(self,other):
return MYExp(SR, super(MYExp, self).__mul__(other))
def __div__(self,other):
return MYExp(SR, super(MYExp, self).__div__(other))
U = lambda x: MYExp(SR,x)
watt = U(units.power.watt)
meter = U(units.length.meter)
newton = U(units.force.newton)
second = U(units.time.second)
joule = U(units.energy.joule)
```

Then, you can do something like:

```
sage: watt-watt
watt
sage: watt+watt
watt
sage: (watt-watt)*(newton+meter) - (watt-watt)*(newton+meter)
(meter + newton)*watt
```

```
sage: sqrt(watt) - sqrt(watt)
0
sage: type(sqrt(watt))
<type 'sage.symbolic.expression.Expression'>
sage: watt^2 - watt^2 # __pow__ method is called, not
```~~product
~~the product by itsef
0
sage: watt*watt - watt*watt
watt^2

Also, some "external" operations use coercion, that should be defined as well:

```
sage: 2*watt-2*watt
0
sage: watt*2-watt*2
2*watt
sage: type(watt*2)
<class 'sage.all_cmdline.MYExp'>
sage: type(2*watt)
<type 'sage.symbolic.expression.Expression'>
```

If you want also to deal with such external products, you will have to define a class `MYSR`

at the same level as `SR`

(`Symbolic Ring`

), and define the

method of an element of ~~parent()~~.parent()`MYExp`

to be `MYSR`

, and define the coercion in `MYSR`

. See those pages if you want to deal with such external products :

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.