How to resolve the algorithm Quaternion type step by step in the PicoLisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Quaternion type step by step in the PicoLisp 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 PicoLisp programming language

Source code in the picolisp programming language

(scl 6)

(def 'quatCopy copy)

(de quatNorm (Q)
   (sqrt (sum * Q Q)) )

(de quatNeg (Q)
   (mapcar - Q) )

(de quatConj (Q)
   (cons (car Q) (mapcar - (cdr Q))) )

(de quatAddR (Q R)
   (cons (+ R (car Q)) (cdr Q)) )

(de quatAdd (Q1 Q2)
   (mapcar + Q1 Q2) )

(de quatMulR (Q R)
   (mapcar */ (mapcar * Q (circ R)) (1.0 .)) )

(de quatMul (Q1 Q2)
   (mapcar
      '((Ops I)
         (sum '((Op R I) (Op (*/ R (get Q2 I) 1.0))) Ops Q1 I) )
      '((+ - - -) (+ + + -) (+ - + +) (+ + - +))
      '((1 2 3 4) (2 1 4 3) (3 4 1 2) (4 3 2 1)) ) )

(de quatFmt (Q)
   (mapcar '((R S) (pack (format R *Scl) S))
      Q
      '(" + " "i + " "j + " "k") ) )

(setq
   Q (1.0 2.0 3.0 4.0)
   Q1 (2.0 3.0 4.0 5.0)
   Q2 (3.0 4.0 5.0 6.0)
   R 7.0 )

(prinl "R  = " (format R *Scl))
(prinl "Q  = " (quatFmt Q))
(prinl "Q1 = " (quatFmt Q1))
(prinl "Q2 = " (quatFmt Q2))
(prinl)
(prinl "norm(Q)  = " (format (quatNorm Q) *Scl))
(prinl "norm(Q1) = " (format (quatNorm Q1) *Scl))
(prinl "norm(Q2) = " (format (quatNorm Q2) *Scl))
(prinl "neg(Q)   = " (quatFmt (quatNeg Q)))
(prinl "conj(Q)  = " (quatFmt (quatConj Q)))
(prinl "Q + R    = " (quatFmt (quatAddR Q R)))
(prinl "Q1 + Q2  = " (quatFmt (quatAdd Q1 Q2)))
(prinl "Q * R    = " (quatFmt (quatMulR Q R)))
(prinl "Q1 * Q2  = " (quatFmt (quatMul Q1 Q2)))
(prinl "Q2 * Q1  = " (quatFmt (quatMul Q2 Q1)))
(prinl (if (= (quatMul Q1 Q2) (quatMul Q2 Q1)) "Equal" "Not equal"))

  

You may also check:How to resolve the algorithm Pathological floating point problems step by step in the Stata programming language
You may also check:How to resolve the algorithm Partial function application step by step in the Perl programming language
You may also check:How to resolve the algorithm Comma quibbling step by step in the Java programming language
You may also check:How to resolve the algorithm Environment variables step by step in the C programming language
You may also check:How to resolve the algorithm Formal power series step by step in the jq programming language