##### How to multiply a matrix with herself, until the zero matrix results
 How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly Unladen Swallow Posts: 2 Threads: 1 Joined: May 2021 Reputation: May-02-2021, 04:09 PM (This post was last modified: May-02-2021, 06:25 PM by Larz60+. Edit Reason: Added inline code tags ) 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 Larz60+ aetate et sapientia Posts: 10,426 Threads: 392 Joined: Sep 2016 Reputation: May-02-2021, 06:25 PM Please show what you have tried so far. Reply peanutbutterandjelly Unladen Swallow Posts: 2 Threads: 1 Joined: May 2021 Reputation: May-02-2021, 07:17 PM (This post was last modified: May-02-2021, 07:17 PM by peanutbutterandjelly.) (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 Posts: 3,025 Threads: 41 Joined: Jan 2018 Reputation: May-03-2021, 06:30 AM (This post was last modified: May-03-2021, 06:30 AM by Gribouillis.) 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 NumPy Matrix help ntailor97 1 289 Apr-08-2021, 03:23 AM Last Post: ntailor97 How do I make a symmetric matrix from a column vector? leocsmith 3 420 Mar-30-2021, 10:17 AM Last Post: leocsmith Matrix indexing and initialization in " for in" loop QuintenR 2 405 Dec-23-2020, 05:59 PM Last Post: QuintenR Matrix Operations Without Numpy or Incorporating Python into Webpage ebryski 1 567 Nov-26-2020, 12:50 PM Last Post: jefsummers matrix convert abbaszandi 2 415 Nov-13-2020, 08:19 PM Last Post: deanhystad I can't see all elements of matrix quest 4 564 Nov-06-2020, 06:41 PM Last Post: Larz60+ finding element of specific field in pandas adjacency matrix amjass12 0 335 Oct-27-2020, 09:27 AM Last Post: amjass12 Creating symbolic matrix automated NMMST 2 566 Oct-05-2020, 01:42 AM Last Post: scidam Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 560 Aug-21-2020, 12:20 PM Last Post: DeaD_EyE I want to multiply two variables, but I get an error LeqendFire 3 595 Aug-09-2020, 03:52 PM Last Post: LeqendFire

Forum Jump:

### User Panel Messages

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