Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more setcoeff! methods and use them to deduplicate PolyRingElem constructors #1911

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
378 changes: 59 additions & 319 deletions src/flint/FlintTypes.jl

Large diffs are not rendered by default.

19 changes: 14 additions & 5 deletions src/flint/fmpz_mod_abs_series.jl
Original file line number Diff line number Diff line change
Expand Up @@ -496,14 +496,23 @@
return nothing
end

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

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

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

Check warning on line 506 in src/flint/fmpz_mod_abs_series.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/fmpz_mod_abs_series.jl#L504-L506

Added lines #L504 - L506 were not covered by tests
end

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

Check warning on line 510 in src/flint/fmpz_mod_abs_series.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/fmpz_mod_abs_series.jl#L509-L510

Added lines #L509 - L510 were not covered by tests
end

setcoeff!(z::($etype), i::Int, x::Integer, ctx::($ctype)=base_ring(z).ninv) = setcoeff!(z, i, flintify(x), ctx)

Check warning on line 513 in src/flint/fmpz_mod_abs_series.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/fmpz_mod_abs_series.jl#L513

Added line #L513 was not covered by tests

setcoeff!(z::($etype), i::Int, x::($mtype), ctx::($ctype)=base_ring(z).ninv) = setcoeff!(z, i, data(x), ctx)

function mul!(z::($etype), a::($etype), b::($etype))
lena = length(a)
Expand Down
25 changes: 14 additions & 11 deletions src/flint/fmpz_mod_poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -777,24 +777,27 @@
return nothing
end

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

function setcoeff!(z::T, i::Int, x::ZZRingElemOrPtr, ctx::fmpz_mod_ctx_struct=base_ring(z).ninv) where T <: Zmodn_fmpz_poly
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should ctx be another (fourth) argument or a kwarg?

The advantage of a kwarg is that it helps understanding what's going on, esp. in the cases were ctx is another integer. If I see set!(z, 3, c, R) it is somewhat more opaque than set!(z, 3, c; ctx=R).

Alas, in the end, all of these are low-level unsafe helpers. I can happily live with either variant.

@ccall libflint.fmpz_mod_poly_set_coeff_fmpz(z::Ref{T}, i::Int, x::Ref{ZZRingElem}, ctx::Ref{fmpz_mod_ctx_struct})::Nothing
return z
end

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

function setcoeff!(x::T, n::Int, y::ZZRingElem) where {T <: Zmodn_fmpz_poly}
@ccall libflint.fmpz_mod_poly_set_coeff_fmpz(x::Ref{T}, n::Int, y::Ref{ZZRingElem}, x.parent.base_ring.ninv::Ref{fmpz_mod_ctx_struct})::Nothing
return x
function setcoeff!(z::T, i::Int, x::Int, ctx::fmpz_mod_ctx_struct=base_ring(z).ninv) where T <: Zmodn_fmpz_poly
@ccall libflint.fmpz_mod_poly_set_coeff_si(z::Ref{T}, i::Int, x::UInt, ctx::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, i::Int, x::Integer, ctx::fmpz_mod_ctx_struct=base_ring(z).ninv) where T <: Zmodn_fmpz_poly = setcoeff!(z, i, flintify(x), ctx)

Check warning on line 796 in src/flint/fmpz_mod_poly.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/fmpz_mod_poly.jl#L796

Added line #L796 was not covered by tests

setcoeff!(x::ZZModPolyRingElem, n::Int, y::ZZModRingElem) = setcoeff!(x, n, y.data)
setcoeff!(z::ZZModPolyRingElem, i::Int, x::ZZModRingElem, ctx::fmpz_mod_ctx_struct=base_ring(z).ninv) = setcoeff!(z, i, data(x), ctx)

#

function add!(z::T, x::T, y::T) where {T <: Zmodn_fmpz_poly}
@ccall libflint.fmpz_mod_poly_add(z::Ref{T}, x::Ref{T}, y::Ref{T}, x.parent.base_ring.ninv::Ref{fmpz_mod_ctx_struct})::Nothing
Expand Down
18 changes: 13 additions & 5 deletions src/flint/fmpz_mod_rel_series.jl
Original file line number Diff line number Diff line change
Expand Up @@ -599,15 +599,23 @@
return nothing
end

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

function setcoeff!(z::($etype), n::Int, x::($mtype))
@ccall libflint.fmpz_mod_poly_set_coeff_fmpz(z::Ref{($etype)}, n::Int, x.data::Ref{ZZRingElem}, z.parent.base_ring.ninv::Ref{($ctype)})::Nothing
function setcoeff!(z::($etype), i::Int, x::UInt, ctx::($ctype)=base_ring(z).ninv)
@ccall libflint.fmpz_mod_poly_set_coeff_ui(z::Ref{($etype)}, i::Int, x::UInt, ctx::Ref{($ctype)})::Nothing

Check warning on line 608 in src/flint/fmpz_mod_rel_series.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/fmpz_mod_rel_series.jl#L607-L608

Added lines #L607 - L608 were not covered by tests
return z
end

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

Check warning on line 613 in src/flint/fmpz_mod_rel_series.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/fmpz_mod_rel_series.jl#L612-L613

Added lines #L612 - L613 were not covered by tests
end

setcoeff!(z::($etype), i::Int, x::Integer, ctx::($ctype)=base_ring(z).ninv) = setcoeff!(z, i, flintify(x), ctx)

Check warning on line 616 in src/flint/fmpz_mod_rel_series.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/fmpz_mod_rel_series.jl#L616

Added line #L616 was not covered by tests

setcoeff!(z::($etype), i::Int, x::($mtype), ctx::($ctype)=base_ring(z).ninv) = setcoeff!(z, i, data(x), ctx)

function mul!(z::($etype), a::($etype), b::($etype))
lena = pol_length(a)
Expand Down
2 changes: 1 addition & 1 deletion src/flint/gfp_fmpz_poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ end
#
################################################################################

setcoeff!(x::FpPolyRingElem, n::Int, y::FpFieldElem) = setcoeff!(x, n, y.data)
setcoeff!(z::FpPolyRingElem, i::Int, x::FpFieldElem, ctx::fmpz_mod_ctx_struct=base_ring(z).ninv) = setcoeff!(z, i, data(x), ctx)

################################################################################
#
Expand Down
2 changes: 1 addition & 1 deletion src/flint/gfp_poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ end
#
################################################################################

setcoeff!(x::fpPolyRingElem, n::Int, y::fpFieldElem) = setcoeff!(x, n, y.data)
setcoeff!(z::fpPolyRingElem, i::Int, x::fpFieldElem, n::UInt=modulus(z)) = setcoeff!(z, i, data(x), n)

################################################################################
#
Expand Down
18 changes: 13 additions & 5 deletions src/flint/nmod_abs_series.jl
Original file line number Diff line number Diff line change
Expand Up @@ -484,16 +484,24 @@
return nothing
end

function setcoeff!(z::($etype), n::Int, x::($mtype))
@ccall libflint.nmod_poly_set_coeff_ui(z::Ref{($etype)}, n::Int, x.data::UInt)::Nothing
function setcoeff!(z::($etype), i::Int, x::UInt, n::UInt=modulus(z))
@ccall libflint.nmod_poly_set_coeff_ui(z::Ref{($etype)}, i::Int, x::UInt)::Nothing
return z
end

function setcoeff!(z::($etype), n::Int, x::ZZRingElem)
R = base_ring(z)
return setcoeff!(z, n, R(x))
function setcoeff!(z::($etype), i::Int, x::Int, n::UInt=modulus(z))
return setcoeff!(z, i, mod(x, n), n)

Check warning on line 493 in src/flint/nmod_abs_series.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/nmod_abs_series.jl#L492-L493

Added lines #L492 - L493 were not covered by tests
end

function setcoeff!(z::($etype), i::Int, x::ZZRingElem, n::UInt=modulus(z))
xx = @ccall libflint.fmpz_fdiv_ui(x::Ref{ZZRingElem}, n::UInt)::UInt
return setcoeff!(z, i, xx, n)
end

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

Check warning on line 501 in src/flint/nmod_abs_series.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/nmod_abs_series.jl#L501

Added line #L501 was not covered by tests

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

function mul!(z::($etype), a::($etype), b::($etype))
lena = length(a)
lenb = length(b)
Expand Down
26 changes: 14 additions & 12 deletions src/flint/nmod_poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -844,25 +844,27 @@ function fit!(x::T, n::Int) where T <: Zmodn_poly
return nothing
end

function setcoeff!(x::T, n::Int, y::UInt) where T <: Zmodn_poly
@ccall libflint.nmod_poly_set_coeff_ui(x::Ref{T}, n::Int, y::UInt)::Nothing
return x
#

function setcoeff!(z::T, i::Int, x::UInt, n::UInt=modulus(z)) where T <: Zmodn_poly
@ccall libflint.nmod_poly_set_coeff_ui(z::Ref{T}, i::Int, x::UInt)::Nothing
return z
end

function setcoeff!(x::T, n::Int, y::Int) where T <: Zmodn_poly
@ccall libflint.nmod_poly_set_coeff_ui(x::Ref{T}, n::Int, mod(y, x.mod_n)::UInt)::Nothing
return x
function setcoeff!(z::T, i::Int, x::Int, n::UInt=modulus(z)) where T <: Zmodn_poly
return setcoeff!(z, i, mod(x, n), n)
end

function setcoeff!(x::T, n::Int, y::ZZRingElem) where T <: Zmodn_poly
r = @ccall libflint.fmpz_fdiv_ui(y::Ref{ZZRingElem}, x.mod_n::UInt)::UInt
setcoeff!(x, n, r)
return x
function setcoeff!(z::T, i::Int, x::ZZRingElem, n::UInt=modulus(z)) where T <: Zmodn_poly
xx = ccall((:fmpz_fdiv_ui, libflint), UInt, (Ref{ZZRingElem}, UInt), x, n)
return setcoeff!(z, i, xx, n)
end

setcoeff!(x::T, n::Int, y::Integer) where T <: Zmodn_poly = setcoeff!(x, n, ZZRingElem(y))
setcoeff!(z::T, i::Int, x::Integer, n::UInt=modulus(z)) where T <: Zmodn_poly = setcoeff!(z, i, flintify(x), n)

setcoeff!(x::zzModPolyRingElem, n::Int, y::zzModRingElem) = setcoeff!(x, n, y.data)
setcoeff!(z::zzModPolyRingElem, i::Int, x::zzModRingElem, n::UInt=modulus(z)) = setcoeff!(z, i, data(x), n)

#

function add!(z::T, x::T, y::T) where T <: Zmodn_poly
@ccall libflint.nmod_poly_add(z::Ref{T}, x::Ref{T}, y::Ref{T})::Nothing
Expand Down
21 changes: 11 additions & 10 deletions src/flint/nmod_rel_series.jl
Original file line number Diff line number Diff line change
Expand Up @@ -641,23 +641,24 @@
return nothing
end

function setcoeff!(z::($etype), n::Int, x::ZZRingElem)
r = @ccall libflint.fmpz_fdiv_ui(x::Ref{ZZRingElem}, modulus(z)::UInt)::UInt
@ccall libflint.nmod_poly_set_coeff_ui(z::Ref{($etype)}, n::Int, r::UInt)::Nothing
function setcoeff!(z::($etype), i::Int, x::UInt, n::UInt=modulus(z))
@ccall libflint.nmod_poly_set_coeff_ui(z::Ref{($etype)}, i::Int, x::UInt)::Nothing
return z
end

function setcoeff!(z::($etype), n::Int, x::UInt)
r = mod(x, modulus(z))
@ccall libflint.nmod_poly_set_coeff_ui(z::Ref{($etype)}, n::Int, r::UInt)::Nothing
return z
function setcoeff!(z::($etype), i::Int, x::Int, n::UInt=modulus(z))
return setcoeff!(z, i, mod(x, n), n)

Check warning on line 650 in src/flint/nmod_rel_series.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/nmod_rel_series.jl#L649-L650

Added lines #L649 - L650 were not covered by tests
end

function setcoeff!(z::($etype), n::Int, x::($mtype))
@ccall libflint.nmod_poly_set_coeff_ui(z::Ref{($etype)}, n::Int, data(x)::UInt)::Nothing
return z
function setcoeff!(z::($etype), i::Int, x::ZZRingElem, n::UInt=modulus(z))
xx = @ccall libflint.fmpz_fdiv_ui(x::Ref{ZZRingElem}, n::UInt)::UInt
return setcoeff!(z, i, xx, n)
end

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

Check warning on line 658 in src/flint/nmod_rel_series.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/nmod_rel_series.jl#L658

Added line #L658 was not covered by tests

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

function mul!(z::($etype), a::($etype), b::($etype))
lena = pol_length(a)
lenb = pol_length(b)
Expand Down
Loading