Skip to content

Commit

Permalink
Fixes for SparsePolyRing (#1692)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens authored Apr 26, 2024
1 parent e6472ff commit 86a9299
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
1 change: 0 additions & 1 deletion src/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ export SetMap
export SimpleNumField
export SimpleNumFieldElem
export SkewDiagram
export SparsePolynomialRing
export Strassen
export SymmetricGroup
export UniversalPolyRing
Expand Down
1 change: 1 addition & 0 deletions src/generic/GenericTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ end
###############################################################################

# Q: Why is SparsePolyRing not a subtype of AbstractAlgebra.PolyRing{T} ?
# A: It is purely internal, and implementing the interface would make it slower.

@attributes mutable struct SparsePolyRing{T <: RingElement} <: AbstractAlgebra.Ring
base_ring::Ring
Expand Down
32 changes: 32 additions & 0 deletions src/generic/SparsePoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ end
#
###############################################################################

function Base.hash(a::SparsePoly, h::UInt)
b = 0x679c879b67bb8385%UInt
for i in 1:length(a)
b = xor(b, hash(a.exps[i], hash(a.coeffs[i], h)))
end
return b
end

function coeff(x::SparsePoly, i::Int)
i < 0 && throw(DomainError(i, "cannot get the i-th coefficient with i < 0"))
return x.coeffs[i + 1]
Expand Down Expand Up @@ -358,6 +366,8 @@ end
###############################################################################

function divides(a::SparsePoly{T}, b::SparsePoly{T}) where {T <: RingElement}
iszero(a) && return true, zero(parent(a))
iszero(b) && return false, parent(a)()
d1 = a.exps[a.length]
d2 = b.exps[b.length] - b.exps[1]
q_alloc = b.length
Expand Down Expand Up @@ -811,6 +821,28 @@ function (a::SparsePolyRing{T})(b::Vector{T}, m::Vector{UInt}) where {T <: RingE
return z
end

# Functions to remove ambiguities
function (a::SparsePolyRing{T})(b::T) where {T <: Integer}
parent(b) != base_ring(a) && error("Unable to coerce to polynomial")
z = SparsePoly{T}(b)
z.parent = a
return z
end

function (a::SparsePolyRing{T})(b::T) where {T <: Rational}
parent(b) != base_ring(a) && error("Unable to coerce to polynomial")
z = SparsePoly{T}(b)
z.parent = a
return z
end

function (a::SparsePolyRing{T})(b::T) where {T <: AbstractFloat}
parent(b) != base_ring(a) && error("Unable to coerce to polynomial")
z = SparsePoly{T}(b)
z.parent = a
return z
end

###############################################################################
#
# SparsePolynomialRing constructor
Expand Down
16 changes: 4 additions & 12 deletions test/generic/SparsePoly-test.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
# FIXME/TODO: get these conformance tests to work and pass
#function test_elem(Rx::AbstractAlgebra.Generic.SparsePolyRing)
# R = base_ring(Rx)
# x = gen(Rx)
# return sum(x^(5*i) * test_elem(R) for i in 1:rand(0:6); init=zero(Rx))
#end
#
#@testset "Generic.SparsePoly.conformance" begin
# R, x = SparsePolynomialRing(ZZ, "x")
# test_Ring_interface(R)
#end

@testset "Generic.SparsePoly.constructors" begin
SparsePolynomialRing = AbstractAlgebra.SparsePolynomialRing

R, x = SparsePolynomialRing(ZZ, "x")
S, y = SparsePolynomialRing(R, "y")

Expand All @@ -25,6 +15,8 @@
end

@testset "Generic.SparsePoly.printing" begin
SparsePolynomialRing = AbstractAlgebra.SparsePolynomialRing

R, x = SparsePolynomialRing(ZZ, "x")

@test string(zero(R)) == "0"
Expand Down

0 comments on commit 86a9299

Please sign in to comment.