p3he/gyro.py

130 lines
2.9 KiB
Python
Raw Normal View History

2024-04-04 18:03:47 +00:00
from numba import jit
2024-04-04 16:05:20 +00:00
from cmath import sqrt
from math import tanh, atanh, pi
2024-04-04 16:05:20 +00:00
from lines import cLineIntersection
def deg2rad(rA): return rA/180*pi
def rad2degd(rA): return rA*180/pi
@jit
def cap(rN):
if rN < 0:
return 0
return rN
2024-04-04 18:03:47 +00:00
@jit
2024-04-03 11:20:42 +00:00
def ZeroCheck(cN):
2024-04-04 18:03:47 +00:00
if cN == complex(0,0):
return complex(1,0)
2024-04-03 11:20:42 +00:00
else:
return cN
2024-04-04 18:03:47 +00:00
@jit
2024-04-03 11:20:42 +00:00
def c_tr(cN):
return cN.real, cN.imag
2024-04-04 18:03:47 +00:00
@jit
2024-04-03 11:20:42 +00:00
def cNorm(cN):
return ZeroCheck(cN)/abs(ZeroCheck(cN))
2024-04-04 18:03:47 +00:00
@jit
2024-04-03 11:20:42 +00:00
def cDot(cA, cB):
return (cA * cB.conjugate()).real
2024-04-04 18:03:47 +00:00
@jit
2024-04-03 11:20:42 +00:00
def cDist(cA, cB):
return abs(cA-cB)
2024-04-04 18:03:47 +00:00
@jit
2024-04-04 16:05:20 +00:00
def MobiusInt(cA,cB,cC,cD): # Bruh
return Klein2Poincare(cLineIntersection(Poincare2Klein(cA),Poincare2Klein(cB),Poincare2Klein(cC),Poincare2Klein(cD)))
2024-04-04 18:03:47 +00:00
@jit
2024-04-03 11:20:42 +00:00
def MobiusAdd(cA, cB):
return (cA + cB) / (1 + cA.conjugate() * cB)
2024-04-04 18:03:47 +00:00
@jit
2024-04-03 11:20:42 +00:00
def MobiusGyr(cA, cB):
return (1 + cA * cB.conjugate()) / (1 + cA.conjugate() * cB)
2024-04-04 18:03:47 +00:00
@jit
2024-04-03 11:20:42 +00:00
def MobiusAddGyr(cA, cB):
return MobiusAdd(cA, cB), MobiusGyr(cA, cB)
2024-04-04 18:03:47 +00:00
@jit
2024-04-04 16:05:20 +00:00
def MobiusScalar(cA, rT):
m = abs(cA)
return tanh(rT * atanh(m)) * cA / m
2024-04-04 18:03:47 +00:00
@jit
2024-04-04 16:05:20 +00:00
def MobiusCoadd(cA,cB):
return MobiusAdd(cA, (MobiusGyr(cA,-cB) * cB))
2024-04-04 18:03:47 +00:00
@jit
2024-04-04 16:05:20 +00:00
def MobiusCosub(cA,cB):
return MobiusAdd(cA, -(MobiusGyr(cA,cB) * cB))
2024-04-04 18:03:47 +00:00
@jit
2024-04-04 16:05:20 +00:00
def MobiusLine(cA,cB,rT):
return MobiusAdd(
cA,
MobiusScalar(
MobiusAdd(-cA,cB),
rT))
2024-04-04 18:03:47 +00:00
@jit
2024-04-04 16:05:20 +00:00
def MobiusDist(cA,cB):
return abs(MobiusAdd(-cB,cA))
2024-04-04 18:03:47 +00:00
@jit
2024-04-03 11:20:42 +00:00
def Poincare2Klein(cN):
return 2*cN / (1 + cDot(cN,cN))
2024-04-04 18:03:47 +00:00
@jit
2024-04-04 16:05:20 +00:00
def Klein2Poincare(cN):
return (1-sqrt(1-cDot(cN,cN)))*cN/cDot(cN,cN)
2024-04-03 11:20:42 +00:00
class GyroVector:
def __init__(self, cPos, cRot):
2024-04-04 18:03:47 +00:00
self.cPos = complex(cPos)
self.cRot = complex(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)
2024-04-04 16:05:20 +00:00
def dist(self,gA):
return abs((-self+gA).cPos)