Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Quaternions Complex numbers have two components usually called the real and imag

ID: 3831267 • Letter: Q

Question

Quaternions

Complex numbers have two components usually called the real and imaginary parts. This concept can be extended to numbers called quaternions that have four components. Quaternions were originally developed in the mid-1800s by William Hamilton. Today they have application in computer graphics, computer vision, flight dynamics, and satellite navigation where they can be used to elegantly describe rotations in three dimensions.

Let quaternion q = (a, b, c, d) be a tuple of four real numbers. The various arithmetic operations on quaternions are defined as follows.

Scalar Multiplication: s*q = (s*a, s*b, s*c, s*d)

Addition: q1 + q2 = (a1 + a2, b1 + b2, c1 + c2, d1 + d2)

Subtraction: q1 - q2 = q1 + (-1)*q2

Multiplication: The multiplication of quaternions is more complicated and is as shown below.

One important property of quaternion multiplication is that it is not commutative. That is, q1 * q2 != q2 * q1.

Before the division of quaternions can be described it is necessary to define two additional operations. The conjugate of a quaternion is (a, -b, -c, -d), and the norm of a quaternion is sqrt(a*a* + b*b + c*c + d*d). Finally, the inverse of a quaternion q is conj(q)/(norm(q)*norm(q)). Notice that the norm of a quaternion is a scalar and so the previous division is just scalar multiplication of conj(q) by 1/(norm(q)*norm(q)).

There are now two different forms of quaternion division. Suppose we are trying to compute q1/q2. Left division is inv(q2)*q1. Right division is q1*inv(q2). Because quaternion multiplication is not commutative, these two values are, in general, different.

Class Quaternion

For this assignment you are to write a class Quaternion. Your class should satisfy the following requirements.

Your class should provide a default constructor that initializes a Quaternion object to zero.

Your class should provide a constructor that allows the values of a, b, c, and d to be set. Use type double for the components of your quaternion.

Your class should provide accessor methods that return the individual components of the quaternion.

Your class should provide overloaded operators +=, -=, and *= that do quaternion addition, subtraction, and multiplication. These operations take one quaternion parameter (by reference to const) and leave their result in the current object. For example

Your class should provide an additional overloaded *= that does scalar multiplication. For example

Provide free functions that are the overloaded binary operators +, -, and * that do quaternion addition, subtraction, and multiplication. These operations take two quaternions and return a new quaternion without changing either of their arguments. They should be written to take both parameters as references to const. For example

Hint: You can trivially implement the binary infix operators in terms of +=, -=, and *=. Do the computations in a local quaternion and return that result.

Provide a free function for the overloaded binary * operator that allows scalar multiplication. For example

Your class should provide a method conj that return the conjugate of the current object and a method norm that returns the norm of the current object. Neither of these methods should take any parameters.

Provide free functions left_division and right_division that compute quaternion division. Do not overload the division operator!

Provide a free function operator<< that allows a quaternion object to be written to an output stream.

Be sure to use const wherever it is appropriate.

Name your source files Quaternion.hpp and Quaternion.cpp

Explanation / Answer

Quaternion.h

====

/*
* Quaternion.h
*
* Created on: 02-May-2017
*      Author: Rj
*/

#ifndef QUATERNION_H_
#define QUATERNION_H_

#include <iostream>
#include <cmath>

class Quaternion
{
private:
   double _a, _b, _c, _d;
public:
   // CONSTRUCTORS
   Quaternion();
   Quaternion(double, double, double, double);
   // DESTRUCTOR
   virtual ~Quaternion();

   friend Quaternion & conj(Quaternion &);
   friend Quaternion & inv(Quaternion &);
   friend double norm(Quaternion &);

   // OVERRIDES
   // Quotient Multiplication.
   friend Quaternion & operator *(double, Quaternion &);
   // Addition
   friend Quaternion & operator +(Quaternion, Quaternion);
   // Subtraction
   friend Quaternion & operator -(Quaternion, Quaternion);
   // Multiplication
   friend Quaternion & operator *(Quaternion, Quaternion);
   // Division
   friend Quaternion & operator /(Quaternion, Quaternion);
   // Addition +=
   friend Quaternion & operator +=(Quaternion &, Quaternion);
   // Subtraction -=
   friend Quaternion & operator -=(Quaternion &, Quaternion);
   // Multiplication *=
   friend Quaternion & operator *=(Quaternion &, Quaternion);
   // Division /=
   friend Quaternion & operator /=(Quaternion &, Quaternion);

   // Output
   friend std::ostream & operator <<(std::ostream &, Quaternion &);
};

#endif /* QUATERNION_H_ */

Quaternion.cpp

====

/*
* Quaternion.cpp
*
* Created on: 02-May-2017
*      Author: ajay
*/

#include "Quaternion.h"
using namespace std;

Quaternion::Quaternion() :
   _a{}, _b{}, _c{}, _d{}
{}

Quaternion::Quaternion(
       double a, double b, double c, double d ) :
   _a{ a }, _b{ b }, _c{ c }, _d{ d }
{}

Quaternion::~Quaternion()
{}


/**
* Conjugate of Quaternion
*/
Quaternion & conj(Quaternion & q) {
   double a = q._a;
   -1 * q;
   q._a = a;
   return q;
}

/**
* Normal
*/
double norm(Quaternion &q) {
   return std::sqrt(
       q._a*q._a + q._b*q._b +
       q._c*q._c + q._d*q._d );
}

/**
* Inverse
*/
Quaternion & inv(Quaternion &q)
{
   return ( 1 / (norm(q)*norm(q)) ) * conj(q);
}

/**
* Multiplies a, b, c, d of given Quarternion with
* a given double s.
*/
Quaternion & operator *(double s, Quaternion & q)
{
   q._a = s*q._a;
   q._b = s*q._b;
   q._c = s*q._c;
   q._d = s*q._d;
   return q;
}

/**
* Adds two Quaternions
*/
Quaternion & operator +(
       Quaternion q1, Quaternion q2 )
{
   Quaternion *q3 = new Quaternion{};
   q3->_a = q1._a + q2._a;
   q3->_b = q1._b + q2._b;
   q3->_c = q1._c + q2._c;
   q3->_d = q1._d + q2._d;
   return *q3;
}

/**
* Subtracts two Quaternions
*/
Quaternion & operator -(Quaternion q1, Quaternion q2)
{
   Quaternion *q3 = new Quaternion{};
   // Multiply q2 with 1
   -1 * q2;
   *q3 = q1 + q2;
   return *q3;
}

/**
* Multiplies two Quaternions
*/
Quaternion & operator *(Quaternion q1, Quaternion q2)
{
   Quaternion *q3 = new Quaternion{};
   q3->_a = q1._a*q2._a - q1._b*q2._b
       - q1._c*q2._c - q1._d*q2._d;
   q3->_b = q1._a*q2._b + q1._b*q2._a
       + q1._c*q2._d - q1._d*q2._c;
   q3->_c = q1._a*q2._c - q1._b*q2._d
       + q1._c*q2._a + q1._d*q2._b;
   q3->_d = q1._a*q2._d + q1._b*q2._c
       - q1._c*q2._b + q1._d*q2._a;
   return *q3;
}

/**
* Division
*/
Quaternion & operator /(Quaternion q1, Quaternion q2)
{
   Quaternion *q3 = new Quaternion{};
   *q3 = inv(q2) * q1;
   return *q3;
}

/**
* Addition +=
*/
Quaternion & operator +=(Quaternion q1, Quaternion q2)
{
   q1 = q1 + q2;
   return q1;
}

/**
* Subtraction -=
*/
Quaternion & operator -=(Quaternion q1, Quaternion q2)
{
   q1 = q1 - q2;
   return q1;
}


/**
* Multiplication *=
*/
Quaternion & operator *=(Quaternion q1, Quaternion q2)
{
   q1 = q1 * q2;
   return q1;
}

/**
* Division /=
*/
Quaternion & operator /=(Quaternion q1, Quaternion q2)
{
   q1 = q1 / q2;
   return q1;
}

/**
* Writes class data to the screen.
*/
std::ostream & operator <<(std::ostream &os, Quaternion &q)
{
   cout << "[" << q._a << ", "
       << q._b << ", "
       << q._c << ", "
       << q._d << "]";
   return os;
}


Hi there, the question is way too long please post smaller parts of questions. I did as much as I coud and I realize there is still some left. The last part is not that hard may be you can have a look at it again after going through this code. If you could not work it out, post the remaining part as another question. You may want to read up on friend functions for sure and operator overriding.

Have a great day. Cheers :D,

PrimeHero