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

simplifying Pow of Div + test #373

Merged
merged 6 commits into from
Jun 15, 2022
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
7 changes: 6 additions & 1 deletion src/polyform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ Note that since PolyForms have different `hash`es than SymbolicUtils expressions
`substitute` may not work if `polyform=true`
"""
function simplify_fractions(x; polyform=false)

x = Postwalk(quick_cancel)(x)

!needs_div_rules(x) && return x
Expand Down Expand Up @@ -394,7 +395,11 @@ But it will simplify `(x - 5)^2*(x - 3) / (x - 5)` to `(x - 5)*(x - 3)`.
Has optimized processes for `Mul` and `Pow` terms.
"""
function quick_cancel(d)
if isdiv(d)
if ispow(d) && isdiv(d.base)
return quick_cancel((d.base.num^d.exp) / (d.base.den^d.exp))
elseif ismul(d) && any(isdiv, unsorted_arguments(d))
return prod(unsorted_arguments(d))
elseif isdiv(d)
num, den = quick_cancel(d.num, d.den)
return Div(num, den)
else
Expand Down
1 change: 1 addition & 0 deletions test/polyform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ include("utils.jl")
@test repr(simplify_fractions(((x-y+z)*(x+4z+1)) /
(y*(2x - 3y + 3z) +
x*(x + z)))) == repr(simplify_fractions((1 + x + 4z) / (x + 3.0y)))
@test simplify_fractions( (1/x)^2 * x^2) == 1
@test simplify_fractions(x/(x+3) + 3/(x+3)) == 1
@test repr(simplify(simplify_fractions(cos(x)/sin(x) + sin(x)/cos(x)))) == "2 / sin(2x)"
end
Expand Down