Skip to content

Commit

Permalink
Apply hash consing to Div
Browse files Browse the repository at this point in the history
  • Loading branch information
Blablablanca committed Nov 13, 2024
1 parent e44a457 commit 3dc1169
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ function ConstructionBase.setproperties(obj::BasicSymbolic{T}, patch::NamedTuple
Term => Term{T}(nt_new.f, nt_new.arguments; nt_new...)
Add => Add(T, nt_new.coeff, nt_new.dict; nt_new...)
Mul => Mul(T, nt_new.coeff, nt_new.dict; nt_new...)
Div => Div{T}(nt_new.num, nt_new.den, nt_new.simplified; nt_new...)
_ => Unityper.rt_constructor(obj){T}(;nt_new...)
end
end
Expand Down Expand Up @@ -502,11 +503,13 @@ function Div{T}(n, d, simplified=false; metadata=nothing) where {T}
end
end

Div{T}(; num=n, den=d, simplified, arguments=[], metadata)
s = Div{T}(; num=n, den=d, simplified, arguments=[], metadata)
BasicSymbolic(s)
end

function Div(n,d, simplified=false; kw...)
Div{promote_symtype((/), symtype(n), symtype(d))}(n, d, simplified; kw...)
s = Div{promote_symtype((/), symtype(n), symtype(d))}(n, d, simplified; kw...)
BasicSymbolic(s)
end

@inline function numerators(x)
Expand Down
19 changes: 18 additions & 1 deletion test/hash_consing.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using SymbolicUtils, Test
using SymbolicUtils: Term, Add, Mul
using SymbolicUtils: Term, Add, Mul, Div

struct Ctx1 end
struct Ctx2 end
Expand Down Expand Up @@ -68,3 +68,20 @@ end
mm1 = setmetadata(m1, Ctx1, "meta_1")
@test m1 !== mm1
end

@testset "Div" begin
v1 = a/b
v2 = a/b
@test v1 === v2
v3 = -1/a
v4 = -1/a
@test v3 === v4
v5 = 3a/6
v6 = 2a/4
@test v5 === v6
v7 = Div{Float64}(-1,a)
@test v7 !== v3

vm1 = setmetadata(v1,Ctx1, "meta_1")
@test vm1 !== v1
end

0 comments on commit 3dc1169

Please sign in to comment.