Use Numba to increase performance.

This commit is contained in:
bʰedoh₂ swé 2024-04-04 23:03:47 +05:00
parent 359edf4742
commit ac6330efd0
3 changed files with 30 additions and 16 deletions

30
gyro.py
View File

@ -1,52 +1,61 @@
try: from numba import jit
from numpy import complex256
except ImportError:
from numpy import complex128 as complex256
from cmath import sqrt from cmath import sqrt
from math import tanh, atanh from math import tanh, atanh
from lines import cLineIntersection from lines import cLineIntersection
@jit
def ZeroCheck(cN): def ZeroCheck(cN):
if cN == complex256(complex(0,0)): if cN == complex(0,0):
return complex256(complex(1,0)) return complex(1,0)
else: else:
return cN return cN
@jit
def c_tr(cN): def c_tr(cN):
return cN.real, cN.imag return cN.real, cN.imag
@jit
def cNorm(cN): def cNorm(cN):
return ZeroCheck(cN)/abs(ZeroCheck(cN)) return ZeroCheck(cN)/abs(ZeroCheck(cN))
@jit
def cDot(cA, cB): def cDot(cA, cB):
return (cA * cB.conjugate()).real return (cA * cB.conjugate()).real
@jit
def cDist(cA, cB): def cDist(cA, cB):
return abs(cA-cB) return abs(cA-cB)
@jit
def MobiusInt(cA,cB,cC,cD): # Bruh def MobiusInt(cA,cB,cC,cD): # Bruh
return Klein2Poincare(cLineIntersection(Poincare2Klein(cA),Poincare2Klein(cB),Poincare2Klein(cC),Poincare2Klein(cD))) return Klein2Poincare(cLineIntersection(Poincare2Klein(cA),Poincare2Klein(cB),Poincare2Klein(cC),Poincare2Klein(cD)))
@jit
def MobiusAdd(cA, cB): def MobiusAdd(cA, cB):
return (cA + cB) / (1 + cA.conjugate() * cB) return (cA + cB) / (1 + cA.conjugate() * cB)
@jit
def MobiusGyr(cA, cB): def MobiusGyr(cA, cB):
return (1 + cA * cB.conjugate()) / (1 + cA.conjugate() * cB) return (1 + cA * cB.conjugate()) / (1 + cA.conjugate() * cB)
@jit
def MobiusAddGyr(cA, cB): def MobiusAddGyr(cA, cB):
return MobiusAdd(cA, cB), MobiusGyr(cA, cB) return MobiusAdd(cA, cB), MobiusGyr(cA, cB)
@jit
def MobiusScalar(cA, rT): def MobiusScalar(cA, rT):
m = abs(cA) m = abs(cA)
return tanh(rT * atanh(m)) * cA / m return tanh(rT * atanh(m)) * cA / m
@jit
def MobiusCoadd(cA,cB): def MobiusCoadd(cA,cB):
return MobiusAdd(cA, (MobiusGyr(cA,-cB) * cB)) return MobiusAdd(cA, (MobiusGyr(cA,-cB) * cB))
@jit
def MobiusCosub(cA,cB): def MobiusCosub(cA,cB):
return MobiusAdd(cA, -(MobiusGyr(cA,cB) * cB)) return MobiusAdd(cA, -(MobiusGyr(cA,cB) * cB))
@jit
def MobiusLine(cA,cB,rT): def MobiusLine(cA,cB,rT):
return MobiusAdd( return MobiusAdd(
cA, cA,
@ -54,19 +63,22 @@ def MobiusLine(cA,cB,rT):
MobiusAdd(-cA,cB), MobiusAdd(-cA,cB),
rT)) rT))
@jit
def MobiusDist(cA,cB): def MobiusDist(cA,cB):
return abs(MobiusAdd(-cB,cA)) return abs(MobiusAdd(-cB,cA))
@jit
def Poincare2Klein(cN): def Poincare2Klein(cN):
return 2*cN / (1 + cDot(cN,cN)) return 2*cN / (1 + cDot(cN,cN))
@jit
def Klein2Poincare(cN): def Klein2Poincare(cN):
return (1-sqrt(1-cDot(cN,cN)))*cN/cDot(cN,cN) return (1-sqrt(1-cDot(cN,cN)))*cN/cDot(cN,cN)
class GyroVector: class GyroVector:
def __init__(self, cPos, cRot): def __init__(self, cPos, cRot):
self.cPos = complex256(cPos) self.cPos = complex(cPos)
self.cRot = complex256(cRot) self.cRot = complex(cRot)
self.normalize() self.normalize()
def __add__(gA, gB): def __add__(gA, gB):

View File

@ -1,14 +1,20 @@
from numba import jit
@jit
def LineIntersection(rX1,rY1,rX2,rY2,rX3,rY3,rX4,rY4): def LineIntersection(rX1,rY1,rX2,rY2,rX3,rY3,rX4,rY4):
rX = ( (rX1*rY2-rY1*rX2)*(rX3-rX4)-(rX1-rX2)*(rX3*rY4-rY3*rX4) ) / ( (rX1-rX2)*(rY3-rY4)-(rY1-rY2)*(rX3-rX4) ) rX = ( (rX1*rY2-rY1*rX2)*(rX3-rX4)-(rX1-rX2)*(rX3*rY4-rY3*rX4) ) / ( (rX1-rX2)*(rY3-rY4)-(rY1-rY2)*(rX3-rX4) )
rY = ( (rX1*rY2-rY1*rX2)*(rY3-rY4)-(rY1-rY2)*(rX3*rY4-rY3*rX4) ) / ( (rX1-rX2)*(rY3-rY4)-(rY1-rY2)*(rX3-rX4) ) rY = ( (rX1*rY2-rY1*rX2)*(rY3-rY4)-(rY1-rY2)*(rX3*rY4-rY3*rX4) ) / ( (rX1-rX2)*(rY3-rY4)-(rY1-rY2)*(rX3-rX4) )
return (rX, rY) return (rX, rY)
@jit
def cLineIntersection(cA0, cA1, cB0, cB1): def cLineIntersection(cA0, cA1, cB0, cB1):
rX, rY = LineIntersection(cA0.real, cA0.imag, cA1.real, cA1.imag, cB0.real, cB0.imag, cB1.real, cB1.imag) rX, rY = LineIntersection(cA0.real, cA0.imag, cA1.real, cA1.imag, cB0.real, cB0.imag, cB1.real, cB1.imag)
return complex(rX, rY) return complex(rX, rY)
@jit
def Between(rA,rB,rN): def Between(rA,rB,rN):
return (rA < rN and rN < rB) or (rB < rN and rN < rA) return (rA < rN and rN < rB) or (rB < rN and rN < rA)
@jit
def cBetween(cA,cB,cN): def cBetween(cA,cB,cN):
return Between(cA.real,cB.real,cN.real) and Between(cA.imag,cB.imag,cN.imag) return Between(cA.real,cB.real,cN.real) and Between(cA.imag,cB.imag,cN.imag)

10
main.py
View File

@ -1,9 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
try: from numba import jit
from numpy import complex256
except ImportError:
from numpy import complex128 as complex256
print("Warning! Cannot use full precision!")
from time import time_ns, sleep from time import time_ns, sleep
from cmath import exp, pi from cmath import exp, pi
@ -15,7 +11,7 @@ from lines import *
def deg2rad(rA): return rA/180*PI def deg2rad(rA): return rA/180*PI
I = complex256(complex(0,1)) I = complex(0,1)
WHITE = (255,255,255) WHITE = (255,255,255)
BLACK = (0,0,0) BLACK = (0,0,0)
OFFSET = 0.015625 OFFSET = 0.015625
@ -25,7 +21,7 @@ IROT = 1/ROT
F = 5 * 10**7 F = 5 * 10**7
F_ = 10**9 F_ = 10**9
gOrigin = GyroVector(complex256(0),1) gOrigin = GyroVector(0,1)
SKY = (127,127,255) SKY = (127,127,255)
GROUND = (102, 51, 0) GROUND = (102, 51, 0)