diff --git a/src/generic/GenericTypes.jl b/src/generic/GenericTypes.jl index 55ae96e3d..67bd41133 100644 --- a/src/generic/GenericTypes.jl +++ b/src/generic/GenericTypes.jl @@ -342,11 +342,12 @@ end ord::Symbol num_vars::Int N::Int + istrivial::Bool function MPolyRing{T}(R::Ring, s::Vector{Symbol}, ord::Symbol, N::Int, cached::Bool = true) where T <: RingElement return get_cached!(MPolyID, (R, s, ord, N), cached) do - new{T}(R, s, ord, length(s), N) + new{T}(R, s, ord, length(s), N, is_zero(one(R))) end::MPolyRing{T} end end @@ -371,6 +372,7 @@ mutable struct MPoly{T <: RingElement} <: AbstractAlgebra.MPolyRingElem{T} return new{T}(Vector{T}(undef, 0), Matrix{UInt}(undef, N, 0), 0, R) end + # assumes that all elements of a are non-zero MPoly{T}(R::MPolyRing, a::Vector{T}, b::Matrix{UInt}) where T <: RingElement = new{T}(a, b, length(a), R) function MPoly{T}(R::MPolyRing, a::T) where T <: RingElement diff --git a/src/generic/MPoly.jl b/src/generic/MPoly.jl index 8225a7b21..2c4ef0878 100644 --- a/src/generic/MPoly.jl +++ b/src/generic/MPoly.jl @@ -36,6 +36,7 @@ number_of_variables(a::MPolyRing) = a.num_vars number_of_generators(a::MPolyRing) = a.num_vars function gen(a::MPolyRing{T}, i::Int, ::Val{:lex}) where {T <: RingElement} + a.istrivial && return zero(a) n = nvars(a) @boundscheck 1 <= i <= n || throw(ArgumentError("variable index out of range")) return a([one(base_ring(a))], reshape(UInt[UInt(j == n - i + 1) @@ -43,6 +44,7 @@ function gen(a::MPolyRing{T}, i::Int, ::Val{:lex}) where {T <: RingElement} end function gen(a::MPolyRing{T}, i::Int, ::Val{:deglex}) where {T <: RingElement} + a.istrivial && return zero(a) n = nvars(a) @boundscheck 1 <= i <= n || throw(ArgumentError("variable index out of range")) return a([one(base_ring(a))], reshape(UInt[(UInt(j == n - i + 1) @@ -50,6 +52,7 @@ function gen(a::MPolyRing{T}, i::Int, ::Val{:deglex}) where {T <: RingElement} end function gen(a::MPolyRing{T}, i::Int, ::Val{:degrevlex}) where {T <: RingElement} + a.istrivial && return zero(a) n = nvars(a) @boundscheck 1 <= i <= n || throw(ArgumentError("variable index out of range")) return a([one(base_ring(a))], reshape(UInt[(UInt(j == i) @@ -838,7 +841,7 @@ Return the number of terms of the polynomial. """ length(x::MPoly) = x.length -isone(x::MPoly) = x.length == 1 && monomial_iszero(x.exps, 1, size(x.exps, 1)) && is_one(x.coeffs[1]) +isone(x::MPoly) = parent(x).istrivial || (x.length == 1 && monomial_iszero(x.exps, 1, size(x.exps, 1)) && is_one(x.coeffs[1])) is_constant(x::MPoly) = x.length == 0 || (x.length == 1 && monomial_iszero(x.exps, 1, size(x.exps, 1))) @@ -3896,6 +3899,7 @@ function zero!(a::MPoly{T}) where {T <: RingElement} end function one!(a::MPoly{T}) where {T <: RingElement} + parent(a).istrivial && return zero!(a) a.length = 1 fit!(a, 1) a.coeffs[1] = one(base_ring(a)) diff --git a/test/generic/MPoly-test.jl b/test/generic/MPoly-test.jl index 16045227a..13e215440 100644 --- a/test/generic/MPoly-test.jl +++ b/test/generic/MPoly-test.jl @@ -1719,3 +1719,11 @@ end S, = polynomial_ring(R, :z => 1:3) test_Ring_interface(S) # _recursive needs too many ressources end + +@testset "Generic.MPoly.zero_rings" begin + R, = residue_ring(ZZ, 1) + S, = polynomial_ring(R, 2) + @test is_zero(gen(S, 1)) && is_one(gen(S, 1)) + @test is_zero(one(S)) + test_Ring_interface(S) # _recursive needs fixes for the univariate case first +end