sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.4k stars 474 forks source link

factor() does not work in a simple case #31687

Open GMS103 opened 3 years ago

GMS103 commented 3 years ago

In Sage 9.2 on SageCell and CoCalc (same on Sage 9.3.rc2):

sage: version()
'SageMath version 9.2, Release Date: 2020-10-24'

sage: x, y = var('x, y')

sage: f = (x - 5)*y/x - (x - 6)*y/x ; f
(x - 5)*y/x - (x - 6)*y/x

sage: f.factor()  # does not work
(x - 5)*y/x - (x - 6)*y/x

sage: (f*x).factor()  # does not work either
x*((x - 5)*y/x - (x - 6)*y/x)

sage: (f*x).expand()/x  # expected result
y/x

Of course, this is a very simple example showing the problem.

CC: @slel

Component: factorization

Issue created by migration from https://trac.sagemath.org/ticket/31687

slel commented 3 years ago
comment:1

A workaround is to factor the numerator and denominator:

sage: f.numerator().factor() / f.denominator().factor()
y/x

Or, in this case, to work in the fraction field of a polynomial ring.

sage: R = PolynomialRing(QQ, ['x', 'y'])
sage: F = R.fraction_field()
sage: F(f)
y/x
sage: F(f).factor()
x^-1 * y
slel commented 3 years ago

Description changed:

--- 
+++ 
@@ -1,23 +1,22 @@
-Tested with https://sagecell.sagemath.org and CoCalc:
+In Sage 9.2 on SageCell and CoCalc (same on Sage 9.3.rc2):

 ```python
 sage: version()
 'SageMath version 9.2, Release Date: 2020-10-24'

-sage: x,y=var('x,y')
+sage: x, y = var('x, y')

-sage: f=(x-5)*y/x-(x-6)*y/x ; f
+sage: f = (x - 5)*y/x - (x - 6)*y/x ; f
 (x - 5)*y/x - (x - 6)*y/x

-sage: f.factor() # does not work
+sage: f.factor()  # does not work
 (x - 5)*y/x - (x - 6)*y/x

-sage: (f*x).factor() # does not work either
+sage: (f*x).factor()  # does not work either
 x*((x - 5)*y/x - (x - 6)*y/x)

-sage: (f*x).expand()/x # expected result
+sage: (f*x).expand()/x  # expected result
 y/x

Of course, this is a very simple example showing the problem.

-(I am sorry, I do not have any Sage 9.3.* to test.)

GMS103 commented 3 years ago
comment:2

Well, actually in this case it suffices to expand:

sage: f.expand()                                                                                                        
y/x

But I was wondering about the cause of this behaviour (I do not think I would be able to find it myself).