Home > other >  Python, how to use the three one-dimensional array structure tridiagonal matrix?
Python, how to use the three one-dimensional array structure tridiagonal matrix?

Time:10-02

The import numpy as np

Assume that there are three one-dimensional array:

Upper_diag=np. Array (,4,3,2,1 [5])

Center_diag=np. Array (,9,9,9,9 [9])

Lower_diag=np. Array ([1, 2, 3, 4, 5])

What can convenient command in the form of the above three array into the following three diagonal matrix:

September 4 0 0 0

1 9 3 0 0

9 2 0 0

0 0 3 9 1

0 0 0 4 9

CodePudding user response:

Didn't think convenient command, is that ok?
 
The import numpy as np

Upper_diag=np. Array (,4,3,2,1 [5])
Center_diag=np. Array (,9,9,9,9 [9])
Lower_diag=np. Array ([1, 2, 3, 4, 5])

D=np. Insert (np) eye (5, dtype=int), 5, values=np. The zeros (5, dtype=int), axis=0) [1]
X=d * upper_diag
Y=np. Diag (center_diag)
Z=which * lower_diag
Print (x + y + z)