Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Lax202 committed Sep 19, 2024
1 parent 68015e3 commit 073fa8a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 76 deletions.
73 changes: 20 additions & 53 deletions src/module/ModuleTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,25 @@
#
###############################################################################

const FreeModID = Dict{Tuple{Union{PolyRing,PluralRing}, Int}, Module}()
const FreeModID = Dict{Tuple{Nemo.NCRing, Int}, Module}()

mutable struct FreeMod{T <: Union{Nemo.RingElem, Singular.spluralg}} <: Module{T}
base_ring::Union{PolyRing, PluralRing}
mutable struct FreeMod{T <: Nemo.NCRingElem} <: Module{T}
base_ring::Nemo.NCRing
rank::Int

function FreeMod{T}(R::PolyRing, r::Int) where T
return get!(FreeModID, (R, r)) do
new(R, r)
end
end

function FreeMod{T}(R::Singular.PluralRing, r::Int) where T
function FreeMod{T}(R::Nemo.NCRing, r::Int) where T
return get!(FreeModID, (R, r)) do
new(R, r)
end
end
end

mutable struct svector{T <: Union{Nemo.RingElem, Singular.spluralg}} <: Nemo.ModuleElem{T}
base_ring::Union{PolyRing, PluralRing}
mutable struct svector{T <: Nemo.NCRingElem} <: Nemo.ModuleElem{T}
base_ring::Nemo.NCRing
ptr::libSingular.poly_ptr # not really a polynomial
rank::Int

function svector{T}(R::PolyRing, r::Int, p::libSingular.poly_ptr) where T
T === elem_type(R) || error("type mismatch")
z = new(R, p, r)
R.refcount += 1
finalizer(_svector_clear_fn, z)
return z
end

function svector{T}(R::PluralRing, r::Int, p::libSingular.poly_ptr) where T
function svector{T}(R::Nemo.NCRing, r::Int, p::libSingular.poly_ptr) where T
T === elem_type(R) || error("type mismatch")
z = new(R, p, r)
R.refcount += 1
Expand All @@ -45,9 +31,9 @@ mutable struct svector{T <: Union{Nemo.RingElem, Singular.spluralg}} <: Nemo.Mod
end
end


"""
(R::PolyRing{T})(m::libSingular.poly,::Val{:vector}) where T
If R is called with a low-level poly pointer, along with
Val(:vector), it will interpret the poly pointer as a vector.
This needs to be indicated due to the fact that Singulars
Expand All @@ -57,6 +43,10 @@ function (R::PolyRing{T})(m::libSingular.poly_ptr, ::Val{:vector}) where T
return svector{T}(R, 1, m)
end

function (R::PluralRing{T})(m::libSingular.poly_ptr, ::Val{:vector}) where T
return svector{T}(R, 1, m)
end

function _svector_clear_fn(p::svector)
R = p.base_ring
libSingular.p_Delete(p.ptr, R.ptr)
Expand All @@ -69,33 +59,25 @@ end
#
###############################################################################

const ModuleClassID = Dict{PolyRing, Set}()
const ModuleClassID = Dict{Nemo.NCRing, Set}()

mutable struct ModuleClass{T <: Nemo.RingElem} <: Set
base_ring::PolyRing
mutable struct ModuleClass{T <: Nemo.NCRingElem} <: Set
base_ring::Nemo.NCRing

function ModuleClass{T}(R::PolyRing) where T
function ModuleClass{T}(R::Nemo.NCRing) where T
return get!(ModuleClassID, R) do
new(R)
end
end
end

mutable struct smodule{T <: Union{Nemo.RingElem, spluralg}} <: Module{T}
base_ring::Union{PolyRing, PluralRing}
mutable struct smodule{T <: Nemo.NCRingElem} <: Module{T}
base_ring::Nemo.NCRing
ptr::libSingular.ideal_ptr # ideal and module types are the same in Singular
isGB::Bool

# take ownership of the pointer - not for general users
function smodule{T}(R::PolyRing, m::libSingular.ideal_ptr) where T
T === elem_type(R) || error("type mismatch")
z = new(R, m, false)
R.refcount += 1
finalizer(_smodule_clear_fn, z)
return z
end

function smodule{T}(R::PluralRing, m::libSingular.ideal_ptr) where T
function smodule{T}(R::Nemo.NCRing, m::libSingular.ideal_ptr) where T
T === elem_type(R) || error("type mismatch")
z = new(R, m, false)
R.refcount += 1
Expand All @@ -111,22 +93,7 @@ function smodule{T}(R::PolyRing, m::libSingular.matrix_ptr) where T
return smodule{T}(R, ptr)
end

function smodule{T}(R::PolyRing, vecs::svector...) where T
n = length(vecs)
r = vecs[1].rank;
for i = 1:n
@assert vecs[i].rank == r
end
m = libSingular.idInit(Cint(n), Cint(r))
z = smodule{T}(R, m)
for i = 1:n
v = libSingular.p_Copy(vecs[i].ptr, R.ptr)
libSingular.setindex_internal(m, v, Cint(i - 1))
end
return z
end

function smodule{T}(R::PluralRing, vecs::svector...) where T
function smodule{T}(R::Nemo.NCRing, vecs::svector...) where T
n = length(vecs)
r = vecs[1].rank;
for i = 1:n
Expand Down
15 changes: 5 additions & 10 deletions src/module/module.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ export jet, minimal_generating_set, ModuleClass, rank, smodule, slimgb,
#
###############################################################################

parent(a::smodule{T}) where T <: Nemo.RingElem = ModuleClass{T}(a.base_ring)
parent(a::smodule{T}) where T <: Nemo.NCRingElem = ModuleClass{T}(a.base_ring)

base_ring(S::ModuleClass) = S.base_ring

base_ring(I::smodule) = I.base_ring

elem_type(::Type{ModuleClass{T}}) where T <: AbstractAlgebra.RingElem = smodule{T}
elem_type(::Type{ModuleClass{T}}) where T <: Nemo.NCRingElem = smodule{T}

parent_type(::Type{smodule{T}}) where T <: AbstractAlgebra.RingElem = ModuleClass{T}
parent_type(::Type{smodule{T}}) where T <: Nemo.NCRingElem = ModuleClass{T}


@doc raw"""
Expand Down Expand Up @@ -60,7 +60,7 @@ function deepcopy_internal(I::smodule, dict::IdDict)
return Module(R, ptr)
end

function check_parent(I::smodule{T}, J::smodule{T}) where T <: Nemo.RingElem
function check_parent(I::smodule{T}, J::smodule{T}) where T <: Nemo.NCRingElem
base_ring(I) != base_ring(J) && error("Incompatible modules")
end

Expand Down Expand Up @@ -462,12 +462,7 @@ function Module(R::PluralRing, vecs::svector{spluralg{T}}...) where T
return smodule{S}(R, vecs...)
end

function Module(R::PolyRing{T}, id::libSingular.ideal_ptr) where T <: Nemo.RingElem
S = elem_type(R)
return smodule{S}(R, id)
end

function Module(R::PluralRing, id::libSingular.ideal_ptr)
function Module(R::Nemo.NCRing, id::libSingular.ideal_ptr)
S = elem_type(R)
return smodule{S}(R, id)
end
Expand Down
33 changes: 20 additions & 13 deletions src/module/vector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ export FreeMod, svector, gens, rank, vector, jet
#
###############################################################################

parent(v::svector{T}) where {T <: Union{Nemo.RingElem, spluralg}} = FreeMod{T}(v.base_ring, v.rank)
parent(v::svector{T}) where {T <: Nemo.NCRingElem} = FreeMod{T}(v.base_ring, v.rank)

base_ring(R::FreeMod) = R.base_ring

base_ring(v::svector) = v.base_ring

elem_type(::Type{FreeMod{T}}) where {T <: Nemo.RingElem} = svector{T}
elem_type(::Type{FreeMod{T}}) where {T <: Nemo.NCRingElem} = svector{T}

parent_type(::Type{svector{T}}) where {T <: Nemo.RingElem} = FreeMod{T}
parent_type(::Type{svector{T}}) where {T <: Nemo.NCRingElem} = FreeMod{T}

@doc raw"""
rank(M::FreeMod)
Expand All @@ -28,28 +28,28 @@ rank(M::FreeMod) = M.rank
Return a Julia array whose entries are the generators of the given free module.
"""
function gens(M::FreeMod{T}) where T <: Union{AbstractAlgebra.RingElem, Singular.spluralg}
function gens(M::FreeMod{T}) where T <: Nemo.NCRingElem
R = base_ring(M)
ptr = GC.@preserve R libSingular.id_FreeModule(Cint(M.rank), R.ptr)
return [svector{T}(R, M.rank, libSingular.getindex(ptr, Cint(i - 1))) for i in 1:M.rank]
end

function gen(M::FreeMod{T}, i::Int) where T <: Union{AbstractAlgebra.RingElem, Singular.spluralg}
function gen(M::FreeMod{T}, i::Int) where T <: Nemo.NCRingElem
1 <= i <= M.rank || error("index out of range")
R = base_ring(M)
ptr = GC.@preserve R libSingular.id_FreeModule(Cint(M.rank), R.ptr)
return svector{T}(R, M.rank, libSingular.getindex(ptr, Cint(i - 1)))
end

number_of_generators(M::FreeMod{T}) where T <: AbstractAlgebra.RingElem = rank(M)
number_of_generators(M::FreeMod{T}) where T <: Nemo.NCRingElem = rank(M)

function deepcopy_internal(p::svector{T}, dict::IdDict) where T <: Union{AbstractAlgebra.RingElem, Singular.spluralg}
function deepcopy_internal(p::svector{T}, dict::IdDict) where T <: Nemo.NCRingElem
R = base_ring(p)
p2 = GC.@preserve p R libSingular.p_Copy(p.ptr, R.ptr)
return svector{T}(p.base_ring, p.rank, p2)
end

function check_parent(a::svector{T}, b::svector{T}) where T <: Union{Nemo.RingElem, Singular.spluralg}
function check_parent(a::svector{T}, b::svector{T}) where T <: Nemo.NCRingElem
base_ring(a) != base_ring(b) && error("Incompatible base rings")
a.rank != b.rank && error("Vectors of incompatible rank")
end
Expand Down Expand Up @@ -86,7 +86,7 @@ end
#
###############################################################################

function -(a::svector{T}) where T <: Union{AbstractAlgebra.RingElem, spluralg}
function -(a::svector{T}) where T <: Nemo.NCRingElem
R = base_ring(a)
GC.@preserve a R begin
a1 = libSingular.p_Copy(a.ptr, R.ptr)
Expand All @@ -103,7 +103,7 @@ iszero(p::svector) = p.ptr.cpp_object == C_NULL
#
###############################################################################

function +(a::svector{T}, b::svector{T}) where T <: Union{AbstractAlgebra.RingElem, spluralg}
function +(a::svector{T}, b::svector{T}) where T <: Nemo.NCRingElem
check_parent(a, b)
R = base_ring(a)
GC.@preserve a b R begin
Expand All @@ -114,7 +114,7 @@ function +(a::svector{T}, b::svector{T}) where T <: Union{AbstractAlgebra.RingEl
end
end

function -(a::svector{T}, b::svector{T}) where T <: Union{AbstractAlgebra.RingElem, spluralg}
function -(a::svector{T}, b::svector{T}) where T <: Nemo.NCRingElem
check_parent(a, b)
R = base_ring(a)
GC.@preserve a b R begin
Expand All @@ -138,11 +138,11 @@ function *(a::svector{spoly{T}}, b::spoly{T}) where T <: AbstractAlgebra.RingEle
return svector{spoly{T}}(R, a.rank, s)
end

function *(b::Singular.spluralg{T}, a::svector{Singular.spluralg{T}}) where T
function *(a::svector{Singular.spluralg{T}}, b::Singular.spluralg{T}) where T <: AbstractAlgebra.RingElem
base_ring(a) != parent(b) && error("Incompatible base rings")
R = base_ring(a)
s = GC.@preserve a b R libSingular.pp_Mult_qq(a.ptr, b.ptr, R.ptr)
return svector{spluralg{T}}(R, a.rank, s)
return svector{spoly{T}}(R, a.rank, s)
end

function (a::spoly{T} * b::svector{spoly{T}}) where T <: Nemo.RingElem
Expand All @@ -152,6 +152,13 @@ function (a::spoly{T} * b::svector{spoly{T}}) where T <: Nemo.RingElem
return svector{spoly{T}}(R, b.rank, s)
end

function *(b::Singular.spluralg{T}, a::svector{Singular.spluralg{T}}) where T
base_ring(a) != parent(b) && error("Incompatible base rings")
R = base_ring(a)
s = GC.@preserve a b R libSingular.pp_Mult_qq(a.ptr, b.ptr, R.ptr)
return svector{spluralg{T}}(R, a.rank, s)
end

(a::svector{spoly{T}} * b::T) where T <: Nemo.RingElem = a*base_ring(a)(b)

(a::T * b::svector{spoly{T}}) where T <: Nemo.RingElem = base_ring(b)(a)*b
Expand Down

0 comments on commit 073fa8a

Please sign in to comment.