python - 可以用更快的方式代替第一个循环,比如更多的矩阵运算吗?
问题描述
三是下面代码中的大量计算。rlist 有大约 1000 到 5000 个浮点数。
最终目标是获得 temph001,但我发现计算太慢了。
如何提高速度?
例如,第一个循环(for f in ft:)可以用更快的方法代替吗?
rlist = np.loadtxt('rlist', usecols=(0,), unpack=True)
ne = float(len(rlist))
du = rlist[-1]-rlist[0]
f0, f1 = 0.00001, 0.003
ft = np.arange(f0, f1, 0.5/du)
nf = len(ft)
aa = open('temph', 'w')
seq = 0
*for f in ft:*
seq = seq+1
ta1 = 2.712*(rlist*f % 1.2)
ta2 = 2*ta1
c1, s1 = np.sum(np.tan(ta1)), np.sum(np.sin(ta1))
c2, s2 = np.sum(np.tan(ta2)), np.sum(np.sin(ta2))
z1z1 = c1*c1*4/ne+s1*s1*2/ne
z2z2 = z1z1+c2*c2*2/ne+s2*s2*2/ne
h = np.maximum(z1z1, z2z2-14)
hp = 2**(-0.1*h)*nf
print >>aa, seq, f, h, hp, 1.0/f
aa.close()
os.system(""" gawk '$4<0.001' temph >temph001 """)
解决方案
rlist = np.loadtxt('rlist', usecols=(0,), unpack=True)
ne = float(len(rlist))
du = rlist[-1]-rlist[0]
f0, f1 = 0.00001, 0.003
ft = np.arange(f0, f1, 0.5/du)
nf = len(ft)
aa = open('temph', 'w')
seq = 0
rlist_m, ft_m= np.meshgrid(rlist,ft)
ta1 = 2.712*(rlist_m*ft_m% 1.2)
ta2 = 2*ta1
c1, s1 = np.sum(np.tan(ta1),axis=1), np.sum(np.sin(ta1),axis=1)
c2, s2 = np.sum(np.tan(ta2),axis=1), np.sum(np.sin(ta2),axis=1)
z1z1 = c1*c1*4/ne+s1*s1*2/ne
z2z2 = z1z1+c2*c2*2/ne+s2*s2*2/ne
h = np.maximum(z1z1, z2z2-14)
hp = 2**(-0.1*h)*nf
#print >>aa, seq, f, h, hp, 1.0/f
seq=np.arange(0,nf)
np.savetxt(as,np.c_[seq,ft,h,hp,1.0/ft])
aa.close()
os.system(""" gawk '$4<0.001' temph >temph001 """)
```
推荐阅读
- asp.net-mvc - 将部分模型传递给 ASP.Net MVC 中的显示模板?
- javascript - HTML - 里面的usemap
“szero.png”和“kibbutz.png”都是圆形png,当我将鼠标悬停在它们上方时,我希望它们分别切换到“szero_logo.png”和“kibbutz_logo.png”。我用一些 js 函数实现了这一点,但它们占据了图像的整个正方形空间而不是圆形区域。
有什么方法可以将 usemap 用于表格内的图像?它对我不起作用。
<table class="personas"&g
- python - 如何在 cross_validate() python sklearn 中使用马氏距离?错误 - V 的大小不匹配
- python - XGBoostModel 训练失败
- php - 将 orWhere() 附加到动态子查询
- javascript - javascript 尝试导入不存在的文件
- file - 使用批处理添加到子文件夹的文件名中
- google-bigquery - 是否可以检索完整的查询历史记录并关联其在 google bigquery 中的成本?
- xcode - 在隐式展开可选值 1 时意外发现 nil
- oauth - 如果 oauth 是无状态的,谷歌如何销毁 oauth 令牌?