Skip to content

Commit

Permalink
Implement more shortcuts and remove code duplication.
Browse files Browse the repository at this point in the history
  • Loading branch information
HechtiDerLachs committed Sep 30, 2024
1 parent 2fe164e commit 09eb2e9
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions src/Modules/UngradedModules/ModuleGens.jl
Original file line number Diff line number Diff line change
Expand Up @@ -263,47 +263,44 @@ Convert a Singular vector to a free module element.
"""
function (F::FreeMod{<:MPolyRingElem})(s::Singular.svector)
Rx = base_ring(F)
R = coefficient_ring(Rx)
ctx = MPolyBuildCtx(Rx)

# shortcut in order not to allocate the dictionary
if isone(length(s))
(i, e, c) = first(s)
push_term!(ctx, R(c), e)
return FreeModElem(sparse_row(Rx, [(i, finish(ctx))]), F)
end

cache = IdDict{Int, typeof(ctx)}()
for (i, e, c) in s
ctx = get!(cache, i) do
MPolyBuildCtx(Rx)
end
push_term!(ctx, R(c), e)
end
return FreeModElem(sparse_row(Rx, [(i, finish(ctx)) for (i, ctx) in cache]), F)
row = _build_sparse_row(Rx, s)
return FreeModElem(row, F)
end

function (F::FreeMod{<:MPolyQuoRingElem})(s::Singular.svector)
is_zero(s) && return zero(F) # `map_entries` throws for empty vectors
Qx = base_ring(F)::MPolyQuoRing
Rx = base_ring(Qx)::MPolyRing
row = _build_sparse_row(Rx, s)
return FreeModElem(map_entries(Qx, row), F)
end

function _build_sparse_row(Rx::Ring, s::Singular.svector)
R = coefficient_ring(Rx)
ctx = MPolyBuildCtx(Rx)

# shortcut in order not to allocate the dictionary
# shortcuts in order not to allocate the dictionary
is_zero(length(s)) && return sparse_row(Rx)
if isone(length(s))
(i, e, c) = first(s)
push_term!(ctx, R(c), e)
return FreeModElem(sparse_row(Qx, [(i, Qx(finish(ctx)))]), F)
return sparse_row(Rx, [(i, finish(ctx))])
end

cache = IdDict{Int, typeof(ctx)}()
last_index = 0
for (i, e, c) in s
if i == last_index
push_term!(ctx, R(c), e)
continue
end
last_index = i
ctx = get!(cache, i) do
MPolyBuildCtx(Rx)
end
push_term!(ctx, R(c), e)
end
return FreeModElem(sparse_row(Qx, [(i, Qx(finish(ctx))) for (i, ctx) in cache]), F)
return sparse_row(Rx, [(i, finish(ctx)) for (i, ctx) in cache])
end

# After creating the required infrastruture in Singular,
Expand Down

0 comments on commit 09eb2e9

Please sign in to comment.