Skip to content

Commit c013e7d

Browse files
Merge pull request #30 from magistere/additional-simplifications
Implement simplify(:(*(-1,x))) ==> -x
2 parents d306a7c + 51bb693 commit c013e7d

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/symbolic.jl

+9
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ function simplify(::SymbolParameter{:+}, args)
145145
end
146146
end
147147

148+
isminus(ex::Expr) = ex.head == :call && ex.args[1] == :- && length(ex.args) == 2
149+
isminus(ex) = false
150+
148151
# Assume length(args) == 3
149152
function simplify(::SymbolParameter{:-}, args)
150153
# Remove any 0's in a subtraction
@@ -154,6 +157,9 @@ function simplify(::SymbolParameter{:-}, args)
154157
# Special Case: simplify(:(x - x)) == 0
155158
elseif length(args) == 2 && args[1] == args[2]
156159
return 0
160+
# Special Case: simplify(:(x - (-y))) == x + y
161+
elseif length(args) == 2 && isminus(args[2])
162+
return Expr(:call, :+, args[1], args[2].args[2])
157163
else
158164
return Expr(:call, :-, args...)
159165
end
@@ -171,6 +177,9 @@ function simplify(::SymbolParameter{:*}, args)
171177
# Special Case: simplify(:(x * y * z * 0)) == 0
172178
elseif any(args .== 0)
173179
return 0
180+
# Special Case: simplify(:(*(-1,x))) == -x
181+
elseif length(args) == 2 && args[1] == -1
182+
return Expr(:call, :-, args[2])
174183
else
175184
(prod, sym_args) = mul_numeric_args(args)
176185
args = prod==1 ? sym_args : [prod, sym_args]

test/symbolic.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717
@test isequal(differentiate(:(a * x ^ 2), :x), :(a * (2 * x)))
1818
@test isequal(differentiate(:(2 ^ x), :x), :(*(0.6931471805599453, ^(2, x))))
1919
@test isequal(differentiate(:(sin(x)), :x), :(cos(x)))
20-
@test isequal(differentiate(:(cos(x)), :x), :(*(-1,sin(x)))) # needs a better simplify
20+
@test isequal(differentiate(:(cos(x)), :x), :(-sin(x)))
2121
@test isequal(differentiate(:(tan(x)), :x), :(1 + tan(x)^2))
2222
@test isequal(differentiate(:(exp(x)), :x), :(exp(x)))
2323
@test isequal(differentiate(:(log(x)), :x), :(1 / x))
2424
@test isequal(differentiate(:(sin(x) + sin(x)), :x), :(cos(x) + cos(x)))
25-
@test isequal(differentiate(:(sin(x) - cos(x)), :x), :(-(cos(x),*(-1,sin(x))))) # Simplify -(a, -(b)) => +(a, b)
25+
@test isequal(differentiate(:(sin(x) - cos(x)), :x), :(cos(x) + sin(x)))
2626
@test isequal(differentiate(:(x * sin(x)), :x), :(sin(x) + x * cos(x)))
2727
@test isequal(differentiate(:(x / sin(x)), :x), :((sin(x) - x * cos(x)) / (sin(x)^2)))
2828
@test isequal(differentiate(:(sin(sin(x))), :x), :(*(cos(x),cos(sin(x)))))
29-
@test isequal(differentiate(:(sin(cos(x) + sin(x))), :x), :(*(+(*(-1,sin(x)),cos(x)),cos(+(cos(x),sin(x)))))) # Clean this up
30-
@test isequal(differentiate(:(exp(-x)), :x), :(*(-1,exp(-(x))))) # Simplify this to -(exp(-x))
29+
@test isequal(differentiate(:(sin(cos(x) + sin(x))), :x), :(*(+(-sin(x),cos(x)),cos(+(cos(x),sin(x))))))
30+
@test isequal(differentiate(:(exp(-x)), :x), :(-exp(-x)))
3131
@test isequal(differentiate(:(log(x^2)), :x), :(/(*(2,x),^(x,2))))
3232
@test isequal(differentiate(:(x^n), :x), :(*(n, ^(x, -(n, 1)))))
3333
@test isequal(differentiate(:(n^x), :x), :(*(^(n, x), log(n))))
@@ -47,7 +47,7 @@
4747
#
4848

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

5252
# TODO: Make these work
5353
# differentiate(:(sin(x)), :x)(0.0)

0 commit comments

Comments
 (0)