python - 程序中的类型错误(预期类型 Float,已设置 [Float];等错误)
问题描述
我对 Python 比较陌生,我用它来解决 Cosmology 项目中的方程组。这是代码:
#Import Math Module for Square Root Function, Numpy for Computations and Matplot for Plotting
import math
import numpy as np
import matplotlib.pyplot as plt
#Planck Mass
Mpl=2.43*10**18*10**9*1.6*10**-19 #Mpl value is 2.43*10^18 GeV
#Compute Initial Values of the Densities
RhoM_today= 2.746*(10**(-27)) #Today's Value of Matter Density
RhoRad_today= 4.546*(10**(-31)) #Today's Value of Radiation Density
RhoL_today=0.0 #Value of Cosmological Constant Density
Rho=RhoM_today+RhoRad_today+RhoL_today #Initial Value of the Overall Density
#Set Initial Values
#Initial Values of the Fields
Sigma = 1.0 # Initial Value of Sigma (a=10)
Phi=1.0
#Values of Parameters
Omega=1.0 #Dummy Value of Omega
Alpha_Three=1.0 #Dummy Value of Alpha_3
Alpha_Four=1.0 #Dummy Value of Alpha_4
Alpha_Sigma=1.0 #Dummy Value of Alpha_Sigma
C1=1.0 #Dummy Value of C1
mg=1.0 #Dummy Value of the Graviton Mass
Lambda=0.0 #Value of the Cosmological Constant
H=2.27*10**-18 #Initial Value of H
# Initial Values for computing the number of Steps for RK4
x=0 #Corresponding to a = 1 (today)
xn=-11.5 #Corresponding to a ~ 10^-5
h = 0.115 #Step Size
n = int((x-xn)/h) #Number of Steps/Points
#Note: n+1 points but n Steps?
print(n)
#Creating Arrays for Storing Data
xp=np.linspace(x,xn,n+1) #Array for Storing x
Sigmap=np.empty(n+1,float) #Array for Storing Sigma
up=np.empty(n+1,float) #Array for Storing u
Hp=np.empty(n+1,float) #Array for Storing H
#Hxp=np.empty(n+1,float) #Array for Storing Hx
#Sigmap[0]=Sigma #Save Initial Value of Sigma
#up[0]=u #Save Initial Value of u
#Hp[0]=H #Save Initial Value of H
#Hp[0]=H/H0 #Save Initial Value of H
#Hxp[0]=Hx #Save Initial Value of Hx
#CHp=np.empty(n+1,float) #Array for Conformal Hubble Parameter
#CHp[0]=H*math.exp(x)
#for i in range (0,n+1):
# print (Hp[i])
#Print Parameters and Initial Values
print('Planck Mass is',Mpl)
print('Initial Value of H is ',H )
print('Initial Value of Sigma is ',Sigma)
print('Initial Value of Energy Density Rho is ',Rho)
print('a goes from ',math.exp(x),'to ',math.exp(xn))
print('x (lna) goes from ',x,'to ',xn)
print('Total Number of Steps in RK1 is ',n)
#The Header of the output table
print('x \t\t\t\t\t\t \t\tSigma\t\t\t\t\t\t u \t\t\t\t\t \t\t\t\t H')
#print(x,'\t\t\t\t\t\t\t',Sigma,'\t\t\t\t\t\t',u,'\t\t\t\t\t\t',H)
#print('%f \t\t\t\t\t \t %f \t\t\t\t \t %f \t\t\t\t\t \t %f'% (x,Sigma,u,H)) # The Initial Values of x, Sigma & u
#Start of RK1
#Start of The RK1 Loop
for i in range(0, n):
# Generate Values of Rho (from RhoM, RhoRad & H, re-written in terms of x)
RhoM = RhoM_today / (math.exp(x) ** 3)
RhoRad = RhoRad_today / (math.exp(x) ** 4)
RhoL = 0.0
Rho = RhoM + RhoRad + RhoL # New Value of the Overall Density
# Generate Values of the Pressures
Pressure_Matter = 0
Pressure_Radiation = RhoRad / 3
Pressure = Pressure_Matter + Pressure_Radiation
#Save the Values of Sigma and H
Sigmap[i] = Sigma # Save the Value of Sigma
Hp[i] = H #Save the Value of H
# Compute Values for X, J, Lambda_X & Their Derivatives
X = math.exp(Sigma) / math.exp(x)
print(X)
J = 3 + 3 * Alpha_Three * (1 - X) + Alpha_Four * (1 - X) ** 2
print(J)
Lambda_X = (mg ** 2) * (X - 1) * [J + (X - 1) * {(Alpha_Three) * (X - 1) - 3}]
# Compute Value of u
u = (1 / H) * (math.sqrt(2 / Omega)) * math.sqrt(3 * H * 2 - Lambda - Lambda_X - Rho / Mpl ** 2)
up[i]=u
print('%.3f \t\t\t\t\t \t %.6f \t\t\t\t \t%.6f \t\t\t\t\t \t %9.3e' % (x, Sigma, u, H)) # Newer Computed values of x,Sigma,u & H
# Compute Value of Lambda_X_Dot & Lambda_X_Dash
Lambda_X_Dot = 3 * (mg ** 2) * math.exp(Sigma) * H * [u - 1] * [J + X * {Alpha_Three * (X - 1) - 2}] / math.exp(x)
Lambda_X_Dash = Lambda_X_Dot / H
# Compute Value of q
q = (Alpha_Sigma ** 0.5) * [u / (X * mg)] / math.sqrt([(1 / H ** 2) - {C1 / (math.exp(x) ** 4 * X * (1 - X) * J * H)}])
# Compute Value of H_Dash
H_Dash = (q - 1) * Lambda_X_Dash / (6 * H * (u - 1)) - Omega * H * u ** 2 / 2 - (Rho + Pressure) / (2 * H)
#Compute the next Values of H and Sigma
H=H+H_Dash*h
Sigma=Sigma+u*h
#Increment x (Move on to next h)
x-=h
为了完整起见,我已经包含了整个代码。我得到的错误是在三个等式中的代码末尾——一个在 Lambda_X 中,另一个在 Lambda_X_Dot 中,最后一个在 q)。
我得到的错误的形式是:'Expected Float, got set[float] instead' & 'Expected Float got list[float] instead'等。
我不确定它们来自哪里,因为我编写了一个没有此类错误的类似程序。我不知道它是否有帮助,但我正在使用 PyCharm 编写代码。非常感谢任何输入!提前致谢。
解决方案
在您的代码中,请具体查看这一行,
Lambda_X = (mg ** 2) * (X - 1) * [J + (X - 1) * {(Alpha_Three) * (X - 1) - 3}]
通常,在数学中,我们使用方括号和花括号以及普通大括号,但在 python 中,它们的含义不同。{}
这是一个集合,[]
这是一个列表。所以你不能像你在这里使用的那样使用它们。您可能想用普通的大括号替换它们。
例如,上面提到的行将变为
Lambda_X = (mg ** 2) * (X - 1) * (J + (X - 1) * ((Alpha_Three) * (X - 1) - 3))
您必须对代码中的其他语句执行相同的操作。
推荐阅读
- swift - swift - 快速显示上下文菜单时如何自定义预览
- excel - Excel根据单元格VBA自动更改工作表名称
- javascript - 使用 react.js 加载数组
- python - 当数据集包含超过 50000 个缺失值时,如何使用 pandas 在数据分析中处理缺失的分类数据?
- regex - 无法找出以下的正则表达式
- python - 在 AWS Lambda (Python) 中将文件从 multipart/form-data 上传到 S3
- python - 加密整行文本
- arrays - 持有一个排序数组 - 反向排序的输入案例
- azure - 从 csv 文件中删除用户的 Azure 许可证
- json - 如何从 Json webtemplate 渲染 HTML