How to resolve the algorithm Quaternion type step by step in the zkl programming language
How to resolve the algorithm Quaternion type step by step in the zkl 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 zkl programming language
Source code in the zkl programming language
class Quat{
fcn init(real=0,i1=0,i2=0,i3=0){
var [const] vector= // Quat(r,i,j,k) or Quat( (r,i,j,k) )
(if(List.isType(real)) real else vm.arglist).apply("toFloat");
var r,i,j,k; r,i,j,k=vector; // duplicate data for ease of coding
var [const] // properties: This is one way to do it
norm2=vector.apply("pow",2).sum(0.0), // Norm squared
abs=norm2.sqrt(), // Norm
arg=(r/abs()).acos(), // Theta !!!this may be incorrect...
;
}
fcn toString { String("[",vector.concat(","),"]") }
var [const proxy] // properties that need calculation (or are recursive)
conj =fcn{ Quat(r,-i,-j,-k) }, // Conjugate
recip =fcn{ n2:=norm2; Quat(r/n2,-i/n2,-j/n2,-k/n2) },// Reciprocal
pureim =fcn{ Quat(0, i, j, k) }, // Pure imagery
versor =fcn{ self / abs; }, // Unit versor
iversor=fcn{ pureim / pureim.abs; }, // Unit versor of imagery part
;
fcn __opEQ(z) { r == z.r and i == z.i and j == z.j and k == z.k }
fcn __opNEQ(z){ (not (self==z)) }
fcn __opNegate{ Quat(-r, -i, -j, -k) }
fcn __opAdd(z){
if (Quat.isInstanceOf(z)) Quat(vector.zipWith('+,z.vector));
else Quat(r+z,i,j,k);
}
fcn __opSub(z){
if (Quat.isInstanceOf(z)) Quat(vector.zipWith('-,z.vector));
else Quat(r-z,vector.xplode(1)); // same as above
}
fcn __opMul(z){
if (Quat.isInstanceOf(z)){
Quat(r*z.r - i*z.i - j*z.j - k*z.k,
r*z.i + i*z.r + j*z.k - k*z.j,
r*z.j - i*z.k + j*z.r + k*z.i,
r*z.k + i*z.j - j*z.i + k*z.r);
}
else Quat(vector.apply('*(z)));
}
fcn __opDiv(z){
if (Quat.isInstanceOf(z)) self*z.recip;
else Quat(r/z,i/z,j/z,k/z);
}
fcn pow(r){ exp(r*iversor*arg)*abs.pow(r) } // Power function
fcn log{ iversor*(r / abs).acos() + abs.log() }
fcn exp{ // e^q
inorm:=pureim.abs;
(iversor*inorm.sin() + inorm.cos()) * r.exp();
}
}
// Demo code
r:=7;
q:=Quat(2,3,4,5); q1:=Quat(2,3,4,5); q2:=Quat(3,4,5,6);
println("1. norm: q.abs: ", q.abs);
println("2. -q: ", -q);
println("3. conjugate: q.conj: ", q.conj);
println("4. Quat(r) + q: ", Quat(r) + q);
println(" q + r: ", q + r);
println("5. q1 + q2: ", q1 + q2);
println("6. Quat(r) * q: ", Quat(r) * q);
println(" q * r: ", q * r);
println("7. q1 * q2: ", q1 * q2);
println(" q2 * q1: ", q2 * q1);
println("8. q1 * q2 == q2 * q1 ? ", q1 * q2 == q2 * q1);
i:=Quat(0,1); j:=Quat(0,0,1); k:=Quat(0,0,0,1);
println("9.1 i * i: ", i * i);
println(" J * j: ", j * j);
println(" k * k: ", k * k);
println(" i * j * k: ", i * j * k);
println("9.2 q1 / q2: ", q1 / q2);
println("9.3 q1 / q2 * q2: ", q1 / q2 * q2);
println(" q2 * q1 / q2: ", q2 * q1 / q2);
println("9.4 (i * pi).exp(): ", (i * (0.0).pi).exp());
println(" exp(j * pi): ", (j * (0.0).pi).exp());
println(" exp(k * pi): ", (k * (0.0).pi).exp());
println(" q.exp(): ", q.exp());
println(" q.log(): ", q.log());
println(" q.log().exp(): ", q.log().exp());
println(" q.exp().log(): ", q.exp().log());
s:=q.exp().log();
println("9.5 let s=q.exp().log(): ", s);
println(" s.exp(): ", s.exp());
println(" s.log(): ", s.log());
println(" s.log().exp(): ", s.log().exp());
println(" s.exp().log(): ", s.exp().log());
You may also check:How to resolve the algorithm Lah numbers step by step in the Wren programming language
You may also check:How to resolve the algorithm Singly-linked list/Element definition step by step in the Rust programming language
You may also check:How to resolve the algorithm Cyclops numbers step by step in the Wren programming language
You may also check:How to resolve the algorithm Jump anywhere step by step in the Ada programming language
You may also check:How to resolve the algorithm List comprehensions step by step in the Scheme programming language