Skip to content

Commit 980e206

Browse files
committed
for whatever reason these rules are necessary
1 parent c93abab commit 980e206

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/polyform.jl

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export PolyForm, simplify_fractions, quick_cancel, flatten_fraction
1+
export PolyForm, simplify_fractions, quick_cancel, flatten_fractions
22
using Bijections
33
using DynamicPolynomials: PolyVar
44

@@ -284,17 +284,32 @@ function simplify_fractions(x)
284284
end
285285

286286
"""
287-
flatten_fraction(x)
287+
flatten_fractions(x)
288288
289289
Flatten nested fractions that are added together.
290290
291291
```julia
292-
julia> flatten_fraction((1+(1+1/a)/a)/a)
292+
julia> flatten_fractions((1+(1+1/a)/a)/a)
293293
(1 + a + a^2) / (a^3)
294294
```
295295
"""
296-
function flatten_fraction(x)
297-
Fixpoint(Postwalk(PassThrough(@acrule ~a::(x->x isa Div) + ~b => add_divs(~a,~b))))(x)
296+
function flatten_fractions(x)
297+
rules = [@acrule ~a::(x->x isa Div) + ~b => add_divs(~a,~b)
298+
@rule *(~~x, ~a / ~b, ~~y) / ~c => *((~~x)..., ~a, (~~y)...) / (~b * ~c)
299+
@rule ~c / *(~~x, ~a / ~b, ~~y) => (~b * ~c) / *((~~x)..., ~a, (~~y)...)]
300+
Fixpoint(Postwalk(Chain(rules)))(x)
301+
end
302+
303+
function fraction_iszero(x)
304+
!istree(x) && return _iszero(x)
305+
# fast path and then slow path
306+
any(_iszero, numerators(flatten_fractions(x))) ||
307+
any(_iszeroexpand, numerators(flatten_fractions(x)))
308+
end
309+
310+
function fraction_isone(x)
311+
!istree(x) && return _isone(x)
312+
_isone(simplify_fractions(flatten_fractions(x)))
298313
end
299314

300315
function needs_div_rules(x)

test/polyform.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,16 @@ end
5353
@eqtest simplify_fractions(3*(x^2)*(y^3)/(3*(x^3)*(y^2))) == y/x
5454
@eqtest simplify_fractions(3*(x^x)/x*y) == 3*(x^x)/x*y
5555
end
56+
57+
@testset "isone iszero" begin
58+
@syms a b c d e f g h i
59+
x = (f + ((((g*(c^2)*(e^2)) / d - e*h*(c^2)) / b + (-c*e*f*g) / d + c*e*i) /
60+
(i + ((c*e*g) / d - c*h) / b + (-f*g) / d) - c*e) / b +
61+
((g*(f^2)) / d + ((-c*e*f*g) / d + c*f*h) / b - f*i) /
62+
(i + ((c*e*g) / d - c*h) / b + (-f*g) / d)) / d
63+
64+
o = (d + (e*((c*(g + (-d*g) / d)) / (i + (-c*(h + (-e*g) / d)) / b + (-f*g) / d))) / b + (-f*(g + (-d*g) / d)) / (i + (-c*(h + (-e*g) / d)) / b + (-f*g) / d)) / d
65+
@test SymbolicUtils.fraction_iszero(x)
66+
@test !SymbolicUtils.fraction_isone(x)
67+
@test SymbolicUtils.fraction_isone(o)
68+
end

0 commit comments

Comments
 (0)