From 38aab563d45c1e932c0be5135e6231a13f7ee8ce Mon Sep 17 00:00:00 2001 From: Max Horn Date: Sat, 21 Sep 2024 00:40:34 +0200 Subject: [PATCH] Add unsafe operations for FinGenAbGroupElem (#1619) --- src/GrpAb/Elem.jl | 54 ++++++++++++++++++++++++++++++++++++++++++++++ test/GrpAb/Elem.jl | 47 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) diff --git a/src/GrpAb/Elem.jl b/src/GrpAb/Elem.jl index 8874317c3a..ec0d97fe54 100644 --- a/src/GrpAb/Elem.jl +++ b/src/GrpAb/Elem.jl @@ -224,6 +224,60 @@ end *(x::FinGenAbGroupElem, y::Integer) = y*x +############################################################################### +# +# Unsafe operators +# +############################################################################### + +function reduce!(x::FinGenAbGroupElem) + assure_reduced!(parent(x), x.coeff) + return x +end + +function zero!(x::FinGenAbGroupElem) + zero!(x.coeff) + # no reduction necessary + return x +end + +function neg!(x::FinGenAbGroupElem) + neg!(x.coeff) + return reduce!(x) +end + +# TODO: set! for ZZMatrix not yet implemented, hence this is not yet implemented +#function set!(x::FinGenAbGroupElem, y::FinGenAbGroupElem) +# set!(x.coeff, y.coeff) +# # no reduction necessary +# return x +#end + +function add!(x::FinGenAbGroupElem, y::FinGenAbGroupElem, z::FinGenAbGroupElem) + add!(x.coeff, y.coeff, z.coeff) + return reduce!(x) +end + +function sub!(x::FinGenAbGroupElem, y::FinGenAbGroupElem, z::FinGenAbGroupElem) + sub!(x.coeff, y.coeff, z.coeff) + return reduce!(x) +end + +function mul!(x::FinGenAbGroupElem, y::FinGenAbGroupElem, z::Union{Int,ZZRingElem}) + mul!(x.coeff, y.coeff, z) + return reduce!(x) +end + +function addmul!(x::FinGenAbGroupElem, y::FinGenAbGroupElem, z::Union{Int,ZZRingElem}) + addmul!(x.coeff, y.coeff, z) + return reduce!(x) +end + +function addmul_delayed_reduction!(x::FinGenAbGroupElem, y::FinGenAbGroupElem, z::Union{Int,ZZRingElem}) + addmul!(x.coeff, y.coeff, z) + return x +end + ################################################################################ # # Neutral element diff --git a/test/GrpAb/Elem.jl b/test/GrpAb/Elem.jl index d1002c5103..2e33680942 100644 --- a/test/GrpAb/Elem.jl +++ b/test/GrpAb/Elem.jl @@ -93,6 +93,53 @@ @test aa == G([2, 0, 0]) end + @testset "Unsafe operators" begin + G = abelian_group([3, 3, 0]) + + a = G([1, 2, 3]) + @test zero!(a) == G([0, 0, 0]) + @test a == G([0, 0, 0]) + + a = G([1, 2, 3]) + @test neg!(a) == G([-1, -2, -3]) + @test a == G([-1, -2, -3]) + + # TODO: set! not yet implemented + #a = G([1, 2, 3]) + #@test Hecke.set!(a, G[1]) == G([1, 0, 0]) + #@test a == G([1, 0, 0]) + + a = G([1, 2, 3]) + @test add!(a, a) == G([2, 1, 6]) + @test a == G([2, 1, 6]) + + a = G([1, 2, 3]) + @test sub!(a, G([4, 4, 4])) == G([-3, -2, -1]) + @test a == G([-3, -2, -1]) + + a = G([1, 2, 3]) + @test mul!(a, 4) == G([1, 2, 12]) + @test a == G([1, 2, 12]) + + a = G([1, 2, 3]) + @test mul!(a, ZZ(4)) == G([1, 2, 12]) + @test a == G([1, 2, 12]) + + a = G([1, 2, 3]) + @test addmul!(a, G([1,1,1]), 4) == G([2, 0, 7]) + @test a == G([2, 0, 7]) + + a = G([1, 2, 3]) + @test addmul!(a, G([1,1,1]), ZZ(4)) == G([2, 0, 7]) + @test a == G([2, 0, 7]) + + a = G([1, 2, 3]) + @test Hecke.addmul_delayed_reduction!(a, G([1,1,1]), ZZ(4)) != G([2, 0, 7]) + reduce!(a) + @test a == G([2, 0, 7]) + + end + @testset "Neutral element" begin G = abelian_group([3, 3, 3]) a = G[1]