我试着让自己熟悉python中的编程,但刚刚开始并且遇到了以下问题。也许有人可以给我一个提示如何继续或在哪里寻找一个好的解决方案。
我想通过循环中的循环在6个不同温度下绘制132波长的普朗克曲线。 planckwavel函数接收两个参数,波长和温度,我在两个循环中分离。
到目前为止,我设法使用了列表,这些列表有效,但可能没有以优雅的方式解决:
plancks = []
temp = [280, 300, 320, 340, 360, 380]
temp_len = len(temp)
### via fun planckwavel
for i in range(temp_len):
t_list = [] # list nach jeder j schleife wieder leeren
for j in range(wl_centers_ar.shape[0]):
t = planckwavel(wl_centers_ar[j],temp[i])
t_list.append(t)
plancks.append(t_list)
### PLOT Planck curves
plancks = np.array(plancks).T # convert list to array and transpose
view_7 = plt.figure(figsize=(8.5, 4.5))
plt.plot(wl_centers_ar,plancks)
plt.xticks(rotation='vertical')
但是我想使用列表中的数组,因为我喜欢继续使用更大的维度图像。所以我尝试了相同的数组但不幸的是失败了这段代码:
plancks_ar = zeros([132,6], dtype=float ) # create array and fill with zeros
temp_ar = array([273, 300, 310, 320, 350, 373])
for i in range(temp_ar.shape[0]):
t_ar = np.zeros(plancks_ar.shape[0])
for j in range(plancks_ar.shape[0]):
t = planck(wl_centers_ar[j]*1e-6,temp[1])/10**6
np.append(t_ar,t)
np.append(plancks_ar, t_ar)
plt.plot(wl_centers_ar,plancks)
如果有人能给我一些建议,我会非常感激。
感谢名单, 最好的问候,
彼得
答案 0 :(得分:0)
我想你在询问如何使用NumPy的broadcasting and vectorization。这是一种删除显式Python循环的方法:
import numpy as np
# Some physical constants we'll need
h, kB, c = 6.626e-34, 1.381e-23, 2.998e8
def planck(lam, T):
# The Planck function, using NumPy vectorization
return 2*h*c**2/lam**5 / (np.exp(h*c/lam/kB/T) - 1)
# wavelength array, 3 - 300 um
lam = np.linspace(3, 75, 132)
# temperature array
T = np.array([280, 300, 320, 340, 360, 380])
# Remember to convert wavelength from um to m
pfuncs = planck(lam * 1.e-6, T[:,None])
import pylab
for pfunc in pfuncs:
pylab.plot(lam, pfunc)
pylab.show()
我们想为每个波长和每个T计算planck
,因此我们需要在两个阵列上广播计算。遵循上面链接的文档中规定的规则,我们可以通过向温度数组添加新轴(使用T[:, None]
)来实现这一点:
lam: 132
T 6 x 1
--------------
6 x 132
T[:, None]
的最终维度为1,因此lam
的132个值可以在其间广播以生成6 x 132
数组:6行(每个T一个)132值(波长)。
答案 1 :(得分:0)
我尝试使用逆(亮度温度)双重检查上述普朗克方程。根据你的代码我定义了以下功能,并期望得到300开尔文(@ 10微米,10 W / m2 / str /微米):
connection.Query<int>("select * from (select 1 as Id union all select 2 union all select 3) as X where Id in @Ids", new { Ids = new int[] { });
但收到了一个奇怪的结果
select * from (select 1 as Id union all select 2 union all select 3) as X where Id in ()
- - &gt; 3.0039933569668916e-08
有什么建议我的亮度温度功能出了什么问题?
感谢, 彼得