首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >程序中的类型错误(期望的类型浮点,得到的set[Float]等错误)

程序中的类型错误(期望的类型浮点,得到的set[Float]等错误)
EN

Stack Overflow用户
提问于 2018-06-11 07:22:40
回答 1查看 119关注 0票数 0

我对Python比较陌生,我用它来解决一个宇宙学项目中的方程系统。代码如下:

代码语言:javascript
复制
#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)中的最后一个。

我得到的错误是这样的:‘预期的浮点数,而不是setfloat’&‘预期的浮点数,而不是listfloat’等等。

我不知道它们是从哪里来的,因为我写了一个类似的程序,没有这样的错误。我不知道它是否有帮助,但我正在使用PyCharm来编写代码。任何意见都是非常感谢的!提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2018-06-11 08:10:29

在您的代码中,请特别查看这一行,

代码语言:javascript
复制
Lambda_X = (mg ** 2) * (X - 1) * [J + (X - 1) * {(Alpha_Three) * (X - 1) - 3}]

通常,在数学中,我们使用方括号和花括号以及普通的大括号,但在python中,它们的含义是不同的。{}这是一个集合,[]这是一个列表。所以你不能像在这里一样使用它们。您可能希望将它们替换为普通的大括号。

例如,上面提到的行将变为

代码语言:javascript
复制
Lambda_X = (mg ** 2) * (X - 1) * (J + (X - 1) * ((Alpha_Three) * (X - 1) - 3))

您必须对代码中的其他语句执行相同的操作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50788874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档