【发布时间】:2018-02-28 16:30:43
【问题描述】:
我将两个正整数相乘得到一个负值,因此我无法计算 sqrt 但得到一个math domain error。
我的变量的大小可以是 10^10 或更大。
sum = math.sqrt ( np.power( x , 2 ) * np.power( y , 2 ) )
哪种 dtype 适合我的需求,或者我还能如何解决这个问题?
编辑:
当前的值都是(偶然相同)59049。但正如我所说,它们已经产生了错误并且可能会变得更高。我不能给你打印,因为这是在 Django 中完成的计算的一部分。
编辑 2:
正如在 cmets 中正确假设的那样,代码应该是 + 没有 *
d.sum = math.sqrt( np.power(d.active , 2) + np.power(d.passive , 2))
更多背景: 在我的项目中,我得到了一个矩阵,需要进行以下计算:
test = np.arange(9).reshape(3,3)
m = test
matrix= m.dot(m).dot(m).dot(m).dot(m)
activearray = matrix.sum(axis=1)
passivearray = matrix.sum(axis=0)
for idx, Descriptor.id in enumerate(projectdescriptors):
d = Descriptor.objects.get(name=Descriptor.id)
d.active = activearray[idx]
d.passive = passivearray[idx]
d.sum = math.sqrt( np.power(d.active , 2) + np.power(d.passive , 2))
d.save()
【问题讨论】:
-
这一行之前的 x 和 y 中的值是什么?您能否在错误之前添加一个 print(x) 和 print(y) 以查看它们的值是什么?
-
这段代码没有意义。可以简化为
sum = a * b -
@MarkDickinson:除了这不是hypot(截至目前)
-
@SergioTulentsev:是的。我看了一眼,把
*误认为+。不过,我不禁怀疑hypot是 OP 真正想要的。 -
@MarkDickinson:是的,我也有这种怀疑。
标签: python python-3.x numpy