Skip to content

Commit

Permalink
Unify constructions of fmpz_mod_poly using setcoeff!
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens committed Oct 18, 2024
1 parent bebbbe4 commit d2be9c1
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 204 deletions.
200 changes: 30 additions & 170 deletions src/flint/FlintTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -802,41 +802,21 @@ mutable struct ZZModPolyRingElem <: PolyRingElem{ZZModRingElem}
return ZZModPolyRingElem(R.ninv, a)
end

function ZZModPolyRingElem(n::fmpz_mod_ctx_struct, arr::Vector{ZZRingElem})
length(arr) == 0 && error("Array must have length > 0")
function ZZModPolyRingElem(n::fmpz_mod_ctx_struct, a::Vector{<:Union{Integer,ZZRingElem,ZZModRingElem}})
length(a) == 0 && error("Array must have length > 0")
z = new()
ccall((:fmpz_mod_poly_init2, libflint), Nothing,
(Ref{ZZModPolyRingElem}, Int, Ref{fmpz_mod_ctx_struct}),
z, length(arr), n)
for i in 1:length(arr)
ccall((:fmpz_mod_poly_set_coeff_fmpz, libflint), Nothing,
(Ref{ZZModPolyRingElem}, Int, Ref{ZZRingElem}, Ref{fmpz_mod_ctx_struct}),
z, i - 1, arr[i], n)
end
finalizer(_fmpz_mod_poly_clear_fn, z)
return z
end

function ZZModPolyRingElem(R::ZZModRing, arr::Vector{ZZRingElem})
return ZZModPolyRingElem(R.ninv, arr)
end

function ZZModPolyRingElem(n::fmpz_mod_ctx_struct, arr::Vector{ZZModRingElem})
z = new()
ccall((:fmpz_mod_poly_init2, libflint), Nothing,
(Ref{ZZModPolyRingElem}, Int, Ref{fmpz_mod_ctx_struct}),
z, length(arr), n)
for i in 1:length(arr)
ccall((:fmpz_mod_poly_set_coeff_fmpz, libflint), Nothing,
(Ref{ZZModPolyRingElem}, Int, Ref{ZZRingElem}, Ref{fmpz_mod_ctx_struct}),
z, i - 1, arr[i].data, n)
z, length(a), n)
for i in 1:length(a)
setcoeff!(z, i-1, a[i])
end
finalizer(_fmpz_mod_poly_clear_fn, z)
return z
end

function ZZModPolyRingElem(R::ZZModRing, arr::Vector{ZZModRingElem})
return ZZModPolyRingElem(R.ninv, arr)
function ZZModPolyRingElem(R::ZZModRing, a::Vector{<:Union{Integer,ZZRingElem,ZZModRingElem}})
return ZZModPolyRingElem(R.ninv, a)
end

function ZZModPolyRingElem(n::fmpz_mod_ctx_struct, f::ZZPolyRingElem)
Expand Down Expand Up @@ -974,41 +954,21 @@ mutable struct FpPolyRingElem <: PolyRingElem{FpFieldElem}
return FpPolyRingElem(R.ninv, a)
end

function FpPolyRingElem(n::fmpz_mod_ctx_struct, arr::Vector{ZZRingElem})
length(arr) == 0 && error("Array must have length > 0")
function FpPolyRingElem(n::fmpz_mod_ctx_struct, a::Vector{<:Union{Integer,ZZRingElem,FpFieldElem}})
length(a) == 0 && error("Array must have length > 0")
z = new()
ccall((:fmpz_mod_poly_init2, libflint), Nothing,
(Ref{FpPolyRingElem}, Int, Ref{fmpz_mod_ctx_struct}),
z, length(arr), n)
for i in 1:length(arr)
ccall((:fmpz_mod_poly_set_coeff_fmpz, libflint), Nothing,
(Ref{FpPolyRingElem}, Int, Ref{ZZRingElem}, Ref{fmpz_mod_ctx_struct}),
z, i - 1, arr[i], n)
end
finalizer(_fmpz_mod_poly_clear_fn, z)
return z
end

function FpPolyRingElem(R::FpField, arr::Vector{ZZRingElem})
FpPolyRingElem(R.ninv, arr)
end

function FpPolyRingElem(n::fmpz_mod_ctx_struct, arr::Vector{FpFieldElem})
z = new()
ccall((:fmpz_mod_poly_init2, libflint), Nothing,
(Ref{FpPolyRingElem}, Int, Ref{fmpz_mod_ctx_struct}),
z, length(arr), n)
for i in 1:length(arr)
ccall((:fmpz_mod_poly_set_coeff_fmpz, libflint), Nothing,
(Ref{FpPolyRingElem}, Int, Ref{ZZRingElem}, Ref{fmpz_mod_ctx_struct}),
z, i - 1, arr[i].data, n)
z, length(a), n)
for i in 1:length(a)
setcoeff!(z, i - 1, a[i])
end
finalizer(_fmpz_mod_poly_clear_fn, z)
return z
end

function FpPolyRingElem(R::FpField, arr::Vector{FpFieldElem})
return FpPolyRingElem(R.ninv, arr)
function FpPolyRingElem(R::FpField, a::Vector{<:Union{Integer,ZZRingElem,FpFieldElem}})
return FpPolyRingElem(R.ninv, a)
end

function FpPolyRingElem(n::fmpz_mod_ctx_struct, f::ZZPolyRingElem)
Expand Down Expand Up @@ -3382,48 +3342,22 @@ mutable struct FpRelPowerSeriesRingElem <: RelPowerSeriesRingElem{FpFieldElem}
return FpRelPowerSeriesRingElem(R.ninv)
end

function FpRelPowerSeriesRingElem(p::fmpz_mod_ctx_struct, a::Vector{ZZRingElem},
function FpRelPowerSeriesRingElem(p::fmpz_mod_ctx_struct, a::Vector{<:Union{Integer,ZZRingElem,FpFieldElem}},
len::Int, prec::Int, val::Int)
z = new()
ccall((:fmpz_mod_poly_init2, libflint), Nothing,
(Ref{FpRelPowerSeriesRingElem}, Int, Ref{fmpz_mod_ctx_struct}),
z, len, p)
for i = 1:len
ccall((:fmpz_mod_poly_set_coeff_fmpz, libflint), Nothing,
(Ref{FpRelPowerSeriesRingElem}, Int, Ref{ZZRingElem},
Ref{fmpz_mod_ctx_struct}),
z, i - 1, a[i], p)
end
z.prec = prec
z.val = val
finalizer(_gfp_fmpz_rel_series_clear_fn, z)
return z
end

function FpRelPowerSeriesRingElem(R::FpField, a::Vector{ZZRingElem},
len::Int, prec::Int, val::Int)
return FpRelPowerSeriesRingElem(R.ninv, a, len, prec, val)
end

function FpRelPowerSeriesRingElem(p::fmpz_mod_ctx_struct, a::Vector{FpFieldElem},
len::Int, prec::Int, val::Int)
z = new()
ccall((:fmpz_mod_poly_init2, libflint), Nothing,
(Ref{FpRelPowerSeriesRingElem}, Int, Ref{fmpz_mod_ctx_struct}),
z, len, p)
for i = 1:len
ccall((:fmpz_mod_poly_set_coeff_fmpz, libflint), Nothing,
(Ref{FpRelPowerSeriesRingElem}, Int, Ref{ZZRingElem},
Ref{fmpz_mod_ctx_struct}),
z, i - 1, data(a[i]), p)
for i in 1:len
setcoeff!(z, i-1, a[i])
end
z.prec = prec
z.val = val
finalizer(_gfp_fmpz_rel_series_clear_fn, z)
return z
end

function FpRelPowerSeriesRingElem(R::FpField, a::Vector{FpFieldElem},
function FpRelPowerSeriesRingElem(R::FpField, a::Vector{<:Union{Integer,ZZRingElem,FpFieldElem}},
len::Int, prec::Int, val::Int)
return FpRelPowerSeriesRingElem(R.ninv, a, len, prec, val)
end
Expand Down Expand Up @@ -3497,48 +3431,22 @@ mutable struct ZZModRelPowerSeriesRingElem <: RelPowerSeriesRingElem{ZZModRingEl
return ZZModRelPowerSeriesRingElem(R.ninv)
end

function ZZModRelPowerSeriesRingElem(p::fmpz_mod_ctx_struct, a::Vector{ZZRingElem},
len::Int, prec::Int, val::Int)
z = new()
ccall((:fmpz_mod_poly_init2, libflint), Nothing,
(Ref{ZZModRelPowerSeriesRingElem}, Int, Ref{fmpz_mod_ctx_struct}),
z, len, p)
for i = 1:len
ccall((:fmpz_mod_poly_set_coeff_fmpz, libflint), Nothing,
(Ref{ZZModRelPowerSeriesRingElem}, Int, Ref{ZZRingElem},
Ref{fmpz_mod_ctx_struct}),
z, i - 1, a[i], p)
end
z.prec = prec
z.val = val
finalizer(_fmpz_mod_rel_series_clear_fn, z)
return z
end

function ZZModRelPowerSeriesRingElem(R::ZZModRing, a::Vector{ZZRingElem},
len::Int, prec::Int, val::Int)
return ZZModRelPowerSeriesRingElem(R.ninv, a, len, prec, val)
end

function ZZModRelPowerSeriesRingElem(p::fmpz_mod_ctx_struct, a::Vector{ZZModRingElem},
function ZZModRelPowerSeriesRingElem(p::fmpz_mod_ctx_struct, a::Vector{<:Union{Integer,ZZRingElem,ZZModRingElem}},
len::Int, prec::Int, val::Int)
z = new()
ccall((:fmpz_mod_poly_init2, libflint), Nothing,
(Ref{ZZModRelPowerSeriesRingElem}, Int, Ref{fmpz_mod_ctx_struct}),
z, len, p)
for i = 1:len
ccall((:fmpz_mod_poly_set_coeff_fmpz, libflint), Nothing,
(Ref{ZZModRelPowerSeriesRingElem}, Int, Ref{ZZRingElem},
Ref{fmpz_mod_ctx_struct}),
z, i - 1, data(a[i]), p)
for i in 1:len
setcoeff!(z, i-1, a[i])
end
z.prec = prec
z.val = val
finalizer(_fmpz_mod_rel_series_clear_fn, z)
return z
end

function ZZModRelPowerSeriesRingElem(R::ZZModRing, a::Vector{ZZModRingElem},
function ZZModRelPowerSeriesRingElem(R::ZZModRing, a::Vector{<:Union{Integer,ZZRingElem,ZZModRingElem}},
len::Int, prec::Int, val::Int)
return ZZModRelPowerSeriesRingElem(R.ninv, a, len, prec, val)
end
Expand Down Expand Up @@ -3611,45 +3519,21 @@ mutable struct FpAbsPowerSeriesRingElem <: AbsPowerSeriesRingElem{FpFieldElem}
return FpAbsPowerSeriesRingElem(R.ninv)
end

function FpAbsPowerSeriesRingElem(p::fmpz_mod_ctx_struct, a::Vector{ZZRingElem},
len::Int, prec::Int)
z = new()
ccall((:fmpz_mod_poly_init2, libflint), Nothing,
(Ref{FpAbsPowerSeriesRingElem}, Int, Ref{fmpz_mod_ctx_struct}),
z, len, p)
for i = 1:len
ccall((:fmpz_mod_poly_set_coeff_fmpz, libflint), Nothing,
(Ref{FpAbsPowerSeriesRingElem}, Int, Ref{ZZRingElem},
Ref{fmpz_mod_ctx_struct}),
z, i - 1, a[i], p)
end
z.prec = prec
finalizer(_gfp_fmpz_abs_series_clear_fn, z)
return z
end

function FpAbsPowerSeriesRingElem(R::FpField, a::Vector{ZZRingElem}, len::Int, prec::Int)
return FpAbsPowerSeriesRingElem(R.ninv, a, len, prec)
end

function FpAbsPowerSeriesRingElem(p::fmpz_mod_ctx_struct, a::Vector{FpFieldElem},
function FpAbsPowerSeriesRingElem(p::fmpz_mod_ctx_struct, a::Vector{<:Union{Integer,ZZRingElem,FpFieldElem}},
len::Int, prec::Int)
z = new()
ccall((:fmpz_mod_poly_init2, libflint), Nothing,
(Ref{FpAbsPowerSeriesRingElem}, Int, Ref{fmpz_mod_ctx_struct}),
z, len, p)
for i = 1:len
ccall((:fmpz_mod_poly_set_coeff_fmpz, libflint), Nothing,
(Ref{FpAbsPowerSeriesRingElem}, Int, Ref{ZZRingElem}, Ref{fmpz_mod_ctx_struct}),
z, i - 1, data(a[i]), p)
for i in 1:len
setcoeff!(z, i-1, a[i])
end
z.prec = prec
finalizer(_gfp_fmpz_abs_series_clear_fn, z)
return z
end

function FpAbsPowerSeriesRingElem(R::FpField, a::Vector{FpFieldElem},
len::Int, prec::Int)
function FpAbsPowerSeriesRingElem(R::FpField, a::Vector{<:Union{Integer,ZZRingElem,FpFieldElem}}, len::Int, prec::Int)
return FpAbsPowerSeriesRingElem(R.ninv, a, len, prec)
end

Expand Down Expand Up @@ -3863,45 +3747,21 @@ mutable struct ZZModAbsPowerSeriesRingElem <: AbsPowerSeriesRingElem{ZZModRingEl
return ZZModAbsPowerSeriesRingElem(R.ninv)
end

function ZZModAbsPowerSeriesRingElem(p::fmpz_mod_ctx_struct, a::Vector{ZZRingElem},
function ZZModAbsPowerSeriesRingElem(p::fmpz_mod_ctx_struct, a::Vector{<:Union{Integer,ZZRingElem,ZZModRingElem}},
len::Int, prec::Int)
z = new()
ccall((:fmpz_mod_poly_init2, libflint), Nothing,
(Ref{ZZModAbsPowerSeriesRingElem}, Int, Ref{fmpz_mod_ctx_struct}),
z, len, p)
for i = 1:len
ccall((:fmpz_mod_poly_set_coeff_fmpz, libflint), Nothing,
(Ref{ZZModAbsPowerSeriesRingElem}, Int, Ref{ZZRingElem},
Ref{fmpz_mod_ctx_struct}),
z, i - 1, a[i], p)
end
z.prec = prec
finalizer(_fmpz_mod_abs_series_clear_fn, z)
return z
end

function ZZModAbsPowerSeriesRingElem(R::ZZModRing, a::Vector{ZZRingElem}, len::Int, prec::Int)
return ZZModAbsPowerSeriesRingElem(R.ninv, a, len, prec)
end

function ZZModAbsPowerSeriesRingElem(p::fmpz_mod_ctx_struct, a::Vector{ZZModRingElem},
len::Int, prec::Int)
z = new()
ccall((:fmpz_mod_poly_init2, libflint), Nothing,
(Ref{ZZModAbsPowerSeriesRingElem}, Int, Ref{fmpz_mod_ctx_struct}),
z, len, p)
for i = 1:len
ccall((:fmpz_mod_poly_set_coeff_fmpz, libflint), Nothing,
(Ref{ZZModAbsPowerSeriesRingElem}, Int, Ref{ZZRingElem}, Ref{fmpz_mod_ctx_struct}),
z, i - 1, data(a[i]), p)
for i in 1:len
setcoeff!(z, i-1, a[i])
end
z.prec = prec
finalizer(_fmpz_mod_abs_series_clear_fn, z)
return z
end

function ZZModAbsPowerSeriesRingElem(R::ZZModRing, a::Vector{ZZModRingElem},
len::Int, prec::Int)
function ZZModAbsPowerSeriesRingElem(R::ZZModRing, a::Vector{<:Union{Integer,ZZRingElem,ZZModRingElem}}, len::Int, prec::Int)
return ZZModAbsPowerSeriesRingElem(R.ninv, a, len, prec)
end

Expand Down
21 changes: 14 additions & 7 deletions src/flint/fmpz_mod_abs_series.jl
Original file line number Diff line number Diff line change
Expand Up @@ -574,16 +574,23 @@ for (etype, rtype, ctype, mtype, brtype, flint_fn) in (
return nothing
end

function setcoeff!(z::($etype), n::Int, x::ZZRingElem)
ccall(($(flint_fn*"_set_coeff_fmpz"), libflint), Nothing,
(Ref{($etype)}, Int, Ref{ZZRingElem}, Ref{($ctype)}),
z, n, x, z.parent.base_ring.ninv)
function setcoeff!(z::($etype), n::Int, x::ZZRingElemOrPtr)
@ccall libflint.fmpz_mod_poly_set_coeff_fmpz(z::Ref{($etype)}, n::Int, x::Ref{ZZRingElem}, base_ring(z).ninv::Ref{($ctype)})::Nothing
return z
end

function setcoeff!(z::($etype), n::Int, x::$(mtype))
return setcoeff!(z, n, data(x))

function setcoeff!(z::($etype), n::Int, x::UInt)
@ccall libflint.fmpz_mod_poly_set_coeff_ui(z::Ref{($etype)}, n::Int, x::UInt, base_ring(z).ninv::Ref{($ctype)})::Nothing
return z
end

function setcoeff!(z::($etype), n::Int, x::Int)
@ccall libflint.fmpz_mod_poly_set_coeff_si(z::Ref{($etype)}, n::Int, x::UInt, base_ring(z).ninv::Ref{($ctype)})::Nothing
end

setcoeff!(z::($etype), n::Int, x::Integer) = setcoeff!(z, n, flintify(x))

setcoeff!(z::($etype), n::Int, x::($mtype)) = setcoeff!(z, n, data(x))

function mul!(z::($etype), a::($etype), b::($etype))
lena = length(a)
Expand Down
31 changes: 14 additions & 17 deletions src/flint/fmpz_mod_poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -891,30 +891,27 @@ function fit!(x::T, n::Int) where {T <: Zmodn_fmpz_poly}
return nothing
end

function setcoeff!(x::T, n::Int, y::UInt) where {T <: Zmodn_fmpz_poly}
ccall((:fmpz_mod_poly_set_coeff_ui, libflint), Nothing,
(Ref{T}, Int, UInt, Ref{fmpz_mod_ctx_struct}),
x, n, y, x.parent.base_ring.ninv)
return x
#

function setcoeff!(z::T, n::Int, x::ZZRingElemOrPtr) where T <: Zmodn_fmpz_poly
@ccall libflint.fmpz_mod_poly_set_coeff_fmpz(z::Ref{T}, n::Int, x::Ref{ZZRingElem}, base_ring(z).ninv::Ref{fmpz_mod_ctx_struct})::Nothing
return z
end

function setcoeff!(x::T, n::Int, y::Int) where {T <: Zmodn_fmpz_poly}
ccall((:fmpz_mod_poly_set_coeff_si, libflint), Nothing,
(Ref{T}, Int, UInt, Ref{fmpz_mod_ctx_struct}),
x, n, y, x.parent.base_ring.ninv)
return x
function setcoeff!(z::T, n::Int, x::UInt) where T <: Zmodn_fmpz_poly
@ccall libflint.fmpz_mod_poly_set_coeff_ui(z::Ref{T}, n::Int, x::UInt, base_ring(z).ninv::Ref{fmpz_mod_ctx_struct})::Nothing
return z
end

function setcoeff!(x::T, n::Int, y::ZZRingElem) where {T <: Zmodn_fmpz_poly}
ccall((:fmpz_mod_poly_set_coeff_fmpz, libflint), Nothing,
(Ref{T}, Int, Ref{ZZRingElem}, Ref{fmpz_mod_ctx_struct}),
x, n, y, x.parent.base_ring.ninv)
return x
function setcoeff!(z::T, n::Int, x::Int) where T <: Zmodn_fmpz_poly
@ccall libflint.fmpz_mod_poly_set_coeff_si(z::Ref{T}, n::Int, x::UInt, base_ring(z).ninv::Ref{fmpz_mod_ctx_struct})::Nothing
end

setcoeff!(x::T, n::Int, y::Integer) where {T <: Zmodn_fmpz_poly} = setcoeff!(x, n, ZZRingElem(y))
setcoeff!(z::T, n::Int, x::Integer) where T <: Zmodn_fmpz_poly = setcoeff!(z, n, flintify(x))

setcoeff!(x::ZZModPolyRingElem, n::Int, y::ZZModRingElem) = setcoeff!(x, n, y.data)
setcoeff!(z::ZZModPolyRingElem, n::Int, x::ZZModRingElem) = setcoeff!(z, n, data(x))

#

function add!(z::T, x::T, y::T) where {T <: Zmodn_fmpz_poly}
ccall((:fmpz_mod_poly_add, libflint), Nothing,
Expand Down
Loading

0 comments on commit d2be9c1

Please sign in to comment.