espressopp.Quaternion

This class provides quaternions with the associate methods. Quaternions can be used as an efficient representation for the orientation and rotation of 3D vector objects in 3D euclidean space. A Quaternion as such has a real part and an imaginary part. For implementation purposes, the representation through one real scalar and one real 3D vector is used here. The vector part is defined using the Real3D class of espressopp.

The format of a quaternion is “(real_part, unreal_part)” with the types “real” and “Real3D”, respectively.

While there are other possible applications for quaternions (rotation) in the simulation code, they will be used at the C++-level in order to per- form the integration of the Euler equations of motion regarding the partic- les angular motion, i.e. the rigid body dynamics.

Usage:

The following methods from C++-level are available at the python-level:

  • getReal()
    return the scalar part of the quaternion
  • setReal(real)
    sets the scalar part of the quaternion
  • getImag()
    returns the vector part of the quaternion
  • getImagItem(i)
    returns element i of vector part of the quaternion
  • setImag(Real3D)
    sets the vector part of the quaternion
  • setImagItem(i, real)
    sets element i of vector part of the quaternion
  • sqr()
    the inner product of the quaternion
  • abs()
    the absolute value of the quaternion
  • normalize()
    normalizes the quaternion to unit length
  • transpose()
    transposes the quaternion (changes sign of unreal_part)

The multiplication operator is overloaded in order to perform quaternion multiplication, see examples below. Furthermore, it is possible to multi- ply a quaternion with a scalar, in order to rescale it.

Examples:

Initialize:

>>> espressopp.Quaternion()
Quaternion(0.0, Real3D(0.0, 0.0, 0.0)) 
>>> espressopp.Quaternion(0.0, 1.0, 2.0, 3.0)
Quaternion(1.0, Real3D(1.0, 2.0, 3.0)) 
>>> vec = espressopp.Real3D(1.0, 2.0, 3.0)
>>> Quaternion(vec) 
Quaternion(0.0, Real3D(1.0, 2.0, 3.0))
>>> espressopp.Quaternion(1.0)
Quaternion(1.0, Real3D(0.0, 0.0, 0.0))

Get:

>>> q = espressopp.Quaternion(0.0, 1.0, 2.0, 3.0)
>>> q.getReal()
0.0
>>> q.getImag()
Real3D(1.0, 2.0, 3.0)
>>> q.getImagItem(0)
1.0

Set:

>>> q = espressopp.Quaternion(0.0, 0.0, 0.0, 0.0)
>>> q.setReal(1.0)
>>> vec = espressopp.Real3D(1.0, 2.0, 3.0)
>>> q.setImag(vec)
>>> q
Quaternion(1.0, Real3D(1.0, 2.0, 3.0))
>>> q.setImagItem(0, 0.0)
Quaternion(1.0, Real3D(0.0, 2.0, 3.0))

Transpose and normalize:

>>> q = Quaternion(0.0, 1.0, 2.0, 3.0) 
>>> q.transpose()
Quaternion(0.0, Real3D(-1.0, -2.0, -3.0))
>>> q = Quaternion(0.0, 1.0, 2.0, 3.0) 
>>> q.normalize()
Quaternion(0.0, Real3D(0.2672612419124244, 0.5345224838248488, 0.8017837257372732))

Inner product and absolute value:

>>> q = Quaternion(0.0, 1.0, 2.0, 3.0) 
>>> q.sqr()
14.0
>>> q.abs()
3.7416573867739413

Quaternion multiplication (compare, e.g., wikipedia):

>>> p = Quaternion(0.0, 1.0, 2.0, 3.0)
>>> q = Quaternion(0.0, 1.0, 2.0, 3.0)
Quaternion(-14.0, Real3D(0.0, 0.0, 0.0))
espressopp.Quaternion.toQuaternion(*args)

Try to convert the arguments to a Quaternion, return the argument if it is already a Quaternion.

espressopp.Quaternion.toQuaternionFromVector(*args)

Try to convert the arguments to a Quaternion.

This function will only convert to a Quaternion if real_part, unreal_part[0], unreal_part[1] and unreal_part[2] are specified.