对此的回答有点棘手,需要掌握Python如何处理整数以及numpy强制类型的知识。感谢@ali_m的评论!
假设64位整数的最大可表示整数是9,223,372,036,854,775,807
(例如参见Wikipedia),其大致为10**19
。但是,只要超过这个值,Python就会回到无限的整数表示形式(就像你的情况10**44
)。但是这种无限精度类型并不是NumPy本身支持的,所以结果将回落到object
s和这些object
阵列不支持支持所有(any?)ufuncs,如np.log10
。
解决方法很简单:将这种大量的浮点数:
>>> # Negative powers will result in floats: 44 -> -44, * instead of/
>>> np.array([10, 20]) * 10**-44
array([ 1.00000000e-43, 2.00000000e-43])
>>> # you could also make the base a float: 10 -> 10.
>>> np.array([10, 20])/10.**44
array([ 1.00000000e-43, 2.00000000e-43])
>>> # or make the exponent a float: 44 -> 44.
>>> np.array([10, 20])/10**44.
array([ 1.00000000e-43, 2.00000000e-43])
>>> # manually cast the result to a float
>>> np.array([10, 20])/float(10**44)
array([ 1.00000000e-43, 2.00000000e-43])
>>> # not working, result is an object array which cannot be used for np.log10
>>> np.array([10, 20])/(10**(44))
array([1e-43, 2e-43], dtype=object)
>>> # ^---------that's the problem!
所有你需要的是改变第三行中的功能:
import numpy as np
def mass(FWHM, lumi):
s1 = FWHM/1000
s2 = lumi * 10**-44 # changed line, using the first option.
s = s1**2 * s2**0.53
mass = np.log10(s) + 6.66
return mass
这个工程至少与我所有的测试输入,例如:
>>> mass(np.array([10., 20]), np.array([10., 20]))
array([-20.13 , -19.36839411])
什么是'bhm'?为什么要标记和导入星座?它不在任何地方使用。什么_exactly_是您的输入(使其成为原稿)?如果我忽略'bhm.L_1450'调用,只使用'lumi',那么整数,浮点数和数组都可以正常工作。你使用的是什么numpy版本? – MSeifert