Skip to content

Commit

Permalink
Merge pull request #373 from jkosata/pow_of_div_simplification
Browse files Browse the repository at this point in the history
simplifying Pow of Div + test
  • Loading branch information
shashi authored Jun 15, 2022
2 parents bbdedf0 + ff1f555 commit c109aa2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
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

0 comments on commit c109aa2

Please sign in to comment.