How to resolve the algorithm Quaternion type step by step in the Common Lisp programming language
How to resolve the algorithm Quaternion type step by step in the Common Lisp programming language
Table of Contents
Problem Statement
Quaternions are an extension of the idea of complex numbers.
A complex number has a real and complex part, sometimes written as a + bi,
where a and b stand for real numbers, and i stands for the square root of minus 1.
An example of a complex number might be -3 + 2i,
where the real part, a is -3.0 and the complex part, b is +2.0.
A quaternion has one real part and three imaginary parts, i, j, and k.
A quaternion might be written as a + bi + cj + dk.
In the quaternion numbering system:
The order of multiplication is important, as, in general, for two quaternions:
An example of a quaternion might be 1 +2i +3j +4k
There is a list form of notation where just the numbers are shown and the imaginary multipliers i, j, and k are assumed by position.
So the example above would be written as (1, 2, 3, 4)
Given the three quaternions and their components: And a wholly real number r = 7.
Create functions (or classes) to perform simple maths with quaternions including computing:
If a language has built-in support for quaternions, then use it.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Quaternion type step by step in the Common Lisp programming language
Source code in the common programming language
(defclass quaternion () ((a :accessor q-a :initarg :a :type real)
(b :accessor q-b :initarg :b :type real)
(c :accessor q-c :initarg :c :type real)
(d :accessor q-d :initarg :d :type real))
(:default-initargs :a 0 :b 0 :c 0 :d 0))
(defun make-q (&optional (a 0) (b 0) (c 0) (d 0))
(make-instance 'quaternion :a a :b b :c c :d d))
(defgeneric sum (x y))
(defmethod sum ((x quaternion) (y quaternion))
(make-q (+ (q-a x) (q-a y))
(+ (q-b x) (q-b y))
(+ (q-c x) (q-c y))
(+ (q-d x) (q-d y))))
(defmethod sum ((x quaternion) (y real))
(make-q (+ (q-a x) y) (q-b x) (q-c x) (q-d x)))
(defmethod sum ((x real) (y quaternion))
(make-q (+ (q-a y) x) (q-b y) (q-c y) (q-d y)))
(defgeneric sub (x y))
(defmethod sub ((x quaternion) (y quaternion))
(make-q (- (q-a x) (q-a y))
(- (q-b x) (q-b y))
(- (q-c x) (q-c y))
(- (q-d x) (q-d y))))
(defmethod sub ((x quaternion) (y real))
(make-q (- (q-a x) y)
(q-b x)
(q-c x)
(q-d x)))
(defmethod sub ((x real) (y quaternion))
(make-q (- (q-a y) x)
(q-b y)
(q-c y)
(q-d y)))
(defgeneric mul (x y))
(defmethod mul ((x quaternion) (y real))
(make-q (* (q-a x) y)
(* (q-b x) y)
(* (q-c x) y)
(* (q-d x) y)))
(defmethod mul ((x real) (y quaternion))
(make-q (* (q-a y) x)
(* (q-b y) x)
(* (q-c y) x)
(* (q-d y) x)))
(defmethod mul ((x quaternion) (y quaternion))
(make-q (- (* (q-a x) (q-a y)) (* (q-b x) (q-b y)) (* (q-c x) (q-c y)) (* (q-d x) (q-d y)))
(- (+ (* (q-a x) (q-b y)) (* (q-b x) (q-a y)) (* (q-c x) (q-d y))) (* (q-d x) (q-c y)))
(- (+ (* (q-a x) (q-c y)) (* (q-c x) (q-a y)) (* (q-d x) (q-b y))) (* (q-b x) (q-d y)))
(- (+ (* (q-a x) (q-d y)) (* (q-b x) (q-c y)) (* (q-d x) (q-a y))) (* (q-c x) (q-b y)))))
(defmethod norm ((x quaternion))
(+ (sqrt (q-a x)) (sqrt (q-b x)) (sqrt (q-c x)) (sqrt (q-d x))))
(defmethod print-object ((x quaternion) stream)
(format stream "~@f~@fi~@fj~@fk" (q-a x) (q-b x) (q-c x) (q-d x)))
(defvar q (make-q 0 1 0 0))
(defvar q1 (make-q 0 0 1 0))
(defvar q2 (make-q 0 0 0 1))
(defvar r 7)
(format t "q+q1+q2 = ~a~&" (reduce #'sum (list q q1 q2)))
(format t "r*(q+q1+q2) = ~a~&" (mul r (reduce #'sum (list q q1 q2))))
(format t "q*q1*q2 = ~a~&" (reduce #'mul (list q q1 q2)))
(format t "q-q1-q2 = ~a~&" (reduce #'sub (list q q1 q2)))
You may also check:How to resolve the algorithm Least common multiple step by step in the SenseTalk programming language
You may also check:How to resolve the algorithm Pig the dice game step by step in the Ring programming language
You may also check:How to resolve the algorithm Matrix multiplication step by step in the Go programming language
You may also check:How to resolve the algorithm Non-continuous subsequences step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Multiplication tables step by step in the Haskell programming language