@@ -25,14 +25,15 @@ function _simplify_constants(O,shorten_tree = true)
25
25
args = Vector{Expression}[O. args[i]. args for i in idxs]
26
26
push! (args,O. args[keep_idxs])
27
27
return Operation (O. op,vcat (args... ))
28
+ end
28
29
# Collapse constants
29
- elseif length ( findall (x -> typeof (x) <: Variable && x . subtype == :Constant , O. args)) > 1
30
- idxs = findall (x -> typeof (x) <: Variable && x . subtype == :Constant ,O . args)
30
+ idxs = findall (is_constant, O. args)
31
+ if length ( idxs) > 1
31
32
other_idxs = 1 : length (O. args) .∉ (idxs,)
32
33
if cur_op == :*
33
- new_var = Constant (prod (x -> x . value, O. args[idxs]))
34
+ new_var = Constant (prod (get, O. args[idxs]))
34
35
elseif cur_op == :+
35
- new_var = Constant (sum (x -> x . value, O. args[idxs]))
36
+ new_var = Constant (sum (get, O. args[idxs]))
36
37
end
37
38
new_args = O. args[other_idxs]
38
39
push! (new_args,new_var)
@@ -50,10 +51,10 @@ function _simplify_constants(O,shorten_tree = true)
50
51
# If any variable is `Constant(0)`, zero the whole thing
51
52
# If any variable is `Constant(1)`, remove that `Constant(1)` unless
52
53
# they are all `Constant(1)`, in which case simplify to a single variable
53
- if any (x -> typeof (x) <: Variable && ( isequal (x, Constant ( 0 )) || isequal (x, Constant ( - 0 ))), O. args)
54
+ if any (iszero, O. args)
54
55
return Constant (0 )
55
- elseif any (x -> typeof (x) <: Variable && isequal (x, Constant ( 1 )), O. args)
56
- idxs = findall (x -> typeof (x) <: Variable && isequal (x, Constant ( 1 )), O. args)
56
+ elseif any (isone, O. args)
57
+ idxs = findall (isone, O. args)
57
58
_O = Operation (O. op,O. args[1 : length (O. args) .∉ (idxs,)])
58
59
if isempty (_O. args)
59
60
return Constant (1 )
@@ -65,10 +66,9 @@ function _simplify_constants(O,shorten_tree = true)
65
66
else
66
67
return O
67
68
end
68
- elseif Symbol (O. op) == :+ && any (x-> typeof (x)<: Variable &&
69
- (isequal (x,Constant (0 )) || isequal (x,Constant (- 0 ))),O. args)
69
+ elseif Symbol (O. op) == :+ && any (iszero, O. args)
70
70
# If there are Constant(0)s in a big `+` expression, get rid of them
71
- idxs = findall (x -> typeof (x) <: Variable && ( isequal (x, Constant ( 0 )) || isequal (x, Constant ( - 0 ))), O. args)
71
+ idxs = findall (iszero, O. args)
72
72
_O = Operation (O. op,O. args[1 : length (O. args) .∉ (idxs,)])
73
73
if isempty (_O. args)
74
74
return Constant (0 )
0 commit comments