Skip to content

Commit 90bace8

Browse files
Merge pull request #474 from SciML/s/hash
hash for Operation
2 parents 1718410 + 6ff7b95 commit 90bace8

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ RecursiveArrayTools = "2.3"
4343
SafeTestsets = "0.0.1"
4444
SpecialFunctions = "0.7, 0.8, 0.9, 0.10"
4545
StaticArrays = "0.10, 0.11, 0.12"
46-
SymbolicUtils = "0.3.3, 0.4"
46+
SymbolicUtils = "0.4.1"
4747
TreeViews = "0.3"
4848
UnPack = "0.1, 1.0"
4949
Unitful = "1.1"

src/equations.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct Equation
1313
rhs::Expression
1414
end
1515
Base.:(==)(a::Equation, b::Equation) = isequal((a.lhs, a.rhs), (b.lhs, b.rhs))
16+
Base.hash(a::Equation, salt::UInt) = hash(a.lhs, hash(a.rhs, salt))
1617

1718
"""
1819
$(TYPEDSIGNATURES)

src/operations.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ Base.isequal(::Variable , ::Operation) = false
5454
Base.isequal(::Operation, ::Constant ) = false
5555
Base.isequal(::Constant , ::Operation) = false
5656

57+
Base.hash(o::Operation, salt::UInt) = hash(o.args, hash(o.op, salt))
58+
5759
# provide iszero for Operations to help sparse addition and multiplication
5860
# e.g., we want to tell the sparse library that iszero(zero(Operation) + zero(Operation)) == true
5961
Base.iszero(x::Operation) = (_x = simplify(x); _x isa Constant && iszero(_x.value))

test/operation_overloads.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ using Test
55

66
@variables a,b,c,d
77

8+
# test hashing
9+
aa = a; # old a
10+
11+
@variables a
12+
13+
@test isequal(a, aa)
14+
@test hash(a) == hash(aa)
15+
16+
@test isequal(get_variables(a+aa+1), [a])
17+
18+
@test hash(a+b ~ c+d) == hash(a+b ~ c+d)
19+
820
# test some matrix operations don't throw errors
921
X = [a b;c d]
1022
det(X)

0 commit comments

Comments
 (0)