Skip to content

Instantly share code, notes, and snippets.

@dniku
Created December 10, 2016 12:01
Show Gist options
  • Save dniku/f1bdf74161e9ada9a6e38895b2001f24 to your computer and use it in GitHub Desktop.
Save dniku/f1bdf74161e9ada9a6e38895b2001f24 to your computer and use it in GitHub Desktop.
import sympy as sp
from sympy.core.expr import Expr
class TP(Expr):
__slots__ = ['is_commutative']
def __new__(cls, l, r):
l = sp.sympify(l)
r = sp.sympify(r)
obj = Expr.__new__(cls, l, r)
obj.is_commutative = False
return obj
def __neg__(self):
return self
def __mul__(self, other):
if isinstance(other, TP):
return TP(self.args[0] * other.args[0], other.args[1] * self.args[1])
elif other.is_number:
if other % 2 == 0:
return 0
else:
return self
else:
return sp.Mul(self, other)
x, y = sp.symbols('x, y', commutative=False)
Ym = TP(y, 1) - TP(1, y)
Yp = TP(y, 1) + TP(1, y)
Xm = TP(x, 1) - TP(1, x)
d1 = Ym * Yp + Xm * 0
print(d1)
print(sp.expand(d1))
d2 = Xm * Ym
print(d2)
print(sp.expand(d2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment