Posted by: Zeeshan Amjad | September 9, 2019

Quaternions in Python


I was trying to learn the syntax of Python so looking for some simple example I can implement. The first think came in my mind is a typical Point class which can be used in Graphics. Before even I write anything, I came across an interesting concept of Quaternions which is an actually extension imaginary number. Here is my attempt to write Quaternions in Python.
import math

class Quaternion:
    def __init__(self, s = 1, i = 0, j = 0, k = 0):
        self.i = i
        self.j = j
        self.k = k
        self.s = s

    def getX(self):
        return self.i

    def setX(self, i):
        self.i = i

    def getY(self):
        return self.j

    def setY(self, j):
        self.j = j

    def getZ(self):
        return self.k

    def setZ(self, k):
        self.k = k

    def getS(self):
        return self.s

    def setS(self, s):
        self.s = s

    def __str__(self):
        return "(" + str(self.getS()) + "," + str(self.getX()) + "," + str(self.getY()) + "," + str(self.getZ()) + ")"

    def __add__(self, quaternion):
        q = Quaternion()
        q.i = self.getX() + quaternion.getX()
        q.j = self.getY() + quaternion.getY()
        q.k = self.getZ() + quaternion.getZ()
        q.s = self.getS() + quaternion.getS()
        return q

    def __mul__(self, quaternion):
        q = Quaternion()
        q.s = self.getS() * quaternion.getS() - self.getX() * quaternion.getX() - self.getY() * quaternion.getY() - self.getZ() * quaternion.getZ()
        q.i = self.getS() * quaternion.getX() + quaternion.getS() * self.getX() + self.getY() * quaternion.getZ() - self.getZ() * quaternion.getY()
        q.j = self.getS() * quaternion.getY() - self.getX() * quaternion.getZ() + self.getY() * quaternion.getS() + self.getZ() * quaternion.getX()
        q.k = self.getS() * quaternion.getZ() + self.getX() * quaternion.getY() - self.getY() * quaternion.getX() + self.getZ() * quaternion.getS()
        return q

    def __eq__(self, quaternion):
        if (self.getS() == quaternion.getS() and self.getX() == quaternion.getX() and self.getY() == quaternion.getY() and self.getZ() == quaternion.getZ()):
            return True
        else:
            return False

    def conjugate(self):
        q = Quaternion(self.getS(), -1 * self.getX(), -1 * self.getY(), -1 * self.getZ())
        return q

    def norm(self):
        return math.sqrt(self.getS()**2 + self.getX()**2 + self.getY()**2 + self.getZ()**2)

    def inverse(self):
        c = self.conjugate()
        norm = self.norm()

        if norm == 0:
            raise Exception("norm is zero can't calculate inverse.")

        c.s = c.getS() / norm ** 2
        c.i = c.getX() / norm ** 2
        c.j = c.getY() / norm ** 2
        c.k = c.getZ() / norm ** 2
        return c

    def dot(self, quaternion):
        return self.getS() * quaternion.getS() + self.getX() * quaternion.getX() + self.getY() * quaternion.getY() + self.getZ() * quaternion.getZ()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Categories

%d bloggers like this: