Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在true#u divide+中遇到被零除的情况;true_divide+;中遇到无效值;reduce中遇到无效值_Python_Numpy_Divide By Zero - Fatal编程技术网

Python 在true#u divide+中遇到被零除的情况;true_divide+;中遇到无效值;reduce中遇到无效值

Python 在true#u divide+中遇到被零除的情况;true_divide+;中遇到无效值;reduce中遇到无效值,python,numpy,divide-by-zero,Python,Numpy,Divide By Zero,下面是我试图绘制的基于温度和距离变化的半圆形图,如下所示 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

下面是我试图绘制的基于温度和距离变化的半圆形图,如下所示

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.0
1.15.4

问题可能来自这样一个事实,即它是一个矩阵除法,python处理得不太好

尝试替换:

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

作者:

它对我有用


SLP

问题可能来自于这样一个事实,即它是一个矩阵除法,python不能很好地处理它

尝试替换:

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

作者:

它对我有用


SLP

代码对我来说运行良好,没有任何警告或错误,生成不同的绘图(不确定是否是您想要的)。我正在使用matplotlib 2.2.2和python 3.6.5。您使用的是什么版本?@Bazingaa:My python是
3.7.0(默认值,2018年6月28日,08:04:48)[MSC v.1912 64位(AMD64)]
,而我的matplotlib是
3.0.2
。这个问题似乎是通过使用浮点数组来解决的,
d=np.arange(d_lower,d_upper,100.)
d.dtype==float64
)而不是整数数组
d=np.arange(d_lower,d_upper,100)
d.dtype==int32
)。我目前不知道为什么会出现这个问题。可能您所在的平台(可能是Windows)上的numpy整数数组的默认数据类型是32位(
np.int32
)。表达式
np.power(D,2)
是使用
D
的数据类型计算的,因此其结果具有类型
np.int32
。但是,该结果中的精确值大于32位整数所能表示的值,因此值会溢出,并可能变为负数或零。例如,
np.array([1060001638400],dtype=np.int32)**2
返回
array([-1648901888,0],dtype=int32)
@ImportantanceOfBeingernest使用浮点的建议将解决这个问题。代码对我来说运行良好,没有任何警告或错误产生不同的绘图(不确定它是否是您想要的)。我正在使用matplotlib 2.2.2和python 3.6.5。您使用的是什么版本?@Bazingaa:My python是
3.7.0(默认值,2018年6月28日,08:04:48)[MSC v.1912 64位(AMD64)]
,而我的matplotlib是
3.0.2
。这个问题似乎是通过使用浮点数组来解决的,
d=np.arange(d_lower,d_upper,100.)
d.dtype==float64
)而不是整数数组
d=np.arange(d_lower,d_upper,100)
d.dtype==int32
)。我目前不知道为什么会出现这个问题。可能您所在的平台(可能是Windows)上的numpy整数数组的默认数据类型是32位(
np.int32
)。表达式
np.power(D,2)
是使用
D
的数据类型计算的,因此其结果具有类型
np.int32
。但是,该结果中的精确值大于32位整数所能表示的值,因此值会溢出,并可能变为负数或零。例如,
np.array([1060001638400],dtype=np.int32)**2
返回
array([-1648901888,0],dtype=int32)
@Beingernest关于使用浮点的建议将解决这个问题。
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