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

Fixes for SparsePolyRing #1692

Merged
merged 5 commits into from
Apr 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
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 @@
#
###############################################################################

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

Check warning on line 42 in src/generic/SparsePoly.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/SparsePoly.jl#L37-L42

Added lines #L37 - L42 were not covered by tests
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 @@
###############################################################################

function divides(a::SparsePoly{T}, b::SparsePoly{T}) where {T <: RingElement}
iszero(a) && return true, zero(parent(a))
iszero(b) && return false, parent(a)()

Check warning on line 370 in src/generic/SparsePoly.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/SparsePoly.jl#L369-L370

Added lines #L369 - L370 were not covered by tests
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 @@
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

Check warning on line 829 in src/generic/SparsePoly.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/SparsePoly.jl#L825-L829

Added lines #L825 - L829 were not covered by tests
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

Check warning on line 836 in src/generic/SparsePoly.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/SparsePoly.jl#L832-L836

Added lines #L832 - L836 were not covered by tests
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

Check warning on line 843 in src/generic/SparsePoly.jl

View check run for this annotation

Codecov / codecov/patch

src/generic/SparsePoly.jl#L839-L843

Added lines #L839 - L843 were not covered by tests
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
Loading