Skip to content

Commit

Permalink
Apply hash consing to Pow
Browse files Browse the repository at this point in the history
  • Loading branch information
Blablablanca committed Nov 14, 2024
1 parent 3ce0492 commit 710f871
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ function ConstructionBase.setproperties(obj::BasicSymbolic{T}, patch::NamedTuple
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...)
Pow => Pow{T}(nt_new.base, nt_new.exp; nt_new...)
_ => Unityper.rt_constructor(obj){T}(;nt_new...)
end
end
Expand Down Expand Up @@ -520,7 +521,8 @@ end
function Pow{T}(a, b; metadata=NO_METADATA) where {T}
_iszero(b) && return 1
_isone(b) && return a
Pow{T}(; base=a, exp=b, arguments=[], metadata)
s = Pow{T}(; base=a, exp=b, arguments=[], metadata)
BasicSymbolic(s)
end

function Pow(a, b; metadata=NO_METADATA)
Expand Down
16 changes: 15 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, Div
using SymbolicUtils: Term, Add, Mul, Div, Pow

struct Ctx1 end
struct Ctx2 end
Expand Down Expand Up @@ -85,3 +85,17 @@ end
vm1 = setmetadata(v1,Ctx1, "meta_1")
@test vm1 !== v1
end

@testset "Pow" begin
p1 = a^b
p2 = a^b
@test p1 === p2
p3 = a^(2^-b)
p4 = a^(2^-b)
@test p3 === p4
p5 = Pow{Float64}(a,b)
@test p1 !== p5

pm1 = setmetadata(p1,Ctx1, "meta_1")
@test pm1 !== p1
end

0 comments on commit 710f871

Please sign in to comment.