@@ -79,20 +79,21 @@ SymbolParameter(s::Symbol) = SymbolParameter{s}()
79
79
#
80
80
# ################################################################
81
81
82
+ isnumber (a) = isa (a, Number)
82
83
83
84
# Numbers and symbols can't be simplified further
84
85
simplify (x) = x
85
86
simplify (n:: Number ) = n
86
87
simplify (s:: SymbolicVariable ) = s
87
88
88
89
# The default is just to simplify arguments.
89
- simplify {T} (x:: SymbolParameter{T} , args) = Expr (:call , T, map (x -> simplify (x) , args)... )
90
+ simplify {T} (x:: SymbolParameter{T} , args) = Expr (:call , T, map (simplify, args)... )
90
91
91
92
function simplify (ex:: Expr )
92
93
if ex. head != :call
93
94
return ex
94
95
end
95
- if all (map (a -> isa (a, Number) , ex. args[2 : end ]))
96
+ if all (map (isnumber , ex. args[2 : end ]))
96
97
return eval (ex)
97
98
end
98
99
new_ex = simplify (SymbolParameter (ex. args[1 ]), ex. args[2 : end ])
@@ -106,10 +107,10 @@ function sum_numeric_args(args)
106
107
sum = 0
107
108
sym_args = {}
108
109
for arg in args
109
- if isa (arg, Number )
110
+ if isnumber (arg)
110
111
sum += arg
111
112
else
112
- sym_args = [sym_args , arg]
113
+ push! ( sym_args, arg)
113
114
end
114
115
end
115
116
(sum, sym_args)
@@ -119,94 +120,94 @@ function mul_numeric_args(args)
119
120
prod = 1
120
121
sym_args = {}
121
122
for arg in args
122
- if isa (arg, Number )
123
+ if isnumber (arg)
123
124
prod *= arg
124
125
else
125
- sym_args = [sym_args , arg]
126
+ push! ( sym_args, arg)
126
127
end
127
128
end
128
129
(prod, sym_args)
129
130
end
130
131
131
- # Handles `args` of all lengths
132
- # Removes any 0's in a sum
132
+ # Handle `args` of all lengths
133
133
function simplify (:: SymbolParameter{:+} , args)
134
- new_args = map (x -> simplify (x), filter (x -> x != 0 , args))
135
- if length (new_args) == 0
134
+ # Remove any 0's in a sum
135
+ args = map (simplify, filter (x -> x != 0 , args))
136
+ if length (args) == 0
136
137
return 0
137
138
# Special Case: simplify(:(+x)) == x
138
- elseif length (new_args ) == 1
139
- return new_args [1 ]
139
+ elseif length (args ) == 1
140
+ return args [1 ]
140
141
else
141
- (sum, sym_args) = sum_numeric_args (new_args )
142
- new_args = sum== 0 ? sym_args : [sum, sym_args]
143
- return Expr (:call , :+ , new_args ... )
142
+ (sum, sym_args) = sum_numeric_args (args )
143
+ args = sum== 0 ? sym_args : [sum, sym_args]
144
+ return Expr (:call , :+ , args ... )
144
145
end
145
146
end
146
147
147
- # Assumes length(args) == 3
148
- # Removes any 0's in a subtraction
148
+ # Assume length(args) == 3
149
149
function simplify (:: SymbolParameter{:-} , args)
150
- new_args = map (x -> simplify (x), filter (x -> x != 0 , args))
151
- if length (new_args) == 0
150
+ # Remove any 0's in a subtraction
151
+ args = map (simplify, filter (x -> x != 0 , args))
152
+ if length (args) == 0
152
153
return 0
153
154
# Special Case: simplify(:(x - x)) == 0
154
- elseif length (new_args ) == 2 && new_args [1 ] == new_args [2 ]
155
+ elseif length (args ) == 2 && args [1 ] == args [2 ]
155
156
return 0
156
157
else
157
- return Expr (:call , :- , new_args ... )
158
+ return Expr (:call , :- , args ... )
158
159
end
159
160
end
160
161
161
- # Handles `args` of all lengths
162
- # Removes any 1's in a product
162
+ # Handle `args` of all lengths
163
163
function simplify (:: SymbolParameter{:*} , args)
164
- new_args = map (x -> simplify (x), filter (x -> x != 1 , args))
165
- if length (new_args) == 0
164
+ # Remove any 1's in a product
165
+ args = map (simplify, filter (x -> x != 1 , args))
166
+ if length (args) == 0
166
167
return 1
167
168
# Special Case: simplify(:(*(x))) == x
168
- elseif length (new_args ) == 1
169
- return new_args [1 ]
169
+ elseif length (args ) == 1
170
+ return args [1 ]
170
171
# Special Case: simplify(:(x * y * z * 0)) == 0
171
- elseif any (new_args .== 0 )
172
+ elseif any (args .== 0 )
172
173
return 0
173
174
else
174
- (prod, sym_args) = mul_numeric_args (new_args )
175
- new_args = prod== 1 ? sym_args : [prod, sym_args]
176
- return Expr (:call , :* , new_args ... )
175
+ (prod, sym_args) = mul_numeric_args (args )
176
+ args = prod== 1 ? sym_args : [prod, sym_args]
177
+ return Expr (:call , :* , args ... )
177
178
end
178
179
end
179
180
180
- # Assumes length(args) == 3
181
+ # Assume length(args) == 3
181
182
function simplify (:: SymbolParameter{:/} , args)
182
- new_args = map (x -> simplify (x) , args)
183
+ args = map (simplify, args)
183
184
# Special Case: simplify(:(x / 1)) == x
184
- if new_args [2 ] == 1
185
- return new_args [1 ]
185
+ if args [2 ] == 1
186
+ return args [1 ]
186
187
# Special Case: simplify(:(0 / x)) == 0
187
- elseif new_args [1 ] == 0
188
+ elseif args [1 ] == 0
188
189
return 0
189
190
else
190
- return Expr (:call , :/ , new_args ... )
191
+ return Expr (:call , :/ , args ... )
191
192
end
192
193
end
193
194
194
- # Assumes length(args) == 3
195
+ # Assume length(args) == 3
195
196
function simplify (:: SymbolParameter{:^} , args)
196
- new_args = map (x -> simplify (x) , args)
197
+ args = map (simplify, args)
197
198
# Special Case: simplify(:(x ^ 0)) == 1
198
- if new_args [2 ] == 0
199
+ if args [2 ] == 0
199
200
return 1
200
201
# Special Case: simplify(:(x ^ 1)) == x
201
- elseif new_args [2 ] == 1
202
- return new_args [1 ]
202
+ elseif args [2 ] == 1
203
+ return args [1 ]
203
204
# Special Case: simplify(:(0 ^ x)) == 0
204
- elseif new_args [1 ] == 0
205
+ elseif args [1 ] == 0
205
206
return 0
206
207
# Special Case: simplify(:(1 ^ x)) == 1
207
- elseif new_args [1 ] == 1
208
+ elseif args [1 ] == 1
208
209
return 1
209
210
else
210
- return Expr (:call , :^ , new_args ... )
211
+ return Expr (:call , :^ , args ... )
211
212
end
212
213
end
0 commit comments