Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define minimal polynomial for any square matrix over any field #39697

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/sage/matrix/matrix2.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3035,6 +3035,61 @@ cdef class Matrix(Matrix1):
self.cache('minpoly', mp)
return mp

def minpoly_lin(self, var='x', **kwds):
r"""
Return the minimal polynomial of ``self``.

This uses a purely linear-algebraic algorithm (essentially
Gaussian elimination, applied to the powers of ``self``).
It is slow but it requires no factorization of polynomials.

EXAMPLES::

sage: # needs sage.rings.finite_rings
sage: A = matrix(GF(9, 'c'), 4, [1,1,0,0, 0,1,0,0, 0,0,5,0, 0,0,0,5])
sage: A.minpoly_lin()
x^3 + 2*x^2 + 2*x + 1
sage: A.minpoly_lin()(A) == 0
True
sage: CF = CyclotomicField()
sage: i = CF.gen(4)
sage: A = matrix(CF, 4, [1,1,0,0, 0,1,0,0, 0,0,1+i,0, 0,0,0,-i])
sage: A.minpoly_lin()
x^4 - 3*x^3 + (4 - E(4))*x^2 + (-3 + 2*E(4))*x + 1 - E(4)

The default variable name is `x`, but you can specify
another name::

sage: # needs sage.rings.finite_rings
sage: A.minpoly_lin('y')
y^4 - 3*y^3 + (4 - E(4))*y^2 + (-3 + 2*E(4))*y + 1 - E(4)
"""
f = self.fetch('minpoly')
if f is not None:
return f.change_variable_name(var)

n = self.nrows()
F = self.base_ring()
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
P = PolynomialRing(F, "x")
x = P.gen()
from sage.modules.free_module_element import vector
from sage.matrix.constructor import matrix

pow = self**0
pows = []
for k in range(n+1):
pows.append(vector(pow.list()))
pow *= self
Mpows = matrix(pows)
try:
cs = Mpows.solve_left(vector(pow.list()))
except ValueError:
continue
mp = x**(k+1) - P.sum(cs[i] * x**i for i in range(k+1))
self.cache('minpoly', mp)
return mp

def _test_minpoly(self, **options):
"""
Check that :meth:`minpoly` works.
Expand Down
Loading