Skip to content

Commit

Permalink
depulicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-roehrich committed Sep 11, 2023
1 parent 0f1ac7f commit bd27188
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 62 deletions.
37 changes: 24 additions & 13 deletions experimental/LieAlgebras/src/AbstractRootSystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
###############################################################################

struct AbstractRootSystem{T<:CoxeterGroup} <: RootSystem
mutable struct AbstractRootSystem{T<:CoxeterGroup} <: RootSystem
# familiy (A, ..., G) of the root system
# fam::Vector{Tuple{Symbol, Int}}

Expand All @@ -21,12 +21,14 @@ struct AbstractRootSystem{T<:CoxeterGroup} <: RootSystem
function AbstractRootSystem(mat::ZZMatrix)
R = new{WeylGroup}()

roots, refl = positive_roots_and_reflections(gcm)
finite = count(refl .== 0) == rank
roots, refl = positive_roots_and_reflections(mat)
finite = count(refl .== 0) > nrows(mat)

R.cartan_matrix = cartan_matrix
R.cartan_matrix = mat
R.positive_roots = roots
R.weyl_group = WeylGroup(finite, mat, refl, R)
R.weyl_group = WeylGroup(finite, refl, R)

return R
end
end

Expand All @@ -38,7 +40,7 @@ end
function root_system(fam::Symbol, rk::Int)
cartan = cartan_matrix(fam, rk)
#fw = solve(change_base_ring(QQ, cartan), identity_matrix(QQ, nrows(cartan)))
return AbstractRootSystem(fam, fw, cartan)
return AbstractRootSystem(cartan)
end

function root_system(types::Tuple{Symbol,Int}...)
Expand All @@ -62,7 +64,7 @@ end

function fundamental_weights(R::RootSystem)
it = 1:rank(R)
return [weight(R, i .== it)) for i in 1:rank(R)]
return [weight(R, i .== it) for i in 1:rank(R)]
end

function positive_roots(R::RootSystem)
Expand Down Expand Up @@ -110,19 +112,19 @@ function Base.:(*)(n::IntegerUnion, w::WeightLatticeElem)
end

function Base.:(+)(r::WeightLatticeElem, s::WeightLatticeElem)
@req r.parent == s.parent "r and s must belong to the same root lattice"
@req r.parent === s.parent "r and s must belong to the same root lattice"

return RootLatticeElem(r.parent, r.vec + s.vec)
end

function Base.:(-)(v::WeightLatticeElem, w::WeightLatticeElem)
@req v.parent == w.parent "v and w must belong to the same weight lattice"
@req v.parent === w.parent "v and w must belong to the same weight lattice"

return WeightLatticeElem(w.parent, r.vec - s.vec)
end

function Base.:(-)(w::WeightLatticeElem)
return WeightLatticeElem(w.parent, -w.vec)
return WeightLatticeElem(w.root_system, -w.vec)
end

function expressify(w::WeightLatticeElem, s=:w; context=nothing)
Expand All @@ -134,12 +136,21 @@ function expressify(w::WeightLatticeElem, s=:w; context=nothing)
end
@enable_all_show_via_expressify WeightLatticeElem

function reflect(w::WeightLatticeElem, s::Int)
return reflect!(WeightLatticeElem(w.root_system, deepcopy(elem.vec)), s)
end

function reflect!(w::WeightLatticeElem, s::Int)
w.vec -= w.vec[s] * w.root_system.cartan_matrix[:, s]
return w
end

###############################################################################
# internal helpers

# cartan matrix in the format <a^v, b>
function positive_roots_and_reflections(cartan_matrix::ZZMatrix)
rank, _ = size(C)
rank, _ = size(cartan_matrix)

roots = [[l == s ? 1 : 0 for l in 1:rank] for s in 1:rank]
rootidx = Dict(roots[s] => s for s in 1:rank)
Expand All @@ -152,8 +163,8 @@ function positive_roots_and_reflections(cartan_matrix::ZZMatrix)
continue
end

pairing = sum(roots[i][l] * C[s, l] for l in 1:rank)
copairing = sum(roots[i][l] * C[l, s] for l in 1:rank)
pairing = sum(roots[i][l] * cartan_matrix[s, l] for l in 1:rank)
copairing = sum(roots[i][l] * cartan_matrix[l, s] for l in 1:rank)
if pairing * copairing >= 4
refl[s, i] = 0
continue
Expand Down
2 changes: 1 addition & 1 deletion experimental/LieAlgebras/src/CartanMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function is_cartan_matrix(mat::ZZMatrix; generalized=true)
return false
end

if !all(mat[i,i] == 2 for 1 in 1:n)
if !all(mat[i,i] == 2 for i in 1:n)
return false
end

Expand Down
9 changes: 5 additions & 4 deletions experimental/LieAlgebras/src/LieAlgebras.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import ..Oscar:
symmetric_power,
tensor_product,
weight,
weyl_vector,
,

Expand Down Expand Up @@ -105,11 +106,11 @@ export trivial_module
export universal_enveloping_algebra

# RootSystem.jl
export RootSystem, RootSystemElem, cartan_matrix, inner_product, inner_product_matrix, rank, root_system, weyl_group, weyl_vector
export RootSystem, RootSystemElem, cartan_matrix, inner_product, inner_product_matrix, rank, root_system, weyl_group
export WeightLatticeElem

# WeylGroup.jl
export WeylGroup, weyl_group, elem, gens, longest_element, reduced_words, word
export WeylGroup, weyl_group, elem, gens, longest_element, reduced_expressions, word

include("Combinatorics.jl")
include("Util.jl")
Expand Down Expand Up @@ -176,8 +177,8 @@ export universal_enveloping_algebra
export cartan_matrix

# RootSystem.jl
export RootSystem, RootSystemElem, cartan_matrix, inner_product, inner_product_matrix, rank, root_system, weyl_group, weyl_vector
export RootSystem, RootSystemElem, cartan_matrix, inner_product, inner_product_matrix, rank, root_system, weyl_group
export WeightLatticeElem

# WeylGroup.jl
export WeylGroup, weyl_group, elem, gens, longest_element, reduced_words, word
export WeylGroup, weyl_group, elem, gens, longest_element, reduced_expressions, word
65 changes: 21 additions & 44 deletions experimental/LieAlgebras/src/WeylGroup.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ end
#end

function longest_element(W::WeylGroup)
@req G.finite "$W is not finite"
@req W.finite "$W is not finite"

rk = rank(G)
rk = rank(W)
w = -weyl_vector(W.root_system)

word = UInt8[]
sizehint!(word, ncols(G.refl))
sizehint!(word, ncols(W.refl))
s = i = 1
while s <= rk
pairing = dot(W.root_system.cartan_matrix[s, :], w)
Expand All @@ -82,8 +82,8 @@ function longest_element(W::WeylGroup)
return WeylGroupElem(G, word)
end

function rank(G::WeylGroup)
return nrows(G.gcm)
function rank(W::WeylGroup)
return rank(W.root_system)
end

function simple_reflection(W::WeylGroup, i::Int)
Expand Down Expand Up @@ -121,26 +121,6 @@ function Base.:(*)(w::WeylGroupElem, elem::WeightLatticeElem)
return WeightLatticeElem(elem.root_system, vec)
end

function Base.:(<)(t::T, w::CoxEltMin) where T <: Integer
if !(1 <= t <= rank(w.group))
throw(DomainError(t, "$t is not a generator in the range [1, $(rank(w.group))]"))
end

# See the comments in is_right_descent(), here we are just reversing the order in which we read the word.
root = UInt8(t)
for s = w.word
if s == root
return true
elseif w.group.refl_table[s, root] == 0
return false
end

root = w.group.refl_table[s, root]
end

return false
end

function Base.:(==)(x::WeylGroupElem, y::WeylGroupElem)
return x.parent === y.parent && x.word == y.word
end
Expand Down Expand Up @@ -182,48 +162,45 @@ end


struct ReducedExprIterator
#letters::Vector{UInt8}
weight::WeightLatticeElem
end

function reduced_words(x::WeylGroupElem)
words = Vector{UInt8}[]
rk = rank(x.parent)

expr = zeros(UInt8, length(x.word))
w = x*weyl_vector(x.parent.root_system)
function reduced_expressions(x::WeylGroupElem)
return ReducedExprIterator(x*weyl_vector(x.parent.root_system))
end

# TODO: only iterate over terms occuring in the reduced expr
#letters = unique(x.word)
#len = length(letters)
function Base.iterate(iter::ReducedExprIterator, word::Vector{UInt8})
next = copy(word)
weight = iter.weight

# we do a depth first traversel of the reduced expression graph
s = i = 1
i = length(word)
s = last(next)
while true
# search for new simple reflection to add to the reflection
while s <= rk && dot(x.parent.gcm[s, :], w) > 0
while s <= rk && weight.vec[s] > 0
s += 1
end

if s == rk + 1
i -= 1
if i == 0
break
return
elseif i == length(x.word)
push!(words, copy(expr))
return next
end

# revert last reflection and continue with next one
s = Int(expr[i])
w[s, 1] -= dot(x.parent.gcm[s, :], w)
reflect!(weight, s)
s += 1
else
expr[i] = UInt8(s)
w[s, 1] -= dot(x.parent.gcm[s, :], w)
next[i] = UInt8(s)
reflect!(weight, s)
i += 1
s = 1
end
end

return words
end

function word(x::WeylGroupElem)
Expand Down

0 comments on commit bd27188

Please sign in to comment.