Skip to content

Commit 75e7b96

Browse files
Use function objects instead of symbols
1 parent b9cc424 commit 75e7b96

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/simplify.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,35 @@ function simplify_constants(O::Operation,shorten_tree = true)
44
while _O != O_last
55
O_last = _O
66
_O = _simplify_constants(_O,shorten_tree)
7-
if typeof(_O) <: Operation
7+
if is_operation(_O)
88
_O = Operation(_O.op,simplify_constants.(_O.args,shorten_tree))
99
end
1010
end
1111
_O
1212
end
1313

14-
const TREE_SHRINK_OPS = [:*,:+]
14+
const TREE_SHRINK_OPS = [*, +]
1515

1616
function _simplify_constants(O,shorten_tree = true)
1717
# Tree shrinking
1818
if shorten_tree
1919
for cur_op in TREE_SHRINK_OPS
20-
if Symbol(O.op) == cur_op
20+
if O.op == cur_op
2121
# Shrink tree
22-
if any(x->typeof(x)<:Operation && Symbol(x.op) == cur_op ,O.args)
23-
idxs = findall(x->typeof(x)<:Operation && Symbol(x.op) == cur_op,O.args)
22+
if any(x -> is_operation(x) && x.op == cur_op, O.args)
23+
idxs = findall(x -> is_operation(x) && x.op == cur_op, O.args)
2424
keep_idxs = 1:length(O.args) .∉ (idxs,)
2525
args = Vector{Expression}[O.args[i].args for i in idxs]
2626
push!(args,O.args[keep_idxs])
27-
return Operation(O.op,vcat(args...))
27+
return Operation(O.op, vcat(args...))
2828
end
2929
# Collapse constants
3030
idxs = findall(is_constant, O.args)
3131
if length(idxs) > 1
3232
other_idxs = 1:length(O.args) .∉ (idxs,)
33-
if cur_op == :*
33+
if cur_op == (*)
3434
new_var = Constant(prod(get, O.args[idxs]))
35-
elseif cur_op == :+
35+
elseif cur_op == (+)
3636
new_var = Constant(sum(get, O.args[idxs]))
3737
end
3838
new_args = O.args[other_idxs]
@@ -47,7 +47,7 @@ function _simplify_constants(O,shorten_tree = true)
4747
end
4848
end
4949

50-
if Symbol(O.op) == :*
50+
if O.op == (*)
5151
# If any variable is `Constant(0)`, zero the whole thing
5252
# If any variable is `Constant(1)`, remove that `Constant(1)` unless
5353
# they are all `Constant(1)`, in which case simplify to a single variable
@@ -66,7 +66,7 @@ function _simplify_constants(O,shorten_tree = true)
6666
else
6767
return O
6868
end
69-
elseif Symbol(O.op) == :+ && any(iszero, O.args)
69+
elseif O.op == (+) && any(iszero, O.args)
7070
# If there are Constant(0)s in a big `+` expression, get rid of them
7171
idxs = findall(iszero, O.args)
7272
_O = Operation(O.op,O.args[1:length(O.args) .∉ (idxs,)])
@@ -79,7 +79,7 @@ function _simplify_constants(O,shorten_tree = true)
7979
end
8080
elseif O.op == identity
8181
return O.args[1]
82-
elseif Symbol(O.op) == :- && length(O.args) == 1
82+
elseif O.op == (-) && length(O.args) == 1
8383
return Operation(*,Expression[-1,O.args[1]])
8484
else
8585
return O

src/utils.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ toexpr(ex) = MacroTools.postwalk(x->x isa Union{Expression,Operation} ? Expr(x)
2121

2222
is_constant(x::Variable) = x.subtype === :Constant
2323
is_constant(::Any) = false
24+
25+
is_operation(::Operation) = true
26+
is_operation(::Any) = false

0 commit comments

Comments
 (0)