Skip to content

Commit 4cb5a76

Browse files
committed
refactor: deprecate combine_operators -> combine_operators!
1 parent 3626d8f commit 4cb5a76

File tree

4 files changed

+27
-26
lines changed

4 files changed

+27
-26
lines changed

src/PatchMethods.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ using ..OperatorEnumModule: AbstractOperatorEnum
55
using ..NodeModule: constructorof
66
using ..ExpressionModule: Expression, get_tree, get_operators
77
using ..ParametricExpressionModule: ParametricExpression
8-
import ..SimplifyModule: combine_operators, simplify_tree!
8+
import ..SimplifyModule: combine_operators!, simplify_tree!
99

1010
# Avoid implementing a generic version for these, as it is less likely to generalize
11-
function combine_operators(
11+
function combine_operators!(
1212
ex::Union{Expression{T,N},ParametricExpression{T,N}},
1313
operators::Union{AbstractOperatorEnum,Nothing}=nothing,
1414
) where {T,N}
1515
return with_contents(
16-
ex, combine_operators(get_contents(ex), get_operators(ex, operators))
16+
ex, combine_operators!(get_contents(ex), get_operators(ex, operators))
1717
)
1818
end
1919
function simplify_tree!(

src/Simplify.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,29 @@ has_identity_one(_) = false
3333
simplifies_given_equal_operands(::typeof(/)) = true
3434
simplifies_given_equal_operands(_) = false
3535

36-
combine_operators(tree::AbstractExpressionNode, ::AbstractOperatorEnum) = tree
36+
combine_operators!(tree::AbstractExpressionNode, ::AbstractOperatorEnum) = tree
3737

38-
function combine_operators(tree::Node{T}, operators::AbstractOperatorEnum) where {T}
38+
function combine_operators!(tree::Node{T}, operators::AbstractOperatorEnum) where {T}
3939
deg = tree.degree
4040
deg == 0 && return tree
41-
tree.l = combine_operators(tree.l, operators)
41+
tree.l = combine_operators!(tree.l, operators)
4242
deg == 1 && return tree
43-
tree.r = combine_operators(tree.r, operators)
44-
return dispatch_deg2_simplify(tree, operators)
43+
tree.r = combine_operators!(tree.r, operators)
44+
return dispatch_deg2_simplify!(tree, operators)
4545
end
46-
@generated function dispatch_deg2_simplify(
46+
@generated function dispatch_deg2_simplify!(
4747
tree::Node{T}, operators::AbstractOperatorEnum
4848
) where {T}
4949
nbin = get_nbin(operators)
5050
quote
5151
op_idx = tree.op
5252
return Base.Cartesian.@nif(
53-
$nbin, i -> i == op_idx, i -> _combine_operators_on(operators.binops[i], tree)
53+
$nbin, i -> i == op_idx, i -> _combine_operators_on!(tree, operators.binops[i])
5454
)
5555
end
5656
end
5757

58-
function _combine_operators_on(f::F, tree::Node{T}) where {F,T}
58+
function _combine_operators_on!(tree::Node{T}, f::F) where {F,T}
5959
# NOTE: This assumes tree.degree == 2 and tree.op corresponds to f
6060
# Handle basic simplifications first
6161
if is_node_constant(tree.r)

src/deprecated.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Base: @deprecate
22
import .NodeModule: Node, GraphNode
33

44
@deprecate simplify_tree(tree, operators) simplify_tree!(tree, operators)
5+
@deprecate combine_operators(tree, operators) combine_operators!(tree, operators)
56

67
for N in (:Node, :GraphNode)
78
@eval begin

test/test_simplification.jl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ end
9494
Node(1, Node(1, Node(; val=0.1), Node(; val=0.2)) + Node(; val=0.2)) +
9595
Node(; val=2.0)
9696
@test repr(tree) "(cos((0.1 + 0.2) + 0.2) + 2.0)"
97-
@test repr(combine_operators(tree, operators)) "(cos(0.4 + 0.1) + 2.0)"
97+
@test repr(combine_operators!(tree, operators)) "(cos(0.4 + 0.1) + 2.0)"
9898
end
9999

100100
@testitem "Basic operator simplifications" begin
101101
using DynamicExpressions, Test
102-
import DynamicExpressions.SimplifyModule: combine_operators
102+
import DynamicExpressions.SimplifyModule: combine_operators!
103103

104104
operators = OperatorEnum(; binary_operators=(+, -, *, /), unary_operators=(cos, sin))
105105
x = Node(; feature=1)
@@ -110,52 +110,52 @@ end
110110

111111
# multiplication by 0
112112
tree = zero_node * x
113-
@test combine_operators(tree, operators) == zero_node
113+
@test combine_operators!(tree, operators) == zero_node
114114
tree = x * zero_node
115-
@test combine_operators(tree, operators) == zero_node
115+
@test combine_operators!(tree, operators) == zero_node
116116

117117
# multiplication by 1
118118
tree = one_node * x
119-
@test combine_operators(tree, operators) == x
119+
@test combine_operators!(tree, operators) == x
120120
tree = x * one_node
121-
@test combine_operators(tree, operators) == x
121+
@test combine_operators!(tree, operators) == x
122122

123123
# addition by 0
124124
tree = zero_node + x
125-
@test combine_operators(tree, operators) == x
125+
@test combine_operators!(tree, operators) == x
126126
tree = x + zero_node
127-
@test combine_operators(tree, operators) == x
127+
@test combine_operators!(tree, operators) == x
128128

129129
# division by self -> 1
130130
tree = x / x
131-
@test combine_operators(tree, operators).val == 1.0
131+
@test combine_operators!(tree, operators).val == 1.0
132132

133133
# nested multiplication by constants
134134
tree1 = (two_node * x) * three_node
135135
tree2 = Node(; val=6.0) * x
136-
@test combine_operators(tree1, operators) == combine_operators(tree2, operators)
136+
@test combine_operators!(tree1, operators) == combine_operators!(tree2, operators)
137137
end
138138

139139
@testitem "Constant combination" begin
140140
using DynamicExpressions, Test
141-
import DynamicExpressions.SimplifyModule: combine_operators
141+
import DynamicExpressions.SimplifyModule: combine_operators!
142142

143143
operators = OperatorEnum(; binary_operators=(+, -, *, /), unary_operators=(cos, sin))
144144
x1 = Node(; feature=1)
145145

146146
# Test commutative constant combination
147147
tree = Node(; val=0.5) + (Node(; val=0.2) + x1)
148-
@test combine_operators(tree, operators) == x1 + Node(; val=0.7)
148+
@test combine_operators!(tree, operators) == x1 + Node(; val=0.7)
149149

150150
# Test nested multiplication by constants
151151
tree = (Node(; val=2.0) * x1) * Node(; val=3.0)
152-
@test combine_operators(tree, operators) == x1 * Node(; val=6.0)
152+
@test combine_operators!(tree, operators) == x1 * Node(; val=6.0)
153153

154154
# Test nested addition by constants
155155
tree = (Node(; val=2.0) + x1) + Node(; val=3.0)
156-
@test combine_operators(tree, operators) == x1 + Node(; val=5.0)
156+
@test combine_operators!(tree, operators) == x1 + Node(; val=5.0)
157157

158158
# Test mixed operations don't combine incorrectly
159159
tree = (Node(; val=2.0) * x1) + Node(; val=3.0)
160-
@test combine_operators(tree, operators) == tree
160+
@test combine_operators!(tree, operators) == tree
161161
end

0 commit comments

Comments
 (0)