diff --git a/src/flint/FlintTypes.jl b/src/flint/FlintTypes.jl index 4ab88eed4..9199983fc 100644 --- a/src/flint/FlintTypes.jl +++ b/src/flint/FlintTypes.jl @@ -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) @@ -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) @@ -3382,40 +3342,14 @@ 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 @@ -3423,7 +3357,7 @@ mutable struct FpRelPowerSeriesRingElem <: RelPowerSeriesRingElem{FpFieldElem} 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 @@ -3497,40 +3431,14 @@ 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 @@ -3538,7 +3446,7 @@ mutable struct ZZModRelPowerSeriesRingElem <: RelPowerSeriesRingElem{ZZModRingEl 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 @@ -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 @@ -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 diff --git a/src/flint/fmpz_mod_abs_series.jl b/src/flint/fmpz_mod_abs_series.jl index 569a7c32f..a42dce6d2 100644 --- a/src/flint/fmpz_mod_abs_series.jl +++ b/src/flint/fmpz_mod_abs_series.jl @@ -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) diff --git a/src/flint/fmpz_mod_poly.jl b/src/flint/fmpz_mod_poly.jl index ac6982c31..36ddd1296 100644 --- a/src/flint/fmpz_mod_poly.jl +++ b/src/flint/fmpz_mod_poly.jl @@ -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, diff --git a/src/flint/fmpz_mod_rel_series.jl b/src/flint/fmpz_mod_rel_series.jl index 07905aef7..c089ebebc 100644 --- a/src/flint/fmpz_mod_rel_series.jl +++ b/src/flint/fmpz_mod_rel_series.jl @@ -710,19 +710,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)) - ccall(($(flint_fn*"_set_coeff_fmpz"), libflint), Nothing, - (Ref{($etype)}, Int, Ref{ZZRingElem}, Ref{($ctype)}), - z, n, x.data, z.parent.base_ring.ninv) + + 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 = pol_length(a) diff --git a/src/flint/gfp_fmpz_poly.jl b/src/flint/gfp_fmpz_poly.jl index b3584a669..a926bc021 100644 --- a/src/flint/gfp_fmpz_poly.jl +++ b/src/flint/gfp_fmpz_poly.jl @@ -462,7 +462,7 @@ end # ################################################################################ -setcoeff!(x::FpPolyRingElem, n::Int, y::FpFieldElem) = setcoeff!(x, n, y.data) +setcoeff!(z::FpPolyRingElem, n::Int, x::FpFieldElem) = setcoeff!(z, n, data(x)) ################################################################################ #