# Why can one not factor 0?

I was trying to understand the structure of (a-b), where "a" and "b" were two very large expressions. So I was doing things like (a-b).simplify_full().factor(). I discovered the following anomaly.

     sage: var ('a', 'b')    #Adequate for illustration
(a, b)
sage: (a-b); (a-b).factor()
a - b
a - b
sage: b=a               #By chance, "a" and "b" were equivalent.
sage: (a-b); (a-b).factor()
0
Traceback (most recent call last):
...
ArithmeticError: factorization of 0 is not defined


I would have expected 0.factor() to give 0.

edit retag close merge delete

A closer look at 0.factor?? shows that this case should not occur:

INPUT:

-  n - an nonzero integer


From my point of view, the implementer was blessed to translate "factorization" as "prime factor decomposition". (With recorded unit.) And zero is not a prime. Note that a factorization object is more complicated, for instance:

sage: f = (-12).factor()
sage: f.__dict__
{'_Factorization__cr': False,
'_Factorization__unit': -1,
'_Factorization__universe': Integer Ring,
'_Factorization__x': [(2, 2), (3, 1)]}
sage: print f
-1 * 2^2 * 3


so there is a difference between the "printed factorization" and the "factorization object" with the above hidden dictionary representation.

My surprise arises from working in the symbolic ring and being unexpectedly dumped into the integer ring, thereby triggering and error. Annoying.

It's not dumped into the integer ring, but rather converted to a polynomial with rational coefficients (line 10812 in symbolic/expression.pyx). This polynomial happens to be zero, and Sage code declares that the zero polynomial cannot be factored. (Note that if you try (a-b+6).factor(), the answer will be 6, not 2*3: the factorization is done over QQ.)

Thanks. In future I will bracket this pattern with a conditional to avoid the Traceback.