0

I'm creating a module in Python to store and manipulate data as Quaternions (composed of a real element w and a vector element v = xi + yj + zk. Creating the quaternion class for storing the data is simple:

class quaternion_c:

   def __init__(self, w, x, y, z):
      self.w = w
      self.x = x
      self.y = y
      self.z = z

   ...

What I want to do is, in tandem with using custom methods for performing arithmetic on quaternion_c, I want to be able to use basic operators on the instances themselves in much the same way NumPy Arrays use them. In short, given two example quaternion_c objects:

qtn0 = quaternion_c(0, 1, 2, 3)
qtn1 = quaternion_c(100, 99, 98, 97)

I want qtn0 + qtn1 to produce the same results as:

def add(qtn0, qtn1):
   w_01 = qtn0.w + qtn1.w
   x_01 = qtn0.x + qtn1.x
   y_01 = qtn0.y + qtn1.y
   z_01 = qtn0.z + qtn1.z

   return quaternion_c(w_01, x_01, y_01, z_01)

add(qtn0, qtn1)

I would also like to specify similar changes to other operators, like * for mult() and ** for exponent(). Is this doable? Is it possible to change how basic operators handle specific classes?

S. Gamgee
  • 501
  • 5
  • 16

0 Answers0