Skip to content

Commit eed6fa3

Browse files
Fix Factor[a == b] (#232)
* Fix Factor[a == b] * Improve Factor documentation
1 parent c518849 commit eed6fa3

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

mathics/builtin/numbers/algebra.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -576,9 +576,21 @@ class Factor(Builtin):
576576
>> Factor[1 / (x^2+2x+1) + 1 / (x^4+2x^2+1)]
577577
= (2 + 2 x + 3 x ^ 2 + x ^ 4) / ((1 + x) ^ 2 (1 + x ^ 2) ^ 2)
578578
579-
## Issue659
580-
#> Factor[{x+x^2}]
581-
= {x (1 + x)}
579+
Factor can also be used with equations:
580+
>> Factor[x a == x b + x c]
581+
= a x == x (b + c)
582+
583+
And lists:
584+
>> Factor[{x + x^2, 2 x + 2 y + 2}]
585+
= {x (1 + x), 2 (1 + x + y)}
586+
587+
It also works with more complex expressions:
588+
>> Factor[x ^ 3 + 3 x ^ 2 y + 3 x y ^ 2 + y ^ 3]
589+
= (x + y) ^ 3
590+
591+
You can use Factor to find when a polynomial is zero:
592+
>> x^2 - x == 0 // Factor
593+
= x (-1 + x) == 0
582594
"""
583595

584596
attributes = listable | protected
@@ -592,14 +604,9 @@ def apply(self, expr, evaluation):
592604

593605
try:
594606
result = sympy.together(expr_sympy)
595-
numer, denom = result.as_numer_denom()
596-
if denom == 1:
597-
result = sympy.factor(expr_sympy)
598-
else:
599-
result = sympy.factor(numer) / sympy.factor(denom)
607+
return from_sympy(sympy.factor(result))
600608
except sympy.PolynomialError:
601609
return expr
602-
return from_sympy(result)
603610

604611

605612
class FactorTermsList(Builtin):

0 commit comments

Comments
 (0)