sagemath / sage

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

Characteristic polynomial of integer matrix has wrong degree #35846

Open mvkma opened 1 year ago

mvkma commented 1 year ago

Is there an existing issue for this?

Did you read the documentation and troubleshoot guide?

Environment

- **OS**: Arch Linux
- **Sage Version**: 10.0

Steps To Reproduce

Consider the following 35 x 35 integer matrix:

M = matrix([
    [0, 113, 0, 229, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 173, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 113, 0, 113, 0, 0, 0, 0, 0, 229, 0, 173, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 113, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 229, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 229, 0, 113, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 113, 0, 113, 0, 0, 0, 113, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 229, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 229, 0, 0, 0, 113, 0, 0, 0, 0, 0, 113, 0, 173, 0, 0, 0, 0, 0, 0, 0, 0, 113, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 113, 0, 0, 0, 0, 0, 0, 0, 0, 0, 229, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 173, 0, 0, 0, 0, 113, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 113, 0, 113, 0, 0, 0, 0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, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 173, 0, 0, 0, 0, 113, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 113],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 173, 0, 0, 0, 0, 113, 113, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 113, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 229, 0, 113, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 173, 0, 229, 0, 0, 113, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 113, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 113, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 113, 0, 0, 0, 0, 0, 173],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 229, 0, 0, 0, 113, 0, 0, 0, 173, 0, 0, 0, 0, 0, 0, 0, 113, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 113, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 173, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

print(M.parent()) # Full MatrixSpace of 35 by 35 dense matrices over Integer Ring

print(M.dimensions()) # (35, 35)

Expected Behavior

The characteristic polynomial is expected to be of degree 35.

Actual Behavior

The characteristic polynomial has a lower degree when computed with linbox (the default). The other algorithms give the expected result:

M._clear_cache()
print(M.charpoly()) # x^27

M._clear_cache()
print(M.charpoly(algorithm="flint")) # x^35
M._clear_cache()
print(M.charpoly(algorithm="generic")) # x^35

Additional Information

Changing the base ring to QQ does not fix the problem:

MQ = M.change_ring(QQ)

print(MQ.parent()) # Full MatrixSpace of 35 by 35 dense matrices over Rational Field
MQ._clear_cache()
print(MQ.charpoly(algorithm="linbox")) # x^27
MQ._clear_cache()
print(MQ.charpoly(algorithm="flint")) # x^35
MQ._clear_cache()
print(MQ.charpoly(algorithm="generic")) # x^35
DaveWitteMorris commented 1 year ago

I am unable to reproduce this: I get x^35 in all cases on MacOS 11.7.8 and Ubuntu 22.04 (CoCalc).

Can someone confirm the problem on Arch Linux? (or other platforms?)

vneiger commented 1 year ago

On my side I get x^31 in both cases (for the LinBox variants). Ubuntu 22.04, Sage 10.1.beta1. (I have LinBox installed from source on my machine, I am not sure if Sage is calling that one or another one built specially for Sage; I can check if that helps.)

gilcu3 commented 1 year ago

I am getting exactly the same results as @mvkma , using archlinux and the sagemath version from the repo 10.0

Marvin-P-Android commented 1 year ago

I also get the same results as @mvkma on Fedora 37 using Sage 9.6 (from the repos). I tried doing the same calculation using LinBox directly and also get x^27 that way (using version 1.6.3 which I believe is the latest stable release), so it seems the problem is with LinBox itself.

vneiger commented 1 year ago

I tried doing the same calculation using LinBox directly and also get x^27 that way

On LinBox directly, latest version 1.7.0, I get the same issue. So I opened the issue https://github.com/linbox-team/linbox/issues/305 there.