为什么numpy.arctan2不支持ufloat?

oewdyzsn  于 2023-08-05  发布在  其他
关注(0)|答案(2)|浏览(88)

对uncertainty.ufloat对象运行np.arctan2会导致错误。“属性错误:“Variable”对象没有属性“arctan2”
Someone else asked this question before,然而,这是因为与sympy的名称冲突。删除名称冲突修复了问题。
相反,我得到的错误没有导入任何其他东西。下面是最小的代码,如果我用它制作一个新的jupyter notebook,它会给我带来错误:

from uncertainties import ufloat
import numpy as np

ccos = ufloat(-3.45694E-05, 1.43107E-06)
csin = ufloat(-0.000363727, 1.36989E-06)

np.arctan2(csin,ccos)

字符串
抛出错误

AttributeError                            Traceback (most recent call last)
Untitled-1.ipynb Cell 1 in ()
      4 ccos = ufloat(-3.45694E-05, 1.43107E-06)
      5 csin = ufloat(-0.000363727, 1.36989E-06)
----> 7 np.arctan2(csin,ccos)

AttributeError: 'Variable' object has no attribute 'arctan2'


为什么会这样?

cu6pst1q

cu6pst1q1#

这很可能是因为ufloat不是原生的numpy数据类型。
您可以使用uncertainties提供的math函数。
我刚刚检查了以下修改后的代码,它工作正常:

from uncertainties import ufloat

from uncertainties.umath import atan2

ccos = ufloat(-3.45694E-05, 1.43107E-06)

csin = ufloat(-0.000363727, 1.36989E-06)

angle = atan2(csin, ccos)

print(angle)

字符串
其输出:

-1.666+/-0.004

7y4bm7vi

7y4bm7vi2#

看起来numpy不支持不确定性包。
要解决这个问题,可以使用数学函数的不确定性包版本:

from uncertainties.umath import *
atan2(ufloat(-3.4, 1.4),ufloat(-2,.2))

字符串
(this可能是太简单了,不确定我是否应该删除这个问题)

相关问题