首页 > 解决方案 > 根据索引/列名将小矩阵中的值添加到更大的矩阵中

问题描述

我有四个 4x4 矩阵(K1、K2、K3、K4),它们的值将被添加到一个大的 8x8 矩阵 K 中。如下所示,每个矩阵都有索引和列名,我想从基于它们的索引/列名称的小矩阵与较大的矩阵相对应。例如:从 K1 (v1, v1) 等于 1,因此应该在大矩阵中将 1 添加到 (v1, v1) 中。

到目前为止,4 个小矩阵是根据给定数学问题中的一些手动插入的值/变量创建的,因此索引/列名称可以在下一个问题中更改。总会有四个 4x4 矩阵,但它们的索引/列名会因大矩阵中的矩阵而异。是否有可能创建一个带有 if 语句的 for 循环来遍历小矩阵的索引/列名的名称,以便在大矩阵中添加什么?我被困住并感谢我能得到的所有帮助!

这是到目前为止的代码:

import pandas as pd
from sympy.interactive import printing
printing.init_printing(use_latex=True)
from sympy import Eq, solve_linear_system, Matrix
from numpy import linalg
import numpy as np
from IPython.display import display
import sympy as sy
import matplotlib.pyplot as plt

sy.init_printing()

A, E, L, Rx, P, rot3, rot2 = sy.symbols("A, E, L, Rx, P, rot3, rot2", real=True)

r2 = round(2**(1/2), 2)
r3 = round(3**(1/2), 2)

x1_e1 = 0
x2_e1 = 0
y1_e1 = 0
y2_e1 = L
L1 = L
c1 = (x2_e1 - x1_e1) / L1
s1 = (y2_e1 - y1_e1) / L1
column_names1 = ['u1', 'v1', 'u3', 'v3']
row_names1 = column_names1

x1_e2 = (2*L)/r3
x2_e2 = (r3*L)/2
y1_e2 = 0
y2_e2 = L/2
L2 = L/(r3)
c2 = (x2_e2 - x1_e2) / L2
s2 = (y2_e2 - y1_e2) / L2
column_names2 = ['u2', 'v2', 'u4', 'v4']
row_names2 = column_names2

x1_e3 = ((r3*L))/2
x2_e3 = 0
y1_e3 = L/2
y2_e3 = L
L3 = L
c3 = (x2_e3 - x1_e3) / L3
s3 = (y2_e3 - y1_e3) / L3
column_names3 = ['u4', 'v4', 'u3', 'v3']
row_names3 = column_names3

x1_e4 = 0
x2_e4 = (r3*L)/2
y1_e4 = 0
y2_e4 = L/2
L4 = L
c4 = (x2_e4 - x1_e4) / L4
s4 = (y2_e4 - y1_e4) / L4
column_names4 = ['u1', 'v1', 'u4', 'v4']
row_names4 = column_names4

def calc_K() :

    A1 = A
    E1 = E
    AEL1 = round(((A1*E1)/L1)/((A*E)/L), 2)
    K_1 = np.zeros((4,4),dtype=np)
    K_1[0][0] = round(c1**2, 2)
    K_1[0][1] = round(c1*s1, 2)
    K_1[0][2] = round(-(c1**2), 2)
    K_1[0][3] = round(-(c1*s1), 2)
    K_1[1][0] = round(c1*s1, 2)
    K_1[1][1] = round(s1**2, 2)
    K_1[1][2] = round(-(c1*s1), 2)
    K_1[1][3] = round(-(s1**2), 2)
    K_1[2][0] = round(-(c1**2), 2)
    K_1[2][1] = round(-(c1*s1), 2)
    K_1[2][2] = round(c1**2, 2)
    K_1[2][3] = round(c1*s1, 2)
    K_1[3][0] = round(-(c1*s1), 2)
    K_1[3][1] = round(-(s1**2), 2)
    K_1[3][2] = round(c1*s1, 2)
    K_1[3][3] = round(s1**2, 2)
    K__1 = (AEL1 * K_1)
    K1 = pd.DataFrame(K__1, columns=column_names1, index=row_names1)
    print('')
    print('---------- K1 ----------')
    print('AE/L *')
    print(K1)
    print('')

    A2 = A
    E2 = E
    AEL2 = round(((A2*E2)/L2)/((A*E)/L), 2)
    K_2 = np.zeros((4,4),dtype=np)
    K_2[0][0] = round(c2**2, 2)
    K_2[0][1] = round(c2*s2, 2)
    K_2[0][2] = round(-(c2**2), 2)
    K_2[0][3] = round(-(c2*s2), 2)
    K_2[1][0] = round(c2*s2, 2)
    K_2[1][1] = round(s2**2, 2)
    K_2[1][2] = round(-(c2*s2), 2)
    K_2[1][3] = round(-(s2**2), 2)
    K_2[2][0] = round(-(c2**2), 2)
    K_2[2][1] = round(-(c2*s2), 2)
    K_2[2][2] = round(c2**2, 2)
    K_2[2][3] = round(c2*s2, 2)
    K_2[3][0] = round(-(c2*s2), 2)
    K_2[3][1] = round(-(s2**2), 2)
    K_2[3][2] = round(c2*s2, 2)
    K_2[3][3] = round(s2**2, 2)
    K__2 = (AEL2 * K_2)
    K2 = pd.DataFrame(K__2, columns=column_names2, index=row_names2)
    print('')
    print('---------- K2 ----------')
    print('AE/L *')
    print(K2)
    print('')

    A3 = A
    E3 = E
    AEL3 = round(((A3*E3)/L3)/((A*E)/L), 2)
    K_3 = np.zeros((4,4),dtype=np)
    K_3[0][0] = round(c3**2, 2)
    K_3[0][1] = round(c3*s3, 2)
    K_3[0][2] = round(-(c3**2), 2)
    K_3[0][3] = round(-(c3*s3), 2)
    K_3[1][0] = round(c3*s3, 2)
    K_3[1][1] = round(s3**2, 2)
    K_3[1][2] = round(-(c3*s3), 2)
    K_3[1][3] = round(-(s3**2), 2)
    K_3[2][0] = round(-(c3**2), 2)
    K_3[2][1] = round(-(c3*s3), 2)
    K_3[2][2] = round(c3**2, 2)
    K_3[2][3] = round(c3*s3, 2)
    K_3[3][0] = round(-(c3*s3), 2)
    K_3[3][1] = round(-(s3**2), 2)
    K_3[3][2] = round(c3*s3, 2)
    K_3[3][3] = round(s3**2, 2)
    K__3 = (AEL3 * K_3)
    K3 = pd.DataFrame(K__3, columns=column_names3, index=row_names3)
    print('')
    print('---------- K3 ----------')
    print('AE/L *')
    print(K3)
    print('')

    A4 = A
    E4 = E
    AEL4 = round(((A4*E4)/L4)/((A*E)/L), 2)
    K_4 = np.zeros((4,4),dtype=np)
    K_4[0][0] = round(c4**2, 2)
    K_4[0][1] = round(c4*s4, 2)
    K_4[0][2] = round(-(c4**2), 2)
    K_4[0][3] = round(-(c4*s4), 2)
    K_4[1][0] = round(c4*s4, 2)
    K_4[1][1] = round(s4**2, 2)
    K_4[1][2] = round(-(c4*s4), 2)
    K_4[1][3] = round(-(s4**2), 2)
    K_4[2][0] = round(-(c4**2), 2)
    K_4[2][1] = round(-(c4*s4), 2)
    K_4[2][2] = round(c4**2, 2)
    K_4[2][3] = round(c4*s4, 2)
    K_4[3][0] = round(-(c4*s4), 2)
    K_4[3][1] = round(-(s4**2), 2)
    K_4[3][2] = round(c4*s4, 2)
    K_4[3][3] = round(s4**2, 2)
    K__4 = (AEL4 * K_4)
    K4 = pd.DataFrame(K__4, columns=column_names4, index=row_names4)
    print('')
    print('---------- K4 ----------')
    print('AE/L *')
    print(K4)
    print('')

    column_names5 = ['u1', 'v1', 'u2', 'v2', 'u3', 'v3', 'u4', 'v4']
    row_names5 = column_names5
    KK__1 = np.zeros((8,8),dtype=np)
    KK_1 = pd.DataFrame(KK__1, columns=column_names5, index=row_names5)
    print(KK_1)

这是现在的输出:

---------- K1 ----------
AE/L *
   u1  v1 u3  v3
u1  0   0  0   0
v1  0   1  0  -1
u3  0   0  0   0
v3  0  -1  0   1

---------- K2 ----------
AE/L *
        u2      v2      u4      v4
u2   0.432  -0.761  -0.432   0.761
v2  -0.761    1.30   0.761   -1.30
u4  -0.432   0.761   0.432  -0.761
v4   0.761   -1.30  -0.761    1.30

---------- K3 ----------
AE/L *
       u4     v4     u3     v3
u4   0.75  -0.43  -0.75   0.43
v4  -0.43   0.25   0.43  -0.25
u3  -0.75   0.43   0.75  -0.43
v3   0.43  -0.25  -0.43   0.25


---------- K4 ----------
AE/L *
       u1     v1     u4     v4
u1   0.75   0.43  -0.75  -0.43
v1   0.43   0.25  -0.43  -0.25
u4  -0.75  -0.43   0.75   0.43
v4  -0.43  -0.25   0.43   0.25

---------- K ----------
   u1 v1 u2 v2 u3 v3 u4 v4
u1  0  0  0  0  0  0  0  0
v1  0  0  0  0  0  0  0  0
u2  0  0  0  0  0  0  0  0
v2  0  0  0  0  0  0  0  0
u3  0  0  0  0  0  0  0  0
v3  0  0  0  0  0  0  0  0
u4  0  0  0  0  0  0  0  0
v4  0  0  0  0  0  0  0  0

期望的输出:

------------------------ K ------------------------
     u1    v1    u2    v2    u3    v3    u4    v4
u1  0.75  0.43   0     0     0     0   -0.75  -0.43
v1  0.43  1.25   0     0     0    -1   -0.43  -0.25
u2   0     0    0.25 -0.43   0     0   -0.25   0.43
v2   0     0   -0.43  0.75   0     0    0.43  -0.75
u3   0     0     0     0     0     0   -0.75   0.43
v3   0    -1     0     0     0    -1    0.43  -0.25
u4 -0.75 -0.43 -0.25  0.43  0.25  0.43  1.75  -0.43
v4 -0.43 -0.25  0.43 -0.75  0.43 -0.25 -0.43   1.25

标签: pythonpandasnumpysympy

解决方案


推荐阅读