Skip to content

Commit

Permalink
add spin tensor
Browse files Browse the repository at this point in the history
  • Loading branch information
jcosborn committed Dec 14, 2024
1 parent abc82b8 commit 74b6861
Show file tree
Hide file tree
Showing 5 changed files with 339 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/maths/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ template declaredVector*(x:ToSingle):untyped = isVector(x[])
template declaredMatrix*(x:ToSingle):untyped = isMatrix(x[])
template re*(x: ToSingle): untyped = toSingle(x[].re)
template im*(x: ToSingle): untyped = toSingle(x[].im)
template simdType*(x: ToSingle): untyped = simdType(x[])
#template simdType*(x: ToSingle): untyped = simdType(x[])
template numberType*(x: ToSingle): untyped = float32

type
Expand Down Expand Up @@ -328,7 +328,7 @@ template declaredVector*(x:ToDouble):untyped = isVector(x[])
template declaredMatrix*(x:ToDouble):untyped = isMatrix(x[])
template re*(x:ToDouble):untyped = toDouble(x[].re)
template im*(x:ToDouble):untyped = toDouble(x[].im)
template simdType*(x: ToDouble): untyped = simdType(x[])
#template simdType*(x: ToDouble): untyped = simdType(x[])
#macro dump2(x: typed): auto =
# result = newEmptyNode()
# echo x.treerepr
Expand Down Expand Up @@ -505,7 +505,7 @@ forwardFunc(Indexed, norm2)
template len*(x:Indexed):untyped = obj(x).len # FIXME Simd types can use Indexed
template nrows*(x:Indexed):untyped = obj(x).nrows
template ncols*(x:Indexed):untyped = obj(x).ncols
template simdType*(x: Indexed): untyped = simdType(obj(x))
#template simdType*(x: Indexed): untyped = simdType(obj(x))
template numberType*(x: Indexed): untyped = numberType(obj(x))
#template declaredVector*(x:Indexed):untyped = isVector(x.obj)
#template declaredMatrix*(x:Indexed):untyped = isMatrix(x.obj)
Expand Down
1 change: 1 addition & 0 deletions src/physics/colorTensor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type
Color3*[T] = Color[T]
Color4*[T] = Color[T]

proc has*(t: typedesc[ColorType], x: typedesc): bool = x is ColorType
#[
template getNc*[K,T](x: SomeTensor[K,T]): int =
when K is ColorType:
Expand Down
8 changes: 4 additions & 4 deletions src/physics/spin.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import spinOld
export spinOld
#import spinOld
#export spinOld

#import spinTensor
#export spinTensor
import spinTensor
export spinTensor
306 changes: 306 additions & 0 deletions src/physics/spinTensor.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
import tensorwrap
export tensorwrap
import maths/[complexType, matrixConcept]

type
SpinType* = object
Spin*[T] = TensorObj[SpinType,T]
Spin2*[T] = Spin[T]
Spin3*[T] = Spin[T]
Spin4*[T] = Spin[T]
template asSpin*[T](x: typedesc[T]): typedesc = Spin[T]
template asSpin*[T](x: T): auto = Spin[T](obj: x)

template spinVector*(x: static[int], a: untyped): untyped =
#const
# I:int = x
#type
# E = T
# VA = VectorArray[I,E]
# VAO = VectorArrayObj[I,E]
#Spin[VA](v: VA(v: a))
#Spin[VA](v: VA(v: VAO(vec: a)))
#asSpin(VA(v: a))
#static: echo "spinVector"
asSpin(asVectorArray(a))
#let t1 = asVectorArray(a)
#static: echo "spinVector1"
#let t = asSpin(t1)
#static: echo "spinVector2"
#t
template spinVector*[T](x: static[int], a: typedesc[T]): untyped =
asSpin(asVectorArray(x, type T))
template spinMatrix*[T](x,y:static[int], a: untyped): untyped =
const
I:int = x
J:int = y
type
E = T
#MA = MatrixArray[I,J,E]
MAO = MatrixArrayObj[I,J,E]
asSpin(asMatrix(MAO(mat: a)))
#template spinMatrix*[I,J:static[int],T](a: untyped): untyped =
# Spin[MatrixArray[I,J,T]](v: MatrixArray[I,J,T](v: MatrixArrayObj[I,J,T](mat: a)))
#template spinMatrix*(I,J,T,a: untyped): untyped =
# Spin[MatrixArray[I,J,T]](v: MatrixArray[I,J,T](v: MatrixArrayObj[I,J,T](mat: a)))

const z0 = newComplex( 0.0, 0.0)
const z1 = newComplex( 1.0, 0.0)
const zi = newComplex( 0.0, 1.0)
const n1 = newComplex(-1.0, 0.0)
const ni = newComplex( 0.0, -1.0)

template s(r,c,x: untyped): untyped =
spinMatrix[ComplexType[float]](r,c,x)
template g(x: untyped): untyped = s(4,4,x)
template p(x: untyped): untyped = s(2,4,x)
template r(x: untyped): untyped = s(4,2,x)

const
gamma0* = g([[ z1, z0, z0, z0 ],
[ z0, z1, z0, z0 ],
[ z0, z0, z1, z0 ],
[ z0, z0, z0, z1 ]])
gamma1* = g([[ z0, z0, z0, zi ],
[ z0, z0, zi, z0 ],
[ z0, ni, z0, z0 ],
[ ni, z0, z0, z0 ]])
gamma2* = g([[ z0, z0, z0, n1 ],
[ z0, z0, z1, z0 ],
[ z0, z1, z0, z0 ],
[ n1, z0, z0, z0 ]])
gamma3* = g([[ z0, z0, zi, z0 ],
[ z0, z0, z0, ni ],
[ ni, z0, z0, z0 ],
[ z0, zi, z0, z0 ]])
gamma4* = g([[ z0, z0, z1, z0 ],
[ z0, z0, z0, z1 ],
[ z1, z0, z0, z0 ],
[ z0, z1, z0, z0 ]])
gamma5* = g([[ z1, z0, z0, z0 ],
[ z0, z1, z0, z0 ],
[ z0, z0, n1, z0 ],
[ z0, z0, z0, n1 ]])

spprojmat1p* = p([[ z1, z0, z0, zi ],
[ z0, z1, zi, z0 ]])
spprojmat1m* = p([[ z1, z0, z0, ni ],
[ z0, z1, ni, z0 ]])
spprojmat2p* = p([[ z1, z0, z0, n1 ],
[ z0, z1, z1, z0 ]])
spprojmat2m* = p([[ z1, z0, z0, z1 ],
[ z0, z1, n1, z0 ]])
spprojmat3p* = p([[ z1, z0, zi, z0 ],
[ z0, z1, z0, ni ]])
spprojmat3m* = p([[ z1, z0, ni, z0 ],
[ z0, z1, z0, zi ]])
spprojmat4p* = p([[ z1, z0, z1, z0 ],
[ z0, z1, z0, z1 ]])
spprojmat4m* = p([[ z1, z0, n1, z0 ],
[ z0, z1, z0, n1 ]])

spreconmat1p* = r([[ z1, z0 ],
[ z0, z1 ],
[ z0, ni ],
[ ni, z0 ]])
spreconmat1m* = r([[ z1, z0 ],
[ z0, z1 ],
[ z0, zi ],
[ zi, z0 ]])
spreconmat2p* = r([[ z1, z0 ],
[ z0, z1 ],
[ z0, z1 ],
[ n1, z0 ]])
spreconmat2m* = r([[ z1, z0 ],
[ z0, z1 ],
[ z0, n1 ],
[ z1, z0 ]])
spreconmat3p* = r([[ z1, z0 ],
[ z0, z1 ],
[ ni, z0 ],
[ z0, zi ]])
spreconmat3m* = r([[ z1, z0 ],
[ z0, z1 ],
[ zi, z0 ],
[ z0, ni ]])
spreconmat4p* = r([[ z1, z0 ],
[ z0, z1 ],
[ z1, z0 ],
[ z0, z1 ]])
spreconmat4m* = r([[ z1, z0 ],
[ z0, z1 ],
[ n1, z0 ],
[ z0, n1 ]])

template I(x: typed): untyped =
newImag(1)*x
template mI(x: typed): untyped =
newImag(-1)*x

proc spproj1p*(r: var auto, x: auto) =
## r: HalfFermion
## x: DiracFermion
#let nc = x[0].len
#for i in 0..<nc:
# r[0][i] = x[0][i] + x[2][i]
# r[1][i] = x[1][i] + x[3][i]
r := spprojmat1p * x

#[
template spproj1p*(x: auto): untyped = spprojmat1p * x
template spproj2p*(x: auto): untyped = spprojmat2p * x
template spproj3p*(x: auto): untyped = spprojmat3p * x
template spproj4p*(x: auto): untyped = spprojmat4p * x
template spproj1m*(x: auto): untyped = spprojmat1m * x
template spproj2m*(x: auto): untyped = spprojmat2m * x
template spproj3m*(x: auto): untyped = spprojmat3m * x
template spproj4m*(x: auto): untyped = spprojmat4m * x
]#

#template spproj1pU*(x: Spin): untyped =
#let v0 = x[][0] + I(x[][3])
#let v1 = x[][1] + I(x[][2])
#spinVector[type(v0)](2,[v0,v1])
#spinVector[type(x[][0])](2,[x[][0]+I(x[][3]),x[][1]+I(x[][2])])
# spinVector(2,[x[][0]+I(x[][3]),x[][1]+I(x[][2])])
#template spproj1p*(x: Spin): untyped =
# flattenCallArgs(spproj1pU, x)
template spproj1p*(xx: Spin): untyped =
let xp = getPtr xx; template x:untyped {.gensym.} = xp[]
#let v = [ x[0]+I(x[3]), x[1]+I(x[2]) ]
#spinVector(2, v)
var v {.noInit.}: spinVector(2,evalType(x[0]))
#static: echo "spproj1p: ", v.type
#v[0] = x[0]+I(x[3])
#v[1] = x[1]+I(x[2])
let t0 = I(x[3])
add(v[0], x[0], t0)
let t1 = I(x[2])
add(v[1], x[1], t1)
v
template spproj2p*(xx: Spin): untyped =
let xp = getPtr xx; template x:untyped {.gensym.} = xp[]
let v = [ x[0]-x[3], x[1]+x[2] ]
spinVector(2, v)
template spproj3p*(xx: Spin): untyped =
let xp = getPtr xx; template x:untyped {.gensym.} = xp[]
let v = [ x[0]+I(x[2]), x[1]-I(x[3]) ]
spinVector(2, v)
template spproj4p*(xx: Spin): untyped =
let xp = getPtr xx; template x:untyped {.gensym.} = xp[]
let v = [ x[0]+x[2], x[1]+x[3] ]
spinVector(2, v)
template spproj1m*(xx: Spin): untyped =
let xp = getPtr xx; template x:untyped {.gensym.} = xp[]
let v = [ x[0]-I(x[3]), x[1]-I(x[2]) ]
spinVector(2, v)
template spproj2m*(xx: Spin): untyped =
let xp = getPtr xx; template x:untyped {.gensym.} = xp[]
let v = [ x[0]+x[3], x[1]-x[2] ]
spinVector(2, v)
template spproj3m*(xx: Spin): untyped =
let xp = getPtr xx; template x:untyped {.gensym.} = xp[]
let v = [ x[0]-I(x[2]), x[1]+I(x[3]) ]
spinVector(2, v)
template spproj4m*(xx: Spin): untyped =
let xp = getPtr xx; template x:untyped {.gensym.} = xp[]
let v = [ x[0]-x[2], x[1]-x[3] ]
spinVector(2, v)

proc sprecon1p*(r: var auto, x: auto) =
## r: DiracFermion
## x: HalfFermion
let nc = x[0].len
for i in 0..<nc:
r[0][i] = x[0][i]
r[1][i] = x[1][i]
r[2][i] = x[0][i]
r[3][i] = x[1][i]

#[
template sprecon1p*(x: Spin): untyped = spreconmat1p * x
template sprecon2p*(x: Spin): untyped = spreconmat2p * x
template sprecon3p*(x: Spin): untyped = spreconmat3p * x
template sprecon4p*(x: Spin): untyped = spreconmat4p * x
template sprecon1m*(x: Spin): untyped = spreconmat1m * x
template sprecon2m*(x: Spin): untyped = spreconmat2m * x
template sprecon3m*(x: Spin): untyped = spreconmat3m * x
template sprecon4m*(x: Spin): untyped = spreconmat4m * x
]#

template sprecon1p*(xx: Spin): untyped =
let xp = getPtr xx; template x:untyped {.gensym.} = xp[]
#let v = [ x[0], x[1], -I(x[1]), -I(x[0]) ]
#spinVector(4, v)
var v {.noInit.}: spinVector(4,evalType(x[0]))
#static: echo "sprecon1p: ", v.type
v[0] = x[0]
v[1] = x[1]
v[2] = mI(x[1])
v[3] = mI(x[0])
v
template sprecon2p*(xx: Spin): untyped =
#let x = xx[]
#let v = [ x[0], x[1], x[1], -x[0] ]
#spinVector(4, v)
let xp = getPtr xx; template x:untyped {.gensym.} = xp[]
var v {.noInit.}: spinVector(4,evalType(x[0]))
v[0] = x[0]
v[1] = x[1]
v[2] = x[1]
v[3] = -x[0]
v
template sprecon3p*(xx: Spin): untyped =
#let x = xx[]
#let v = [ x[0], x[1], -I(x[0]), I(x[1]) ]
#spinVector(4, v)
let xp = getPtr xx; template x:untyped {.gensym.} = xp[]
var v {.noInit.}: spinVector(4,evalType(x[0]))
v[0] = x[0]
v[1] = x[1]
v[2] = mI(x[0])
v[3] = I(x[1])
v
template sprecon4p*(xx: Spin): untyped =
#let x = xx[]
#let v = [ x[0], x[1], x[0], x[1] ]
#spinVector(4, v)
let xp = getPtr xx; template x:untyped {.gensym.} = xp[]
var v {.noInit.}: spinVector(4,evalType(x[0]))
v[0] = x[0]
v[1] = x[1]
v[2] = x[0]
v[3] = x[1]
v
template sprecon1m*(xx: Spin): untyped =
#let x = toRef xx[]
#let v = [ x[0], x[1], I(x[1]), I(x[0]) ]
#spinVector(4, v)
#type T = evalType(x[0])
let xp = getPtr xx; template x:untyped {.gensym.} = xp[]
var v {.noInit.}: spinVector(4,evalType(x[0]))
v[0] = x[0]
v[1] = x[1]
v[2] = I(x[1])
v[3] = I(x[0])
v
template sprecon2m*(xx: Spin): untyped =
#let x = xx[]
let xp = getPtr xx; template x:untyped {.gensym.} = xp[]
let v = [ x[0], x[1], -x[1], x[0] ]
spinVector(4, v)
template sprecon3m*(xx: Spin): untyped =
#let x = xx[]
let xp = getPtr xx; template x:untyped {.gensym.} = xp[]
let v = [ x[0], x[1], I(x[0]), -I(x[1]) ]
spinVector(4, v)
template sprecon4m*(xx: Spin): untyped =
#let x = xx[]
let xp = getPtr xx; template x:untyped {.gensym.} = xp[]
let v = [ x[0], x[1], -x[0], -x[1] ]
spinVector(4, v)

when isMainModule:
echo gamma0[0,0]
let g2 = gamma0 + gamma4
#echo g2[0,0]
Loading

0 comments on commit 74b6861

Please sign in to comment.