Skip to content

Commit d306a7c

Browse files
Merge pull request #29 from magistere/cleanup
Code cleanup in symbolic.jl
2 parents f42f543 + 9d48000 commit d306a7c

File tree

1 file changed

+46
-45
lines changed

1 file changed

+46
-45
lines changed

src/symbolic.jl

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,21 @@ SymbolParameter(s::Symbol) = SymbolParameter{s}()
7979
#
8080
#################################################################
8181

82+
isnumber(a) = isa(a, Number)
8283

8384
# Numbers and symbols can't be simplified further
8485
simplify(x) = x
8586
simplify(n::Number) = n
8687
simplify(s::SymbolicVariable) = s
8788

8889
# 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)...)
9091

9192
function simplify(ex::Expr)
9293
if ex.head != :call
9394
return ex
9495
end
95-
if all(map(a -> isa(a, Number), ex.args[2:end]))
96+
if all(map(isnumber, ex.args[2:end]))
9697
return eval(ex)
9798
end
9899
new_ex = simplify(SymbolParameter(ex.args[1]), ex.args[2:end])
@@ -106,10 +107,10 @@ function sum_numeric_args(args)
106107
sum = 0
107108
sym_args = {}
108109
for arg in args
109-
if isa(arg, Number)
110+
if isnumber(arg)
110111
sum += arg
111112
else
112-
sym_args = [sym_args, arg]
113+
push!(sym_args, arg)
113114
end
114115
end
115116
(sum, sym_args)
@@ -119,94 +120,94 @@ function mul_numeric_args(args)
119120
prod = 1
120121
sym_args = {}
121122
for arg in args
122-
if isa(arg, Number)
123+
if isnumber(arg)
123124
prod *= arg
124125
else
125-
sym_args = [sym_args, arg]
126+
push!(sym_args, arg)
126127
end
127128
end
128129
(prod, sym_args)
129130
end
130131

131-
# Handles `args` of all lengths
132-
# Removes any 0's in a sum
132+
# Handle `args` of all lengths
133133
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
136137
return 0
137138
# 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]
140141
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...)
144145
end
145146
end
146147

147-
# Assumes length(args) == 3
148-
# Removes any 0's in a subtraction
148+
# Assume length(args) == 3
149149
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
152153
return 0
153154
# 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]
155156
return 0
156157
else
157-
return Expr(:call, :-, new_args...)
158+
return Expr(:call, :-, args...)
158159
end
159160
end
160161

161-
# Handles `args` of all lengths
162-
# Removes any 1's in a product
162+
# Handle `args` of all lengths
163163
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
166167
return 1
167168
# 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]
170171
# Special Case: simplify(:(x * y * z * 0)) == 0
171-
elseif any(new_args .== 0)
172+
elseif any(args .== 0)
172173
return 0
173174
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...)
177178
end
178179
end
179180

180-
# Assumes length(args) == 3
181+
# Assume length(args) == 3
181182
function simplify(::SymbolParameter{:/}, args)
182-
new_args = map(x -> simplify(x), args)
183+
args = map(simplify, args)
183184
# 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]
186187
# Special Case: simplify(:(0 / x)) == 0
187-
elseif new_args[1] == 0
188+
elseif args[1] == 0
188189
return 0
189190
else
190-
return Expr(:call, :/, new_args...)
191+
return Expr(:call, :/, args...)
191192
end
192193
end
193194

194-
# Assumes length(args) == 3
195+
# Assume length(args) == 3
195196
function simplify(::SymbolParameter{:^}, args)
196-
new_args = map(x -> simplify(x), args)
197+
args = map(simplify, args)
197198
# Special Case: simplify(:(x ^ 0)) == 1
198-
if new_args[2] == 0
199+
if args[2] == 0
199200
return 1
200201
# 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]
203204
# Special Case: simplify(:(0 ^ x)) == 0
204-
elseif new_args[1] == 0
205+
elseif args[1] == 0
205206
return 0
206207
# Special Case: simplify(:(1 ^ x)) == 1
207-
elseif new_args[1] == 1
208+
elseif args[1] == 1
208209
return 1
209210
else
210-
return Expr(:call, :^, new_args...)
211+
return Expr(:call, :^, args...)
211212
end
212213
end

0 commit comments

Comments
 (0)