python 对角阵_python – 如何将块变换成块对角矩阵(NumPy)

scipy.linalg有一个block_diag函数自动执行

>>> a1 = np.array([[1,1,1],[1,1,1],[1,1,1]])

>>> a2 = np.array([[2,2,2],[2,2,2],[2,2,2]])

>>> a3 = np.array([[3,3,3],[3,3,3],[3,3,3]])

>>> import scipy.linalg

>>> scipy.linalg.block_diag(a1, a2, a3)

array([[1, 1, 1, 0, 0, 0, 0, 0, 0],

[1, 1, 1, 0, 0, 0, 0, 0, 0],

[1, 1, 1, 0, 0, 0, 0, 0, 0],

[0, 0, 0, 2, 2, 2, 0, 0, 0],

[0, 0, 0, 2, 2, 2, 0, 0, 0],

[0, 0, 0, 2, 2, 2, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 3, 3, 3],

[0, 0, 0, 0, 0, 0, 3, 3, 3],

[0, 0, 0, 0, 0, 0, 3, 3, 3]])

>>> r = np.array([[1,1,1,0,0,0,0,0,0],[1,1,1,0,0,0,0,0,0],[1,1,1,0,0,0,0,0,0], [0,0,0,2,2,2,0,0,0],[0,0,0,2,2,2,0,0,0],[0,0,0,2,2,2,0,0,0],[0,0,0,0,0,0,3,3,3],[0,0,0,0,0,0,3,3,3],[0,0,0,0,0,0,3,3,3]])

>>> (scipy.linalg.block_diag(a1, a2, a3) == r).all()

True

你可能感兴趣的:(python,对角阵)