Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve 'simplify' function #22

Merged
merged 1 commit into from
Oct 13, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/symbolic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,31 @@ function simplify(ex::Expr)
return new_ex
end

function sum_numeric_args(args)
sum = 0
sym_args = {}
for arg in args
if isa(arg, Number)
sum += arg
else
sym_args = [sym_args, arg]
end
end
(sum, sym_args)
end

function mul_numeric_args(args)
prod = 1
sym_args = {}
for arg in args
if isa(arg, Number)
prod *= arg
else
sym_args = [sym_args, arg]
end
end
(prod, sym_args)
end

# Handles all lengths for ex.args
# Removes any 0's in a sum
Expand All @@ -113,6 +138,8 @@ function simplify(::SymbolParameter{:+}, args)
elseif length(new_args) == 1
return new_args[1]
else
(sum, sym_args) = sum_numeric_args(new_args)
new_args = sum==0 ? sym_args : [sum, sym_args]
return Expr(:call, :+, new_args...)
end
end
Expand Down Expand Up @@ -144,6 +171,8 @@ function simplify(::SymbolParameter{:*}, args)
elseif any(new_args .== 0)
return 0
else
(prod, sym_args) = mul_numeric_args(new_args)
new_args = prod==1 ? sym_args : [prod, sym_args]
return Expr(:call, :*, new_args...)
end
end
Expand Down
16 changes: 15 additions & 1 deletion test/symbolic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@assert isequal(differentiate(:(x * a), :x), :a)
@assert isequal(differentiate(:(x ^ 2), :x), :(2 * x))
@assert isequal(differentiate(:(a * x ^ 2), :x), :(a * (2 * x)))
@assert isequal(differentiate(:(2 ^ x), :x), :(*(^(2, x), 0.6931471805599453)))
@assert isequal(differentiate(:(2 ^ x), :x), :(*(0.6931471805599453, ^(2, x))))
@assert isequal(differentiate(:(sin(x)), :x), :(cos(x)))
@assert isequal(differentiate(:(cos(x)), :x), :(*(-1,sin(x)))) # needs a better simplify
@assert isequal(differentiate(:(tan(x)), :x), :(1 + tan(x)^2))
Expand Down Expand Up @@ -74,3 +74,17 @@ end
@assert isequal(testfun(x), :(^($(x),2)))
@assert isequal(testfun(3), 9)
@assert isequal(testfun(@sexpr(x+y)), :(^(+($x,$y),2)))

#
# Simplify tests
#

@assert isequal(simplify(:(x+y)), :(+(x,y)))
@assert isequal(simplify(:(x+3)), :(+(3,x)))
@assert isequal(simplify(:(x+3+4)), :(+(7,x)))
@assert isequal(simplify(:(2+y+x+3)), :(+(5,y,x)))

@assert isequal(simplify(:(x*y)), :(*(x,y)))
@assert isequal(simplify(:(x*3)), :(*(3,x)))
@assert isequal(simplify(:(x*3*4)), :(*(12,x)))
@assert isequal(simplify(:(2*y*x*3)), :(*(6,y,x)))