python - 在 true_divide 中遇到被零除 + 在 true_divide 中遇到无效值 + 在 reduce 中遇到无效值

标签 python numpy divide-by-zero

这是我试图绘制的半 Planck law基于温度和距离的变化如下。

import numpy as np

k = 1.381*np.power(10,-23, dtype=np.float)
c = 3*np.power(10,8)
h = 6.626*np.power(10,-34, dtype=np.float)  
l = 3*np.power(10,-6, dtype=np.float)

d_lower = 16*np.power(10,4)
d_upper = 2*np.power(10,6)

t_lower = 740
t_upper = 5200

d = np.arange(d_lower,d_upper,100)
t = np.arange(t_lower,t_upper,10)
D,T = np.meshgrid(d, t)
I = (2*h*np.power(c,2))/(np.power(D,2)*np.power(l,5)*(np.exp((h*c)/(l*k*T))-1))  

解释器返回以下错误:

RuntimeWarning: divide by zero encountered in true_divide
  I = (2*h*np.power(c,2))/(np.power(D,2)*np.power(l,5)*(np.exp((h*c)/(l*k*T))-1))

我不应该遇到任何被零除,因为 T 值是开尔文单位,所以 np.exp((h*c)/(l*k* T))-1 不能变为零。

这里有什么问题?!

我的python和numpy版本分别是3.7.01.15.4

最佳答案

问题可能来自于它是一个矩阵除法,而 python 不能很好地处理它。

尝试替换:

I = (2*h*np.power(c,2))/(np.power(D,2)np.power(l,5)(np.exp(( hc)/(lk*T))-1))

作者:

aaa = (2*h*np.power(c,2))
bbb = (np.power(D,2)*np.power(l,5)*(np.exp((h*c)/(l*k*T))-1))  
I = aaa/bbb

对我有用

SLP

关于python - 在 true_divide 中遇到被零除 + 在 true_divide 中遇到无效值 + 在 reduce 中遇到无效值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54255454/

相关文章:

python - "dict object is not callable"尝试从 request.json 获取值时

python - 在循环中使用 numpy load 时内存溢出

python - 如何修复 np.where 函数的错误?

geometry - 在对几何形状进行数学计算时避免被零除的最佳方法

python - 从 Python 中的生成器中删除重复代码

python - Scikit learn中的R2值是怎么计算出来的?

python - math.log 的错误输出

python - 如何加速python中的循环

java - Java 如何处理被零除?

c# - 除以零错误,我该如何解决这个问题?