【问题标题】:TypeError: 'Quantity' object is not callableTypeError:“数量”对象不可调用
【发布时间】:2016-02-06 21:23:56
【问题描述】:

我正在尝试了解数量错误。
我正在使用 Python 2.7.1 版和 numpy 和数量包。 我已经发布了定义的变量和方程以及随后的错误。我正在尝试生成一个 Teffpv 值随 theta (th) 变化的数组。有人可以解释我为什么会出错吗?

a = q.Quantity(0.2,'au').simplified
e = 0.2
r = q.Quantity(0.4,'Rsun').simplified # Radius of blackbody
t = q.Quantity(3500,'K').simplified #temperature for Teff of the star
al = 0.3
N = 100
th = np.linspace(0.,4*np.pi,N)
def d(a,e,th):
    return a*(1-e**2)/(1+e*np.cos(th))
def Teffp(al,t,r,d):
    return ((1.- al)**(1/4))*t*(r/d(a,e,th))**(1/2)
Teffpv = np.zeros(N) # make arrays to store results
for i in range(N-1):
    Teffpv=Teffp(al,t,r,d(a,e,th[i]))

错误信息

TypeError                                 Traceback (most recent call last)
<ipython-input-17-bdcc9550c9ae> in <module>()
     14 Teffpv = np.zeros(N) # make arrays to store results
     15 for i in range (N-1):
---> 16     Teffpv=Teffp(al,t,r,d(a,e,th[i]))

<ipython-input-17-bdcc9550c9ae> in Teffp(al, t, r, d)
     11     return a*(1-e**2)/(1+e*np.cos(th))
     12 def Teffp(al,t,r,d):
---> 13     return ((1.- al)**(1/4))*t*(r/d(a,e,th))**(1/2)
     14 Teffpv = np.zeros(N) # make arrays to store results
     15 for i in range (N-1):

TypeError: 'Quantity' object is not callable

【问题讨论】:

  • 您有一个d 函数和一个d 参数,但您似乎并不清楚要使用哪一个。

标签: python numpy astronomy astropy


【解决方案1】:

您的代码有一些错误:

  • 无需使用np.zeros(N) 初始化Teffpv 并使用循环。
  • 在Teffp 的公式中将d(a,e,th) 替换为d -> 这解释了您的错误消息
  • 对有理指数使用浮点数,尤其是在Teffp 函数中

这段代码应该做你想做的:

import quantities as q
import numpy as np

a = q.Quantity(0.2,'au').simplified
e = 0.2
Rsun = q.UnitQuantity('solar radius', q.m*6.9599e8, symbol='Rsun')
r = q.Quantity(0.4,'Rsun').simplified # Radius of blackbody
t = q.Quantity(3500.,'K').simplified #temperature for Teff of the star
al = 0.3
N = 100
th = np.linspace(0.,4.*np.pi,N)

def d(a,e,th):
  return a*(1-e**2)/(1+e*np.cos(th))

def Teffp(al,t,r,d):
  return ((1.- al)**(0.25))*t*(r/d)**(0.5)

Teffpv = Teffp(al,t,r,d(a,e,th))
print Teffpv

注意:

我在此代码中添加了 Rsun 单位的定义,否则我的数量包无法识别它。

我不是天体物理学家,但我发现here ;)

【讨论】:

    猜你喜欢
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 2021-04-15
    • 2011-10-01
    • 2020-11-10
    • 2017-09-09
    • 2016-07-23
    相关资源
    最近更新 更多