Cleaned up groups modules

This commit is contained in:
Alberto Ramos 2021-10-03 18:22:32 +02:00
parent a27ac98554
commit 3f71a5222f
10 changed files with 149 additions and 32 deletions

View file

@ -11,7 +11,7 @@
SU2alg(x::T) where T <: AbstractFloat = SU2alg{T}(x,0.0,0.0)
SU2alg(v::Vector{T}) where T <: AbstractFloat = SU2alg{T}(v[1],v[2],v[3])
projalg(g::SU2{T}) where T <: AbstractFloat = SU2alg{T}(imag(g.t1), real(g.t2), imag(g.t2))
projalg(g::SU2{T}) where T <: AbstractFloat = SU2alg{T}(imag(g.t2), real(g.t2), imag(g.t1))
dot(a::SU2alg{T}, b::SU2alg{T}) where T <: AbstractFloat = a.t1*b.t1 + a.t2*b.t2 + a.t3*b.t3
norm(a::SU2alg{T}) where T <: AbstractFloat = sqrt(a.t1^2 + a.t2^2 + a.t3^2)
norm2(a::SU2alg{T}) where T <: AbstractFloat = a.t1^2 + a.t2^2 + a.t3^2
@ -25,6 +25,22 @@ Base.:*(a::SU2alg{T},b::Number) where T <: AbstractFloat = SU2alg{T}(a.t1*b,a
Base.:*(b::Number,a::SU2alg{T}) where T <: AbstractFloat = SU2alg{T}(a.t1*b,a.t2*b,a.t3*b)
Base.:/(a::SU2alg{T},b::Number) where T <: AbstractFloat = SU2alg{T}(a.t1/b,a.t2/b,a.t3/b)
function alg2mat(a::SU2alg{T}) where T <: AbstractFloat
u11::Complex{T} = complex(0.0, a.t3)/2
u22::Complex{T} = conj(u11)
u12::Complex{T} = complex(a.t2,a.t1)/2
u21::Complex{T} = -conj(u12)
return M2x2{T}(u11,u12,u21,u22)
end
Base.:*(a::SU2alg,b::SU2) = alg2mat(a)*b
Base.:*(a::SU2,b::SU2alg) = a*alg2mat(b)
Base.:/(a::SU2alg,b::SU2) = alg2mat(a)/b
Base.:\(a::SU2,b::SU2alg) = a\alg2mat(b)
"""
function Base.exp(a::T, t::Number=1) where {T <: Algebra}
@ -42,8 +58,8 @@ function Base.exp(a::SU2alg{T}) where T <: AbstractFloat
sa = CUDA.sin(rm)/(2.0*rm)
end
t1 = complex(ca,sa*a.t1)
t2 = complex(sa*a.t2,sa*a.t3)
t1 = complex(ca,sa*a.t3)
t2 = complex(sa*a.t2,sa*a.t1)
return SU2{T}(t1,t2)
end
@ -59,8 +75,8 @@ function Base.exp(a::SU2alg{T}, t::T) where T <: AbstractFloat
sa = t*CUDA.sin(rm)/(2.0*rm)
end
t1 = complex(ca,sa*a.t1)
t2 = complex(sa*a.t2,sa*a.t3)
t1 = complex(ca,sa*a.t3)
t2 = complex(sa*a.t2,sa*a.t1)
return SU2{T}(t1,t2)
end
@ -83,8 +99,8 @@ function expm(g::SU2{T}, a::SU2alg{T}) where T <: AbstractFloat
sa = CUDA.sin(rm)/(2.0*rm)
end
t1 = complex(ca,sa*a.t1)*g.t1-complex(sa*a.t2,sa*a.t3)*conj(g.t2)
t2 = complex(ca,sa*a.t1)*g.t2+complex(sa*a.t2,sa*a.t3)*conj(g.t1)
t1 = complex(ca,sa*a.t3)*g.t1-complex(sa*a.t2,sa*a.t1)*conj(g.t2)
t2 = complex(ca,sa*a.t3)*g.t2+complex(sa*a.t2,sa*a.t1)*conj(g.t1)
return SU2{T}(t1,t2)
end
@ -106,8 +122,8 @@ function expm(g::SU2{T}, a::SU2alg{T}, t::T) where T <: AbstractFloat
sa = t*CUDA.sin(rm)/(2.0*rm)
end
t1 = complex(ca,sa*a.t1)*g.t1-complex(sa*a.t2,sa*a.t3)*conj(g.t2)
t2 = complex(ca,sa*a.t1)*g.t2+complex(sa*a.t2,sa*a.t3)*conj(g.t1)
t1 = complex(ca,sa*a.t3)*g.t1-complex(sa*a.t2,sa*a.t1)*conj(g.t2)
t2 = complex(ca,sa*a.t3)*g.t2+complex(sa*a.t2,sa*a.t1)*conj(g.t1)
return SU2{T}(t1,t2)
end

View file

@ -24,21 +24,6 @@ function projalg(a::SU3{T}) where T <: AbstractFloat
sr3ov2*(ditr))
end
function projalg(a::M3x3{T}) where T <: AbstractFloat
sr3ov2::T = 0.866025403784438646763723170752
ditr = ( imag(a.u11) + imag(a.u22) + 2.0*imag(a.u33) )/3.0
m12 = (a.u12 - conj(a.u21))/2.0
m13 = (a.u13 - conj(a.u31))/2.0
m23 = (a.u23 - conj(a.u32))/2.0
return SU3alg{T}(imag( m12 ), imag( m13 ), imag( m23 ),
real( m12 ), real( m13 ), real( m23 ),
(imag(a.u11)-imag(a.u22))/2.0,
sr3ov2*(ditr))
end
dot(a::SU3alg{T},b::SU3alg{T}) where T <: AbstractFloat = a.t1*b.t1 + a.t2*b.t2 + a.t3*b.t3 + a.t4*b.t4 + a.t5*b.t5 + a.t6*b.t6 + a.t7*b.t7 + a.t8*b.t8
norm2(a::SU3alg{T}) where T <: AbstractFloat = a.t1^2 + a.t2^2 + a.t3^2 + a.t4^2 + a.t5^2 + a.t6^2 + a.t7^2 + a.t8^2
norm(a::SU3alg{T}) where T <: AbstractFloat = sqrt(a.t1^2 + a.t2^2 + a.t3^2 + a.t4^2 + a.t5^2 + a.t6^2 + a.t7^2 + a.t8^2)

View file

@ -25,9 +25,10 @@ export Group, Algebra
# SU(2) and 2x2 matrix operations
##
include("SU2Types.jl")
export SU2, SU2alg
export SU2, SU2alg, M2x2
include("GroupSU2.jl")
include("M2x2.jl")
include("AlgebraSU2.jl")
## END SU(2)
@ -46,7 +47,7 @@ include("GroupU1.jl")
export U1, U1alg
export dot, expm, exp, dag, normalize, inverse, tr, projalg, norm, norm2, isgroup
export dot, expm, exp, dag, normalize, inverse, tr, projalg, norm, norm2, isgroup, alg2mat
end # module

62
src/Groups/M2x2.jl Normal file
View file

@ -0,0 +1,62 @@
###
### "THE BEER-WARE LICENSE":
### Alberto Ramos wrote this file. As long as you retain this
### notice you can do whatever you want with this stuff. If we meet some
### day, and you think this stuff is worth it, you can buy me a beer in
### return. <alberto.ramos@cern.ch>
###
### file: M3x3.jl
### created: Sun Oct 3 09:03:34 2021
###
Base.:*(a::M2x2{T},b::M2x2{T}) where T <: AbstractFloat = M2x2{T}(a.u11*b.u11 + a.u12*b.u21,
a.u11*b.u12 + a.u12*b.u22,
a.u21*b.u11 + a.u22*b.u21,
a.u21*b.u12 + a.u22*b.u22)
Base.:*(a::SU2{T},b::M2x2{T}) where T <: AbstractFloat = M2x2{T}(a.t1*b.u11+a.t2*b.u21,
a.t1*b.u12+a.t2*b.u22,
-conj(a.t2)*b.u11+conj(a.t1)*b.u21,
-conj(a.t2)*b.u12+conj(a.t1)*b.u22)
Base.:*(a::M2x2{T},b::SU2{T}) where T <: AbstractFloat = M2x2{T}(a.u11*b.t1-a.u12*conj(b.t2),
a.u11*b.t2+a.u12*conj(b.t1),
a.u21*b.t1-a.u22*conj(b.t2),
-a.u21*b.t2+a.u22*conj(b.t1))
Base.:/(a::M2x2{T},b::SU2{T}) where T <: AbstractFloat = M2x2{T}(a.u11*conj(b.t1)-a.u12*conj(b.t2),
a.u11*b.t2+a.u12*b.t1,
a.u21*conj(b.t1)-a.u22*conj(b.t2),
-a.u21*b.t2+a.u22*b.t1)
Base.:\(a::SU2{T},b::M2x2{T}) where T <: AbstractFloat = M2x2{T}(conj(a.t1)*b.u11+a.t2*b.u21,
conj(a.t1)*b.u12+a.t2*b.u22,
-conj(a.t2)*b.u11+a.t1*b.u21,
-conj(a.t2)*b.u12+a.t1*b.u22)
Base.:*(a::Number,b::M2x2{T}) where T <: AbstractFloat = M2x2{T}(a*b.u11, a*b.u12,
a*b.u21, a*b.u22)
Base.:*(b::M2x2{T},a::Number) where T <: AbstractFloat = M2x2{T}(a*b.u11, a*b.u12,
a*b.u21, a*b.u22)
Base.:+(a::M2x2{T},b::M2x2{T}) where T <: AbstractFloat = M2x2{T}(a.u11+b.u11, a.u12+b.u12,
a.u21+b.u21, a.u22+b.u22)
Base.:-(a::M2x2{T},b::M2x2{T}) where T <: AbstractFloat = M2x2{T}(a.u11-b.u11, a.u12-b.u12,
a.u21-b.u21, a.u22-b.u22)
Base.:-(b::M2x2{T}) where T <: AbstractFloat = M2x2{T}(-b.u11, -b.u12,
-b.u21, -b.u22)
Base.:+(b::M2x2{T}) where T <: AbstractFloat = M2x2{T}(b.u11, b.u12,
b.u21, b.u22)
function projalg(a::M2x2{T}) where T <: AbstractFloat
m12 = (a.u12 - conj(a.u21))/2
return SU2alg{T}(imag( m12 ), real( m12 ), (imag(a.u11) - imag(a.u22))/2)
end

View file

@ -113,3 +113,18 @@ Base.:-(b::M3x3{T}) where T <: AbstractFloat = M3x3{T}(-b.u11, -b.u12
Base.:+(b::M3x3{T}) where T <: AbstractFloat = M3x3{T}(b.u11, b.u12, bu13,
b.u21, b.u22, bu23,
b.u31, b.u32, bu33)
function projalg(a::M3x3{T}) where T <: AbstractFloat
sr3ov2::T = 0.866025403784438646763723170752
ditr = ( imag(a.u11) + imag(a.u22) + 2.0*imag(a.u33) )/3.0
m12 = (a.u12 - conj(a.u21))/2.0
m13 = (a.u13 - conj(a.u31))/2.0
m23 = (a.u23 - conj(a.u32))/2.0
return SU3alg{T}(imag( m12 ), imag( m13 ), imag( m23 ),
real( m12 ), real( m13 ), real( m23 ),
(imag(a.u11)-imag(a.u22))/2.0,
sr3ov2*(ditr))
end

View file

@ -14,6 +14,13 @@ struct SU2{T} <: Group
t2::Complex{T}
end
struct M2x2{T}
u11::Complex{T}
u12::Complex{T}
u21::Complex{T}
u22::Complex{T}
end
struct SU2alg{T} <: Algebra
t1::T
t2::T
@ -21,6 +28,9 @@ struct SU2alg{T} <: Algebra
end
Base.zero(::Type{SU2alg{T}}) where T <: AbstractFloat = SU2alg{T}(zero(T),zero(T),zero(T))
Base.zero(::Type{M2x2{T}}) where T <: AbstractFloat = M2x2{T}(zero(T),zero(T),zero(T),zero(T))
Base.one(::Type{SU2{T}}) where T <: AbstractFloat = SU2{T}(one(T),zero(T))
Base.one(::Type{M2x2{T}}) where T <: AbstractFloat = M2x2{T}(one(T),zero(T),zero(T),one(T))
Random.rand(rng::AbstractRNG, ::Random.SamplerType{SU2alg{T}}) where T <: AbstractFloat = SU2alg{T}(randn(rng,T),randn(rng,T),randn(rng,T))
Base.one(::Type{SU2{T}}) where T <: AbstractFloat = SU2{T}(one(T),zero(T))
Random.rand(rng::AbstractRNG, ::Random.SamplerType{SU2{T}}) where T <: AbstractFloat = exp(SU2alg{T}(randn(rng,T),randn(rng,T),randn(rng,T)))
Random.rand(rng::AbstractRNG, ::Random.SamplerType{SU2{T}}) where T <: AbstractFloat = exp(SU2alg{T}(randn(rng,T),randn(rng,T),randn(rng,T)))

View file

@ -39,6 +39,8 @@ struct M3x3{T}
u32::Complex{T}
u33::Complex{T}
end
Base.one(::Type{M3x3{T}}) where T <: AbstractFloat = M3x3{T}(one(T),zero(T),zero(T),zero(T),one(T),zero(T),zero(T),zero(T),one(T))
Base.zero(::Type{M3x3{T}}) where T <: AbstractFloat = M3x3{T}(zero(T),zero(T),zero(T),zero(T),zero(T),zero(T),zero(T),zero(T),zero(T))
struct SU3alg{T} <: Algebra
t1::T

View file

@ -16,8 +16,8 @@ include("Groups/Groups.jl")
using .Groups
export Group, Algebra
export SU2, SU2alg, SU3, SU3alg, M3x3, U1, U1alg
export dot, expm, exp, dag, normalize, inverse, tr, projalg, norm, norm2, isgroup
export SU2, SU2alg, SU3, SU3alg, M3x3, M2x2, U1, U1alg
export dot, expm, exp, dag, normalize, inverse, tr, projalg, norm, norm2, isgroup, alg2mat
include("Space/Space.jl")

View file

@ -6,7 +6,7 @@ Pkg.activate("/home/alberto/code/julia/LatticeGPU")
using LatticeGPU
T = Float32
T = Float64
b = rand(SU2{T})
println(b)
@ -23,9 +23,30 @@ println("Inverse B: ", c)
d = b*c
println("Test: ", d)
Ma = Array{SU2{T}}(undef, 100)
c = exp(ba, -1.0)
println("Inverse B: ", c)
d = b*c
println("Test: ", d)
Ma = Array{SU2{T}}(undef, 2)
rand!(Ma)
println(Ma)
fill!(Ma, one(eltype(Ma)))
println(Ma)
println("## Aqui test M2x2")
ba = rand(SU2alg{T})
ga = exp(ba)
println("Matrix: ", alg2mat(ba))
println("Exp: ", ga)
mo = one(M2x2{T})
println(mo)
mp = mo*ga
println(mp)
println(projalg(mp))
println(projalg(ga))

View file

@ -129,3 +129,8 @@ b = g2*a
println("b is one: ", b)
println("## Aqui test M3x3")
ba = rand(SU3alg{T})
ga = exp(ba)
println("Matrix: ", alg2mat(ba))
println("Exp: ", ga)