Skip to content

Commit 1c57740

Browse files
authored
fix: multivariate polynomial rings over zero rings (#1910)
1 parent dd23a38 commit 1c57740

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/generic/GenericTypes.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,12 @@ end
343343
ord::Symbol
344344
num_vars::Int
345345
N::Int
346+
istrivial::Bool
346347

347348
function MPolyRing{T}(R::Ring, s::Vector{Symbol}, ord::Symbol, N::Int,
348349
cached::Bool = true) where T <: RingElement
349350
return get_cached!(MPolyID, (R, s, ord, N), cached) do
350-
new{T}(R, s, ord, length(s), N)
351+
new{T}(R, s, ord, length(s), N, is_trivial(R))
351352
end::MPolyRing{T}
352353
end
353354
end
@@ -372,6 +373,7 @@ mutable struct MPoly{T <: RingElement} <: AbstractAlgebra.MPolyRingElem{T}
372373
return new{T}(Vector{T}(undef, 0), Matrix{UInt}(undef, N, 0), 0, R)
373374
end
374375

376+
# assumes that all elements of a are non-zero
375377
MPoly{T}(R::MPolyRing, a::Vector{T}, b::Matrix{UInt}) where T <: RingElement = new{T}(a, b, length(a), R)
376378

377379
function MPoly{T}(R::MPolyRing, a::T) where T <: RingElement

src/generic/MPoly.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ elem_type(::Type{MPolyRing{T}}) where T <: RingElement = MPoly{T}
1818

1919
base_ring(R::MPolyRing{T}) where T <: RingElement = R.base_ring::parent_type(T)
2020

21+
is_trivial(R::MPolyRing) = R.istrivial
22+
2123
@doc raw"""
2224
symbols(a::MPolyRing)
2325
@@ -36,20 +38,23 @@ number_of_variables(a::MPolyRing) = a.num_vars
3638
number_of_generators(a::MPolyRing) = a.num_vars
3739

3840
function gen(a::MPolyRing{T}, i::Int, ::Val{:lex}) where {T <: RingElement}
41+
is_trivial(a) && return zero(a)
3942
n = nvars(a)
4043
@boundscheck 1 <= i <= n || throw(ArgumentError("variable index out of range"))
4144
return a([one(base_ring(a))], reshape(UInt[UInt(j == n - i + 1)
4245
for j = 1:n], n, 1))
4346
end
4447

4548
function gen(a::MPolyRing{T}, i::Int, ::Val{:deglex}) where {T <: RingElement}
49+
is_trivial(a) && return zero(a)
4650
n = nvars(a)
4751
@boundscheck 1 <= i <= n || throw(ArgumentError("variable index out of range"))
4852
return a([one(base_ring(a))], reshape(UInt[(UInt(j == n - i + 1)
4953
for j in 1:n)..., UInt(1)], n + 1, 1))
5054
end
5155

5256
function gen(a::MPolyRing{T}, i::Int, ::Val{:degrevlex}) where {T <: RingElement}
57+
is_trivial(a) && return zero(a)
5358
n = nvars(a)
5459
@boundscheck 1 <= i <= n || throw(ArgumentError("variable index out of range"))
5560
return a([one(base_ring(a))], reshape(UInt[(UInt(j == i)
@@ -838,7 +843,7 @@ Return the number of terms of the polynomial.
838843
"""
839844
length(x::MPoly) = x.length
840845

841-
isone(x::MPoly) = x.length == 1 && monomial_iszero(x.exps, 1, size(x.exps, 1)) && is_one(x.coeffs[1])
846+
isone(x::MPoly) = is_trivial(parent(x)) || (x.length == 1 && monomial_iszero(x.exps, 1, size(x.exps, 1)) && is_one(x.coeffs[1]))
842847

843848
is_constant(x::MPoly) = x.length == 0 || (x.length == 1 && monomial_iszero(x.exps, 1, size(x.exps, 1)))
844849

@@ -3896,6 +3901,7 @@ function zero!(a::MPoly{T}) where {T <: RingElement}
38963901
end
38973902

38983903
function one!(a::MPoly{T}) where {T <: RingElement}
3904+
is_trivial(parent(a)) && return zero!(a)
38993905
a.length = 1
39003906
fit!(a, 1)
39013907
a.coeffs[1] = one(base_ring(a))

test/generic/MPoly-test.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,3 +1719,11 @@ end
17191719
S, = polynomial_ring(R, :z => 1:3)
17201720
test_Ring_interface(S) # _recursive needs too many ressources
17211721
end
1722+
1723+
@testset "Generic.MPoly.zero_rings" begin
1724+
R, = residue_ring(ZZ, 1)
1725+
S, = polynomial_ring(R, 2)
1726+
@test is_zero(gen(S, 1)) && is_one(gen(S, 1))
1727+
@test is_zero(one(S))
1728+
test_Ring_interface_recursive(S)
1729+
end

0 commit comments

Comments
 (0)