Skip to content

Commit

Permalink
Add unsafe operations for FinGenAbGroupElem (#1619)
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin authored Sep 20, 2024
1 parent b491ab9 commit 38aab56
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/GrpAb/Elem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions test/GrpAb/Elem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 38aab56

Please sign in to comment.