英文:
how to Add lists into a numpy matrix
问题
我想将以下列表作为元素添加到NumPy矩阵中,以便以后可以消除一些行和列。
ValueError: 使用序列设置数组元素。在2维之后,请求的数组具有不均匀的形状。检测到的形状是(5, 5) + 不均匀部分。
```python
import numpy as np
import math
import sympy as sp
def FSDT(a, b, E11, E22, Nu12, Nu21, G12, G13, G23, theta, PlyNumber, K, h):
#----------------------------------------------------------
i = 0
h = PlyNumber * LaminaThickness
Z = []
Z.append(-h / 2.0)
for j in range(1, PlyNumber + 1, 1):
Z.append(Z[j - 1] + LaminaThickness)
# Matrix Q
Q11 = []
Q12 = []
Q22 = []
Q66 = []
Q44 = []
Q55 = []
Q11_B = []
Q12_B = []
Q16_B = []
Q22_B = []
Q26_B = []
Q66_B = []
Q44_B = []
Q45_B = []
Q55_B = []
for i in range(0, 1, 1):
Q11.append(E11 / (1 - Nu12 * Nu21))
Q12.append((Nu12 * E22) / (1 - Nu12 * Nu21))
Q22.append((E22) / (1 - Nu12 * Nu21))
Q44.append(G23)
Q66.append(G12)
Q55.append(G13)
i = 0
for j in range(0, PlyNumber, 1):
Q11_B.append(Q11[i] * (np.cos(theta[j] * np.pi / 180.0) ** 4) + 2 * (
Q12[i] + 2 * Q66[i]) * (np.sin(theta[j] * np.pi / 180.0)) ** 2 * (
np.cos(theta[j] * np.pi / 180.0)) ** 2 + Q22[i] * (
np.sin(theta[j] * np.pi / 180.0)) ** 4)
Q12_B.append((Q11[i] + Q22[i] - 4 * Q66[i]) * (np.sin(theta[j] * np.pi / 180.0)) ** 2 * (
np.cos(theta[j] * np.pi / 180.0)) ** 2 + Q12[i] * (
(np.sin(theta[j] * np.pi / 180.0)) ** 4 + (
(np.cos(theta[j] * np.pi / 180.0)) ** 4)))
Q16_B.append((Q11[i] - Q12[i] - 2 * Q66[i]) * np.sin(theta[j] * np.pi / 180.0) * np.cos(
theta[j] * np.pi / 180.0) ** 3 + (Q12[i] - Q22[i] + 2 * Q66[i]) * np.cos(
theta[j] * np.pi / 180.0) * np.sin(theta[j] * np.pi / 180.0) ** 3)
Q22_B.append(Q11[i] * np.sin(theta[j] * np.pi / 180.0) ** 4 + 2 * (Q12[i] + 2 * Q66[i]) * np.sin(
theta[j] * np.pi / 180.0) ** 2 * np.cos(theta[j] * np.pi / 180.0) ** 2 + Q22[i] * np.cos(
theta[j] * np.pi / 180.0) ** 4)
Q26_B.append((Q11[i] - Q12[i] - 2 * Q12[i]) * np.sin(theta[j] * np.pi / 180.0) ** 3 * np.cos(
theta[j] * np.pi / 180.0) + (Q12[i] - Q22[i] + 2 * Q66[i]) * np.cos(
theta[j] * np.pi / 180.0) ** 3 * np.sin(theta[j] * np.pi / 180.0))
Q66_B.append((Q11[i] + Q22[i] - 2 * Q12[i] - 2 * Q66[i]) ** np.sin(theta[j] * np.pi / 180.0) ** 2 ** np.cos(
theta[j] * np.pi / 180.0) ** 2 + Q66[i] * (np.sin(theta[j] * np.pi / 180.0) ** 4 + np.cos(
theta[j] * np.pi / 180.0) ** 4))
Q44_B.append(Q44[i] * np.cos(theta[j] * np.pi / 180.0) ** 2 + Q55[i] * np.sin(theta[j] * np.pi / 180.0) ** 2)
Q55_B.append(Q44[i] * np.sin(theta[j] * np.pi / 180.0) ** 2 + Q55[i] * np.cos(theta[j] * np.pi / 180.0) ** 2)
Q45_B.append((Q55[i] - Q44[i]) * np.cos(theta[j] * np.pi / 180.0) * np.sin(theta[j] * np.pi / 180.0))
Q = np.matrix([[Q11_B, Q12_B, Q16_B, 0, 0], [Q12_B, Q22_B, Q26_B, 0, 0], [Q16_B, Q26_B, Q66_B, 0, 0],
[0, 0, 0, Q44_B, Q45_B], [0, 0, 0, Q45_B, Q55_B]])
return Q
# -----------------------------------------------------------------------------------------------
# def FSDT(E11,E22,G12,G13,G23,Nu12,Nu21,LaminaThickness,PlyNumber):
a = 10
b = 5
E11 = 131000
E22 = 32750
Nu12 = 0.25
Nu21 = 0.33
G12 = 19785
G13 = 16854
G23 = 19785
rho = 1650
K = np.pi ** 2 / 12
theta = [0, 90, 0, 90, 0]
PlyNumber = len(theta)
LaminaThickness = 0.01
h = 0.5
Q = FSDT(a, b, E11, E22, Nu12, Nu21, G12, G13, G23, theta, PlyNumber, K, h)
还有另一种方式可以编写Q
英文:
I want to add the following lists as elements into a NumPy matrix so that later on, I could eliminate some rows and columns.
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (5, 5) + inhomogeneous part.
import numpy as np
import math
import sympy as sp
def FSDT(a,b,E11,E22,Nu12,Nu21,G12,G13,G23,theta,PlyNumber,K,h):
#----------------------------------------------------------
i=0
h = PlyNumber*LaminaThickness
Z = []
Z.append(-h/2.0)
for j in range(1,PlyNumber+1,1):
Z.append(Z[j-1]+LaminaThickness)
#Matrix Q
Q11=[]
Q12=[]
Q22=[]
Q66=[]
Q44=[]
Q55=[]
Q11_B = []
Q12_B = []
Q16_B = []
Q22_B = []
Q26_B = []
Q66_B = []
Q44_B = []
Q45_B = []
Q55_B = []
for i in range(0,1,1):
Q11.append(E11/(1-Nu12*Nu21))
Q12.append((Nu12*E22)/(1-Nu12*Nu21))
Q22.append((E22)/(1-Nu12*Nu21))
Q44.append(G23)
Q66.append(G12)
Q55.append(G13)
i=0
for j in range(0,PlyNumber,1):
Q11_B.append(Q11[i]*(np.cos(theta[j]*np.pi/180.0)**4)+2*(Q12[i]+2*Q66[i])* (np.sin(theta[j]*np.pi/180.0))**2*(np.cos(theta[j]*np.pi/180.0))**2+Q22[i]*(np.sin(theta[j]*np.pi/180.0))**4)
Q12_B.append((Q11[i]+Q22[i]-4*Q66[i])*(np.sin(theta[j]*np.pi/180.0))**2*(np.cos(theta[j]*np.pi/180.0))**2+Q12[i]*((np.sin(theta[j]*np.pi/180.0))**4+((np.cos(theta[j]*np.pi/180.0))**4)))
Q16_B.append((Q11[i]-Q12[i]-2*Q66[i])*np.sin(theta[j]*np.pi/180.0)*np.cos(theta[j]*np.pi/180.0)**3+(Q12[i]-Q22[i]+2*Q66[i])*np.cos(theta[j]*np.pi/180.0)*np.sin(theta[j]*np.pi/180.0)**3)
Q22_B.append(Q11[i]*np.sin(theta[j]*np.pi/180.0)**4+2*(Q12[i]+2*Q66[i])*np.sin(theta[j]*np.pi/180.0)**2*np.cos(theta[j]*np.pi/180.0)**2+Q22[i]*np.cos(theta[j]*np.pi/180.0)**4)
Q26_B.append((Q11[i]-Q12[i]-2*Q12[i])*np.sin(theta[j]*np.pi/180.0)**3*np.cos(theta[j]*np.pi/180.0)+(Q12[i]-Q22[i]+2*Q66[i])*np.cos(theta[j]*np.pi/180.0)**3*np.sin(theta[j]*np.pi/180.0))
Q66_B.append((Q11[i]+Q22[i]-2*Q12[i]-2*Q66[i])**np.sin(theta[j]*np.pi/180.0)**2**np.cos(theta[j]*np.pi/180.0)**2+Q66[i]*(np.sin(theta[j]*np.pi/180.0)**4+np.cos(theta[j]*np.pi/180.0)**4))
Q44_B.append(Q44[i]*np.cos(theta[j]*np.pi/180.0)**2 + Q55[i]*np.sin(theta[j]*np.pi/180.0)**2)
Q55_B.append(Q44[i]*np.sin(theta[j]*np.pi/180.0)**2 + Q55[i]*np.cos(theta[j]*np.pi/180.0)**2)
Q45_B.append((Q55[i]-Q44[i])*np.cos(theta[j]*np.pi/180.0)*np.sin(theta[j]*np.pi/180.0))
Q = np.matrix([[Q11_B, Q12_B, Q16_B, 0, 0],[Q12_B, Q22_B, Q26_B, 0, 0],[Q16_B, Q26_B, Q66_B, 0, 0],[0, 0, 0, Q44_B, Q45_B],[0, 0, 0, Q45_B, Q55_B]])
return Q
#-----------------------------------------------------------------------------------------------
# def FSDT(E11,E22,G12,G13,G23,Nu12,Nu21,LaminaThickness,PlyNumber):
a = 10
b = 5
E11 = 131000
E22 = 32750
Nu12 = 0.25
Nu21 = 0.33
G12 = 19785
G13 = 16854
G23 = 19785
rho = 1650
K= np.pi**2/12
theta = [0,90,0,90,0]
PlyNumber = len(theta)
LaminaThickness = 0.01
h = 0.5
Q = FSDT(a,b,E11,E22,Nu12,Nu21,G12,G13,G23,theta,PlyNumber,K,h)
Or there is another way I could write the Q matrix??
Thank you for replying
I have tried to use np. array np. mat to define Q, but the same problem keeps occurring when running the code, it seems like the Q matrix is inhomogeneous.
答案1
得分: 1
我试图弄清楚您的确切需求,根据我的最佳猜测,我认为您不需要大部分循环和列表。您的return
语句也在错误的位置,因此总体而言,您的代码试图返回某种弗兰肯斯坦矩阵。如果这不符合您的需求,请告诉我,或者提供更多关于您的用例的解释,以便进行修改:
import numpy as np
import math
def FSDT(a, b, E11, E22, Nu12, Nu21, G12, G13, G23, theta, PlyNumber, K, h):
#----------------------------------------------------------
## 删除未在下面使用的值
## 实例化一个零数组来存储信息
Q = np.zeros((5, 5, PlyNumber))
Q11 = (E11 / (1 - Nu12 * Nu21))
Q12 = ((Nu12 * E22) / (1 - Nu12 * Nu21))
Q22 = ((E22) / (1 - Nu12 * Nu21))
Q44 = (G23)
Q66 = (G12)
Q55 = (G13)
for j in range(0, PlyNumber, 1):
Q11_B = (Q11 * (np.cos(theta[j] * np.pi / 180.0) ** 4) + 2 * (Q12 + 2 * Q66) * (np.sin(theta[j] * np.pi / 180.0)) ** 2 * (np.cos(theta[j] * np.pi / 180.0)) ** 2 + Q22 * (np.sin(theta[j] * np.pi / 180.0)) ** 4)
Q12_B = ((Q11 + Q22 - 4 * Q66) * (np.sin(theta[j] * np.pi / 180.0)) ** 2 * (np.cos(theta[j] * np.pi / 180.0)) ** 2 + Q12 * ((np.sin(theta[j] * np.pi / 180.0)) ** 4 + ((np.cos(theta[j] * np.pi / 180.0)) ** 4)))
Q16_B = ((Q11 - Q12 - 2 * Q66) * np.sin(theta[j] * np.pi / 180.0) * np.cos(theta[j] * np.pi / 180.0) ** 3 + (Q12 - Q22 + 2 * Q66) * np.cos(theta[j] * np.pi / 180.0) * np.sin(theta[j] * np.pi / 180.0) ** 3)
Q22_B = (Q11 * np.sin(theta[j] * np.pi / 180.0) ** 4 + 2 * (Q12 + 2 * Q66) * np.sin(theta[j] * np.pi / 180.0) ** 2 * np.cos(theta[j] * np.pi / 180.0) ** 2 + Q22 * np.cos(theta[j] * np.pi / 180.0) ** 4)
Q26_B = ((Q11 - Q12 - 2 * Q12) * np.sin(theta[j] * np.pi / 180.0) ** 3 * np.cos(theta[j] * np.pi / 180.0) + (Q12 - Q22 + 2 * Q66) * np.cos(theta[j] * np.pi / 180.0) ** 3 * np.sin(theta[j] * np.pi / 180.0))
Q66_B = ((Q11 + Q22 - 2 * Q12 - 2 * Q66) ** np.sin(theta[j] * np.pi / 180.0) ** 2 ** np.cos(theta[j] * np.pi / 180.0) ** 2 + Q66 * (np.sin(theta[j] * np.pi / 180.0) ** 4 + np.cos(theta[j] * np.pi / 180.0) ** 4))
Q44_B = (Q44 * np.cos(theta[j] * np.pi / 180.0) ** 2 + Q55 * np.sin(theta[j] * np.pi / 180.0) ** 2)
Q55_B = (Q44 * np.sin(theta[j] * np.pi / 180.0) ** 2 + Q55 * np.cos(theta[j] * np.pi / 180.0) ** 2)
Q45_B = ((Q55 - Q44) * np.cos(theta[j] * np.pi / 180.0) * np.sin(theta[j] * np.pi / 180.0))
Q[:, :, j] = np.array([[Q11_B, Q12_B, Q16_B, 0, 0], [Q12_B, Q22_B, Q26_B, 0, 0], [Q16_B, Q26_B, Q66_B, 0, 0], [0, 0, 0, Q44_B, Q45_B], [0, 0, 0, Q45_B, Q55_B]])
return Q
a = 10
b = 5
E11 = 131000
E22 = 32750
Nu12 = 0.25
Nu21 = 0.33
G12 = 19785
G13 = 16854
G23 = 19785
rho = 1650
K = np.pi ** 2 / 12
theta = [0, 90, 0, 90, 0]
PlyNumber = len(theta)
LaminaThickness = 0.01
h = 0.5
Q = FSDT(a, b, E11, E22, Nu12, Nu21, G12, G13, G23, theta, PlyNumber, K, h)
print(Q.shape)
输出结果为:(5, 5, 5)
。
英文:
I tried to sort out what exactly what you were going for, and based on my best guess I don't think you need most of the looping and lists. Your return
statement was also in the wrong spot, so overall your code was trying to return some kind of Frankenstein matrix. Let me know if this gives you what you're looking for, and if not, give some more explanation on your use case so this could be altered:
import numpy as np
import math
def FSDT(a,b,E11,E22,Nu12,Nu21,G12,G13,G23,theta,PlyNumber,K,h):
#----------------------------------------------------------
## removed values aren't being used below
## instantiated an array of zeros to hold information
Q = np.zeros((5,5,PlyNumber))
Q11 = (E11/(1-Nu12*Nu21))
Q12 = ((Nu12*E22)/(1-Nu12*Nu21))
Q22 = ((E22)/(1-Nu12*Nu21))
Q44 = (G23)
Q66 = (G12)
Q55 = (G13)
for j in range(0,PlyNumber,1):
Q11_B = (Q11*(np.cos(theta[j]*np.pi/180.0)**4)+2*(Q12+2*Q66)* (np.sin(theta[j]*np.pi/180.0))**2*(np.cos(theta[j]*np.pi/180.0))**2+Q22*(np.sin(theta[j]*np.pi/180.0))**4)
Q12_B = ((Q11+Q22-4*Q66)*(np.sin(theta[j]*np.pi/180.0))**2*(np.cos(theta[j]*np.pi/180.0))**2+Q12*((np.sin(theta[j]*np.pi/180.0))**4+((np.cos(theta[j]*np.pi/180.0))**4)))
Q16_B = ((Q11-Q12-2*Q66)*np.sin(theta[j]*np.pi/180.0)*np.cos(theta[j]*np.pi/180.0)**3+(Q12-Q22+2*Q66)*np.cos(theta[j]*np.pi/180.0)*np.sin(theta[j]*np.pi/180.0)**3)
Q22_B = (Q11*np.sin(theta[j]*np.pi/180.0)**4+2*(Q12+2*Q66)*np.sin(theta[j]*np.pi/180.0)**2*np.cos(theta[j]*np.pi/180.0)**2+Q22*np.cos(theta[j]*np.pi/180.0)**4)
Q26_B = ((Q11-Q12-2*Q12)*np.sin(theta[j]*np.pi/180.0)**3*np.cos(theta[j]*np.pi/180.0)+(Q12-Q22+2*Q66)*np.cos(theta[j]*np.pi/180.0)**3*np.sin(theta[j]*np.pi/180.0))
Q66_B = ((Q11+Q22-2*Q12-2*Q66)**np.sin(theta[j]*np.pi/180.0)**2**np.cos(theta[j]*np.pi/180.0)**2+Q66*(np.sin(theta[j]*np.pi/180.0)**4+np.cos(theta[j]*np.pi/180.0)**4))
Q44_B = (Q44*np.cos(theta[j]*np.pi/180.0)**2 + Q55*np.sin(theta[j]*np.pi/180.0)**2)
Q55_B = (Q44*np.sin(theta[j]*np.pi/180.0)**2 + Q55*np.cos(theta[j]*np.pi/180.0)**2)
Q45_B = ((Q55-Q44)*np.cos(theta[j]*np.pi/180.0)*np.sin(theta[j]*np.pi/180.0))
Q[:,:,j] = np.array([[Q11_B, Q12_B, Q16_B, 0, 0],[Q12_B, Q22_B, Q26_B, 0, 0],[Q16_B, Q26_B, Q66_B, 0, 0],[0, 0, 0, Q44_B, Q45_B],[0, 0, 0, Q45_B, Q55_B]])
return Q
#-----------------------------------------------------------------------------------------------
a = 10
b = 5
E11 = 131000
E22 = 32750
Nu12 = 0.25
Nu21 = 0.33
G12 = 19785
G13 = 16854
G23 = 19785
rho = 1650
K= np.pi**2/12
theta = [0,90,0,90,0]
PlyNumber = len(theta)
LaminaThickness = 0.01
h = 0.5
Q = FSDT(a,b,E11,E22,Nu12,Nu21,G12,G13,G23,theta,PlyNumber,K,h)
print(Q.shape)
(5, 5, 5)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论