admin管理员组

文章数量:1313280

I am trying to solve a mathematical problem related to matrices using numpy as shown below:

I am really finding it hard to represent this kind matrix structure using numpy. I really donot want to type these values because I want to understand how this kind of structures are represented using python. Consider the empty places as zero. Thank you for great help.

I am trying to solve a mathematical problem related to matrices using numpy as shown below:

I am really finding it hard to represent this kind matrix structure using numpy. I really donot want to type these values because I want to understand how this kind of structures are represented using python. Consider the empty places as zero. Thank you for great help.

Share Improve this question edited Jan 31 at 15:02 ThomasIsCoding 103k9 gold badges36 silver badges101 bronze badges asked Jan 30 at 20:18 mchaudh4mchaudh4 2171 silver badge9 bronze badges 2
  • To clarify, this is not tridiagonal (tridiagonal only has nonzero values on the main diagonal and the two off-diagonals). Can you clarify what you mean by "understand[ing] how this kind of structures are represented using python"? – jared Commented Jan 30 at 20:47
  • Thank you for the response @jared. There are ways to put a complete diagonal or off-diagonal to same value, but here there are three continuous -1, and then there is zero. Not sure what this kind of structure should be called. – mchaudh4 Commented Jan 30 at 20:59
Add a comment  | 

2 Answers 2

Reset to default 2

The matrix has to be decomposed into several parts. First, the middle region forms a block diagonal matrix, where each block is a 4x4 Toeplitz matrix.

# makes a shifted diagonal matrix
def E(n, v, k):
    return v * np.eye(n, k=k)

def toeplitz_block(n, v_list, k_list):
    return sum(E(n, v, k) for v, k in zip(v_list, k_list))

Then we can do the following:

n = 4 # size of block
m = 3 # how many blocks

# Make block diagonal matrix A
A = np.zeros((m, n, m, n))
u, v = np.diag_indices(m)
A[u, :, v, :] = toeplitz_block(n, [-1, 3, -1], [-1, 0, 1])
A = A.reshape(m * n, m * n)

print(A.astype(int))
# Output:
[[ 3 -1  0  0  0  0  0  0  0  0  0  0]
 [-1  3 -1  0  0  0  0  0  0  0  0  0]
 [ 0 -1  3 -1  0  0  0  0  0  0  0  0]
 [ 0  0 -1  3  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  3 -1  0  0  0  0  0  0]
 [ 0  0  0  0 -1  3 -1  0  0  0  0  0]
 [ 0  0  0  0  0 -1  3 -1  0  0  0  0]
 [ 0  0  0  0  0  0 -1  3  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  3 -1  0  0]
 [ 0  0  0  0  0  0  0  0 -1  3 -1  0]
 [ 0  0  0  0  0  0  0  0  0 -1  3 -1]
 [ 0  0  0  0  0  0  0  0  0  0 -1  3]]

To get the final desired matrix, we can just add another Toeplitz matrix that has the diagonal of -1s.

B = A + E(n * m, -1, -4)
print(B.astype(int))
# Output:
[[ 3 -1  0  0  0  0  0  0  0  0  0  0]
 [-1  3 -1  0  0  0  0  0  0  0  0  0]
 [ 0 -1  3 -1  0  0  0  0  0  0  0  0]
 [ 0  0 -1  3  0  0  0  0  0  0  0  0]
 [-1  0  0  0  3 -1  0  0  0  0  0  0]
 [ 0 -1  0  0 -1  3 -1  0  0  0  0  0]
 [ 0  0 -1  0  0 -1  3 -1  0  0  0  0]
 [ 0  0  0 -1  0  0 -1  3  0  0  0  0]
 [ 0  0  0  0 -1  0  0  0  3 -1  0  0]
 [ 0  0  0  0  0 -1  0  0 -1  3 -1  0]
 [ 0  0  0  0  0  0 -1  0  0 -1  3 -1]
 [ 0  0  0  0  0  0  0 -1  0  0 -1  3]]

Here is another option

n = 12
msk = (v:=np.arange(n))-v[:,None]
A = 3*(msk==0) - (np.logical_or(abs(msk)==1,msk==-4))

such that

A=
 [[ 3 -1  0  0  0  0  0  0  0  0  0  0]
 [-1  3 -1  0  0  0  0  0  0  0  0  0]
 [ 0 -1  3 -1  0  0  0  0  0  0  0  0]
 [ 0  0 -1  3 -1  0  0  0  0  0  0  0]
 [-1  0  0 -1  3 -1  0  0  0  0  0  0]
 [ 0 -1  0  0 -1  3 -1  0  0  0  0  0]
 [ 0  0 -1  0  0 -1  3 -1  0  0  0  0]
 [ 0  0  0 -1  0  0 -1  3 -1  0  0  0]
 [ 0  0  0  0 -1  0  0 -1  3 -1  0  0]
 [ 0  0  0  0  0 -1  0  0 -1  3 -1  0]
 [ 0  0  0  0  0  0 -1  0  0 -1  3 -1]
 [ 0  0  0  0  0  0  0 -1  0  0 -1  3]]

本文标签: pythonRepresenting tridiagonal matrix using numpyStack Overflow