From 301950c4c4bf864eb1816602c05bbd7f13f39348 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Fri, 20 Dec 2024 15:07:17 +0100 Subject: [PATCH] Add serialization for `WeightLattice` --- src/Serialization/LieTheory.jl | 42 +++++++++++++++ test/Serialization/LieTheory.jl | 92 +++++++++++++++++++++++++++------ 2 files changed, 119 insertions(+), 15 deletions(-) diff --git a/src/Serialization/LieTheory.jl b/src/Serialization/LieTheory.jl index b696c86ec87..2d06aa6769f 100644 --- a/src/Serialization/LieTheory.jl +++ b/src/Serialization/LieTheory.jl @@ -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 diff --git a/test/Serialization/LieTheory.jl b/test/Serialization/LieTheory.jl index 9cee6151044..29363274698 100644 --- a/test/Serialization/LieTheory.jl +++ b/test/Serialization/LieTheory.jl @@ -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 @@ -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