Python Forum
How to multiply a matrix with herself, until the zero matrix results - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to multiply a matrix with herself, until the zero matrix results (/thread-33530.html)



How to multiply a matrix with herself, until the zero matrix results - peanutbutterandjelly - May-02-2021

Hi there!

I'm not quite sure how to code the following problem:
I have to multiply a given matrix A with herself that often, until the zero matrix results.

The matrix A is the following (using 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]])

Could it be the while loop? But I haven't succeeded yet.


RE: How to multiply a matrix with herself, until the zero matrix results - Larz60+ - May-02-2021

Please show what you have tried so far.


RE: How to multiply a matrix with herself, until the zero matrix results - peanutbutterandjelly - May-02-2021

(May-02-2021, 06:25 PM)Larz60+ Wrote: Please show what you have tried so far.

Yes, sorry could have added it right from the beginning.
Here my "best" try:

1.So first I defined the exponent as 1
n = 1
2.Then I defined a zero-matrix with 5 rows and 5 colums
Zero_Matrix = np.zeros(25).reshape(5,5)
3. Then I TRIED to define the while loop:
while A**n != Zero_Matrix:
n= n + 1



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]])     
n = 1
Zero_Matrix = np.zeros(25).reshape(5,5)  

while A**n != Zero_Matrix:
    n= n + 1
My idea is to run the while loop as long as the Matrix A to the exponent n does NOT equal the zero matrix. But something is still wrong with the while loop (I'm quite new with Python so I certainly made somewhere a big mistake ;) )


RE: How to multiply a matrix with herself, until the zero matrix results - Gribouillis - May-03-2021

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.