p3he/gyro.py

77 lines
2.0 KiB
Python
Raw Normal View History

2024-04-03 14:15:42 +00:00
try:
from numpy import complex256
except ImportError:
from numpy import complex128 as complex256
2024-04-03 11:20:42 +00:00
def ZeroCheck(cN):
2024-04-03 13:06:28 +00:00
if cN == complex256(complex(0,0)):
return complex256(complex(1,0))
2024-04-03 11:20:42 +00:00
else:
return cN
def c_tr(cN):
return cN.real, cN.imag
def cNorm(cN):
return ZeroCheck(cN)/abs(ZeroCheck(cN))
def cDot(cA, cB):
return (cA * cB.conjugate()).real
def cDist(cA, cB):
return abs(cA-cB)
def MobiusAdd(cA, cB):
return (cA + cB) / (1 + cA.conjugate() * cB)
def MobiusGyr(cA, cB):
return (1 + cA * cB.conjugate()) / (1 + cA.conjugate() * cB)
def MobiusAddGyr(cA, cB):
return MobiusAdd(cA, cB), MobiusGyr(cA, cB)
def Poincare2Klein(cN):
return 2*cN / (1 + cDot(cN,cN))
class GyroVector:
def __init__(self, cPos, cRot):
2024-04-03 13:06:28 +00:00
self.cPos = complex256(cPos)
self.cRot = complex256(cRot)
2024-04-03 11:20:42 +00:00
self.normalize()
def __add__(gA, gB):
2024-04-03 12:35:29 +00:00
#cAdd, cGyr = MobiusAddGyr(gA.cPos, gB.cPos / gA.cRot)
cAdd, cGyr = MobiusAddGyr(gA.cPos, gB.cPos)
2024-04-03 11:20:42 +00:00
return GyroVector(cAdd, gA.cRot * gB.cRot * cGyr)
def __neg__(self):
return GyroVector(-(self.cRot * self.cPos), 1/self.cRot)
def __sub__(gA, gB):
return gA + (-gB)
def copy(self):
return GyroVector(self.cPos,self.cRot)
def normalize(self):
if abs(self.cPos) > 1:
self.cPos = cNorm(self.cPos)
self.cRot = cNorm(self.cRot)
def rotate(self,cRot):
self.cRot *= cRot
def transform(self, cA):
2024-04-03 12:29:07 +00:00
#cAdd, cGyr = MobiusAddGyr(self.cPos, cA / ZeroCheck(self.cRot))
cAdd, cGyr = MobiusAddGyr(self.cPos, cA / self.cRot)
2024-04-03 11:20:42 +00:00
self.cPos = cAdd
self.cRot = self.cRot * cGyr
def transformed(self, cA):
#cAdd, cGyr = MobiusAddGyr(self.cPos, cA / ZeroCheck(self.cRot))
#return GyroVector(cAdd, self.cRot * cGyr)
return gA.copy().transform(cA)
def nrtransformed(self, cA):
2024-04-03 12:29:07 +00:00
return MobiusAdd(self.cPos, cA / self.cRot)