How to resolve the algorithm Quaternion type step by step in the Delphi programming language
How to resolve the algorithm Quaternion type step by step in the Delphi 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 Delphi programming language
Source code in the delphi programming language
unit Quaternions;
interface
type
TQuaternion = record
A, B, C, D: double;
function Init (aA, aB, aC, aD : double): TQuaternion;
function Norm : double;
function Conjugate : TQuaternion;
function ToString : string;
class operator Negative (Left : TQuaternion): TQuaternion;
class operator Positive (Left : TQuaternion): TQuaternion;
class operator Add (Left, Right : TQuaternion): TQuaternion;
class operator Add (Left : TQuaternion; Right : double): TQuaternion; overload;
class operator Add (Left : double; Right : TQuaternion): TQuaternion; overload;
class operator Subtract (Left, Right : TQuaternion): TQuaternion;
class operator Multiply (Left, Right : TQuaternion): TQuaternion;
class operator Multiply (Left : TQuaternion; Right : double): TQuaternion; overload;
class operator Multiply (Left : double; Right : TQuaternion): TQuaternion; overload;
end;
implementation
uses
SysUtils;
{ TQuaternion }
function TQuaternion.Init(aA, aB, aC, aD: double): TQuaternion;
begin
A := aA;
B := aB;
C := aC;
D := aD;
result := Self;
end;
function TQuaternion.Norm: double;
begin
result := sqrt(sqr(A) + sqr(B) + sqr(C) + sqr(D));
end;
function TQuaternion.Conjugate: TQuaternion;
begin
result.B := -B;
result.C := -C;
result.D := -D;
end;
class operator TQuaternion.Negative(Left: TQuaternion): TQuaternion;
begin
result.A := -Left.A;
result.B := -Left.B;
result.C := -Left.C;
result.D := -Left.D;
end;
class operator TQuaternion.Positive(Left: TQuaternion): TQuaternion;
begin
result := Left;
end;
class operator TQuaternion.Add(Left, Right: TQuaternion): TQuaternion;
begin
result.A := Left.A + Right.A;
result.B := Left.B + Right.B;
result.C := Left.C + Right.C;
result.D := Left.D + Right.D;
end;
class operator TQuaternion.Add(Left: TQuaternion; Right: double): TQuaternion;
begin
result.A := Left.A + Right;
result.B := Left.B;
result.C := Left.C;
result.D := Left.D;
end;
class operator TQuaternion.Add(Left: double; Right: TQuaternion): TQuaternion;
begin
result.A := Left + Right.A;
result.B := Right.B;
result.C := Right.C;
result.D := Right.D;
end;
class operator TQuaternion.Subtract(Left, Right: TQuaternion): TQuaternion;
begin
result.A := Left.A - Right.A;
result.B := Left.B - Right.B;
result.C := Left.C - Right.C;
result.D := Left.D - Right.D;
end;
class operator TQuaternion.Multiply(Left, Right: TQuaternion): TQuaternion;
begin
result.A := Left.A * Right.A - Left.B * Right.B - Left.C * Right.C - Left.D * Right.D;
result.B := Left.A * Right.B + Left.B * Right.A + Left.C * Right.D - Left.D * Right.C;
result.C := Left.A * Right.C - Left.B * Right.D + Left.C * Right.A + Left.D * Right.B;
result.D := Left.A * Right.D + Left.B * Right.C - Left.C * Right.B + Left.D * Right.A;
end;
class operator TQuaternion.Multiply(Left: double; Right: TQuaternion): TQuaternion;
begin
result.A := Left * Right.A;
result.B := Left * Right.B;
result.C := Left * Right.C;
result.D := Left * Right.D;
end;
class operator TQuaternion.Multiply(Left: TQuaternion; Right: double): TQuaternion;
begin
result.A := Left.A * Right;
result.B := Left.B * Right;
result.C := Left.C * Right;
result.D := Left.D * Right;
end;
function TQuaternion.ToString: string;
begin
result := Format('%f + %fi + %fj + %fk', [A, B, C, D]);
end;
end.
program QuaternionTest;
{$APPTYPE CONSOLE}
uses
Quaternions in 'Quaternions.pas';
var
r : double;
q, q1, q2 : TQuaternion;
begin
r := 7;
q := q .Init(1, 2, 3, 4);
q1 := q1.Init(2, 3, 4, 5);
q2 := q2.Init(3, 4, 5, 6);
writeln('q = ', q.ToString);
writeln('q1 = ', q1.ToString);
writeln('q2 = ', q2.ToString);
writeln('r = ', r);
writeln('Norm(q ) = ', q.Norm);
writeln('Norm(q1) = ', q1.Norm);
writeln('Norm(q2) = ', q2.Norm);
writeln('-q = ', (-q).ToString);
writeln('Conjugate q = ', q.Conjugate.ToString);
writeln('q1 + q2 = ', (q1 + q2).ToString);
writeln('q2 + q1 = ', (q2 + q1).ToString);
writeln('q * r = ', (q * r).ToString);
writeln('r * q = ', (r * q).ToString);
writeln('q1 * q2 = ', (q1 * q2).ToString);
writeln('q2 * q1 = ', (q2 * q1).ToString);
end.
You may also check:How to resolve the algorithm Middle three digits step by step in the Groovy programming language
You may also check:How to resolve the algorithm Assertions step by step in the Eiffel programming language
You may also check:How to resolve the algorithm ISBN13 check digit step by step in the IS-BASIC programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the Lang programming language
You may also check:How to resolve the algorithm Chaocipher step by step in the D programming language