【问题标题】:I am currently solving a problem for my class on Plancks blackbody function我目前正在为我的课程解决普朗克黑体功能的问题
【发布时间】:2019-10-15 11:36:32
【问题描述】:
    import math
    import matplotlib.pyplot as plt

import numpy as np
hc=1.23984186E3

k=1.3806503E-23

T=(np.linspace(5000,10000,50))

lamb = np.linspace(0.00001,.0000001,50)
print(len(lamb))
print (len(T))

planck_top=8*math.pi*hc
planck_bottom1=lamb*1000000
planck_bottom2=math.exp(hc//lamb*k*T)
planck=planck_top//planck_bottom

我在这里不断收到此错误;

>
planck_bottom2=math.exp(hc//lamb*k*T)
TypeError: only size-1 arrays can be converted to Python scalars

我不知道如何纠正这个问题,因为我们在这里处理一个大数组

【问题讨论】:

    标签: python


    【解决方案1】:

    hc//lamb*k*T 返回一个数组,math.exp() 只能处理数字。因此,以下方法将起作用:

    planck_bottom2=[math.exp(i) for i in hc//lamb*k*T]
    

    它返回一个列表,其中包含数组hc//lamb*k*T中的每个数字,对应地取幂。

    【讨论】:

    • 解决了!非常感谢!
    【解决方案2】:

    您还可以选择使用numpy 而不是math

    使用 Numpy 进行数组计算

    只需将 math 替换为 np,因为您已经 import numpy as np

    planck_top=8*np.pi*hc
    planck_bottom2 = np.exp(hc//lamb*k*T)
    

    关于使用math 和/或numpy

    作为附加信息,我鼓励您查看以下参考资料,以评估何时/是否应该选择math 和/或numpy

    1. What is the difference between import numpy and import math [duplicate]
    2. Are NumPy's math functions faster than Python's?
    3. "Math module functions cannot be used directly on ndarrays because they only accept scalar, not array arguments."

    【讨论】:

      猜你喜欢
      • 2014-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 2022-12-03
      相关资源
      最近更新 更多