Skip to content

Commit

Permalink
Add serialization for WeightLattice
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens committed Dec 20, 2024
1 parent 65a82d9 commit 301950c
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 15 deletions.
42 changes: 42 additions & 0 deletions src/Serialization/LieTheory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,48 @@ function load_type_params(
return load_typed_object(s)
end

###############################################################################
#
# Weight lattices
#
###############################################################################

@register_serialization_type WeightLattice uses_id

function save_object(s::SerializerState, P::WeightLattice)
save_data_dict(s) do
save_typed_object(s, root_system(P), :root_system)
end
end

function load_object(s::DeserializerState, ::Type{WeightLattice})
R = load_typed_object(s, :root_system)
return weight_lattice(R)
end

@register_serialization_type WeightLatticeElem uses_params

function save_object(s::SerializerState, w::WeightLatticeElem)
save_object(s, _vec(coefficients(w)))
end

function load_object(s::DeserializerState, ::Type{WeightLatticeElem}, P::WeightLattice)
return WeightLatticeElem(P, load_object(s, Vector, ZZ))
end

function save_type_params(s::SerializerState, w::WeightLatticeElem)
save_data_dict(s) do
save_object(s, encode_type(typeof(w)), :name)
parent_w = parent(w)
parent_ref = save_as_ref(s, parent_w)
save_object(s, parent_ref, :params)
end
end

function load_type_params(s::DeserializerState, ::Type{WeightLatticeElem})
return load_typed_object(s)
end

###############################################################################
#
# Weyl groups
Expand Down
92 changes: 77 additions & 15 deletions test/Serialization/LieTheory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# nothing, cause `R === loaded` anyway
end

if n_roots(R) >= 1
if rank(R) >= 1
r = positive_root(R, n_positive_roots(R))
test_save_load_roundtrip(path, r) do loaded
@test root_system(loaded) === R
Expand Down Expand Up @@ -59,25 +59,87 @@
end
end

@testset "WeylGroup" begin
@testset "WeightLattice" begin
@testset "simple saving and loading" begin
W = weyl_group((:A, 2), (:B, 4))
for R in [
root_system(zero_matrix(ZZ, 0, 0)), # rk 0
root_system((:A, 2), (:B, 4)),
]
P = weight_lattice(R)

test_save_load_roundtrip(path, P) do loaded
# nothing, cause `P === loaded` anyway
end

test_save_load_roundtrip(path, W) do loaded
# nothing, cause `W === loaded` anyway
end
w = WeightLatticeElem(P, [rand(ZZ, -10:10) for _ in 1:rank(P)])
test_save_load_roundtrip(path, w) do loaded
@test parent(loaded) === P
@test coefficients(loaded) == coefficients(w)
end

x = rand(W)
test_save_load_roundtrip(path, x) do loaded
@test parent(loaded) === W
@test word(loaded) == word(x)
test_save_load_roundtrip(path, gens(P)) do loaded
@test length(loaded) == rank(P)
@test all(coefficients(loaded[i]) == coefficients(gen(P, i)) for i in 1:rank(R))
end
end
end

test_save_load_roundtrip(path, gens(W)) do loaded
@test length(loaded) == ngens(W)
@test all(
word(loaded[i]) == word(gen(W, i)) for i in 1:ngens(W)
)
@testset "cyclic reference between R and P survives" begin
Oscar.reset_global_serializer_state()

R_filename = joinpath(path, "R.mrdi")
P_filename = joinpath(path, "P.mrdi")

R = root_system(:D, 5)
P = weight_lattice(R)

save(R_filename, R)
save(P_filename, P)

Oscar.reset_global_serializer_state()

loaded_R = load(R_filename)
loaded_P = load(P_filename)

@test loaded_R === root_system(loaded_P)
@test loaded_P === weight_lattice(loaded_R)

loaded_R = loaded_P = nothing # unset all references

Oscar.reset_global_serializer_state()

loaded_P = load(P_filename)
loaded_R = load(R_filename)

@test loaded_R === root_system(loaded_P)
@test loaded_P === weight_lattice(loaded_R)

loaded_R = loaded_P = nothing # unset all references
end
end

@testset "WeylGroup" begin
@testset "simple saving and loading" begin
for W in [
weyl_group(zero_matrix(ZZ, 0, 0)), # rk 0
weyl_group((:A, 2), (:B, 4)),
]
test_save_load_roundtrip(path, W) do loaded
# nothing, cause `W === loaded` anyway
end

x = rand(W)
test_save_load_roundtrip(path, x) do loaded
@test parent(loaded) === W
@test word(loaded) == word(x)
end

test_save_load_roundtrip(path, gens(W)) do loaded
@test length(loaded) == ngens(W)
@test all(
word(loaded[i]) == word(gen(W, i)) for i in 1:ngens(W)
)
end
end
end

Expand Down

0 comments on commit 301950c

Please sign in to comment.