Methods ------- Basic Methods ^^^^^^^^^^^^^ __repr__() """""""""" .. py:method:: __repr__(self) 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. __getitem__() """"""""""""" .. py:method:: __getitem__(self,key) Returns one of the four components of the quaternion. This method allows to get the components by ``quaternion[key]`` __setitem__() """"""""""""" .. py:method:: __setitem__(self,key, number) Set one of the four components of the quaternion. This method allows to set the components by ``quaternion[key] = number`` __delitem__() """"""""""""" .. py:method:: __delitem__(self,key) Delete (set to zero) one of the four components of the quaternion. This method allows to write ``del quaternion[key]`` __delslice__() """""""""""""" .. py:method:: __delslice__(self,key_1,key_2) Delete (set to zero) the components of the quaternion from the `key_1-th` to the `key_2-th`. This method allows to write ``del quaternion[1:3]`` __contains__() """""""""""""" .. py:method:: __contains__(self,key) Returns 0 if the component of the quaternion with respect to `key` is zero, 1 otherwise. This method allows to write ``del 'k' in quaternion`` >>> q = qmath.quaternion('1+1i+5k') >>> 'j' in q False >>> 'i' in q True Algebraic Operations ^^^^^^^^^^^^^^^^^^^^ __eq__() """""""" .. py:method:: __eq__(self,other) 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 __ne__() """""""" .. py:method:: __ne__(self,other) 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 __int__() """"""""" .. py:method:: __int__(self) Converts `self` components into integers. >>> q = qmath.quaternion('1+1i+5k') >>> q (1.0+1.0i+5.0k) >>> q._int_() (1+1i+5k) __iadd__(), __isub__(), __imul__(), __idiv__() """""""""""""""""""""""""""""""""""""""""""""" .. py:method:: __iadd__(self,other) .. py:method:: __isub__(self,other) .. py:method:: __imul__(self,other) .. py:method:: __idiv__(self,other) 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) __imod__() """""""""" .. py:method:: __imod__(self,other) 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. __add__(), __sub__(), __mul__(), __div__(), __mod__() """"""""""""""""""""""""""""""""""""""""""""""""""""""""" .. py:method:: __add__(self,other) .. py:method:: __sub__(self,other) .. py:method:: __mul__(self,other) .. py:method:: __div__(self,other) .. py:method:: __mod__(self,other) 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__()``). __rmul__(), __rdiv__() """""""""""""""""""""" .. py:method:: __rmul__(self,other) .. py:method:: __rdiv__(self,other) 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__()``). __neg__() """"""""" .. py:method:: __neg__(self) Return the opposite of a quaternion. You can write ``- quaternion``. __pow__() """"""""" .. py:method:: __pow__(self,exponent[,modulo]) 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) __abs__() """"""""" .. py:method:: __abs__(self) Returns the modulus of the quaternion. equal() """"""" .. py:method:: equal(self,other[,tolerance]) 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 Other Algebraic Manipulations ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ real() """""" .. py:method:: real(self) Returns the real part of the quaternion. imag() """""" .. py:method:: imag(self) Returns the imaginary part of the quaternion. trace() """"""" .. py:method:: trace(self) Returns the trace of the quaternion (the double of its real part). conj() """""" .. py:method:: conj(self) Returns the conjugate of the quaternion. norm() """""" .. py:method:: norm(self) Returns the norm of the quaternion (the square of the modulus). delta() """"""" .. py:method:: delta(self) Returns the `delta` of the quaternion, that is the opposite of the norm of the imaginary part of the quaternion (see :ref:`[2]` for details). inverse() """"""""" .. py:method:: inverse(self[,modulo]) 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) unitary() """"""""" .. py:method:: unitary(self) Returns the normalized quaternion, if different from zero. sqrt() """""" .. py:method:: sqrt(self) 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). croot() """"""" .. py:method:: croot(self) Computes the cube root (unique) of a quaternion (see [1]_ and [2]_ for details on roots extraction). QuaternionToRotation() """""""""""""""""""""" .. py:method:: QuaternonToRotation(self) Converts the quaternion, if unitary, into a rotation matrix. .. rubric:: References .. [1] M. Abrate, `Quadratic Formulas for Generalized Quaternions `_, The Journal of Algebra and its Applications, Vol. 8, Issue 3 (2009), pp. 289-306. .. [2] M. Abrate `The Roots of a Generalized Quaternion`, Congressus Numeratium, Vol. 201 (2010), pp. 179-186, Kluwer Academic Publishers.