Skip to content

Commit

Permalink
Fix hash
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Jul 13, 2017
1 parent 1daada8 commit af934b2
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/mono.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export name, monovec, monovectype, sortmonovec, mergemonovec
export name, constantmonomial, monovec, monovectype, sortmonovec, mergemonovec

Base.copy(x::AbstractVariable) = x

Expand Down
4 changes: 1 addition & 3 deletions src/poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ Base.norm(p::AbstractPolynomialLike, r::Int=2) = norm(coefficients(p), r)
function Base.hash(p::AbstractPolynomial, u::UInt)
if iszero(p)
hash(0, u)
elseif nterms(p) == 1
hash(term(p))
else
hash(coefficients(p), hash(monomial(p), hash(u)))
reduce((u, t) -> hash(t, u), u, terms(p))
end
end

Expand Down
10 changes: 9 additions & 1 deletion src/term.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
export term, termtype, zeroterm, coefficient, monomial, exponent, deg, isconstant, divides

Base.hash(t::AbstractTerm, u::UInt) = coefficient(t) == 1 ? hash(monomial(t), u) : hash(monomial(t), hash(coefficient(t), u))
function Base.hash(t::AbstractTerm, u::UInt)
if iszero(t)
hash(0, u)
elseif coefficient(t) == 1
hash(monomial(t), u)
else
hash(monomial(t), hash(coefficient(t), u))
end
end

Base.zero{T, TT<:AbstractTermLike{T}}(::Type{TT}) = zero(T) * constantmonomial(TT)
Base.one{T, TT<:AbstractTermLike{T}}(::Type{TT}) = one(T) * constantmonomial(TT)
Expand Down
2 changes: 1 addition & 1 deletion test/alltests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ include("alg.jl")
include("diff.jl")
#include("subs.jl")
#include("algebraicset.jl")
#include("hash.jl")
include("hash.jl")
#include("div.jl")

include("show.jl")
Expand Down
10 changes: 5 additions & 5 deletions test/hash.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
@test hash(1x+3y) == hash(1.0x+3.0y)
@test hash(one(x)) == hash(x^0)
@test hash(x*y) == hash(polynomial(x*y))
@test hash(Term(1.0, Monomial(x))) == hash(x)
@test hash(1.0x) == hash(x)
@test hash(x-x) == hash(zero(x))
@test hash(MonomialVector([z, y, x], [[3, 0, 0], [1,0,1]])) == hash(MonomialVector([z, x], [[3, 0], [1, 1]]))
@test hash(Monomial([z, y, x], [3, 0, 0])) == hash(Monomial([z, x], [3, 0]))
@test hash(monovec([z^3, z*x, y])[1:2]) == hash(monovec([z^3, z*x]))
@test hash(monovec([z^3, x, y])[1:1]) == hash(monovec([z^3, x])[1:1])
@test hash(1) == hash(one(x))
@test hash(1) == hash(Monomial([x, y], [0, 0]))
@test hash(2) != hash(Monomial([x, y], [0, 0]))
@test hash(1) == hash(constantmonomial(x * y))
@test hash(2) != hash(constantmonomial(x * y))
@test hash(0.0) == hash(x-x)
end

0 comments on commit af934b2

Please sign in to comment.