To compute the n-th matrix power of a square numpy array, use

`numpy.linalg.matrix_power(A, n)`

.

In your case however the loop will fail to find an n such that A to the power n is zero. Such matrices are nilpotent matrices and their only eigenvalue is zero.

The following code shows that A has non-zero eigenvalues, hence it is not nilpotent.

>>> import numpy as np
>>> A = np.array([[2,2,2,2,-4], [7,1,1,1,-5], [1,7,1,1,-5], [1,1,7,1,-5], [1,1,1,7,-5]])
>>> np.linalg.eigvals(A)
array([ 0.00474885+0.j , 0.00146542+0.00451575j,
0.00146542-0.00451575j, -0.00383984+0.00278848j,
-0.00383984-0.00278848j])
>>>

Conclusion: there is no such integer n.

Also note that if a matrix of shape (d, d) is nilpotent, the smallest power n which annihilates A satisfies 1 <= n <= d, so that you don't have to examine an infinite number of n's.

EDIT: apparently there is a precision issue with numpy.linalg.eigvals() which I don't understand. Using sympy instead of numpy it turns out that the matrix's only eigenvalue is 0 as the following code shows

>>> import sympy as sy
>>> M = sy.Matrix(A)
>>> M
Matrix([
[2, 2, 2, 2, -4],
[7, 1, 1, 1, -5],
[1, 7, 1, 1, -5],
[1, 1, 7, 1, -5],
[1, 1, 1, 7, -5]])
>>> M.eigenvals()
{0: 5}

This means that the only eigenvalue of M is 0 with multiplicity 5. Hence M is nilpotent, in fact

>>> M ** 5
Matrix([
[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]])
>>>
>>>
>>> np.linalg.matrix_power(A, 5)
array([[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]])

Note that the power 5 here is not related to the multiplicity of the eigenvalue. The multiplicity of 0 as the eigenvalue of a (d, d) nilpotent matrix is always d.

Sympy uses multiprecision numbers, this may be the cause of the better result, but let us remark that sympy gives us an exact integer eigenvalue here.

Numpy computes eigenvalues by calling a numerical library named LAPACK. I don't know the precision issues that it has.