qmath非常奇怪的AttributeError

时间:2014-03-28 21:53:10

标签: python quaternions

我试图使用qmath,一个四元数lib。

from qmath import  qmathcore
a = qmathcore.quaternion([1,2,3,4])
print a.conj()

给了我这样的追溯

Traceback (most recent call last):
  File "*******/q_test.py", line 25, in <module>
    print str(a.conj())
  File "*******/venv/lib/python2.7/site-packages/qmath/qmathcore.py", line 788, in conj
    return self.real() - self.imag()
  File "*******/venv/lib/python2.7/site-packages/qmath/qmathcore.py", line 762, in imag
    return self - self.real()      
  File "*******/venv/lib/python2.7/site-packages/qmath/qmathcore.py", line 522, in __sub__
    self -= other
  File "*******/venv/lib/python2.7/site-packages/qmath/qmathcore.py", line 407, in __isub__
    self.other = quaternion(other)
  File "*******/venv/lib/python2.7/site-packages/qmath/qmathcore.py", line 81, in __init__
    self.q = q.q
AttributeError: quaternion instance has no attribute 'q'

但他们在文档中说,这必须有效:

def conj(self):
    """
    Returns the conjugate of the quaternion
    >>> import qmathcore
    >>> a = qmathcore.quaternion([1,2,3,4])
    >>> a.conj()
    (1.0-2.0i-3.0j-4.0k)
    >>> a = qmathcore.hurwitz([1,2,3,4])
    >>> a.conj()
    (1-2i-3j-4k)
    """
    return self.real() - self.imag()
这是什么?

1 个答案:

答案 0 :(得分:0)

qmathcore.py使用较新的(1.9)numpy失败了自己的doctest。

将此测试添加到quatereon()

    elif isinstance(q,float) or isinstance(q,int): # accept np.float64
        self.q = 1.0 * np.array([q,0.,0.,0.])

允许qmath.quaternion([1,2,3,4]).imag()(和conj)。

quaternion方法正在使用大量type(q)==xxx次测试。 isinstance()是一个更强大的测试。它也以else:pass结尾,因此不会捕获它无法处理的q值。

纠正一些导入错误后,qmathcore doctest运行正常。