Skip to content
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
25 changes: 25 additions & 0 deletions src/symbolic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,29 @@ function mul_numeric_args(args)
(prod, sym_args)
end

cancel_common(num, den) = (num, den)
cancel_common(num::Symbol, den::Symbol) = num==den ? (1, 1) : (num, den)
cancel_common(num::Expr, den::Symbol) = cancel_common(num, Expr(:call, :*, den))
cancel_common(num::Symbol, den::Expr) = cancel_common(Expr(:call, :*, num), den)

function cancel_common(num::Expr, den::Expr)
if num.args[1] != :* || den.args[1] != :*
return (num, den)
end
an = num.args[2:end]
ad = den.args[2:end]
i = 1
while i <= length(ad)
idx = findfirst(an, ad[i])
if idx != 0
splice!(ad, i)
splice!(an, idx)
end
i += 1
end
(Expr(:call, :*, an...), Expr(:call, :*, ad...))
end

# Handle `args` of all lengths
function simplify(::SymbolParameter{:+}, args)
# Remove any 0's in a sum
Expand Down Expand Up @@ -188,6 +211,8 @@ function simplify(::SymbolParameter{:/}, args)
elseif args[1] == 0
return 0
else
args = cancel_common(args[1], args[2])
args = map(simplify, args)
return Expr(:call, :/, args...)
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/symbolic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,11 @@ 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)), 1.0)
@test isequal(simplify(:((2*x)/x)), 2.0)
@test isequal(simplify(:(x/(2*x))), 0.5)
@test isequal(simplify(:((2*y)/x)), :(/(*(2,y),x)))
@test isequal(simplify(:((5*x)/(2*x))), 2.5)
@test isequal(simplify(:((y*x*5)/(2*x))), :(5*y/2))
@test isequal(simplify(:((5*z)/(2*x))), :((5*z)/(2*x)))