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
2 Answers
Reset to default 2The 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
版权声明:本文标题:python - Representing tridiagonal matrix using numpy - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741937801a2405961.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论