Skip to content

Commit ed1ceaa

Browse files
committed
Implement mutable addition
1 parent ca6a4f2 commit ed1ceaa

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ ExproniconLite = "55351af7-c7e9-48d6-89ff-24e801d99491"
1717
LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800"
1818
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1919
MultivariatePolynomials = "102ac46a-7ee4-5c85-9060-abc95bfdeaa3"
20+
MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
2021
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
2122
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
2223
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
@@ -50,6 +51,7 @@ DynamicPolynomials = "0.5, 0.6"
5051
ExproniconLite = "0.10.14"
5152
LabelledArrays = "1.5"
5253
MultivariatePolynomials = "0.5"
54+
MutableArithmetics = "1.6.4"
5355
NaNMath = "0.3, 1.1.2"
5456
OhMyThreads = "0.7"
5557
ReverseDiff = "1"

src/SymbolicUtils.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,7 @@ include("code.jl")
8282
# Adjoints
8383
include("adjoints.jl")
8484

85+
# Mutable Arithmetics
86+
include("mutable_arithmetics.jl")
8587

8688
end # module

src/mutable_arithmetics.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import MutableArithmetics as MA
2+
3+
function MA.operate!!(::typeof(+), a::BasicSymbolic, b::BasicSymbolic)
4+
if SymbolicUtils.isadd(a)
5+
if SymbolicUtils.isadd(b)
6+
a.coeff = MA.add!!(a.coeff, b.coeff)
7+
for (k, v) in b.dict
8+
a.dict[k] = MA.add!!(get(a.dict, k, 0)::Int, v)
9+
end
10+
else
11+
a.dict[b] = MA.add!!(get(a.dict, b, 0), 1)
12+
end
13+
return a
14+
else
15+
return a + b
16+
end
17+
end

0 commit comments

Comments
 (0)