Python Forum
How to multiply a matrix with herself, until the zero matrix results
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to multiply a matrix with herself, until the zero matrix results
#1
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.
Reply
#2
Please show what you have tried so far.
Reply
#3
(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 ;) )
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help with scrolling text on RGB Matrix Foutsy 3 266 Apr-09-2024, 09:00 PM
Last Post: deanhystad
  Help with Python Script to generate SVG Dot Matrix Pattern for LED Light Guide iamrickm 2 756 Aug-25-2023, 06:07 PM
Last Post: iamrickm
  Check if two matrix are equal and of not add the matrix to the list quest 3 823 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  Print confusion matrix MrSonoa 1 910 Apr-20-2023, 04:17 PM
Last Post: farshid
  Can't modify a simple matrix Lewkiy 3 910 Feb-05-2023, 02:59 PM
Last Post: Lewkiy
  matrix multiplication term by term karolina 1 827 Dec-10-2022, 10:11 AM
Last Post: Gribouillis
  using StandardScaler on a heterogenous matrix. Led_Zeppelin 0 934 Jun-23-2022, 05:24 PM
Last Post: Led_Zeppelin
  Loading an array into a matrix Scott 1 1,178 Jun-01-2022, 07:08 PM
Last Post: paul18fr
  Numpy error while filling up matrix with Characters august 4 1,853 Apr-13-2022, 10:28 PM
Last Post: august
  matrix number assignement to the random indices juniorcoder 4 1,920 Feb-19-2022, 02:18 PM
Last Post: juniorcoder

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020