Skip to content

Commit

Permalink
Sort out some generic/concrete poly code (#1655)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens authored Apr 5, 2024
1 parent 7e759e8 commit dd141b5
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 40 deletions.
2 changes: 2 additions & 0 deletions src/Fraction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#
###############################################################################

base_ring_type(::Type{<:FracField{T}}) where T<:RingElement = parent_type(T)

function is_domain_type(::Type{T}) where {S <: RingElement, T <: FracElem{S}}
return is_domain_type(S)
end
Expand Down
4 changes: 2 additions & 2 deletions src/MPoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ Return the variables actually occurring in $p$.
function vars(p::MPolyRingElem{T}) where {T <: RingElement}
U = typeof(p)
vars_in_p = Vector{U}(undef, 0)
n = nvars(p.parent)
gen_list = gens(p.parent)
n = nvars(parent(p))
gen_list = gens(parent(p))
biggest = [0 for i in 1:n]
for v in exponent_vectors(p)
for j = 1:n
Expand Down
40 changes: 30 additions & 10 deletions src/NCPoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
#
###############################################################################

base_ring_type(::Type{NCPolyRing{T}}) where T <: NCRingElem = parent_type(T)

base_ring(R::NCPolyRing{T}) where T <: NCRingElem = R.base_ring::parent_type(T)
base_ring_type(::Type{<:NCPolyRing{T}}) where T<:NCRingElement = parent_type(T)

coefficient_ring(R::NCPolyRing) = base_ring(R)

Expand Down Expand Up @@ -85,15 +83,30 @@ dense_poly_ring_type(x) = parent_type(dense_poly_type(x))
Return the internal name of the generator of the polynomial ring. Note that
this is returned as a `Symbol` not a `String`.
"""
var(a::NCPolyRing) = a.S
var(a::NCPolyRing)

@doc raw"""
symbols(a::NCPolyRing)
Return an array of the variable names for the polynomial ring. Note that
this is returned as an array of `Symbol` not `String`.
"""
symbols(a::NCPolyRing) = [a.S]
symbols(a::NCPolyRing) = [var(a)]

@doc raw"""
number_of_variables(a::NCPolyRing)
Return the number of variables of the polynomial ring, which is 1.
"""
number_of_variables(a::NCPolyRing) = 1

characteristic(a::NCPolyRing) = characteristic(base_ring(a))

function check_parent(a::NCPolyRingElem{T}, b::NCPolyRingElem{T}, throw::Bool = true) where T<:RingElement
c = parent(a) != parent(b)
c && throw && error("Incompatible polynomial rings in polynomial operation")
return !c
end

###############################################################################
#
Expand All @@ -110,9 +123,9 @@ function Base.hash(a::NCPolyRingElem, h::UInt)
return b
end

zero(R::NCPolyRing) = R(0)
zero(R::NCPolyRing) = R(zero(base_ring(R)))

one(R::NCPolyRing) = R(1)
one(R::NCPolyRing) = R(one(base_ring(R)))

@doc raw"""
gen(R::NCPolyRing)
Expand All @@ -121,7 +134,14 @@ Return the generator of the given polynomial ring.
"""
gen(R::NCPolyRing) = R([zero(base_ring(R)), one(base_ring(R))])

is_term(a::T) where T <: NCRingElem = true
@doc raw"""
gens(R::NCPolyRing)
Return an array containing the generator of the given polynomial ring.
"""
gens(R::NCPolyRing) = [gen(R)]

number_of_generators(R::NCPolyRing) = 1

###############################################################################
#
Expand Down Expand Up @@ -202,11 +222,11 @@ function *(a::NCPolyRingElem{T}, b::NCPolyRingElem{T}) where T <: NCRingElem
d[i] = coeff(a, i - 1)*coeff(b, 0)
end
for i = 2:lenb
d[lena + i - 1] = a.coeffs[lena]*coeff(b, i - 1)
d[lena + i - 1] = coeff(a,lena-1)*coeff(b, i - 1)
end
for i = 1:lena - 1
for j = 2:lenb
t = mul!(t, coeff(a, i - 1), b.coeffs[j])
t = mul!(t, coeff(a, i - 1), coeff(b, j - 1))
d[i + j - 1] = addeq!(d[i + j - 1], t)
end
end
Expand Down
35 changes: 10 additions & 25 deletions src/Poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@
#
###############################################################################

base_ring_type(::Type{PolyRing{T}}) where {T} = parent_type(T)

base_ring(R::PolyRing{T}) where T <: RingElement = R.base_ring::parent_type(T)
base_ring_type(::Type{<:PolyRing{T}}) where T<:RingElement = parent_type(T)

coefficient_ring(R::PolyRing) = base_ring(R)

parent(a::PolynomialElem) = a.parent

dense_poly_type(::Type{T}) where T<:RingElement = Generic.Poly{T}

function is_domain_type(::Type{T}) where {S <: RingElement, T <: PolyRingElem{S}}
Expand All @@ -34,15 +30,15 @@ end
Return the internal name of the generator of the polynomial ring. Note that
this is returned as a `Symbol` not a `String`.
"""
var(a::PolyRing) = a.S
var(a::PolyRing)

@doc raw"""
symbols(a::PolyRing)
Return an array of the variable names for the polynomial ring. Note that
this is returned as an array of `Symbol` not `String`.
"""
symbols(a::PolyRing) = [a.S]
symbols(a::PolyRing) = [var(a)]

@doc raw"""
number_of_variables(a::PolyRing)
Expand All @@ -51,14 +47,14 @@ Return the number of variables of the polynomial ring, which is 1.
"""
number_of_variables(a::PolyRing) = 1

function check_parent(a::PolynomialElem, b::PolynomialElem, throw::Bool = true)
characteristic(a::PolyRing) = characteristic(base_ring(a))

function check_parent(a::PolyRingElem{T}, b::PolyRingElem{T}, throw::Bool = true) where T<:RingElement
c = parent(a) != parent(b)
c && throw && error("Incompatible polynomial rings in polynomial operation")
return !c
end

characteristic(a::PolyRing) = characteristic(base_ring(a))

###############################################################################
#
# Basic manipulation
Expand All @@ -83,7 +79,7 @@ zero coefficients. Thus naturally the zero polynomial has length zero and
additionally for nonzero polynomials the length is one more than the degree.
(Note that the leading coefficient will always be nonzero.)
"""
length(a::PolynomialElem) = a.length
length(a::PolynomialElem)

@doc raw"""
degree(a::PolynomialElem)
Expand Down Expand Up @@ -184,11 +180,6 @@ function set_coefficient!(c::PolynomialElem{T}, n::Int, a::T) where T <: Integer
return setcoeff!(c, n, a) # merely acts as generic fallback
end

@doc raw"""
zero(R::PolyRing)
Return the zero polynomial in the given polynomial ring.
"""
zero(R::PolyRing) = R(zero(base_ring(R)))

one(R::PolyRing) = R(one(base_ring(R)))
Expand All @@ -207,6 +198,8 @@ Return an array containing the generator of the given polynomial ring.
"""
gens(R::PolyRing) = [gen(R)]

number_of_generators(R::PolyRing) = 1

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

isone(a::PolynomialElem) = length(a) == 1 && isone(coeff(a, 0))
Expand Down Expand Up @@ -299,15 +292,7 @@ end
Return `true` if the given polynomial is a monomial.
"""
function is_monomial(a::PolynomialElem)
if !isone(leading_coefficient(a))
return false
end
for i = 1:length(a) - 1
if !iszero(coeff(a, i - 1))
return false
end
end
return true
return is_one(leading_coefficient(a)) && is_term(a)
end

is_monomial_recursive(a::T) where T <: RingElement = isone(a)
Expand Down
4 changes: 1 addition & 3 deletions src/generic/Fraction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ parent_type(::Type{FracFieldElem{T}}) where T <: RingElem = FracField{T}

elem_type(::Type{FracField{T}}) where {T <: RingElem} = FracFieldElem{T}

base_ring_type(::Type{FracField{T}}) where T <: RingElem = parent_type(T)

base_ring(a::FracField{T}) where T <: RingElem = a.base_ring::parent_type(T)
base_ring(a::FracField{T}) where T <: RingElem = a.base_ring::base_ring_type(typeof(a))

###############################################################################
#
Expand Down
8 changes: 8 additions & 0 deletions src/generic/NCPoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ parent_type(::Type{NCPoly{T}}) where T <: NCRingElem = NCPolyRing{T}

elem_type(::Type{NCPolyRing{T}}) where T <: NCRingElem = NCPoly{T}

base_ring(R::NCPolyRing{T}) where T <: NCRingElem = R.base_ring::base_ring_type(typeof(R))

parent(a::NCPoly) = a.parent

var(a::NCPolyRing) = a.S

###############################################################################
#
# Basic manipulation
#
###############################################################################

length(a::NCPoly) = a.length

function setcoeff!(c::NCPoly{T}, n::Int, a::T) where T <: NCRingElem
if !iszero(a) || n + 1 <= length(c)
fit!(c, n + 1)
Expand Down
8 changes: 8 additions & 0 deletions src/generic/Poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ parent_type(::Type{Poly{T}}) where T <: RingElement = PolyRing{T}

elem_type(::Type{PolyRing{T}}) where T <: RingElement = Poly{T}

base_ring(R::PolyRing{T}) where T <: RingElement = R.base_ring::base_ring_type(typeof(R))

parent(a::Poly) = a.parent

var(a::PolyRing) = a.S

###############################################################################
#
# Basic manipulation
#
###############################################################################

length(a::Poly) = a.length

function setcoeff!(c::Poly{T}, n::Int, a::S) where {T <: RingElement, S <: RingElement}
if !iszero(a) || n + 1 <= length(c)
fit!(c, n + 1)
Expand Down

0 comments on commit dd141b5

Please sign in to comment.