Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unsafe operations for FinGenAbGroupElem #1619

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading