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

Combine terms in multiplication and addition #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions src/symbolic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ function simplify(::SymbolParameter{:+}, args)
# Special Case: simplify(:(+x)) == x
elseif length(args) == 1
return args[1]
# Special Case: simplify(:(x + x + x)) == 3*x
elseif all(args .== args[1])
return Expr(:call, :*, args[1], length(args))
else
(sum, sym_args) = sum_numeric_args(args)
args = sum==0 ? sym_args : [sum, sym_args]
Expand Down Expand Up @@ -171,6 +174,9 @@ function simplify(::SymbolParameter{:*}, args)
# Special Case: simplify(:(x * y * z * 0)) == 0
elseif any(args .== 0)
return 0
# Special Case: simplify(:(x * x * x * x)) == x^4
elseif all(args .== args[1])
return Expr(:call, :^, args[1], length(args))
else
(prod, sym_args) = mul_numeric_args(args)
args = prod==1 ? sym_args : [prod, sym_args]
Expand Down
10 changes: 6 additions & 4 deletions test/symbolic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
@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) + sin(x)), :x), :(2*cos(x)))
@test isequal(differentiate(:(sin(x) - cos(x)), :x), :(-(cos(x),*(-1,sin(x))))) # Simplify -(a, -(b)) => +(a, b)
@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)))
Expand All @@ -32,16 +32,14 @@
@test isequal(differentiate(:(x^n), :x), :(*(n, ^(x, -(n, 1)))))
@test isequal(differentiate(:(n^x), :x), :(*(^(n, x), log(n))))
@test isequal(differentiate(:(n^n), :x), 0)
@test isequal(differentiate(:(sin(x)^2), :x), :(2 * cos(x) * sin(x)))

#
# Multivariate Calculus
#

@test isequal(differentiate(:(sin(x) + sin(y)), [:x, :y]), [:(cos(x)), :(cos(y))])

# TODO: Get the generalized power rule right.
# @test isequal(differentiate(:(sin(x)^2), :x), :(2 * sin(x) * cos(x)))

#
# Strings instead of symbols
#
Expand Down Expand Up @@ -88,3 +86,7 @@ end
@test isequal(simplify(:(x*3)), :(*(3,x)))
@test isequal(simplify(:(x*3*4)), :(*(12,x)))
@test isequal(simplify(:(2*y*x*3)), :(*(6,y,x)))

@test isequal(simplify(:(x*x*x)), :(x^3))
@test isequal(simplify(:(x+x+x)), :(3*x))
@test isequal(simplify(:(f(x,y)+f(x,y))), :(2*f(x,y)))