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

Published on 12 May 2024 09:40 PM

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

Source code in the prolog programming language

% A quaternion is represented as a complex term qx/4
add(qx(R0,I0,J0,K0), qx(R1,I1,J1,K1), qx(R,I,J,K)) :-
	!, R is R0+R1, I is I0+I1, J is J0+J1, K is K0+K1.
add(qx(R0,I,J,K), F, qx(R,I,J,K)) :-
	number(F), !, R is R0 + F.
add(F, qx(R0,I,J,K), Qx) :-
	add(qx(R0,I,J,K), F, Qx).
mul(qx(R0,I0,J0,K0), qx(R1,I1,J1,K1), qx(R,I,J,K)) :- !,
	R is R0*R1 - I0*I1 - J0*J1 - K0*K1,
	I is R0*I1 + I0*R1 + J0*K1 - K0*J1,
	J is R0*J1 - I0*K1 + J0*R1 + K0*I1,
	K is R0*K1 + I0*J1 - J0*I1 + K0*R1.
mul(qx(R0,I0,J0,K0), F, qx(R,I,J,K)) :-
	number(F), !, R is R0*F, I is I0*F, J is J0*F, K is K0*F.
mul(F, qx(R0,I0,J0,K0), Qx) :-
	mul(qx(R0,I0,J0,K0),F,Qx).
abs(qx(R,I,J,K), Norm) :-
	Norm is sqrt(R*R+I*I+J*J+K*K).
negate(qx(Ri,Ii,Ji,Ki),qx(R,I,J,K)) :-
	R is -Ri, I is -Ii, J is -Ji, K is -Ki.
conjugate(qx(R,Ii,Ji,Ki),qx(R,I,J,K)) :-
	I is -Ii, J is -Ji, K is -Ki.


data(q,  qx(1,2,3,4)).
data(q1, qx(2,3,4,5)).
data(q2, qx(3,4,5,6)).
data(r, 7).

test :-	data(Name, qx(A,B,C,D)), abs(qx(A,B,C,D), Norm),
	writef('abs(%w) is %w\n', [Name, Norm]), fail.
test :- data(q, Qx), negate(Qx, Nqx),
	writef('negate(%w) is %w\n', [q, Nqx]), fail.
test :- data(q, Qx), conjugate(Qx, Nqx),
	writef('conjugate(%w) is %w\n', [q, Nqx]), fail.
test :- data(q1, Q1), data(q2, Q2), add(Q1, Q2, Qx),
	writef('q1+q2 is %w\n', [Qx]), fail.
test :- data(q1, Q1), data(q2, Q2), add(Q2, Q1, Qx),
	writef('q2+q1 is %w\n', [Qx]), fail.
test :- data(q, Qx), data(r, R), mul(Qx, R, Nqx),
	writef('q*r is %w\n', [Nqx]), fail.
test :- data(q, Qx), data(r, R), mul(R, Qx, Nqx),
	writef('r*q is %w\n', [Nqx]), fail.
test :- data(q1, Q1), data(q2, Q2), mul(Q1, Q2, Qx),
	writef('q1*q2 is %w\n', [Qx]), fail.
test :- data(q1, Q1), data(q2, Q2), mul(Q2, Q1, Qx),
	writef('q2*q1 is %w\n', [Qx]), fail.
test.


  

You may also check:How to resolve the algorithm Sleep step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Non-decimal radices/Input step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Non-decimal radices/Output step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Fibonacci n-step number sequences step by step in the Befunge programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the Fermat programming language