Skip to content

Implement mutable addition #743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ExproniconLite = "55351af7-c7e9-48d6-89ff-24e801d99491"
LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MultivariatePolynomials = "102ac46a-7ee4-5c85-9060-abc95bfdeaa3"
MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Expand Down Expand Up @@ -50,6 +51,7 @@ DynamicPolynomials = "0.5, 0.6"
ExproniconLite = "0.10.14"
LabelledArrays = "1.5"
MultivariatePolynomials = "0.5"
MutableArithmetics = "1.6.4"
NaNMath = "0.3, 1.1.2"
OhMyThreads = "0.7"
ReverseDiff = "1"
Expand Down
2 changes: 2 additions & 0 deletions src/SymbolicUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,7 @@ include("code.jl")
# Adjoints
include("adjoints.jl")

# Mutable Arithmetics
include("mutable_arithmetics.jl")

end # module
16 changes: 16 additions & 0 deletions src/mutable_arithmetics.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import MutableArithmetics as MA

function MA.operate!!(::typeof(+), a::BasicSymbolic, b::BasicSymbolic)
if SymbolicUtils.isadd(a)
if SymbolicUtils.isadd(b)
for (k, v) in b.dict
a.dict[k] = MA.add!!(get(a.dict, k, 0), v)
end
else
a.dict[b] = MA.add!!(get(a.dict, b, 0), 1)
end
return a
else
return a + b
end
end
17 changes: 17 additions & 0 deletions test/mutable_arithmetics.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Test
using MutableArithmetics
using SymbolicUtils

@syms x::Real y::Real
v = repeat([x, y], 10)
s = sum(v, init = 0)
@test s.dict[x] == 10
@test s.dict[y] == 10
@test isequal(s, 10x + 10y) # ????

a = x + y
b = 2x + y
c = add!!(a, b)
@test c.dict[x] == 3
@test c.dict[y] == 2
@test isequal(c, 3x + 2y)
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ using Pkg, Test, SafeTestsets
@safetestset "Adjoints" begin include("adjoints.jl") end
@safetestset "Hash Consing" begin include("hash_consing.jl") end
@safetestset "Cache macro" begin include("cache_macro.jl") end
@safetestset "Cache macro" begin include("mutable_arithmetics.jl") end
end
end
Loading