# LCM of 3 polynomials Anonymous

why changing the order of the parameters gives different reults ?

sage: x,y = var('x y')
sage: LCM([x^2 - y^2, x^2 + 2*x*y + y^2, x^3 + y^3])
(x^3 + y^3)*(x^2 + 2*x*y + y^2)*(x^2 - y^2)/(x + y)^2
sage: LCM([x^2 - y^2, x^3 + y^3, x^2 + 2*x*y + y^2])
(x^3 + y^3)*(x^2 + 2*x*y + y^2)*(x^2 - y^2)/(x + y)^3

edit retag close merge delete

Sort by » oldest newest most voted

This is clearly a bug in the lcm method of symbolic expressions. Note that there is no bug if you use a proper polynomial ring instead of mere symbolic expressions:

sage: P.<x, y> = QQ[]
sage: P
Multivariate Polynomial Ring in x, y over Rational Field
sage: a, b, c = x^2 - y^2, x^2 + 2*x*y + y^2, x^3 + y^3
sage: LCM([a, b, c])
x^5 - x^3*y^2 + x^2*y^3 - y^5
sage: LCM([a, c, b])
x^5 - x^3*y^2 + x^2*y^3 - y^5


With the symbolic ring (i.e. with x and y being symbolic variables created via var()), there appears the bug that you pointed out:

sage: x, y = var('x y')
sage: a, b, c = x^2 - y^2, x^2 + 2*x*y + y^2, x^3 + y^3
sage: LCM([a, b, c]).simplify_rational()  # correct answer
x^5 - x^3*y^2 + x^2*y^3 - y^5
sage: LCM([a, c, b]).simplify_rational()  # wrong answer!
x^4 - x^3*y + x*y^3 - y^4


Looking at Sage's source code, this bug can be traced back to

sage: s = a.lcm(c)
sage: s
(x^3 + y^3)*(x^2 - y^2)/(x + y)
sage: s.gcd(b)
(x + y)^2
sage: s.simplify_rational().gcd(b)
x + y


A fix consists in modifying the code of Expression.lcm in line 8097 of src∕sage/symbolic/expression.pyx from

return 0 if sb.is_trivial_zero() else sb / self.gcd(b)


to

return 0 if sb.is_trivial_zero() else sb / self.simplify_rational().gcd(b)


I've opened https://trac.sagemath.org/ticket/33509 for this.

more