Skip to content

Commit 710f871

Browse files
committed
Apply hash consing to Pow
1 parent 3ce0492 commit 710f871

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/types.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ function ConstructionBase.setproperties(obj::BasicSymbolic{T}, patch::NamedTuple
101101
Add => Add(T, nt_new.coeff, nt_new.dict; nt_new...)
102102
Mul => Mul(T, nt_new.coeff, nt_new.dict; nt_new...)
103103
Div => Div{T}(nt_new.num, nt_new.den, nt_new.simplified; nt_new...)
104+
Pow => Pow{T}(nt_new.base, nt_new.exp; nt_new...)
104105
_ => Unityper.rt_constructor(obj){T}(;nt_new...)
105106
end
106107
end
@@ -520,7 +521,8 @@ end
520521
function Pow{T}(a, b; metadata=NO_METADATA) where {T}
521522
_iszero(b) && return 1
522523
_isone(b) && return a
523-
Pow{T}(; base=a, exp=b, arguments=[], metadata)
524+
s = Pow{T}(; base=a, exp=b, arguments=[], metadata)
525+
BasicSymbolic(s)
524526
end
525527

526528
function Pow(a, b; metadata=NO_METADATA)

test/hash_consing.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using SymbolicUtils, Test
2-
using SymbolicUtils: Term, Add, Mul, Div
2+
using SymbolicUtils: Term, Add, Mul, Div, Pow
33

44
struct Ctx1 end
55
struct Ctx2 end
@@ -85,3 +85,17 @@ end
8585
vm1 = setmetadata(v1,Ctx1, "meta_1")
8686
@test vm1 !== v1
8787
end
88+
89+
@testset "Pow" begin
90+
p1 = a^b
91+
p2 = a^b
92+
@test p1 === p2
93+
p3 = a^(2^-b)
94+
p4 = a^(2^-b)
95+
@test p3 === p4
96+
p5 = Pow{Float64}(a,b)
97+
@test p1 !== p5
98+
99+
pm1 = setmetadata(p1,Ctx1, "meta_1")
100+
@test pm1 !== p1
101+
end

0 commit comments

Comments
 (0)