SymPy – Quaternion

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 โˆ’

x2+x3i+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 โˆ’

1/10+(โˆ’1/5)i+(โˆ’1/10)j+(โˆ’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 โˆ’

ecos(โˆš29)+(2โˆš29e sin(โˆš29)/29)i/29+4โˆš29e sin(โˆš29)j/29+3โˆš29e sink/29

Leave a Reply