Skip to content

Commit

Permalink
Stop degree(::MPoly,...) from overflowing
Browse files Browse the repository at this point in the history
  • Loading branch information
thofma committed Aug 15, 2023
1 parent 38fd074 commit 4c439b1
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 12 deletions.
16 changes: 13 additions & 3 deletions src/flint/fmpq_mpoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,13 @@ end
function degree(a::QQMPolyRingElem, i::Int)
n = nvars(parent(a))
(i <= 0 || i > n) && error("Index must be between 1 and $n")
d = ccall((:fmpq_mpoly_degree_si, libflint), Int,
(Ref{QQMPolyRingElem}, Int, Ref{QQMPolyRing}), a, i - 1, a.parent)
return d
if degrees_fit_int(a)
d = ccall((:fmpq_mpoly_degree_si, libflint), Int,
(Ref{QQMPolyRingElem}, Int, Ref{QQMPolyRing}), a, i - 1, a.parent)
return d
else
return Int(degree_fmpz(a, i))
end
end

# Degree in the i-th variable as an ZZRingElem
Expand All @@ -225,6 +229,9 @@ end

# Return an array of the max degrees in each variable
function degrees(a::QQMPolyRingElem)
if !degrees_fit_int(a)
throw(OverflowError("degrees of polynomial do not fit into Int"))
end
degs = Vector{Int}(undef, nvars(parent(a)))
ccall((:fmpq_mpoly_degrees_si, libflint), Nothing,
(Ptr{Int}, Ref{QQMPolyRingElem}, Ref{QQMPolyRing}),
Expand Down Expand Up @@ -254,6 +261,9 @@ function total_degree_fits_int(a::QQMPolyRingElem)

# Total degree as an Int
function total_degree(a::QQMPolyRingElem)
if !total_degree_fits_int(a)
throw(OverflowError("Total degree of polynomial does not fit into Int"))
end
d = ccall((:fmpq_mpoly_total_degree_si, libflint), Int,
(Ref{QQMPolyRingElem}, Ref{QQMPolyRing}), a, a.parent)
return d
Expand Down
14 changes: 12 additions & 2 deletions src/flint/fmpz_mod_mpoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,14 @@ end
function degree(a::($etype), i::Int)
n = nvars(parent(a))
(i <= 0 || i > n) && error("Index must be between 1 and $n")
d = ccall((:fmpz_mod_mpoly_degree_si, libflint), Int,
if degrees_fit_int(a)
d = ccall((:fmpz_mod_mpoly_degree_si, libflint), Int,
(Ref{($etype)}, Int, Ref{($rtype)}),
a, i - 1, parent(a))
return d
return d
else
return Int(degree_fmpz(a, i))
end
end

# Degree in the i-th variable as an ZZRingElem
Expand All @@ -234,6 +238,9 @@ end

# Return an array of the max degrees in each variable
function degrees(a::($etype))
if !degrees_fit_int(a)
throw(OverflowError("degrees of polynomial do not fit into Int"))
end
degs = Vector{Int}(undef, nvars(parent(a)))
ccall((:fmpz_mod_mpoly_degrees_si, libflint), Nothing,
(Ptr{Int}, Ref{($etype)}, Ref{($rtype)}),
Expand Down Expand Up @@ -263,6 +270,9 @@ end

# Total degree as an Int
function total_degree(a::($etype))
if !total_degree_fits_int(a)
throw(OverflowError("Total degree of polynomial does not fit into Int"))
end
d = ccall((:fmpz_mod_mpoly_total_degree_si, libflint), Int,
(Ref{($etype)}, Ref{($rtype)}),
a, a.parent)
Expand Down
16 changes: 13 additions & 3 deletions src/flint/fmpz_mpoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,13 @@ end
function degree(a::ZZMPolyRingElem, i::Int)
n = nvars(parent(a))
(i <= 0 || i > n) && error("Index must be between 1 and $n")
d = ccall((:fmpz_mpoly_degree_si, libflint), Int,
(Ref{ZZMPolyRingElem}, Int, Ref{ZZMPolyRing}), a, i - 1, a.parent)
return d
if degrees_fit_int(a)
d = ccall((:fmpz_mpoly_degree_si, libflint), Int,
(Ref{ZZMPolyRingElem}, Int, Ref{ZZMPolyRing}), a, i - 1, a.parent)
return d
else
return Int(degree_fmpz(a, i))
end
end

# Degree in the i-th variable as an ZZRingElem
Expand All @@ -205,6 +209,9 @@ end

# Return an array of the max degrees in each variable
function degrees(a::ZZMPolyRingElem)
if !degrees_fit_int(a)
throw(OverflowError("degrees of polynomial do not fit into Int"))
end
degs = Vector{Int}(undef, nvars(parent(a)))
ccall((:fmpz_mpoly_degrees_si, libflint), Nothing,
(Ptr{Int}, Ref{ZZMPolyRingElem}, Ref{ZZMPolyRing}),
Expand Down Expand Up @@ -234,6 +241,9 @@ function total_degree_fits_int(a::ZZMPolyRingElem)

# Total degree as an Int
function total_degree(a::ZZMPolyRingElem)
if !total_degree_fits_int(a)
throw(OverflowError("Total degree of polynomial does not fit into Int"))
end
d = ccall((:fmpz_mpoly_total_degree_si, libflint), Int,
(Ref{ZZMPolyRingElem}, Ref{ZZMPolyRing}), a, a.parent)
return d
Expand Down
14 changes: 12 additions & 2 deletions src/flint/fq_nmod_mpoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,14 @@ end
function degree(a::fqPolyRepMPolyRingElem, i::Int)
n = nvars(parent(a))
!(1 <= i <= n) && error("Index must be between 1 and $n")
d = ccall((:fq_nmod_mpoly_degree_si, libflint), Int,
if degrees_fit_int(a)
d = ccall((:fq_nmod_mpoly_degree_si, libflint), Int,
(Ref{fqPolyRepMPolyRingElem}, Int, Ref{fqPolyRepMPolyRing}),
a, i - 1, a.parent)
return d
return d
else
return Int(degree_fmpz(a, i))
end
end

# Degree in the i-th variable as an ZZRingElem
Expand All @@ -215,6 +219,9 @@ end

# Return an array of the max degrees in each variable
function degrees(a::fqPolyRepMPolyRingElem)
if !degrees_fit_int(a)
throw(OverflowError("degrees of polynomial do not fit into Int"))
end
degs = Vector{Int}(undef, nvars(parent(a)))
ccall((:fq_nmod_mpoly_degrees_si, libflint), Nothing,
(Ptr{Int}, Ref{fqPolyRepMPolyRingElem}, Ref{fqPolyRepMPolyRing}),
Expand Down Expand Up @@ -245,6 +252,9 @@ end

# Total degree as an Int
function total_degree(a::fqPolyRepMPolyRingElem)
if !total_degree_fits_int(a)
throw(OverflowError("Total degree of polynomial does not fit into Int"))
end
d = ccall((:fq_nmod_mpoly_total_degree_si, libflint), Int,
(Ref{fqPolyRepMPolyRingElem}, Ref{fqPolyRepMPolyRing}),
a, a.parent)
Expand Down
13 changes: 11 additions & 2 deletions src/flint/nmod_mpoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,13 @@ end
function degree(a::($etype), i::Int)
n = nvars(parent(a))
(i <= 0 || i > n) && error("Index must be between 1 and $n")
d = ccall((:nmod_mpoly_degree_si, libflint), Int,
if degrees_fit_int(a)
d = ccall((:nmod_mpoly_degree_si, libflint), Int,
(Ref{($etype)}, Int, Ref{($rtype)}),
a, i - 1, parent(a))
return d
else
return Int(degree_fmpz(a, i))
end
end

# Degree in the i-th variable as an ZZRingElem
Expand All @@ -219,6 +222,9 @@ end

# Return an array of the max degrees in each variable
function degrees(a::($etype))
if !degrees_fit_int(a)
throw(OverflowError("degrees of polynomial do not fit into Int"))
end
degs = Vector{Int}(undef, nvars(parent(a)))
ccall((:nmod_mpoly_degrees_si, libflint), Nothing,
(Ptr{Int}, Ref{($etype)}, Ref{($rtype)}),
Expand Down Expand Up @@ -248,6 +254,9 @@ end

# Total degree as an Int
function total_degree(a::($etype))
if !total_degree_fits_int(a)
throw(OverflowError("Total degree of polynomial does not fit into Int"))
end
d = ccall((:nmod_mpoly_total_degree_si, libflint), Int,
(Ref{($etype)}, Ref{($rtype)}),
a, a.parent)
Expand Down
6 changes: 6 additions & 0 deletions test/flint/fmpq_mpoly-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ end
@test trailing_coefficient(x) == 1
@test trailing_coefficient(S(2)) == 2
@test trailing_coefficient(S()) == 0

f = x^(ZZ(2)^100) * y^100
@test_throws InexactError degree(f, 1)
@test degree(f, 2) == 100
@test_throws OverflowError degrees(f)
@test_throws OverflowError total_degree(f)
end

@testset "QQMPolyRingElem.multivariate_coeff" begin
Expand Down
6 changes: 6 additions & 0 deletions test/flint/fmpz_mpoly-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ end
@test trailing_coefficient(x) == 1
@test trailing_coefficient(S(2)) == 2
@test trailing_coefficient(S()) == 0

f = x^(ZZ(2)^100) * y^100
@test_throws InexactError degree(f, 1)
@test degree(f, 2) == 100
@test_throws OverflowError degrees(f)
@test_throws OverflowError total_degree(f)
end

@testset "ZZMPolyRingElem.multivariate_coeff" begin
Expand Down
6 changes: 6 additions & 0 deletions test/flint/fq_nmod_mpoly-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ end
@test trailing_coefficient(x) == 1
@test trailing_coefficient(S(2)) == 2
@test trailing_coefficient(S()) == 0

f = x^(ZZ(2)^100) * y^100
@test_throws InexactError degree(f, 1)
@test degree(f, 2) == 100
@test_throws OverflowError degrees(f)
@test_throws OverflowError total_degree(f)
end

@testset "fqPolyRepMPolyRingElem.multivariate_coeff" begin
Expand Down
6 changes: 6 additions & 0 deletions test/flint/gfp_fmpz_mpoly-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ end
@test trailing_coefficient(x) == 1
@test trailing_coefficient(S(2)) == 2
@test trailing_coefficient(S()) == 0

f = x^(ZZ(2)^100) * y^100
@test_throws InexactError degree(f, 1)
@test degree(f, 2) == 100
@test_throws OverflowError degrees(f)
@test_throws OverflowError total_degree(f)
end

@testset "FpMPolyRingElem.multivariate_coeff" begin
Expand Down
6 changes: 6 additions & 0 deletions test/flint/nmod_mpoly-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ end
@test trailing_coefficient(x) == 1
@test trailing_coefficient(S(2)) == 2
@test trailing_coefficient(S()) == 0

f = x^(ZZ(2)^100) * y^100
@test_throws InexactError degree(f, 1)
@test degree(f, 2) == 100
@test_throws OverflowError degrees(f)
@test_throws OverflowError total_degree(f)
end

@testset "zzModMPolyRingElem.multivariate_coeff" begin
Expand Down

0 comments on commit 4c439b1

Please sign in to comment.