# Revision history [back]

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)
if self == -other:
return self
else:

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


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)
if self == -other:
return self
else:

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


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))
if self == -other:
return self
else:
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


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))
if self == -other:
return self
else:
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


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))
if self == -other:
return self
else:
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'>


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))
if self == -other:
return self
else:
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'>


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))
if self == -other:
return self
else:
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 :

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))
if self == -other:
return self
else:
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 :

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))
if self == -other:
return self
else:
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 :

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))
if self == -other:
return self
else:
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


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

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(MYExp, self).__sub__(other))
if self == -other:
return self
else:
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


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

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:
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


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'>
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 :

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:
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


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'>
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 :

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):
if self == other:
return self
else:
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


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'>
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 :

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):
if self == other:
return self
else:
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


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'>
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 :

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):
if self == other:
return self
else:
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


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'>
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 parent().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 :