Skip to content

Commit

Permalink
feat: add coprime base
Browse files Browse the repository at this point in the history
  • Loading branch information
thofma committed Sep 16, 2024
1 parent 86996ee commit c9929de
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/src/euclidean_interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ crt(r1::T, m1::T, r2::T, m2::T; check::Bool=true) where T <: RingElement
crt(r::Vector{T}, m::Vector{T}; check::Bool=true) where T <: RingElement
crt_with_lcm(r1::T, m1::T, r2::T, m2::T; check::Bool=true) where T <: RingElement
crt_with_lcm(r::Vector{T}, m::Vector{T}; check::Bool=true) where T <: RingElement
coprime_base
coprime_base_insert!
```
1 change: 1 addition & 0 deletions src/AbstractAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ include("algorithms/MPolyEvaluate.jl")
include("algorithms/MPolyFactor.jl")
include("algorithms/MPolyNested.jl")
include("algorithms/DensePoly.jl")
include("algorithms/coprime_base.jl")

###############################################################################
#
Expand Down
68 changes: 68 additions & 0 deletions src/algorithms/coprime_base.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
################################################################################
#
# Coprime bases
#
################################################################################

function augment_coprime_base(S::Vector{E}, a::E, start::Int = 1) where E
i = start
if is_unit(a)
return S
end

g = a
new = true

while i<=length(S) && !isone(a)
if new
g = gcd(S[i], a)
new = false
else
g = gcd!(g, S[i], a)
end
if is_unit(g)
i += 1
continue
end
si = divexact(S[i], g)
a = divexact(a, g)
if is_unit(si) # g = S[i] and S[i] | a
continue
end
S[i] = si
if is_unit(a) # g = a and a | S[i]
a = copy(g)
continue
end
augment_coprime_base(S, copy(g), i)
continue
end
if !is_unit(a)
push!(S, a)
end

return S
end

@doc raw"""
coprime_base(S::Vector{RingElem}) -> Vector{RingElem}
Returns a coprime base for $S$, i.e. the resulting array contains pairwise coprime objects that multiplicatively generate the same set as the input array.
"""
function coprime_base(S::Vector{E}) where E
@assert !isempty(S)
T = Array{E}(undef, 1)
T[1] = S[1]
for i=2:length(S)
augment_coprime_base(T, S[i])
end
return T
end

@doc raw"""
coprime_base_insert(S::Vector{RingElem}, a::RingElem) -> Vector{RingElem}
Given an array $S$ of coprime elements, insert a new element, that is, find a
coprime base for `push(S, a)`.
"""
coprime_base_push!(S, a) = augment_coprime_base(S, a)
2 changes: 2 additions & 0 deletions src/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ export compose
export conj!
export constant_coefficient
export content
export coprime_base
export coprime_base_push!
export crt
export crt_with_lcm
export cycles
Expand Down
1 change: 1 addition & 0 deletions test/Rings-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ include("algorithms/MPolyFactor-test.jl")
include("algorithms/MPolyNested-test.jl")
include("algorithms/DensePoly-test.jl")
include("algorithms/GenericFunctions-test.jl")
include("algorithms/coprime_base-test.jl")
include("generic/PolyRingHom-test.jl")

@testset "Generic.Rings.broadcast" begin
Expand Down
17 changes: 17 additions & 0 deletions test/algorithms/coprime_base-test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@testset "coprime base" begin
c = BigInt[6, 10]
d = coprime_base(c)
@test issetequal(d, BigInt[2, 3, 5])

c = BigInt[1]
d = coprime_base(c)
@test issetequal(d, BigInt[1])

c = BigInt[6, 10, 1]
d = coprime_base(c)
@test issetequal(d, BigInt[2, 3, 5])
coprime_base_push!(d, BigInt(1))
@test issetequal(d, BigInt[2, 3, 5])
coprime_base_push!(d, BigInt(14))
@test issetequal(d, BigInt[2, 3, 5, 7])
end

0 comments on commit c9929de

Please sign in to comment.