Skip to content

Commit

Permalink
Add mutating arithmetics for root system types
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens committed Sep 30, 2024
1 parent 0165ad5 commit 64f909d
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 12 deletions.
1 change: 1 addition & 0 deletions experimental/LieAlgebras/src/LieAlgebras.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import ..Oscar:
tensor_product,
weyl_vector,
word,
zero!,
zero_map,
,
Expand Down
98 changes: 86 additions & 12 deletions experimental/LieAlgebras/src/RootSystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,14 @@ function RootSpaceElem(w::WeightLatticeElem)
return RootSpaceElem(root_system(w), w)
end

function zero(::Type{RootSpaceElem}, R::RootSystem)
return RootSpaceElem(R, zero_matrix(QQ, 1, rank(R)))
end

function zero(r::RootSpaceElem)
return zero(RootSpaceElem, root_system(r))
end

function Base.:*(q::RationalUnion, r::RootSpaceElem)
return RootSpaceElem(root_system(r), q * r.vec)
end
Expand All @@ -544,6 +552,29 @@ function Base.:-(r::RootSpaceElem)
return RootSpaceElem(root_system(r), -r.vec)
end

function zero!(r::RootSpaceElem)
r.vec = zero!(r.vec)
return r
end

function add!(rr::RootSpaceElem, r1::RootSpaceElem, r2::RootSpaceElem)
@req root_system(rr) === root_system(r1) === root_system(r2) "parent root system mismatch"
rr.vec = add!(rr.vec, r1.vec, r2.vec)
return rr
end

function neg!(rr::RootSpaceElem, r::RootSpaceElem)
@req root_system(rr) === root_system(r) "parent root system mismatch"
rr.vec = neg!(rr.vec, r.vec)
return rr
end

function sub!(rr::RootSpaceElem, r1::RootSpaceElem, r2::RootSpaceElem)
@req root_system(rr) === root_system(r1) === root_system(r2) "parent root system mismatch"
rr.vec = sub!(rr.vec, r1.vec, r2.vec)
return rr
end

function Base.:(==)(r::RootSpaceElem, r2::RootSpaceElem)
return r.root_system === r2.root_system && r.vec == r2.vec
end
Expand Down Expand Up @@ -682,6 +713,14 @@ function DualRootSpaceElem(root_system::RootSystem, vec::Vector{<:RationalUnion}
return DualRootSpaceElem(root_system, matrix(QQ, 1, length(vec), vec))
end

function zero(::Type{DualRootSpaceElem}, R::RootSystem)
return DualRootSpaceElem(R, zero_matrix(QQ, 1, rank(R)))
end

function zero(r::DualRootSpaceElem)
return zero(DualRootSpaceElem, root_system(r))
end

function Base.:*(q::RationalUnion, r::DualRootSpaceElem)
return DualRootSpaceElem(root_system(r), q * r.vec)
end
Expand All @@ -702,6 +741,29 @@ function Base.:-(r::DualRootSpaceElem)
return DualRootSpaceElem(root_system(r), -r.vec)
end

function zero!(r::DualRootSpaceElem)
r.vec = zero!(r.vec)
return r
end

function add!(rr::DualRootSpaceElem, r1::DualRootSpaceElem, r2::DualRootSpaceElem)
@req root_system(rr) === root_system(r1) === root_system(r2) "parent root system mismatch"
rr.vec = add!(rr.vec, r1.vec, r2.vec)
return rr
end

function neg!(rr::DualRootSpaceElem, r::DualRootSpaceElem)
@req root_system(rr) === root_system(r) "parent root system mismatch"
rr.vec = neg!(rr.vec, r.vec)
return rr
end

function sub!(rr::DualRootSpaceElem, r1::DualRootSpaceElem, r2::DualRootSpaceElem)
@req root_system(rr) === root_system(r1) === root_system(r2) "parent root system mismatch"
rr.vec = sub!(rr.vec, r1.vec, r2.vec)
return rr
end

function Base.:(==)(r::DualRootSpaceElem, r2::DualRootSpaceElem)
return r.root_system === r2.root_system && r.vec == r2.vec
end
Expand Down Expand Up @@ -839,18 +901,26 @@ function WeightLatticeElem(r::RootSpaceElem)
return WeightLatticeElem(root_system(r), r)
end

function zero(::Type{WeightLatticeElem}, R::RootSystem)
return WeightLatticeElem(R, zero_matrix(ZZ, rank(R), 1))
end

function zero(r::WeightLatticeElem)
return zero(WeightLatticeElem, root_system(r))
end

function Base.:*(n::IntegerUnion, w::WeightLatticeElem)
return WeightLatticeElem(root_system(w), n * w.vec)
end

function Base.:+(w::WeightLatticeElem, w2::WeightLatticeElem)
@req root_system(w) === root_system(w2) "parent weight lattics mismatch"
@req root_system(w) === root_system(w2) "parent root system mismatch"

return WeightLatticeElem(root_system(w), w.vec + w2.vec)
end

function Base.:-(w::WeightLatticeElem, w2::WeightLatticeElem)
@req root_system(w) === root_system(w2) "parent weight lattics mismatch"
@req root_system(w) === root_system(w2) "parent root system mismatch"

return WeightLatticeElem(root_system(w), w.vec - w2.vec)
end
Expand All @@ -859,25 +929,29 @@ function Base.:-(w::WeightLatticeElem)
return WeightLatticeElem(root_system(w), -w.vec)
end

function zero!(w::WeightLatticeElem)
w.vec = zero!(w.vec)
return w
end

function add!(wr::WeightLatticeElem, w1::WeightLatticeElem, w2::WeightLatticeElem)
add!(wr.vec, w1.vec, w2.vec)
@req root_system(wr) === root_system(w1) === root_system(w2) "parent root system mismatch"
wr.vec = add!(wr.vec, w1.vec, w2.vec)
return wr
end

add!(w1::WeightLatticeElem, w2::WeightLatticeElem) = add!(w1, w1, w2)

function neg!(w::WeightLatticeElem)
neg!(w.vec)
return w
function neg!(wr::WeightLatticeElem, w::WeightLatticeElem)
@req root_system(wr) === root_system(w) "parent root system mismatch"
wr.vec = neg!(wr.vec, w.vec)
return wr
end

function sub!(wr::WeightLatticeElem, w1::WeightLatticeElem, w2::WeightLatticeElem)
sub!(wr.vec, w1.vec, w2.vec)
@req root_system(wr) === root_system(w1) === root_system(w2) "parent root system mismatch"
wr.vec = sub!(wr.vec, w1.vec, w2.vec)
return wr
end

sub!(w1::WeightLatticeElem, w2::WeightLatticeElem) = sub!(w1, w1, w2)

function Base.:(==)(w::WeightLatticeElem, w2::WeightLatticeElem)
return w.root_system === w2.root_system && w.vec == w2.vec
end
Expand Down Expand Up @@ -983,7 +1057,7 @@ function conjugate_dominant_weight_with_elem!(w::WeightLatticeElem)
end

function dot(w1::WeightLatticeElem, w2::WeightLatticeElem)
@req root_system(w1) === root_system(w2) "parent weight lattices mismatch"
@req root_system(w1) === root_system(w2) "parent root system mismatch"
R = root_system(w1)

return dot(
Expand Down
73 changes: 73 additions & 0 deletions experimental/LieAlgebras/test/RootSystem-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,79 @@
)
end

@testset "Mutating arithmetics for $T" for T in (
RootSpaceElem, DualRootSpaceElem, WeightLatticeElem
)
rk = rank(R)

x1 = T(R, rand(-10:10, rk))
x2 = T(R, rand(-10:10, rk))
x3 = T(R, rand(-10:10, rk))
r1c = deepcopy(x1)
r2c = deepcopy(x2)
r3c = deepcopy(x3)

for (f, f!) in ((zero, zero!),)
x1 = f!(x1)
@test x1 == f(r1c)
x1 = deepcopy(r1c)
end

for (f, f!) in ((-, neg!),)
x1 = f!(x1, x2)
@test x1 == f(r2c)
@test x2 == r2c
x1 = deepcopy(r1c)
x2 = deepcopy(r2c)

x1 = f!(x1)
@test x1 == f(r1c)
x1 = deepcopy(r1c)
end

for (f, f!) in ((+, add!), (-, sub!))
x1 = f!(x1, x2, x3)
@test x1 == f(r2c, r3c)
@test x2 == r2c
@test x3 == r3c
x1 = deepcopy(r1c)
x2 = deepcopy(r2c)
x3 = deepcopy(r3c)

x1 = f!(x1, x1, x2)
@test x1 == f(r1c, r2c)
@test x2 == r2c
x1 = deepcopy(r1c)
x2 = deepcopy(r2c)

x1 = f!(x1, x2, x1)
@test x1 == f(r2c, r1c)
@test x2 == r2c
x1 = deepcopy(r1c)
x2 = deepcopy(r2c)

x1 = f!(x1, x2, x2)
@test x1 == f(r2c, r2c)
@test x2 == r2c
x1 = deepcopy(r1c)
x2 = deepcopy(r2c)

x1 = f!(x1, x1, x1)
@test x1 == f(r1c, r1c)
x1 = deepcopy(r1c)

x1 = f!(x1, x2)
@test x1 == f(r1c, r2c)
@test x2 == r2c
x1 = deepcopy(r1c)
x2 = deepcopy(r2c)

x1 = f!(x1, x1)
@test x1 == f(r1c, r1c)
x1 = deepcopy(r1c)
end
end

Check warning on line 206 in experimental/LieAlgebras/test/RootSystem-test.jl

View check run for this annotation

Codecov / codecov/patch

experimental/LieAlgebras/test/RootSystem-test.jl#L206

Added line #L206 was not covered by tests

@testset "Serialization" begin
mktempdir() do path
test_save_load_roundtrip(path, R) do loaded
Expand Down

0 comments on commit 64f909d

Please sign in to comment.