Skip to content

Implement simplify(:(*(-1,x))) ==> -x #30

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

Merged
merged 2 commits into from
Oct 25, 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
9 changes: 9 additions & 0 deletions src/symbolic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ function simplify(::SymbolParameter{:+}, args)
end
end

isminus(ex::Expr) = ex.head == :call && ex.args[1] == :- && length(ex.args) == 2
isminus(ex) = false

# Assume length(args) == 3
function simplify(::SymbolParameter{:-}, args)
# Remove any 0's in a subtraction
Expand All @@ -154,6 +157,9 @@ function simplify(::SymbolParameter{:-}, args)
# Special Case: simplify(:(x - x)) == 0
elseif length(args) == 2 && args[1] == args[2]
return 0
# Special Case: simplify(:(x - (-y))) == x + y
elseif length(args) == 2 && isminus(args[2])
return Expr(:call, :+, args[1], args[2].args[2])
else
return Expr(:call, :-, args...)
end
Expand All @@ -171,6 +177,9 @@ function simplify(::SymbolParameter{:*}, args)
# Special Case: simplify(:(x * y * z * 0)) == 0
elseif any(args .== 0)
return 0
# Special Case: simplify(:(*(-1,x))) == -x
elseif length(args) == 2 && args[1] == -1
return Expr(:call, :-, args[2])
else
(prod, sym_args) = mul_numeric_args(args)
args = prod==1 ? sym_args : [prod, sym_args]
Expand Down
10 changes: 5 additions & 5 deletions test/symbolic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
@test isequal(differentiate(:(a * x ^ 2), :x), :(a * (2 * x)))
@test isequal(differentiate(:(2 ^ x), :x), :(*(0.6931471805599453, ^(2, x))))
@test isequal(differentiate(:(sin(x)), :x), :(cos(x)))
@test isequal(differentiate(:(cos(x)), :x), :(*(-1,sin(x)))) # needs a better simplify
@test isequal(differentiate(:(cos(x)), :x), :(-sin(x)))
@test isequal(differentiate(:(tan(x)), :x), :(1 + tan(x)^2))
@test isequal(differentiate(:(exp(x)), :x), :(exp(x)))
@test isequal(differentiate(:(log(x)), :x), :(1 / x))
@test isequal(differentiate(:(sin(x) + sin(x)), :x), :(cos(x) + cos(x)))
@test isequal(differentiate(:(sin(x) - cos(x)), :x), :(-(cos(x),*(-1,sin(x))))) # Simplify -(a, -(b)) => +(a, b)
@test isequal(differentiate(:(sin(x) - cos(x)), :x), :(cos(x) + sin(x)))
@test isequal(differentiate(:(x * sin(x)), :x), :(sin(x) + x * cos(x)))
@test isequal(differentiate(:(x / sin(x)), :x), :((sin(x) - x * cos(x)) / (sin(x)^2)))
@test isequal(differentiate(:(sin(sin(x))), :x), :(*(cos(x),cos(sin(x)))))
@test isequal(differentiate(:(sin(cos(x) + sin(x))), :x), :(*(+(*(-1,sin(x)),cos(x)),cos(+(cos(x),sin(x)))))) # Clean this up
@test isequal(differentiate(:(exp(-x)), :x), :(*(-1,exp(-(x))))) # Simplify this to -(exp(-x))
@test isequal(differentiate(:(sin(cos(x) + sin(x))), :x), :(*(+(-sin(x),cos(x)),cos(+(cos(x),sin(x))))))
@test isequal(differentiate(:(exp(-x)), :x), :(-exp(-x)))
@test isequal(differentiate(:(log(x^2)), :x), :(/(*(2,x),^(x,2))))
@test isequal(differentiate(:(x^n), :x), :(*(n, ^(x, -(n, 1)))))
@test isequal(differentiate(:(n^x), :x), :(*(^(n, x), log(n))))
Expand All @@ -47,7 +47,7 @@
#

# @test isequal(differentiate("sin(x) + cos(x)^2"), :(+(cos(x),*(2,cos(x)))))
@test isequal(differentiate("x + exp(-x) + sin(exp(x))", :x), :(+(1,*(-1,exp(-(x))),*(exp(x),cos(exp(x))))))
@test isequal(differentiate("x + exp(-x) + sin(exp(x))", :x), :(+(1,-exp(-x),*(exp(x),cos(exp(x))))))

# TODO: Make these work
# differentiate(:(sin(x)), :x)(0.0)
Expand Down