From 3dc116930a0685e4c8da107df2ce8fd10523d6b3 Mon Sep 17 00:00:00 2001 From: Blanca Luo Date: Wed, 13 Nov 2024 09:47:06 -0500 Subject: [PATCH] Apply hash consing to `Div` --- src/types.jl | 7 +++++-- test/hash_consing.jl | 19 ++++++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/types.jl b/src/types.jl index 96c137d0..24b30dce 100644 --- a/src/types.jl +++ b/src/types.jl @@ -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 @@ -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) diff --git a/test/hash_consing.jl b/test/hash_consing.jl index 548d3bf6..b9725fdc 100644 --- a/test/hash_consing.jl +++ b/test/hash_consing.jl @@ -1,5 +1,5 @@ using SymbolicUtils, Test -using SymbolicUtils: Term, Add, Mul +using SymbolicUtils: Term, Add, Mul, Div struct Ctx1 end struct Ctx2 end @@ -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