Skip to content

Commit 8181398

Browse files
committed
Unify constructions of nmod_poly using setcoeff!
1 parent 633112d commit 8181398

File tree

5 files changed

+55
-214
lines changed

5 files changed

+55
-214
lines changed

src/flint/FlintTypes.jl

Lines changed: 17 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -573,35 +573,13 @@ mutable struct zzModPolyRingElem <: PolyRingElem{zzModRingElem}
573573
return zzModPolyRingElem(n, mod(a, n) % UInt)
574574
end
575575

576-
function zzModPolyRingElem(n::UInt, arr::Vector{ZZRingElem})
576+
function zzModPolyRingElem(n::UInt, a::Vector{<:Union{Integer,ZZRingElem,zzModRingElem}})
577577
z = new()
578578
ccall((:nmod_poly_init2, libflint), Nothing,
579-
(Ref{zzModPolyRingElem}, UInt, Int), z, n, length(arr))
579+
(Ref{zzModPolyRingElem}, UInt, Int), z, n, length(a))
580580
finalizer(_nmod_poly_clear_fn, z)
581-
for i in 1:length(arr)
582-
setcoeff!(z, i - 1, arr[i])
583-
end
584-
return z
585-
end
586-
587-
function zzModPolyRingElem(n::UInt, arr::Vector{UInt})
588-
z = new()
589-
ccall((:nmod_poly_init2, libflint), Nothing,
590-
(Ref{zzModPolyRingElem}, UInt, Int), z, n, length(arr))
591-
finalizer(_nmod_poly_clear_fn, z)
592-
for i in 1:length(arr)
593-
setcoeff!(z, i - 1, arr[i])
594-
end
595-
return z
596-
end
597-
598-
function zzModPolyRingElem(n::UInt, arr::Vector{zzModRingElem})
599-
z = new()
600-
ccall((:nmod_poly_init2, libflint), Nothing,
601-
(Ref{zzModPolyRingElem}, UInt, Int), z, n, length(arr))
602-
finalizer(_nmod_poly_clear_fn, z)
603-
for i in 1:length(arr)
604-
setcoeff!(z, i-1, arr[i].data)
581+
for i in 1:length(a)
582+
setcoeff!(z, i - 1, a[i])
605583
end
606584
return z
607585
end
@@ -700,18 +678,7 @@ mutable struct fpPolyRingElem <: PolyRingElem{fpFieldElem}
700678
return fpPolyRingElem(n, mod(a, n) % UInt)
701679
end
702680

703-
function fpPolyRingElem(n::UInt, arr::Vector{ZZRingElem})
704-
z = new()
705-
ccall((:nmod_poly_init2, libflint), Nothing,
706-
(Ref{fpPolyRingElem}, UInt, Int), z, n, length(arr))
707-
finalizer(_gfp_poly_clear_fn, z)
708-
for i in 1:length(arr)
709-
setcoeff!(z, i - 1, arr[i])
710-
end
711-
return z
712-
end
713-
714-
function fpPolyRingElem(n::UInt, arr::Vector{UInt})
681+
function fpPolyRingElem(n::UInt, arr::Vector{<:Union{Integer,ZZRingElem,fpFieldElem}})
715682
z = new()
716683
ccall((:nmod_poly_init2, libflint), Nothing,
717684
(Ref{fpPolyRingElem}, UInt, Int), z, n, length(arr))
@@ -722,17 +689,6 @@ mutable struct fpPolyRingElem <: PolyRingElem{fpFieldElem}
722689
return z
723690
end
724691

725-
function fpPolyRingElem(n::UInt, arr::Vector{fpFieldElem})
726-
z = new()
727-
ccall((:nmod_poly_init2, libflint), Nothing,
728-
(Ref{fpPolyRingElem}, UInt, Int), z, n, length(arr))
729-
finalizer(_gfp_poly_clear_fn, z)
730-
for i in 1:length(arr)
731-
setcoeff!(z, i-1, arr[i].data)
732-
end
733-
return z
734-
end
735-
736692
function fpPolyRingElem(n::UInt, f::ZZPolyRingElem)
737693
z = new()
738694
ccall((:nmod_poly_init2, libflint), Nothing,
@@ -3282,43 +3238,12 @@ mutable struct fpRelPowerSeriesRingElem <: RelPowerSeriesRingElem{fpFieldElem}
32823238
return z
32833239
end
32843240

3285-
function fpRelPowerSeriesRingElem(p::UInt, a::Vector{ZZRingElem}, len::Int, prec::Int, val::Int)
3286-
z = new()
3287-
ccall((:nmod_poly_init2, libflint), Nothing,
3288-
(Ref{fpRelPowerSeriesRingElem}, UInt, Int), z, p, len)
3289-
for i = 1:len
3290-
tt = ccall((:fmpz_fdiv_ui, libflint), UInt,
3291-
(Ref{ZZRingElem}, UInt), a[i], p)
3292-
ccall((:nmod_poly_set_coeff_ui, libflint), Nothing,
3293-
(Ref{fpRelPowerSeriesRingElem}, Int, UInt), z, i - 1, tt)
3294-
end
3295-
z.prec = prec
3296-
z.val = val
3297-
finalizer(_gfp_rel_series_clear_fn, z)
3298-
return z
3299-
end
3300-
3301-
function fpRelPowerSeriesRingElem(p::UInt, a::Vector{UInt}, len::Int, prec::Int, val::Int)
3302-
z = new()
3303-
ccall((:nmod_poly_init2, libflint), Nothing,
3304-
(Ref{fpRelPowerSeriesRingElem}, UInt, Int), z, p, len)
3305-
for i = 1:len
3306-
ccall((:nmod_poly_set_coeff_ui, libflint), Nothing,
3307-
(Ref{fpRelPowerSeriesRingElem}, Int, UInt), z, i - 1, a[i])
3308-
end
3309-
z.prec = prec
3310-
z.val = val
3311-
finalizer(_gfp_rel_series_clear_fn, z)
3312-
return z
3313-
end
3314-
3315-
function fpRelPowerSeriesRingElem(p::UInt, a::Vector{fpFieldElem}, len::Int, prec::Int, val::Int)
3241+
function fpRelPowerSeriesRingElem(p::UInt, a::Vector{<:Union{Integer,ZZRingElem,fpFieldElem}}, len::Int, prec::Int, val::Int)
33163242
z = new()
33173243
ccall((:nmod_poly_init2, libflint), Nothing,
33183244
(Ref{fpRelPowerSeriesRingElem}, UInt, Int), z, p, len)
3319-
for i = 1:len
3320-
ccall((:nmod_poly_set_coeff_ui, libflint), Nothing,
3321-
(Ref{fpRelPowerSeriesRingElem}, Int, UInt), z, i - 1, data(a[i]))
3245+
for i in 1:len
3246+
setcoeff!(z, i-1, a[i])
33223247
end
33233248
z.prec = prec
33243249
z.val = val
@@ -3383,42 +3308,12 @@ mutable struct zzModRelPowerSeriesRingElem <: RelPowerSeriesRingElem{zzModRingEl
33833308
return z
33843309
end
33853310

3386-
function zzModRelPowerSeriesRingElem(p::UInt, a::Vector{ZZRingElem}, len::Int, prec::Int, val::Int)
3387-
z = new()
3388-
ccall((:nmod_poly_init2, libflint), Nothing,
3389-
(Ref{zzModRelPowerSeriesRingElem}, UInt, Int), z, p, len)
3390-
for i = 1:len
3391-
tt = ccall((:fmpz_fdiv_ui, libflint), UInt, (Ref{ZZRingElem}, UInt), a[i], p)
3392-
ccall((:nmod_poly_set_coeff_ui, libflint), Nothing,
3393-
(Ref{zzModRelPowerSeriesRingElem}, Int, UInt), z, i - 1, tt)
3394-
end
3395-
z.prec = prec
3396-
z.val = val
3397-
finalizer(_nmod_rel_series_clear_fn, z)
3398-
return z
3399-
end
3400-
3401-
function zzModRelPowerSeriesRingElem(p::UInt, a::Vector{UInt}, len::Int, prec::Int, val::Int)
3311+
function zzModRelPowerSeriesRingElem(p::UInt, a::Vector{<:Union{Integer,ZZRingElem,zzModRingElem}}, len::Int, prec::Int, val::Int)
34023312
z = new()
34033313
ccall((:nmod_poly_init2, libflint), Nothing,
34043314
(Ref{zzModRelPowerSeriesRingElem}, UInt, Int), z, p, len)
3405-
for i = 1:len
3406-
ccall((:nmod_poly_set_coeff_ui, libflint), Nothing,
3407-
(Ref{zzModRelPowerSeriesRingElem}, Int, UInt), z, i - 1, a[i])
3408-
end
3409-
z.prec = prec
3410-
z.val = val
3411-
finalizer(_nmod_rel_series_clear_fn, z)
3412-
return z
3413-
end
3414-
3415-
function zzModRelPowerSeriesRingElem(p::UInt, a::Vector{zzModRingElem}, len::Int, prec::Int, val::Int)
3416-
z = new()
3417-
ccall((:nmod_poly_init2, libflint), Nothing,
3418-
(Ref{zzModRelPowerSeriesRingElem}, UInt, Int), z, p, len)
3419-
for i = 1:len
3420-
ccall((:nmod_poly_set_coeff_ui, libflint), Nothing,
3421-
(Ref{zzModRelPowerSeriesRingElem}, Int, UInt), z, i - 1, data(a[i]))
3315+
for i in 1:len
3316+
setcoeff!(z, i-1, a[i])
34223317
end
34233318
z.prec = prec
34243319
z.val = val
@@ -3822,40 +3717,12 @@ mutable struct zzModAbsPowerSeriesRingElem <: AbsPowerSeriesRingElem{zzModRingEl
38223717
return z
38233718
end
38243719

3825-
function zzModAbsPowerSeriesRingElem(n::UInt, arr::Vector{ZZRingElem}, len::Int, prec::Int)
3826-
z = new()
3827-
ccall((:nmod_poly_init2, libflint), Nothing,
3828-
(Ref{zzModAbsPowerSeriesRingElem}, UInt, Int), z, n, length(arr))
3829-
for i in 1:len
3830-
tt = ccall((:fmpz_fdiv_ui, libflint), UInt, (Ref{ZZRingElem}, UInt), arr[i], n)
3831-
ccall((:nmod_poly_set_coeff_ui, libflint), Nothing,
3832-
(Ref{zzModAbsPowerSeriesRingElem}, Int, UInt), z, i - 1, tt)
3833-
end
3834-
z.prec = prec
3835-
finalizer(_nmod_abs_series_clear_fn, z)
3836-
return z
3837-
end
3838-
3839-
function zzModAbsPowerSeriesRingElem(n::UInt, arr::Vector{UInt}, len::Int, prec::Int)
3840-
z = new()
3841-
ccall((:nmod_poly_init2, libflint), Nothing,
3842-
(Ref{zzModAbsPowerSeriesRingElem}, UInt, Int), z, n, length(arr))
3843-
for i in 1:len
3844-
ccall((:nmod_poly_set_coeff_ui, libflint), Nothing,
3845-
(Ref{zzModAbsPowerSeriesRingElem}, Int, UInt), z, i - 1, arr[i])
3846-
end
3847-
z.prec = prec
3848-
finalizer(_nmod_abs_series_clear_fn, z)
3849-
return z
3850-
end
3851-
3852-
function zzModAbsPowerSeriesRingElem(n::UInt, arr::Vector{zzModRingElem}, len::Int, prec::Int)
3720+
function zzModAbsPowerSeriesRingElem(n::UInt, a::Vector{<:Union{Integer,ZZRingElem,zzModRingElem}}, len::Int, prec::Int)
38533721
z = new()
38543722
ccall((:nmod_poly_init2, libflint), Nothing,
3855-
(Ref{zzModAbsPowerSeriesRingElem}, UInt, Int), z, n, length(arr))
3723+
(Ref{zzModAbsPowerSeriesRingElem}, UInt, Int), z, n, length(a))
38563724
for i in 1:len
3857-
ccall((:nmod_poly_set_coeff_ui, libflint), Nothing,
3858-
(Ref{zzModAbsPowerSeriesRingElem}, Int, UInt), z, i-1, arr[i].data)
3725+
setcoeff!(z, i-1, a[i])
38593726
end
38603727
z.prec = prec
38613728
finalizer(_nmod_abs_series_clear_fn, z)
@@ -3923,40 +3790,12 @@ mutable struct fpAbsPowerSeriesRingElem <: AbsPowerSeriesRingElem{fpFieldElem}
39233790
return z
39243791
end
39253792

3926-
function fpAbsPowerSeriesRingElem(n::UInt, arr::Vector{ZZRingElem}, len::Int, prec::Int)
3927-
z = new()
3928-
ccall((:nmod_poly_init2, libflint), Nothing,
3929-
(Ref{fpAbsPowerSeriesRingElem}, UInt, Int), z, n, length(arr))
3930-
for i in 1:len
3931-
tt = ccall((:fmpz_fdiv_ui, libflint), UInt, (Ref{ZZRingElem}, UInt), arr[i], n)
3932-
ccall((:nmod_poly_set_coeff_ui, libflint), Nothing,
3933-
(Ref{fpAbsPowerSeriesRingElem}, Int, UInt), z, i - 1, tt)
3934-
end
3935-
z.prec = prec
3936-
finalizer(_gfp_abs_series_clear_fn, z)
3937-
return z
3938-
end
3939-
3940-
function fpAbsPowerSeriesRingElem(n::UInt, arr::Vector{UInt}, len::Int, prec::Int)
3941-
z = new()
3942-
ccall((:nmod_poly_init2, libflint), Nothing,
3943-
(Ref{fpAbsPowerSeriesRingElem}, UInt, Int), z, n, length(arr))
3944-
for i in 1:len
3945-
ccall((:nmod_poly_series_set_coeff_ui, libflint), Nothing,
3946-
(Ref{fpAbsPowerSeriesRingElem}, Int, UInt), z, i - 1, arr[i])
3947-
end
3948-
z.prec = prec
3949-
finalizer(_gfp_abs_series_clear_fn, z)
3950-
return z
3951-
end
3952-
3953-
function fpAbsPowerSeriesRingElem(n::UInt, arr::Vector{fpFieldElem}, len::Int, prec::Int)
3793+
function fpAbsPowerSeriesRingElem(n::UInt, a::Vector{<:Union{Integer,ZZRingElem,fpFieldElem}}, len::Int, prec::Int)
39543794
z = new()
39553795
ccall((:nmod_poly_init2, libflint), Nothing,
3956-
(Ref{fpAbsPowerSeriesRingElem}, UInt, Int), z, n, length(arr))
3796+
(Ref{fpAbsPowerSeriesRingElem}, UInt, Int), z, n, length(a))
39573797
for i in 1:len
3958-
ccall((:nmod_poly_set_coeff_ui, libflint), Nothing,
3959-
(Ref{fpAbsPowerSeriesRingElem}, Int, UInt), z, i-1, arr[i].data)
3798+
setcoeff!(z, i-1, a[i])
39603799
end
39613800
z.prec = prec
39623801
finalizer(_gfp_abs_series_clear_fn, z)

src/flint/gfp_poly.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ end
511511
#
512512
################################################################################
513513

514-
setcoeff!(x::fpPolyRingElem, n::Int, y::fpFieldElem) = setcoeff!(x, n, y.data)
514+
setcoeff!(z::fpPolyRingElem, n::Int, x::fpFieldElem) = setcoeff!(z, n, x.data)
515515

516516
################################################################################
517517
#

src/flint/nmod_abs_series.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -524,17 +524,24 @@ for (etype, rtype, mtype, brtype, flint_fn) in (
524524
return nothing
525525
end
526526

527-
function setcoeff!(z::($etype), n::Int, x::($mtype))
528-
ccall(($(flint_fn*"_set_coeff_ui"), libflint), Nothing,
529-
(Ref{($etype)}, Int, UInt), z, n, x.data)
527+
function setcoeff!(z::($etype), n::Int, x::UInt)
528+
@ccall libflint.nmod_poly_set_coeff_ui(z::Ref{($etype)}, n::Int, x::UInt)::Nothing
530529
return z
531530
end
532531

532+
function setcoeff!(z::($etype), n::Int, x::Int)
533+
return setcoeff!(z, n, mod(x, modulus(z)))
534+
end
535+
533536
function setcoeff!(z::($etype), n::Int, x::ZZRingElem)
534-
R = base_ring(z)
535-
return setcoeff!(z, n, R(x))
537+
xx = ccall((:fmpz_fdiv_ui, libflint), UInt, (Ref{ZZRingElem}, UInt), x, modulus(z))
538+
return setcoeff!(z, n, xx)
536539
end
537540

541+
setcoeff!(z::($etype), n::Int, x::Integer) = setcoeff!(z, n, flintify(x))
542+
543+
setcoeff!(z::($etype), n::Int, x::($mtype)) = setcoeff!(z, n, data(x))
544+
538545
function mul!(z::($etype), a::($etype), b::($etype))
539546
lena = length(a)
540547
lenb = length(b)

src/flint/nmod_poly.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -901,27 +901,27 @@ function fit!(x::T, n::Int) where T <: Zmodn_poly
901901
return nothing
902902
end
903903

904-
function setcoeff!(x::T, n::Int, y::UInt) where T <: Zmodn_poly
905-
ccall((:nmod_poly_set_coeff_ui, libflint), Nothing,
906-
(Ref{T}, Int, UInt), x, n, y)
907-
return x
904+
#
905+
906+
function setcoeff!(z::T, n::Int, x::UInt) where T <: Zmodn_poly
907+
@ccall libflint.nmod_poly_set_coeff_ui(z::Ref{T}, n::Int, x::UInt)::Nothing
908+
return z
908909
end
909910

910-
function setcoeff!(x::T, n::Int, y::Int) where T <: Zmodn_poly
911-
ccall((:nmod_poly_set_coeff_ui, libflint), Nothing,
912-
(Ref{T}, Int, UInt), x, n, mod(y, x.mod_n))
913-
return x
911+
function setcoeff!(z::T, n::Int, x::Int) where T <: Zmodn_poly
912+
return setcoeff!(z, n, mod(x, modulus(z)))
914913
end
915914

916-
function setcoeff!(x::T, n::Int, y::ZZRingElem) where T <: Zmodn_poly
917-
r = ccall((:fmpz_fdiv_ui, libflint), UInt, (Ref{ZZRingElem}, UInt), y, x.mod_n)
918-
setcoeff!(x, n, r)
919-
return x
915+
function setcoeff!(z::T, n::Int, x::ZZRingElem) where T <: Zmodn_poly
916+
xx = ccall((:fmpz_fdiv_ui, libflint), UInt, (Ref{ZZRingElem}, UInt), x, modulus(z))
917+
return setcoeff!(z, n, xx)
920918
end
921919

922-
setcoeff!(x::T, n::Int, y::Integer) where T <: Zmodn_poly = setcoeff!(x, n, ZZRingElem(y))
920+
setcoeff!(z::T, n::Int, x::Integer) where T <: Zmodn_poly = setcoeff!(z, n, flintify(x))
923921

924-
setcoeff!(x::zzModPolyRingElem, n::Int, y::zzModRingElem) = setcoeff!(x, n, y.data)
922+
setcoeff!(z::zzModPolyRingElem, n::Int, x::zzModRingElem) = setcoeff!(z, n, data(x))
923+
924+
#
925925

926926
function add!(z::T, x::T, y::T) where T <: Zmodn_poly
927927
ccall((:nmod_poly_add, libflint), Nothing,

src/flint/nmod_rel_series.jl

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -710,29 +710,24 @@ for (etype, rtype, mtype, brtype, flint_fn) in (
710710
return nothing
711711
end
712712

713-
function setcoeff!(z::($etype), n::Int, x::ZZRingElem)
714-
r = ccall((:fmpz_fdiv_ui, libflint), UInt, (Ref{ZZRingElem}, UInt), x, modulus(z))
715-
ccall(($(flint_fn*"_set_coeff_ui"), libflint), Nothing,
716-
(Ref{($etype)}, Int, UInt),
717-
z, n, r)
713+
function setcoeff!(z::($etype), n::Int, x::UInt)
714+
@ccall libflint.nmod_poly_set_coeff_ui(z::Ref{($etype)}, n::Int, x::UInt)::Nothing
718715
return z
719716
end
720717

721-
function setcoeff!(z::($etype), n::Int, x::UInt)
722-
r = mod(x, modulus(z))
723-
ccall(($(flint_fn*"_set_coeff_ui"), libflint), Nothing,
724-
(Ref{($etype)}, Int, UInt),
725-
z, n, r)
726-
return z
718+
function setcoeff!(z::($etype), n::Int, x::Int)
719+
return setcoeff!(z, n, mod(x, modulus(z)))
727720
end
728721

729-
function setcoeff!(z::($etype), n::Int, x::($mtype))
730-
ccall(($(flint_fn*"_set_coeff_ui"), libflint), Nothing,
731-
(Ref{($etype)}, Int, UInt),
732-
z, n, data(x))
733-
return z
722+
function setcoeff!(z::($etype), n::Int, x::ZZRingElem)
723+
xx = ccall((:fmpz_fdiv_ui, libflint), UInt, (Ref{ZZRingElem}, UInt), x, modulus(z))
724+
return setcoeff!(z, n, xx)
734725
end
735726

727+
setcoeff!(z::($etype), n::Int, x::Integer) = setcoeff!(z, n, flintify(x))
728+
729+
setcoeff!(z::($etype), n::Int, x::($mtype)) = setcoeff!(z, n, data(x))
730+
736731
function mul!(z::($etype), a::($etype), b::($etype))
737732
lena = pol_length(a)
738733
lenb = pol_length(b)

0 commit comments

Comments
 (0)