SymPy - Quaternion


Advertisements

In mathematics, Quaternion number system is an extension of complex numbers. Each Quaternion object contains four scalar variables and four dimensions, one real dimension and three imaginary dimensions.

Quaternion is represented by following expression −

q=a+bi+cj+dk

where a, b, c and d are real numbers and i, j, k are quaternion units such that,i2==j2==k2==ijk

The sympy.algebras.quaternion module has Quaternion class.

>>> from sympy.algebras.quaternion import Quaternion 
>>> q=Quaternion(2,3,1,4) 
>>> q

The above code snippet gives an output equivalent to the below expression −

$2 + 3i + 1j + 4k$

Quaternions are used in pure mathematics, as well as in applied mathematics, computer graphics, computer vision, etc.

>>> from sympy import * 
>>> x=Symbol('x') 
>>> q1=Quaternion(x**2, x**3, x) >>> q1

The above code snippet gives an output equivalent to the below expression −

$x^2 + x^3i + xj + 0k$

Quaternion object can also have imaginary co-efficients

>>> q2=Quaternion(2,(3+2*I), x**2, 3.5*I) 
>>> q2

The above code snippet gives an output equivalent to the below expression −

$2 + (3 + 2i)i + x2j + 3.5ik$

add()

This method available in Quaternion class performs addition of two Quaternion objects.

>>> q1=Quaternion(1,2,3,4) 
>>> q2=Quaternion(4,3,2,1) 
>>> q1.add(q2)

The above code snippet gives an output equivalent to the below expression −

$5 + 5i + 5j + 5k$

It is possible to add a number or symbol in a Quaternion object.

>>> q1+2

The following output is obtained after executing the above code snippet −

$3 + 2i + 3j + 4k$

>>> q1+x

The following output is obtained after executing the above code snippet −

$(x + 1) + 2i + 3j + 4k$

mul()

This method performs multiplication of two quaternion objects.

>>> q1=Quaternion(1,2,1,2) 
>>> q2=Quaternion(2,4,3,1) 
>>> q1.mul(q2)

The above code snippet gives an output equivalent to the below expression −

$(-11) + 3i + 11j + 7k$

inverse()

This method returns inverse of a quaternion object.

>>> q1.inverse()

The above code snippet gives an output equivalent to the below expression −

$\frac{1}{10} + (-\frac{1}{5})i + (-\frac{1}{10})j + (-\frac{1}{5})k$

pow()

This method returns power of a quaternion object.

>>> q1.pow(2)

The following output is obtained after executing the above code snippet −

$(-8) + 4i + 2j + 4k$

exp()

This method computes exponential of a Quaternion object i.e. eq

>>> q=Quaternion(1,2,4,3) 
>>> q.exp()

The following output is obtained after executing the above code snippet −

$e\cos(\sqrt29) + \frac{2\sqrt29e\sin(\sqrt29)}{29}i + \frac{4\sqrt29e\sin(\sqrt29)}{29}j + \frac{3\sqrt29e\sin}{29}k$

Advertisements