Ask Your Question

Revision history [back]

Sage has several ways to factor polynomial expressions.

Defined as symbolic expressions living in the symbolic ring, they factor as observed in the question.

It seems symbolic expressions in Sage are not able to hold a factor if it is a constant. As observed in the answer by @Emmanuel Charpentier, this is in contrast to Maxima.

To further illustrate that, consider the following example:

sage: a3, a6 = SR.var('a3, a6')
sage: q = (a3 - a6)
sage: p = x * q
sage: p
(a3 - a6)*x
sage: p.subs({x: 2})
2*a3 - 2*a6

There might or might not be a way to remedy that for symbolic expressions.

Defined in a polynomial ring, such expressions will factor differently though.

Define a polynomial ring over the integers:

sage: R.<a3, a6> = ZZ[]
sage: R
Multivariate Polynomial Ring in a3, a6 over Integer Ring

Define a polynomial in that ring:

sage: p = 2*a3 - 2*a6
sage: p
2*a3 - 2*a6

Factor it:

sage: p.factor()
2 * (a3 - a6)

Note that factoring in polynomials over the rationals would work differently.