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

Published on 12 May 2024 09:40 PM

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

Source code in the freebasic programming language

Dim Shared As Integer q(3)  = {1, 2, 3, 4}
Dim Shared As Integer q1(3) = {2, 3, 4, 5}
Dim Shared As Integer q2(3) = {3, 4, 5, 6}
Dim Shared As Integer i, r = 7, t(3)

Function q_norm(q() As Integer) As Double
    ' medida o valor absoluto de un cuaternión
    Dim As Double a = 0
    For i = 0 To 3 
        a += q(i)^2
    Next i
    Return Sqr(a)
End Function

Sub q_neg(q() As Integer) 
    For i = 0 To 3 
        q(i) *= -1
    Next i
End Sub

Sub q_conj(q() As Integer)
    ' conjugado de un cuaternión
    For i = 1 To 3 
        q(i) *= -1
    Next i
End Sub

Sub q_addreal(q() As Integer, r As Integer)
    q(0) += r 
End Sub

Sub q_add(q() As Integer, r() As Integer)
    ' adición entre cuaternios
    For i = 0 To 3 
        q(i) += r(i)
    Next i
End Sub

Sub q_mulreal(q() As Integer, r As Integer)
    For i = 0 To 3 
        q(i) *= r
    Next i
End Sub

Sub q_mul(q() As Integer, r() As Integer)
    ' producto entre cuaternios
    Dim As Integer m(3)
    m(0) = q(0)*r(0) - q(1)*r(1) - q(2)*r(2) - q(3)*r(3) 
    m(1) = q(0)*r(1) + q(1)*r(0) + q(2)*r(3) - q(3)*r(2) 
    m(2) = q(0)*r(2) - q(1)*r(3) + q(2)*r(0) + q(3)*r(1) 
    m(3) = q(0)*r(3) + q(1)*r(2) - q(2)*r(1) + q(3)*r(0) 
    For i = 0 To 3 : q(i) = m(i) : Next i
End Sub

Function q_show(q() As Integer) As String
    Dim As String a = "("
    For i = 0 To 3 
        a += Str(q(i)) + ", " 
    Next i
    Return Mid(a,1,Len(a)-2) + ")"
End Function

'--- Programa Principal ---
Print " q = "; q_show(q())
Print "q1 = "; q_show(q1())
Print "q2 = "; q_show(q2())
Print " r = "; r
Print "norm(q) ="; q_norm(q())
For i = 0 To 3 : t(i) = q(i) : Next i : q_neg(t())  : Print " neg(q) = "; q_show(t())
For i = 0 To 3 : t(i) = q(i) : Next i : q_conj(t()) : Print "conj(q) = "; q_show(t())
For i = 0 To 3 : t(i) = q(i) : Next i : q_addreal(t(),r) : Print " r + q  = "; q_show(t())
For i = 0 To 3 : t(i) = q1(i) : Next i : q_add(t(),q2()) : Print "q1 + q2 = "; q_show(t())
For i = 0 To 3 : t(i) = q2(i) : Next i : q_add(t(),q1()) : Print "q2 + q1 = "; q_show(t())
For i = 0 To 3 : t(i) = q(i) : Next i : q_mulreal(t(),r) : Print " r * q  = "; q_show(t())
For i = 0 To 3 : t(i) = q1(i) : Next i : q_mul(t(),q2()) : Print "q1 * q2 = "; q_show(t())
For i = 0 To 3 : t(i) = q2(i) : Next i : q_mul(t(),q1()) : Print "q2 * q1 = "; q_show(t())
End

  

You may also check:How to resolve the algorithm Kernighans large earthquake problem step by step in the Amazing Hopper programming language
You may also check:How to resolve the algorithm Symmetric difference step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Monte Carlo methods step by step in the Rust programming language
You may also check:How to resolve the algorithm Truncatable primes step by step in the RPL programming language
You may also check:How to resolve the algorithm Array length step by step in the NewLISP programming language