Home > front end >  Issues with generating the following block matrix using horizontal stacking and vertical stacking
Issues with generating the following block matrix using horizontal stacking and vertical stacking

Time:05-25

I am trying to generate the following block matrix consisting of submatrices A and B, and N is a positive integer. So far, my code is as follows:

C_lower = B
for j in range(0,N):
    for i in range(0,N-j):
        col = np.linalg.matrix_power(A,i) @ B
        C = np.hstack(np.vstack((C_lower,col)))


enter image description here

However, it seems like my code is not working because the loop continues forever. Any suggestions?

Similarly, I'm also having issues with constructing the following block diagonal matrices: enter image description here

I tried using block_diag from scipy, but there is no way I can repeat Q as many times as N is equal to (i.e., N = 50 in my case). I had to do block_diag(Q,Q,Q,Q,Q,Q,Q.......) in order to get the block diagonal matrix I want.

CodePudding user response:

Here's the answer to your first question. There are a number of issues in your code. This is a better way of achieving what you want:

C = np.zeros((N, N, A.shape[0], B.shape[1]))
for i in range(N):
    for j in range(i   1):
        C[i, j] = np.linalg.matrix_power(A, i - j) @ B

Similarly for your second question:

Q_ = np.zeros((N, N, *Q.shape))
for i in range(N):
    Q_[i, i] = Q
  • Related