|
1 | | -export PolyForm, simplify_fractions, quick_cancel, flatten_fraction |
| 1 | +export PolyForm, simplify_fractions, quick_cancel, flatten_fractions |
2 | 2 | using Bijections |
3 | 3 | using DynamicPolynomials: PolyVar |
4 | 4 |
|
@@ -284,17 +284,32 @@ function simplify_fractions(x) |
284 | 284 | end |
285 | 285 |
|
286 | 286 | """ |
287 | | - flatten_fraction(x) |
| 287 | + flatten_fractions(x) |
288 | 288 |
|
289 | 289 | Flatten nested fractions that are added together. |
290 | 290 |
|
291 | 291 | ```julia |
292 | | -julia> flatten_fraction((1+(1+1/a)/a)/a) |
| 292 | +julia> flatten_fractions((1+(1+1/a)/a)/a) |
293 | 293 | (1 + a + a^2) / (a^3) |
294 | 294 | ``` |
295 | 295 | """ |
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(_iszero∘expand, 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))) |
298 | 313 | end |
299 | 314 |
|
300 | 315 | function needs_div_rules(x) |
|
0 commit comments