Why can one not factor 0?

asked 2017-10-11 21:50:44 +0200

Richard_L gravatar image

updated 2017-10-12 22:37:40 +0200

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()
    Traceback (most recent call last):
    ArithmeticError: factorization of 0 is not defined

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

edit retag flag offensive close merge delete


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


-  ``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.

dan_fulea gravatar imagedan_fulea ( 2017-10-11 23:26:55 +0200 )edit

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

Richard_L gravatar imageRichard_L ( 2017-10-12 23:29:55 +0200 )edit

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.)

John Palmieri gravatar imageJohn Palmieri ( 2017-10-13 19:46:59 +0200 )edit

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

Richard_L gravatar imageRichard_L ( 2017-10-13 22:38:57 +0200 )edit