如何将几个2x2矩阵合并为一个已经存在的矩阵?

时间:2019-07-10 10:01:00

标签: python-3.x numpy

我是Python的新手,但是正在使用Numpy创建将成为线性方程系统Ax = B一部分的矩阵。现在,我想定义A-Matrix,但是在将2x2矩阵“合并”到与状态向量相对应的更大Matrix时遇到麻烦。

除第一行和最后一行外,A矩阵是一个对称矩阵,对角线上有2x2矩阵(其余为零)。我的第一个尝试是创建一个零矩阵,然后插入计算得出的2x2矩阵(它们是分别计算的),但是由于结果很复杂,因此变成了实数值。这可能是实现我想要的东西的一种拙劣的方式,因此,如果有更好的解决方案,我将不胜感激。先感谢您!

def impedance_matrix_A(self):      
    impedance_matrix = np.zeros((12,12))
    #Add A-matrices to impedance-matrix
    impedance_matrix[0:2,0:2] = self.A_back_load
    impedance_matrix[2:4,2:4] = self.A_o_backing
    impedance_matrix[4:6,4:6] = self.A_i_backing
    impedance_matrix[6:8,6:8] = self.A_composite
    impedance_matrix[8:10,8:10] = self.A_i_front
    impedance_matrix[10:12,10:12] = self.A_o_front
    impedance_matrix[10:12,10:12] = self.A_front_load
    #Removes layers where its zero
    impedance_matrix[~np.all(impedance_matrix == 0, axis=1)]
    print(impedance_matrix)
    return impedance_matrix

A _...矩阵是复杂的2x2矩阵,在另一个模块中计算

这是我得到的警告:

ComplexWarning: Casting complex values to real discards the imaginary part

但是,我想返回具有复杂值的矩阵。

0 个答案:

没有答案