Skip to content

Commit

Permalink
Merge pull request #139 from wbhart/hash
Browse files Browse the repository at this point in the history
Add hash functions.
  • Loading branch information
hannes14 authored Sep 24, 2019
2 parents 5168029 + af8a0fb commit 239bc13
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Singular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import AbstractAlgebra
using Markdown
using Nemo

import Base: abs, checkbounds, deepcopy, deepcopy_internal,
import Base: abs, checkbounds, convert, deepcopy, deepcopy_internal,
denominator, div, divrem, exponent,
gcd, gcdx, getindex, inv, isequal, isless, lcm, length,
mod, numerator, one, reduce, rem, setindex!, show,
gcd, gcdx, getindex, hash, inv, isequal, isless, lcm,
length, mod, numerator, one, reduce, rem, setindex!, show,
zero, +, -, *, ==, ^, &, |, <<, >>, ~, <=, >=, <, >, //,
/, !=

Expand Down
8 changes: 8 additions & 0 deletions src/ideal/ideal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ function check_parent(I::sideal{T}, J::sideal{T}) where T <: Nemo.RingElem
base_ring(I) != base_ring(J) && error("Incompatible ideals")
end

function hash(I::sideal, h::UInt)
v = 0xebd7f23adcde5067%UInt
for p in gens(I)
v = xor(hash(p, h), v)
end
return v
end

###############################################################################
#
# String I/O
Expand Down
10 changes: 10 additions & 0 deletions src/matrix/matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ function deepcopy_internal(M::smatrix, dict::IdDict)
return parent(M)(ptr)
end

function hash(M::smatrix, h::UInt)
v = 0x68bf046fc9d6afbf%UInt
for i in 1:nrows(M)
for j in 1:ncols(M)
v = xor(hash(M[i, j], h), v)
end
end
return v
end

###############################################################################
#
# String I/O
Expand Down
8 changes: 8 additions & 0 deletions src/module/module.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ function deepcopy_internal(I::smodule, dict::IdDict)
return Module(R, ptr)
end

function hash(M::smodule, h::UInt)
v = 0x403fd5a7748e75c9%UInt
for i in 1:ngens(M)
v = xor(hash(M[i], h), v)
end
return v
end

###############################################################################
#
# String I/O
Expand Down
8 changes: 8 additions & 0 deletions src/module/vector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ function check_parent(a::svector{T}, b::svector{T}) where T <: Nemo.RingElem
a.rank != b.rank && error("Vectors of incompatible rank")
end

function hash(V::svector, h::UInt)
v = 0xd2fd44bc67ee655e%UInt
for p in Array(V)
v = xor(hash(p, h), v)
end
return v
end

###############################################################################
#
# String I/O
Expand Down
7 changes: 7 additions & 0 deletions src/number/n_GF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ function deepcopy_internal(a::n_GF, dict::IdDict)
return parent(a)(libSingular.n_Copy(a.ptr, parent(a).ptr))
end

function hash(a::n_GF, h::UInt)
i = reinterpret(Int, a.ptr.cpp_object)
chash = hash(characteristic(parent(a)), h)
ihash = hash(i, h)
return xor(xor(chash, ihash), 0x2c42e12d0c837511%UInt)
end

###############################################################################
#
# Basic manipulation
Expand Down
8 changes: 8 additions & 0 deletions src/number/n_Q.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ function deepcopy_internal(a::n_Q, dict::IdDict)
return parent(a)(libSingular.n_Copy(a.ptr, parent(a).ptr))
end

function hash(a::n_Q, h::UInt)
n = numerator(a)
d = denominator(a)
nhash = hash(n, h)
dhash = hash(d, h)
return xor(xor(nhash, dhash), 0xf348b78c190e8fc1%UInt)
end

###############################################################################
#
# Basic manipulation
Expand Down
5 changes: 4 additions & 1 deletion src/number/n_Z.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Base.convert
export n_Z, Integers, crt

###############################################################################
Expand All @@ -21,6 +20,10 @@ function deepcopy_internal(a::n_Z, dict::IdDict)
return parent(a)(libSingular.n_Copy(a.ptr, parent(a).ptr))
end

function hash(a::n_Z, h::UInt)
return xor(hash(convert(BigInt, a), h), 0x8ab976d24b6a0cca%UInt)
end

###############################################################################
#
# Basic manipulation
Expand Down
15 changes: 15 additions & 0 deletions src/number/n_Zn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ function deepcopy_internal(a::n_Zn, dict::IdDict)
return parent(a)(libSingular.n_Copy(a.ptr, parent(a).ptr))
end

function hash(a::n_Zn, h::UInt)
# get the number of limbs
sptr = convert(Ptr{Cint}, a.ptr.cpp_object) + sizeof(Cint)
s = unsafe_load(sptr)
# get the pointer after the first two Cints
d = convert(Ptr{Ptr{UInt}}, a.ptr.cpp_object) + 2*sizeof(Cint)
p = unsafe_load(d)
b = unsafe_load(p)
h = xor(Base.hash_uint(xor(ifelse(s < 0, -b, b), h)), h)
for k = 2:abs(s)
h = xor(Base.hash_uint(xor(unsafe_load(p, k), h)), h)
end
return xor(h, 0xe6ebab8a56a5461b%UInt)
end

###############################################################################
#
# Basic manipulation
Expand Down
6 changes: 6 additions & 0 deletions src/number/n_Zp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ function deepcopy_internal(a::n_Zp, dict::IdDict)
return parent(a)(libSingular.n_Copy(a.ptr, parent(a).ptr))
end

function hash(a::n_Zp, h::UInt)
chash = hash(characteristic(parent(a)), h)
ahash = hash(Int(a), h)
return xor(xor(chash, ahash), 0x77dc334c1532ce3c%UInt)
end

###############################################################################
#
# Basic manipulation
Expand Down
5 changes: 5 additions & 0 deletions src/number/n_unknown.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ function zero(R::CoefficientRing)
return R(libSingular.n_Init(0, R.ptr))
end

function hash(a::n_unknown, h::UInt)
n = libSingular.julia(a.ptr)
return xor(hash(n, h), 0x664e59de562461fe%UInt)
end

###############################################################################
#
# String I/O
Expand Down
10 changes: 10 additions & 0 deletions src/poly/poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ function canonical_unit(a::spoly{T}) where T <: Nemo.RingElem
return a == 0 ? one(base_ring(a)) : canonical_unit(coeff(a, 0))
end

function Base.hash(p::spoly{T}, h::UInt) where T <: Nemo.RingElem
v = 0x37eec82e994ab710%UInt
v = xor(hash(collect(exponent_vectors(p)), h), v)
for c in coeffs(p)
v = xor(hash(c, h), v)
v = (v << 1) | (v >> (sizeof(Int)*8 - 1))
end
return v
end

###############################################################################
#
# Iterators
Expand Down
8 changes: 8 additions & 0 deletions src/resolution/resolution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ function deepcopy_internal(r::sresolution, dict::IdDict)
return S(ptr, r.len)
end

function hash(r::sresolution, h::UInt)
v = 0xc3143e8e499f1ba3%UInt
for i in 1:length(r)
v = xor(hash(r[i], h), v)
end
return v
end

###############################################################################
#
# Betti numbers
Expand Down
13 changes: 13 additions & 0 deletions test/poly/spoly-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,18 @@ function test_spoly_factor()
println("PASS")
end

function test_spoly_hash()
print("spoly.hash...")

R, (x, y) = PolynomialRing(QQ, ["x", "y"])

@test hash(x) == hash(x+y-y)
@test hash(x,zero(UInt)) == hash(x+y-y,zero(UInt))
@test hash(x,one(UInt)) == hash(x+y-y,one(UInt))

println("PASS")
end

function test_spoly()
test_spoly_constructors()
test_spoly_printing()
Expand All @@ -522,6 +534,7 @@ function test_spoly()
# test_convert_between_MPoly_and_SingularPoly()
test_spoly_differential()
test_spoly_factor()
test_spoly_hash()
println("")
end

0 comments on commit 239bc13

Please sign in to comment.