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

fix: univariate polynomial rings over zero rings #1911

Merged
merged 1 commit into from
Nov 26, 2024
Merged
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
5 changes: 3 additions & 2 deletions src/Poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ number_of_generators(R::PolyRing) = 1

iszero(a::PolynomialElem) = length(a) == 0

isone(a::PolynomialElem) = length(a) == 1 && isone(coeff(a, 0))
isone(a::PolynomialElem) = length(a) <= 1 && isone(coeff(a, 0))
Copy link
Collaborator

Choose a reason for hiding this comment

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

does coeff(a, 0) even work for length(a) == 0 or does that error?

Copy link
Member Author

Choose a reason for hiding this comment

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


@doc raw"""
is_gen(a::PolynomialElem)
Expand Down Expand Up @@ -780,6 +780,7 @@ function ^(a::PolyRingElem{T}, b::Int) where T <: RingElement
b < 0 && throw(DomainError(b, "exponent must be >= 0"))
# special case powers of x for constructing polynomials efficiently
R = parent(a)
is_trivial(R) && return zero(R)
if is_gen(a)
z = R()
fit!(z, b + 1)
Expand Down Expand Up @@ -1352,7 +1353,7 @@ end

function divexact(f::PolyRingElem{T}, g::PolyRingElem{T}; check::Bool=true) where T <: RingElement
check_parent(f, g)
iszero(g) && throw(DivideError())
is_trivial(parent(f)) || (iszero(g) && throw(DivideError()))
if iszero(f)
return zero(parent(f))
end
Expand Down
3 changes: 2 additions & 1 deletion src/generic/GenericTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,11 @@ end
@attributes mutable struct PolyRing{T <: RingElement} <: AbstractAlgebra.PolyRing{T}
base_ring::Ring
S::Symbol
istrivial::Bool

function PolyRing{T}(R::Ring, s::Symbol, cached::Bool = true) where T <: RingElement
return get_cached!(PolyID, (R, s), cached) do
new{T}(R, s)
new{T}(R, s, is_trivial(R))
end::PolyRing{T}
end
end
Expand Down
3 changes: 3 additions & 0 deletions src/generic/Poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ parent(a::Poly) = a.parent

var(a::PolyRing) = a.S

is_trivial(a::PolyRing) = a.istrivial

###############################################################################
#
# Basic manipulation
Expand Down Expand Up @@ -199,6 +201,7 @@ function zero!(c::Poly{T}) where T <: RingElement
end

function one!(c::Poly{T}) where T <: RingElement
is_trivial(parent(c)) && return zero!(c)
fit!(c, 1)
c = set_length!(c, 1)
c.coeffs[1] = one(base_ring(c))
Expand Down
1 change: 1 addition & 0 deletions src/generic/imports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ import ..AbstractAlgebra: is_square
import ..AbstractAlgebra: is_square_with_sqrt
import ..AbstractAlgebra: is_term
import ..AbstractAlgebra: is_terse
import ..AbstractAlgebra: is_trivial
import ..AbstractAlgebra: is_unit
import ..AbstractAlgebra: is_univariate
import ..AbstractAlgebra: is_zero_divisor
Expand Down
7 changes: 7 additions & 0 deletions test/generic/Poly-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3034,3 +3034,10 @@ end
@test collect(Base.Iterators.take(V, d)) == collect(Base.Iterators.take(W, d))
end
end

@testset "Generic.Poly.zero_rings" begin
R, = residue_ring(ZZ, 1)
Rx, = R[:x]
@test is_zero(gen(Rx)) && is_one(gen(Rx))
test_Ring_interface_recursive(Rx)
end
Loading