Quaternions are represented in the algebraic way: q = a+bi+cj+dk, and a, b, c, d are floats. Components are of float type if self is a quaternion, of integer type if self is a Hurwitz quaternion.
Returns one of the four components of the quaternion. This method allows to get the components by
quaternion[key]
Set one of the four components of the quaternion. This method allows to set the components by
quaternion[key] = number
Delete (set to zero) one of the four components of the quaternion. This method allows to write
del quaternion[key]
Returns True if two quaternion are equal, False otherwise. This method allows to write
quaternion1 == quaternion2
>>> q = qmath.quaternion('1+1k')
>>> q == 0
False
>>> q == '1+1k'
True
Also equalities with a tolerance are admitted:
>>> q == qmath.quaternion([1,0,1e-15,1])|1e-9
True
>>> q == [1,1,1e-15,0]
False
Returns False if two quaternion are equal, True otherwise. This method allows to write
quaternion1 != quaternion2
>>> q = qmath.quaternion('1+1k')
>>> q != 0
True
>>> q != '1+1k'
False
Converts self components into integers.
>>> q = qmath.quaternion('1+1i+5k')
>>> q
(1.0+1.0i+5.0k)
>>> q._int_()
(1+1i+5k)
These methods are called to implement the augmented arithmetic assignments. These methods do the operation in-place (modifying self). If other is a Hurwitz quaternion but its inverse is not, the __idiv__() method raises an error.
Methods __imul__() and __idiv__() depend on the choice of matrix in the initialization of the quaternions.
>>> a = qmathcore.quaternion([0,-1,0,-1], matrix = np.array([[-1,0,0],[0,1,0],[0,0,-1]]))
>>> b = qmathcore.quaternion([0,0,2,0], matrix = np.array([[-1,0,0],[0,1,0],[0,0,-1]]))
>>> a *= b
>>> a
(-2.0i+2.0k)
This method is called to implement the modular reduction. It is performed only if self is a Hurwitz quaternion and other is an integer. Otherwise an error is raised.
These methods are called to implement the binary arithmetic operations. For instance, to evaluate the expression x + y, where x is a quaternion, x.__add__(y) is called. y can either be a quaternion (or a Hurwitz quaternion) or something that can be converted to quaternion.
If other is a Hurwitz quaternion but its inverse is not, the __div__ method raises an error.
The __mod__ method is performed only if self is a Hurwitz quaternion and other is integer (see __imod__).
Methods __mul__() and __div__() depend on the choice of matrix in the initialization of the quaternions (see __imul__() and __idiv__()).
These methods are called to implement the binary arithmetic operations with reflected operands. If other is a Hurwitz quaternion but its inverse is not, the __rdiv__ method raises an error.
Methods __rmul__() and __rdiv__() depend on the choice of matrix in the initialization of the quaternions (see __imul__() and __idiv__()).
Implements the operator **. The power of a quaternion can be computed for integer power (both positive or negative) and also if the exponent is half or third a number: for example square or cube roots are evaluated (see also sqrt and croot).
This method depends on the choice of matrix in the initialization of the quaternions (see __imul__()).
If self is a Hurwitz quaternion, powers are computed only for natural exponents. Modular reduction is performed for Hurwitz quaternions (if self is a Hamilton quaternion, modulo is ignored).
>>> base = qmath.quaternion('1+1i+2j-2k')
>>> base ** 3
(-26.0-6.0i-12.0j+12.0k)
>>> base ** (-2)
(-0.08-0.02i-0.04j+0.04k)
>>> qmath.quaternion([-5,1,0,1]) ** (1.0/3)
(1.0+1.0i+1.0k)
>>> qmath.quaternion([-5,1,0,1]) ** (2.0/3)
(-1.0+2.0i+2.0k)
>>> qmath.quaternion('1.0+1.0i+1.0k') ** 2
(-1.0+2.0i+2.0k)
>>> qmathcore.hurwitz('1+1i+1k') ** 2
(-1+2i+2k)
>>> pow(qmathcore.hurwitz('1+1i+1k'),2,3)
(2+2i+2k)
Returns quaternion equality with arbitrary tolerance. If no tolerance is admitted it is the same as __eq__(self,other).
>>> a = qmath.quaternion([1,1,1e-15,0])
>>> b = qmath.quaternion(1+1j)
>>> a.equal(b,1e-9)
True
>>> a.equal(b)
False
Returns the delta of the quaternion, that is the opposite of the norm of the imaginary part of the quaternion (see [2] for details).
Quaternionic inverse, if it exists. It is equivalent to quaternion ** (-1).
Modular inversion can be performed for Hurwitz quaternions (if self is a Hamilton quaternion, modulo is ignored).
>>> a = qmath.quaternion([2,-2,-4,-1])
>>> a.inverse()
(0.08+0.08i+0.16j+0.04k)
>>> b = qmath.hurwitz([0,-2,-2,0])
>>> b.inverse(13)
(10i+10j)
Computes the square root of the quaternion. If the quaternion has only two roots, the one with positive trace is given: if this method returns r, also -r is a root (see [1] and [2] for details on roots extraction).
Computes the cube root (unique) of a quaternion (see [1] and [2] for details on roots extraction).
Converts the quaternion, if unitary, into a rotation matrix.
References
[1] | (1, 2)
|
[2] | (1, 2)
|