Quaternion type: Difference between revisions

Added Easylang
(useg a bigger font as CODE doesn't show subscripts very clearly, use a true "not equal" symbol instead of !=, elided some pronouns, split some long sentences (which contain WHERE), added other whitespace and highlighting to the task's preamble.)
(Added Easylang)
 
(84 intermediate revisions by 40 users not shown)
Line 4:
 
A complex number has a real and complex part, &nbsp; sometimes written as &nbsp; <big> <code> a + bi, </code> </big>
<br>where &nbsp; <big> <code> a </code> </big> &nbsp; and &nbsp; <big> <code> b </code> </big> &nbsp; stand for real numbers, and &nbsp; <big> <code> i </code> </big> &nbsp; stands for &nbsp;the <big>square <math>root of minus \sqrt{-1}</math>. </big>
 
An example of a complex number might be &nbsp; <big> <code> -3 + 2i, </code> </big> &nbsp;
Line 13:
A quaternion might be written as &nbsp; <big> <code> a + bi + cj + dk. </code> </big>
 
In thisthe quaternion numbering system:
:::* &nbsp; <big> <code> i∙i = j∙j = k∙k = i∙j∙k = -1, </code> </big> &nbsp; &nbsp; &nbsp; or more simply,
:::* &nbsp; <big> <code> ii &nbsp;= jj &nbsp;= kk &nbsp;= ijk &nbsp; = -1. </code> </big>
Line 36:
 
Create functions &nbsp; (or classes) &nbsp; to perform simple maths with quaternions including computing:
# The norm of a quaternion: <br> <big> <code> <math> = \sqrt{ a^2 + b^2 + c^2 + d^2 } </math> </code> </big>
# The negative of a quaternion: <br> <big> <code> = (-a, -b, -c, -d)</code> </big>
# The conjugate of a quaternion: <br> <big> <code> = ( a, -b, -c, -d)</code> </big>
# Addition of a real number &nbsp; <big> <code> r </code> </big> &nbsp; and &nbsp; <big> <code> a </code> </big> &nbsp; quaternion &nbsp; <big> <code> q: </code> </big> <br> <big> <code> r + q = q + r = (a+r, b, c, d) </code> </big>
# Addition of two quaternions: <br> <big> <code> q<sub>1</sub> + q<sub>2</sub> = (a<sub>1</sub>+a<sub>2</sub>, b<sub>1</sub>+b<sub>2</sub>, c<sub>1</sub>+c<sub>2</sub>, d<sub>1</sub>+d<sub>2</sub>) </code> </big>
# Multiplication of a real number and a quaternion: <br> <big> <code> qr = rq = (ar, br, cr, dr) </code> </big>
Line 53:
* &nbsp; [http://www.maths.tcd.ie/pub/HistMath/People/Hamilton/QLetter/QLetter.pdf On Quaternions]; &nbsp; or on a new System of Imaginaries in Algebra. &nbsp; By Sir William Rowan Hamilton LL.D, P.R.I.A., F.R.A.S., Hon. M. R. Soc. Ed. and Dub., Hon. or Corr. M. of the Royal or Imperial Academies of St. Petersburgh, Berlin, Turin and Paris, Member of the American Academy of Arts and Sciences, and of other Scientific Societies at Home and Abroad, Andrews' Prof. of Astronomy in the University of Dublin, and Royal Astronomer of Ireland.
<br><br>
 
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<syntaxhighlight lang="action!">INCLUDE "H6:REALMATH.ACT"
 
DEFINE A_="+0"
DEFINE B_="+6"
DEFINE C_="+12"
DEFINE D_="+18"
 
TYPE Quaternion=[CARD a1,a2,a3,b1,b2,b3,c1,c2,c3,d1,d2,d3]
REAL neg
 
PROC Init()
ValR("-1",neg)
RETURN
 
BYTE FUNC Positive(REAL POINTER x)
BYTE ARRAY tmp
 
tmp=x
IF (tmp(0)&$80)=$00 THEN
RETURN (1)
FI
RETURN (0)
 
PROC PrintQuat(Quaternion POINTER q)
PrintR(q A_)
IF Positive(q B_) THEN Put('+) FI
PrintR(q B_) Put('i)
IF Positive(q C_) THEN Put('+) FI
PrintR(q C_) Put('j)
IF Positive(q D_) THEN Put('+) FI
PrintR(q D_) Put('k)
RETURN
 
PROC PrintQuatE(Quaternion POINTER q)
PrintQuat(q) PutE()
RETURN
 
PROC QuatIntInit(Quaternion POINTER q INT ia,ib,ic,id)
IntToReal(ia,q A_)
IntToReal(ib,q B_)
IntToReal(ic,q C_)
IntToReal(id,q D_)
RETURN
 
PROC Sqr(REAL POINTER a,b)
RealMult(a,a,b)
RETURN
 
PROC QuatNorm(Quaternion POINTER q REAL POINTER res)
REAL r1,r2,r3
 
Sqr(q A_,r1) ;r1=q.a^2
Sqr(q B_,r2) ;r2=q.b^2
RealAdd(r1,r2,r3) ;r3=q.a^2+q.b^2
Sqr(q C_,r1) ;r1=q.c^2
RealAdd(r3,r1,r2) ;r2=q.a^2+q.b^2+q.c^2
Sqr(q D_,r1) ;r1=q.d^2
RealAdd(r2,r1,r3) ;r3=q.a^2+q.b^2+q.c^2+q.d^2
Sqrt(r3,res) ;res=sqrt(q.a^2+q.b^2+q.c^2+q.d^2)
RETURN
 
PROC QuatNegative(Quaternion POINTER q,res)
RealMult(q A_,neg,res A_) ;res.a=-q.a
RealMult(q B_,neg,res B_) ;res.b=-q.b
RealMult(q C_,neg,res C_) ;res.c=-q.c
RealMult(q D_,neg,res D_) ;res.d=-q.d
RETURN
 
PROC QuatConjugate(Quaternion POINTER q,res)
RealAssign(q A_,res A_) ;res.a=q.a
RealMult(q B_,neg,res B_) ;res.b=-q.b
RealMult(q C_,neg,res C_) ;res.c=-q.c
RealMult(q D_,neg,res D_) ;res.d=-q.d
RETURN
 
PROC QuatAddReal(Quaternion POINTER q REAL POINTER r
Quaternion POINTER res)
RealAdd(q A_,r,res A_) ;res.a=q.a+r
RealAssign(q B_,res B_) ;res.b=q.b
RealAssign(q C_,res C_) ;res.c=q.c
RealAssign(q D_,res D_) ;res.d=q.d
RETURN
 
PROC QuatAdd(Quaternion POINTER q1,q2,res)
RealAdd(q1 A_,q2 A_,res A_) ;res.a=q1.a+q2.a
RealAdd(q1 B_,q2 B_,res B_) ;res.b=q1.b+q2.b
RealAdd(q1 C_,q2 C_,res C_) ;res.c=q1.c+q2.c
RealAdd(q1 D_,q2 D_,res D_) ;res.d=q1.d+q2.d
RETURN
 
PROC QuatMultReal(Quaternion POINTER q REAL POINTER r
Quaternion POINTER res)
RealMult(q A_,r,res A_) ;res.a=q.a*r
RealMult(q B_,r,res B_) ;res.b=q.b*r
RealMult(q C_,r,res C_) ;res.c=q.c*r
RealMult(q D_,r,res D_) ;res.d=q.d*r
RETURN
 
PROC QuatMult(Quaternion POINTER q1,q2,res)
REAL r1,r2
 
RealMult(q1 A_,q2 A_,r1) ;r1=q1.a*q2.a
RealMult(q1 B_,q2 B_,r2) ;r2=q1.b*q2.b
RealSub(r1,r2,r3) ;r3=q1.a*q2.a-q1.b*q2.b
RealMult(q1 C_,q2 C_,r1) ;r1=q1.c*q2.c
RealSub(r3,r1,r2) ;r2=q1.a*q2.a-q1.b*q2.b-q1.c*q2.c
RealMult(q1 D_,q2 D_,r1) ;r1=q1.d*q2.d
RealSub(r2,r1,res A_) ;res.a=q1.a*q2.a-q1.b*q2.b-q1.c*q2.c-q1.d*q2.d
 
RealMult(q1 A_,q2 B_,r1) ;r1=q1.a*q2.b
RealMult(q1 B_,q2 A_,r2) ;r2=q1.b*q2.a
RealAdd(r1,r2,r3) ;r3=q1.a*q2.b+q1.b*q2.a
RealMult(q1 C_,q2 D_,r1) ;r1=q1.c*q2.d
RealAdd(r3,r1,r2) ;r2=q1.a*q2.b+q1.b*q2.a+q1.c*q2.d
RealMult(q1 D_,q2 C_,r1) ;r1=q1.d*q2.c
RealSub(r2,r1,res B_) ;res.b=q1.a*q2.b+q1.b*q2.a+q1.c*q2.d-q1.d*q2.c
 
RealMult(q1 A_,q2 C_,r1) ;r1=q1.a*q2.c
RealMult(q1 B_,q2 D_,r2) ;r2=q1.b*q2.d
RealSub(r1,r2,r3) ;r3=q1.a*q2.c-q1.b*q2.d
RealMult(q1 C_,q2 A_,r1) ;r1=q1.c*q2.a
RealAdd(r3,r1,r2) ;r2=q1.a*q2.c-q1.b*q2.d+q1.c*q2.a
RealMult(q1 D_,q2 B_,r1) ;r1=q1.d*q2.b
RealAdd(r2,r1,res C_) ;res.c=q1.a*q2.c-q1.b*q2.d+q1.c*q2.a+q1.d*q2.b
 
RealMult(q1 A_,q2 D_,r1) ;r1=q1.a*q2.d
RealMult(q1 B_,q2 C_,r2) ;r2=q1.b*q2.c
RealAdd(r1,r2,r3) ;r3=q1.a*q2.d+q1.b*q2.c
RealMult(q1 C_,q2 B_,r1) ;r1=q1.c*q2.b
RealSub(r3,r1,r2) ;r2=q1.a*q2.d+q1.b*q2.c-q1.c*q2.b
RealMult(q1 D_,q2 A_,r1) ;r1=q1.d*q2.a
RealAdd(r2,r1,res D_) ;res.d=q1.a*q2.d+q1.b*q2.c-q1.c*q2.b+q1.d*q2.a
RETURN
 
PROC Main()
Quaternion q,q1,q2,q3
REAL r,r2
 
Put(125) PutE() ;clear the screen
MathInit()
Init()
 
QuatIntInit(q,1,2,3,4)
QuatIntInit(q1,2,3,4,5)
QuatIntInit(q2,3,4,5,6)
IntToReal(7,r)
 
Print(" q = ") PrintQuatE(q)
Print("q1 = ") PrintQuatE(q1)
Print("q2 = ") PrintQuatE(q2)
Print(" r = ") PrintRE(r) PutE()
 
QuatNorm(q,r2) Print(" Norm(q) = ") PrintRE(r2)
QuatNorm(q1,r2) Print("Norm(q1) = ") PrintRE(r2)
QuatNorm(q2,r2) Print("Norm(q2) = ") PrintRE(r2)
QuatNegative(q,q3) Print(" -q = ") PrintQuatE(q3)
QuatConjugate(q,q3) Print(" Conj(q) = ") PrintQuatE(q3)
QuatAddReal(q,r,q3) Print(" q+r = ") PrintQuatE(q3)
QuatAdd(q1,q2,q3) Print(" q1+q2 = ") PrintQuatE(q3)
QuatAdd(q2,q1,q3) Print(" q2+q1 = ") PrintQuatE(q3)
QuatMultReal(q,r,q3) Print(" q*r = ") PrintQuatE(q3)
QuatMult(q1,q2,q3) Print(" q1*q2 = ") PrintQuatE(q3)
QuatMult(q2,q1,q3) Print(" q2*q1 = ") PrintQuatE(q3)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Quaternion_type.png Screenshot from Atari 8-bit computer]
<pre>
q = 1+2i+3j+4k
q1 = 2+3i+4j+5k
q2 = 3+4i+5j+6k
r = 7
 
Norm(q) = 5.47722543
Norm(q1) = 7.34846906
Norm(q2) = 9.27361833
-q = -1-2i-3j-4k
Conj(q) = 1-2i-3j-4k
q+r = 8+2i+3j+4k
q1+q2 = 5+7i+9j+11k
q2+q1 = 5+7i+9j+11k
q*r = 7+14i+21j+28k
q1*q2 = -56+16i+24j+26k
q2*q1 = -56+18i+20j+28k
</pre>
 
=={{header|Ada}}==
The package specification (works with any floating-point type):
<langsyntaxhighlight Adalang="ada">generic
type Real is digits <>;
package Quaternions is
Line 71 ⟶ 259:
function "*" (Left, Right : Quaternion) return Quaternion;
function Image (Left : Quaternion) return String;
end Quaternions;</langsyntaxhighlight>
The package implementation:
<langsyntaxhighlight Adalang="ada">with Ada.Numerics.Generic_Elementary_Functions;
package body Quaternions is
package Elementary_Functions is
Line 131 ⟶ 319:
Real'Image (Left.D) & "k";
end Image;
end Quaternions;</langsyntaxhighlight>
Test program:
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Quaternions;
procedure Test_Quaternion is
Line 158 ⟶ 346:
Put_Line ("q1 * q2 = " & Image (q1 * q2));
Put_Line ("q2 * q1 = " & Image (q2 * q1));
end Test_Quaternion;</langsyntaxhighlight>
{{out}}
<pre>
Line 184 ⟶ 372:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.6 algol68g-2.6].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
'''File: prelude/Quaternion.a68'''<langsyntaxhighlight lang="algol68"># -*- coding: utf-8 -*- #
 
COMMENT REQUIRES:
Line 442 ⟶ 630:
PROC quat exp = (QUAT q)QUAT: (exp OF class quat)(LOC QUAT := q);
 
SKIP # missing: quat arc{sin, cos, tan}h, log, exp, ln etc END #</langsyntaxhighlight>'''File: test/Quaternion.a68'''<langsyntaxhighlight lang="algol68">#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #
 
Line 496 ⟶ 684:
));
print((REPR(-q1*q2), ", ", REPR(-q2*q1), new line))
)</langsyntaxhighlight>
{{out}}
<pre>
Line 530 ⟶ 718:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin
% Quaternion record type %
record Quaternion ( real a, b, c, d );
Line 631 ⟶ 819:
 
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 651 ⟶ 839:
q2q1:(-56.0, 18.0, 20.0, 28.0)
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">qnorm: $ => [sqrt fold & [x y] -> x + y*y]
 
qneg: $ => [map & => neg]
 
qconj: $[q] [@[q\0] ++ qneg drop q]
 
qaddr: function [q r][
[a b c d]: q
@[a+r b c d]
]
 
qadd: $ => [map couple & & => sum]
 
qmulr: $[q r] [map q'x -> x*r]
 
qmul: function [q1 q2][
[a1 b1 c1 d1]: q1
[a2 b2 c2 d2]: q2
@[
(((a1*a2) - b1*b2) - c1*c2) - d1*d2,
(((a1*b2) + b1*a2) + c1*d2) - d1*c2,
(((a1*c2) - b1*d2) + c1*a2) + d1*b2,
(((a1*d2) + b1*c2) - c1*b2) + d1*a2
]
]
 
; --- test quaternions ---
q: [1 2 3 4]
q1: [2 3 4 5]
q2: [3 4 5 6]
r: 7
 
print ['qnorm q '= qnorm q]
print ['qneg q '= qneg q]
print ['qconj q '= qconj q]
print ['qaddr q r '= qaddr q r]
print ['qmulr q r '= qmulr q r]
print ['qadd q1 q2 '= qadd q1 q2]
print ['qmul q1 q2 '= qmul q1 q2]
print ['qmul q2 q1 '= qmul q2 q1]</syntaxhighlight>
 
{{out}}
 
<pre>qnorm [1 2 3 4] = 5.477225575051661
qneg [1 2 3 4] = [-1 -2 -3 -4]
qconj [1 2 3 4] = [1 -2 -3 -4]
qaddr [1 2 3 4] 7 = [8 2 3 4]
qmulr [1 2 3 4] 7 = [7 14 21 28]
qadd [2 3 4 5] [3 4 5 6] = [5 7 9 11]
qmul [2 3 4 5] [3 4 5 6] = [-56 16 24 26]
qmul [3 4 5 6] [2 3 4 5] = [-56 18 20 28]</pre>
 
=={{header|ATS}}==
{{libheader|ats2-xprelude}}
 
<syntaxhighlight lang="ATS">
//--------------------------------------------------------------------
 
#include "share/atspre_staload.hats"
 
//--------------------------------------------------------------------
 
(* Here is one way to get a sqrt function without going beyond the ATS
prelude. The prelude (at the time of this writing) contains some
templates for which implementations were never added. Here I add an
implementation.
 
The ats2-xprelude package at
https://sourceforge.net/p/chemoelectric/ats2-xprelude contains a
much more extensive and natural interface to the C math library. *)
 
%{^
#include <math.h>
%}
 
implement (* "Generic" square root. *)
gsqrt_val<double> x =
(* Call "sqrt" from the C math library. *)
$extfcall (double, "sqrt", x)
 
//--------------------------------------------------------------------
 
abst@ype quaternion (tk : tkind) =
(* The following determines the SIZE of a quaternion, but not its
actual representation: *)
@(g0float tk, g0float tk, g0float tk, g0float tk)
 
extern fn {tk : tkind} quaternion_make :
(g0float tk, g0float tk, g0float tk, g0float tk) -<> quaternion tk
 
extern fn {tk : tkind} fprint_quaternion :
(FILEref, quaternion tk) -> void
extern fn {tk : tkind} print_quaternion :
quaternion tk -> void
 
extern fn {tk : tkind} quaternion_norm_squared :
quaternion tk -<> g0float tk
extern fn {tk : tkind} quaternion_norm :
quaternion tk -< !exn > g0float tk
 
extern fn {tk : tkind} quaternion_neg :
quaternion tk -<> quaternion tk
extern fn {tk : tkind} quaternion_conj :
quaternion tk -<> quaternion tk
 
extern fn {tk : tkind} add_quaternion_g0float :
(quaternion tk, g0float tk) -<> quaternion tk
extern fn {tk : tkind} add_g0float_quaternion :
(g0float tk, quaternion tk) -<> quaternion tk
extern fn {tk : tkind} add_quaternion_quaternion :
(quaternion tk, quaternion tk) -<> quaternion tk
 
extern fn {tk : tkind} mul_quaternion_g0float :
(quaternion tk, g0float tk) -<> quaternion tk
extern fn {tk : tkind} mul_g0float_quaternion :
(g0float tk, quaternion tk) -<> quaternion tk
extern fn {tk : tkind} mul_quaternion_quaternion :
(quaternion tk, quaternion tk) -<> quaternion tk
 
extern fn {tk : tkind} quaternion_eq :
(quaternion tk, quaternion tk) -<> bool
 
overload fprint with fprint_quaternion
overload print with print_quaternion
 
overload norm_squared with quaternion_norm_squared
overload norm with quaternion_norm
 
overload ~ with quaternion_neg
overload conj with quaternion_conj
 
overload + with add_quaternion_g0float
overload + with add_g0float_quaternion
overload + with add_quaternion_quaternion
 
overload * with mul_quaternion_g0float
overload * with mul_g0float_quaternion
overload * with mul_quaternion_quaternion
 
overload = with quaternion_eq
 
//--------------------------------------------------------------------
 
local
 
(* Now we decide the REPRESENTATION of a quaternion. A quaternion is
represented as an unboxed 4-tuple of "real" numbers of any one
particular typekind. *)
typedef _quaternion (tk : tkind) =
@(g0float tk, g0float tk, g0float tk, g0float tk)
 
assume quaternion tk = _quaternion tk
 
in (* local *)
 
implement {tk}
quaternion_make (a, b, c, d) =
@(a, b, c, d)
 
implement {tk}
fprint_quaternion (outf, q) =
let
typedef t = g0float tk
val @(a, b, c, d) = q
in
fprint_val<t> (outf, a);
if g0i2f 0 <= b then fprint_val<string> (outf, "+");
fprint_val<t> (outf, b);
fprint_val<string> (outf, "i");
if g0i2f 0 <= c then fprint_val<string> (outf, "+");
fprint_val<t> (outf, c);
fprint_val<string> (outf, "j");
if g0i2f 0 <= d then fprint_val<string> (outf, "+");
fprint_val<t> (outf, d);
fprint_val<string> (outf, "k");
end
 
implement {tk}
print_quaternion q =
fprint_quaternion (stdout_ref, q)
 
implement {tk}
quaternion_norm_squared q =
let
val @(a, b, c, d) = q
in
(a * a) + (b * b) + (c * c) + (d * d)
end
 
implement {tk}
quaternion_norm q =
gsqrt_val<g0float tk> (quaternion_norm_squared q)
 
implement {tk}
quaternion_neg q =
let
val @(a, b, c, d) = q
in
@(~a, ~b, ~c, ~d)
end
 
implement {tk}
quaternion_conj q =
let
val @(a, b, c, d) = q
in
@(a, ~b, ~c, ~d)
end
 
implement {tk}
add_quaternion_g0float (q, r) =
let
val @(a, b, c, d) = q
in
@(a + r, b, c, d)
end
 
implement {tk}
add_g0float_quaternion (r, q) =
let
val @(a, b, c, d) = q
in
@(r + a, b, c, d)
end
 
implement {tk}
add_quaternion_quaternion (q1, q2) =
let
val @(a1, b1, c1, d1) = q1
and @(a2, b2, c2, d2) = q2
in
@(a1 + a2, b1 + b2, c1 + c2, d1 + d2)
end
 
implement {tk}
mul_quaternion_g0float (q, r) =
let
val @(a, b, c, d) = q
in
@(a * r, b * r, c * r, d * r)
end
 
implement {tk}
mul_g0float_quaternion (r, q) =
let
val @(a, b, c, d) = q
in
@(r * a, r * b, r * c, r * d)
end
 
implement {tk}
mul_quaternion_quaternion (q1, q2) =
let
val @(a1, b1, c1, d1) = q1
and @(a2, b2, c2, d2) = q2
in
@((a1 * a2) - (b1 * b2) - (c1 * c2) - (d1 * d2),
(a1 * b2) + (b1 * a2) + (c1 * d2) - (d1 * c2),
(a1 * c2) - (b1 * d2) + (c1 * a2) + (d1 * b2),
(a1 * d2) + (b1 * c2) - (c1 * b2) + (d1 * a2))
end
 
implement {tk}
quaternion_eq (q1, q2) =
let
val @(a1, b1, c1, d1) = q1
and @(a2, b2, c2, d2) = q2
in
(a1 = a2) * (b1 = b2) * (c1 = c2) * (d1 = d2)
end
 
end (* local *)
 
//--------------------------------------------------------------------
 
val q = quaternion_make (1.0, 2.0, 3.0, 4.0)
and q1 = quaternion_make (2.0, 3.0, 4.0, 5.0)
and q2 = quaternion_make (3.0, 4.0, 5.0, 6.0)
and r = 7.0
 
implement
main0 () =
let
(* Let us print double precision numbers in a format more readable
than is the prelude's default. *)
implement
fprint_val<double> (outf, x) =
let
typedef f = $extype"FILE *"
val _ = $extfcall (int, "fprintf", $UNSAFE.cast{f} outf,
"%g", x)
in
end
in
println! ("q = ", q);
println! ("q1 = ", q1);
println! ("q2 = ", q2);
println! ();
println! ("||q|| = ", norm q);
println! ("||q1|| = ", norm q1);
println! ("||q2|| = ", norm q2);
println! ();
println! ("-q = ", ~q);
println! ("-q1 = ", ~q1);
println! ("-q2 = ", ~q2);
println! ();
println! ("conj q = ", conj q);
println! ("conj q1 = ", conj q1);
println! ("conj q2 = ", conj q2);
println! ();
println! ("q + r = ", q + r);
println! ("r + q = ", r + q);
println! ("q1 + q2 = ", q1 + q2);
println! ();
println! ("q * r = ", q * r);
println! ("r * q = ", r * q);
println! ("q1 * q2 = ", q1 * q2);
println! ("q2 * q1 = ", q2 * q1);
println! ("((q1 * q2) = (q2 * q1)) is ", (q1 * q2) = (q2 * q1))
end
 
//--------------------------------------------------------------------
</syntaxhighlight>
 
{{out}}
<pre>$ patscc -std=gnu2x -O2 quaternions_task.dats -lm && ./a.out
q = 1+2i+3j+4k
q1 = 2+3i+4j+5k
q2 = 3+4i+5j+6k
 
||q|| = 5.477226
||q1|| = 7.348469
||q2|| = 9.273618
 
-q = -1-2i-3j-4k
-q1 = -2-3i-4j-5k
-q2 = -3-4i-5j-6k
 
conj q = 1-2i-3j-4k
conj q1 = 2-3i-4j-5k
conj q2 = 3-4i-5j-6k
 
q + r = 8+2i+3j+4k
r + q = 8+2i+3j+4k
q1 + q2 = 5+7i+9j+11k
 
q * r = 7+14i+21j+28k
r * q = 7+14i+21j+28k
q1 * q2 = -56+16i+24j+26k
q2 * q1 = -56+18i+20j+28k
((q1 * q2) = (q2 * q1)) is false</pre>
 
=={{header|AutoHotkey}}==
{{works with|AutoHotkey_L}} (AutoHotkey1.1+)
<langsyntaxhighlight AutoHotkeylang="autohotkey">q := [1, 2, 3, 4]
q1 := [2, 3, 4, 5]
q2 := [3, 4, 5, 6]
Line 725 ⟶ 1,267:
b .= v (A_Index = q.MaxIndex() ? ")" : ", ")
return b
}</langsyntaxhighlight>
{{out}}
<pre>q = (1, 2, 3, 4)
Line 743 ⟶ 1,285:
=={{header|Axiom}}==
Axiom has built-in support for quaternions.
<langsyntaxhighlight Axiomlang="axiom">qi := quatern$Quaternion(Integer);
 
Type: ((Integer,Integer,Integer,Integer) -> Quaternion(Integer))
Line 790 ⟶ 1,332:
 
(13) true
Type: Boolean</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{works with|BASIC256|2.0.0.11}}
<syntaxhighlight lang="basic256">
dim q(4)
dim q1(4)
dim q2(4)
q[0] = 1: q[1] = 2: q[2] = 3: q[3] = 4
q1[0] = 2: q1[1] = 3: q1[2] = 4: q1[3] = 5
q2[0] = 3: q2[1] = 4: q2[2] = 5: q2[3] = 6
r = 7
 
function printq(q)
return "("+q[0]+", "+q[1]+", "+q[2]+", "+q[3]+")"
end function
 
function q_equal(q1, q2)
return q1[0]=q2[0] and q1[1]=q2[1] and q1[2]=q2[2] and q1[3]=q2[3]
end function
 
function q_norm(q)
return sqr(q[0]*q[0]+q[1]*q[1]+q[2]*q[2]+q[3]*q[3])
end function
 
function q_neg(q)
dim result[4]
result[0] = -q[0]
result[1] = -q[1]
result[2] = -q[2]
result[3] = -q[3]
return result
end function
 
function q_conj(q)
dim result[4]
result[0] = q[0]
result[1] = -q[1]
result[2] = -q[2]
result[3] = -q[3]
return result
end function
 
function q_addreal(q, r)
dim result[4]
result[0] = q[0]+r
result[1] = q[1]
result[2] = q[2]
result[3] = q[3]
return result
end function
 
function q_add(q1, q2)
dim result[4]
result[0] = q1[0]+q2[0]
result[1] = q1[1]+q2[1]
result[2] = q1[2]+q2[2]
result[3] = q1[3]+q2[3]
return result
end function
 
function q_mulreal(q, r)
dim result[4]
result[0] = q[0]*r
result[1] = q[1]*r
result[2] = q[2]*r
result[3] = q[3]*r
return result
end function
 
function q_mul(q1, q2)
dim result[4]
result[0] = q1[0]*q2[0]-q1[1]*q2[1]-q1[2]*q2[2]-q1[3]*q2[3]
result[1] = q1[0]*q2[1]+q1[1]*q2[0]+q1[2]*q2[3]-q1[3]*q2[2]
result[2] = q1[0]*q2[2]-q1[1]*q2[3]+q1[2]*q2[0]+q1[3]*q2[1]
result[3] = q1[0]*q2[3]+q1[1]*q2[2]-q1[2]*q2[1]+q1[3]*q2[0]
return result
end function
 
print "q = ";printq(q)
print "q1 = ";printq(q1)
print "q2 = ";printq(q2)
print "r = "; r
print "norm(q) = "; q_norm(q)
print "neg(q) = ";printq(q_neg(q))
print "conjugate(q) = ";printq(q_conj(q))
print "q+r = ";printq(q_addreal(q,r))
print "q1+q2 = ";printq(q_add(q1,q2))
print "qr = ";printq(q_mulreal(q,r))
print "q1q2 = ";printq(q_mul(q1,q2))
print "q2q1 = ";printq(q_mul(q2,q1))
</syntaxhighlight>
{{out}}
<pre>
q = (1, 2, 3, 4)
q1 = (2, 3, 4, 5)
q2 = (3, 4, 5, 6)
r = 7
norm(q) = 5.47722557505
neg(q) = (-1, -2, -3, -4)
conjugate(q) = (1, -2, -3, -4)
q+r = (8, 2, 3, 4)
q1+q2 = (5, 7, 9, 11)
qr = (7, 14, 21, 28)
q1q2 = (-56, 16, 24, 26)
q2q1 = (-56, 18, 20, 28)
</pre>
 
==={{header|BBC BASIC}}===
Although BBC BASIC doesn't have native support for quaternions its array arithmetic provides all of the required operations either directly or very straightforwardly.
<langsyntaxhighlight lang="bbcbasic"> DIM q(3), q1(3), q2(3), t(3)
q() = 1, 2, 3, 4
q1() = 2, 3, 4, 5
Line 834 ⟶ 1,484:
DEF FNq_show(q()) : LOCAL i%, a$ : a$ = "("
FOR i% = 0 TO 3 : a$ += STR$(q(i%)) + ", " : NEXT
= LEFT$(LEFT$(a$)) + ")"</langsyntaxhighlight>
{{out}}
<pre>
Line 853 ⟶ 1,503:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
Line 983 ⟶ 1,633:
printf("(%lf, %lf, %lf, %lf)\n",
q->q[0], q->q[1], q->q[2], q->q[3]);
}</langsyntaxhighlight>
 
<langsyntaxhighlight lang="c">int main()
{
size_t i;
Line 1,042 ⟶ 1,692:
free(q[0]); free(q[1]); free(q[2]); free(r);
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
=={{header|C sharp}}==
<syntaxhighlight lang="csharp">using System;
 
struct Quaternion : IEquatable<Quaternion>
{
public readonly double A, B, C, D;
 
public Quaternion(double a, double b, double c, double d)
{
this.A = a;
this.B = b;
this.C = c;
this.D = d;
}
 
public double Norm()
{
return Math.Sqrt(A * A + B * B + C * C + D * D);
}
 
public static Quaternion operator -(Quaternion q)
{
return new Quaternion(-q.A, -q.B, -q.C, -q.D);
}
 
public Quaternion Conjugate()
{
return new Quaternion(A, -B, -C, -D);
}
 
// implicit conversion takes care of real*quaternion and real+quaternion
public static implicit operator Quaternion(double d)
{
return new Quaternion(d, 0, 0, 0);
}
 
public static Quaternion operator +(Quaternion q1, Quaternion q2)
{
return new Quaternion(q1.A + q2.A, q1.B + q2.B, q1.C + q2.C, q1.D + q2.D);
}
 
public static Quaternion operator *(Quaternion q1, Quaternion q2)
{
return new Quaternion(
q1.A * q2.A - q1.B * q2.B - q1.C * q2.C - q1.D * q2.D,
q1.A * q2.B + q1.B * q2.A + q1.C * q2.D - q1.D * q2.C,
q1.A * q2.C - q1.B * q2.D + q1.C * q2.A + q1.D * q2.B,
q1.A * q2.D + q1.B * q2.C - q1.C * q2.B + q1.D * q2.A);
}
 
public static bool operator ==(Quaternion q1, Quaternion q2)
{
return q1.A == q2.A && q1.B == q2.B && q1.C == q2.C && q1.D == q2.D;
}
 
public static bool operator !=(Quaternion q1, Quaternion q2)
{
return !(q1 == q2);
}
 
#region Object Members
 
public override bool Equals(object obj)
{
if (obj is Quaternion)
return Equals((Quaternion)obj);
 
return false;
}
 
public override int GetHashCode()
{
return A.GetHashCode() ^ B.GetHashCode() ^ C.GetHashCode() ^ D.GetHashCode();
}
 
public override string ToString()
{
return string.Format("Q({0}, {1}, {2}, {3})", A, B, C, D);
}
 
#endregion
 
#region IEquatable<Quaternion> Members
 
public bool Equals(Quaternion other)
{
return other == this;
}
 
#endregion
}</syntaxhighlight>
 
Demonstration:
<syntaxhighlight lang="csharp">using System;
 
static class Program
{
static void Main(string[] args)
{
Quaternion q = new Quaternion(1, 2, 3, 4);
Quaternion q1 = new Quaternion(2, 3, 4, 5);
Quaternion q2 = new Quaternion(3, 4, 5, 6);
double r = 7;
 
Console.WriteLine("q = {0}", q);
Console.WriteLine("q1 = {0}", q1);
Console.WriteLine("q2 = {0}", q2);
Console.WriteLine("r = {0}", r);
 
Console.WriteLine("q.Norm() = {0}", q.Norm());
Console.WriteLine("q1.Norm() = {0}", q1.Norm());
Console.WriteLine("q2.Norm() = {0}", q2.Norm());
 
Console.WriteLine("-q = {0}", -q);
Console.WriteLine("q.Conjugate() = {0}", q.Conjugate());
 
Console.WriteLine("q + r = {0}", q + r);
Console.WriteLine("q1 + q2 = {0}", q1 + q2);
Console.WriteLine("q2 + q1 = {0}", q2 + q1);
 
Console.WriteLine("q * r = {0}", q * r);
Console.WriteLine("q1 * q2 = {0}", q1 * q2);
Console.WriteLine("q2 * q1 = {0}", q2 * q1);
 
Console.WriteLine("q1*q2 {0} q2*q1", (q1 * q2) == (q2 * q1) ? "==" : "!=");
}
}</syntaxhighlight>
 
{{out}}
<pre>q = Q(1, 2, 3, 4)
q1 = Q(2, 3, 4, 5)
q2 = Q(3, 4, 5, 6)
r = 7
q.Norm() = 5.47722557505166
q1.Norm() = 7.34846922834953
q2.Norm() = 9.2736184954957
-q = Q(-1, -2, -3, -4)
q.Conjugate() = Q(1, -2, -3, -4)
q + r = Q(8, 2, 3, 4)
q1 + q2 = Q(5, 7, 9, 11)
q2 + q1 = Q(5, 7, 9, 11)
q * r = Q(7, 14, 21, 28)
q1 * q2 = Q(-56, 16, 24, 26)
q2 * q1 = Q(-56, 18, 20, 28)
q1*q2 != q2*q1</pre>
 
=={{header|C++}}==
Line 1,048 ⟶ 1,844:
This example uses templates to provide the underlying data-type, and includes several extra functions and constructors that often come up when using quaternions.
 
<langsyntaxhighlight lang="cpp">#include <iostream>
using namespace std;
 
Line 1,163 ⟶ 1,959:
(q.z < T()) ? (io << " - " << (-q.z) << "k") : (io << " + " << q.z << "k");
return io;
}</langsyntaxhighlight>
 
Test program:
<langsyntaxhighlight lang="cpp">int main()
{
Quaternion<> q0(1, 2, 3, 4);
Line 1,205 ⟶ 2,001:
Quaternion<int> q5(2), q6(3);
cout << endl << q5*q6 << endl;
}</langsyntaxhighlight>
 
{{out}}
Line 1,241 ⟶ 2,037:
</pre>
 
=={{header|C sharpCLU}}==
<syntaxhighlight lang="clu">quat = cluster is make, minus, norm, conj, add, addr, mul, mulr,
<lang csharp>using System;
equal, get_a, get_b, get_c, get_d, q_form
 
rep = struct[a,b,c,d: real]
struct Quaternion : IEquatable<Quaternion>
{
make = proc (a,b,c,d: real) returns (cvt)
public readonly double A, B, C, D;
return (rep${a:a, b:b, c:c, d:d})
 
end make
public Quaternion(double a, double b, double c, double d)
{
minus = proc (q: this.Acvt) =returns a;(cvt)
thisreturn (down(make(-q.B =a, -q.b;, -q.c, -q.d)))
end this.C = c;minus
this.D = d;
norm = proc (q: cvt) returns (real)
}
return ((q.a**2.0 + q.b**2.0 + q.c**2.0 + q.d**2.0) ** 0.5)
 
publicend double Norm()norm
{
conj = proc (q: cvt) returns (cvt)
return Math.Sqrt(A * A + B * B + C * C + D * D);
return (down(make(q.a, -q.b, -q.c, q.d)))
}
end conj
 
public static Quaternion operator -(Quaternion q)
add = proc (q1, q2: cvt) returns (cvt)
{
return new Quaternion(-qdown(make(q1.Aa+q2.a, -qq1.Bb+q2.b, -qq1.Cc+q2.c, -qq1.Dd+q2.d)));
}end add
 
addr = proc (q: cvt, r: real) returns (cvt)
public Quaternion Conjugate()
return (down(make(q.a+r, q.b+r, q.c+r, q.d+r)))
{
end addr
return new Quaternion(A, -B, -C, -D);
}
mul = proc (q1, q2: cvt) returns (cvt)
 
a: real := q1.a*q2.a - q1.b*q2.b - q1.c*q2.c - q1.d*q2.d
// implicit conversion takes care of real*quaternion and real+quaternion
b: real := q1.a*q2.b + q1.b*q2.a + q1.c*q2.d - q1.d*q2.c
public static implicit operator Quaternion(double d)
c: real := q1.a*q2.c - q1.b*q2.d + q1.c*q2.a + q1.d*q2.b
{
d: real := q1.a*q2.d + q1.b*q2.c - q1.c*q2.b + q1.d*q2.a
return new Quaternion(d, 0, 0, 0);
return (down(make(a,b,c,d)))
}
end mul
 
public static Quaternion operator +(Quaternion q1, Quaternion q2)
mulr = proc (q: cvt, r: real) returns (cvt)
{
return new Quaternion(q1down(make(q.A + q2.Aa*r, q1q.B + q2.Bb*r, q1q.C + q2.Cc*r, q1q.D + q2.Dd*r)));
}end mulr
 
equal = proc (q1, q2: cvt) returns (bool)
public static Quaternion operator *(Quaternion q1, Quaternion q2)
return (q1.a = q2.a & q1.b = q2.b & q1.c = q2.c & q1.d = q2.d)
{
end equal
return new Quaternion(
q1.A * q2.A - q1.B * q2.B - q1.C * q2.C - q1.D * q2.D,
get_a = proc (q: cvt) returns (real) return (q.a) end get_a
q1.A * q2.B + q1.B * q2.A + q1.C * q2.D - q1.D * q2.C,
get_b = proc (q: cvt) returns (real) return (q.b) end get_b
q1.A * q2.C - q1.B * q2.D + q1.C * q2.A + q1.D * q2.B,
get_c = proc (q: cvt) returns (real) return (q.c) end get_c
q1.A * q2.D + q1.B * q2.C - q1.C * q2.B + q1.D * q2.A);
get_d = proc (q: cvt) returns (real) return (q.d) end get_d
}
 
public static bool operator ==(Quaternion q1, Quaternion q2)
{
return q1.A == q2.A && q1.B == q2.B && q1.C == q2.C && q1.D == q2.D;
}
 
public static bool operator !=(Quaternion q1, Quaternion q2)
{
return !(q1 == q2);
}
 
#region Object Members
 
public override bool Equals(object obj)
{
if (obj is Quaternion)
return Equals((Quaternion)obj);
 
return false;
}
 
q_form = proc (q: cvt, a, b: int) returns (string)
public override int GetHashCode()
return ( f_form(q.a, a, b) || " + "
{
|| f_form(q.b, a, b) || "i + "
return A.GetHashCode() ^ B.GetHashCode() ^ C.GetHashCode() ^ D.GetHashCode();
|| f_form(q.c, a, b) || "j + "
}
|| f_form(q.d, a, b) || "k" )
end q_form
end quat
start_up = proc ()
po: stream := stream$primary_output()
q0: quat := quat$make(1.0, 2.0, 3.0, 4.0)
q1: quat := quat$make(2.0, 3.0, 4.0, 5.0)
q2: quat := quat$make(3.0, 4.0, 5.0, 6.0)
r: real := 7.0
stream$putl(po, " q0 = " || quat$q_form(q0, 3, 3))
stream$putl(po, " q1 = " || quat$q_form(q1, 3, 3))
stream$putl(po, " q2 = " || quat$q_form(q2, 3, 3))
stream$putl(po, " r = " || f_form(r, 3, 3))
stream$putl(po, "")
stream$putl(po, "norm(q0) = " || f_form(quat$norm(q0), 3, 3))
stream$putl(po, " -q0 = " || quat$q_form(-q0, 3, 3))
stream$putl(po, "conj(q0) = " || quat$q_form(quat$conj(q0), 3, 3))
stream$putl(po, " q0 + r = " || quat$q_form(quat$addr(q0, r), 3, 3))
stream$putl(po, " q1 + q2 = " || quat$q_form(q1 + q2, 3, 3))
stream$putl(po, " q0 * r = " || quat$q_form(quat$mulr(q0, r), 3, 3))
stream$putl(po, " q1 * q2 = " || quat$q_form(q1 * q2, 3, 3))
stream$putl(po, " q2 * q1 = " || quat$q_form(q2 * q1, 3, 3))
if q1*q2 ~= q2*q1 then stream$putl(po, "q1 * q2 ~= q2 * q1") end
end start_up</syntaxhighlight>
{{out}}
<pre> q0 = 1.000 + 2.000i + 3.000j + 4.000k
q1 = 2.000 + 3.000i + 4.000j + 5.000k
q2 = 3.000 + 4.000i + 5.000j + 6.000k
r = 7.000
 
norm(q0) = 5.477
public override string ToString()
-q0 = -1.000 + -2.000i + -3.000j + -4.000k
{
conj(q0) = 1.000 + -2.000i + -3.000j + 4.000k
return string.Format("Q({0}, {1}, {2}, {3})", A, B, C, D);
q0 + r = 8.000 + 9.000i + 10.000j + 11.000k
}
q1 + q2 = 5.000 + 7.000i + 9.000j + 11.000k
 
q0 * r = 7.000 + 14.000i + 21.000j + 28.000k
#endregion
q1 * q2 = -56.000 + 16.000i + 24.000j + 26.000k
 
q2 * q1 = -56.000 + 18.000i + 20.000j + 28.000k
#region IEquatable<Quaternion> Members
q1 * q2 ~= q2 * q1</pre>
 
public bool Equals(Quaternion other)
{
return other == this;
}
 
#endregion
}</lang>
 
Demonstration:
<lang csharp>using System;
 
static class Program
{
static void Main(string[] args)
{
Quaternion q = new Quaternion(1, 2, 3, 4);
Quaternion q1 = new Quaternion(2, 3, 4, 5);
Quaternion q2 = new Quaternion(3, 4, 5, 6);
double r = 7;
 
Console.WriteLine("q = {0}", q);
Console.WriteLine("q1 = {0}", q1);
Console.WriteLine("q2 = {0}", q2);
Console.WriteLine("r = {0}", r);
 
Console.WriteLine("q.Norm() = {0}", q.Norm());
Console.WriteLine("q1.Norm() = {0}", q1.Norm());
Console.WriteLine("q2.Norm() = {0}", q2.Norm());
 
Console.WriteLine("-q = {0}", -q);
Console.WriteLine("q.Conjugate() = {0}", q.Conjugate());
 
Console.WriteLine("q + r = {0}", q + r);
Console.WriteLine("q1 + q2 = {0}", q1 + q2);
Console.WriteLine("q2 + q1 = {0}", q2 + q1);
 
Console.WriteLine("q * r = {0}", q * r);
Console.WriteLine("q1 * q2 = {0}", q1 * q2);
Console.WriteLine("q2 * q1 = {0}", q2 * q1);
 
Console.WriteLine("q1*q2 {0} q2*q1", (q1 * q2) == (q2 * q1) ? "==" : "!=");
}
}</lang>
 
{{out}}
<pre>q = Q(1, 2, 3, 4)
q1 = Q(2, 3, 4, 5)
q2 = Q(3, 4, 5, 6)
r = 7
q.Norm() = 5.47722557505166
q1.Norm() = 7.34846922834953
q2.Norm() = 9.2736184954957
-q = Q(-1, -2, -3, -4)
q.Conjugate() = Q(1, -2, -3, -4)
q + r = Q(8, 2, 3, 4)
q1 + q2 = Q(5, 7, 9, 11)
q2 + q1 = Q(5, 7, 9, 11)
q * r = Q(7, 14, 21, 28)
q1 * q2 = Q(-56, 16, 24, 26)
q2 * q1 = Q(-56, 18, 20, 28)
q1*q2 != q2*q1</pre>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">
(defclass quaternion () ((a :accessor q-a :initarg :a :type real)
(b :accessor q-b :initarg :b :type real)
Line 1,466 ⟶ 2,215:
(format t "q*q1*q2 = ~a~&" (reduce #'mul (list q q1 q2)))
(format t "q-q1-q2 = ~a~&" (reduce #'sub (list q q1 q2)))
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,475 ⟶ 2,224:
q-q1-q2 = +0.0+1.0i-1.0j-1.0k
</pre>
 
=={{header|Crystal}}==
{{trans|Rust and Ruby}}
<syntaxhighlight lang="ruby">class Quaternion
property a, b, c, d
 
def initialize(@a : Int64, @b : Int64, @c : Int64, @d : Int64) end
 
def norm; Math.sqrt(a**2 + b**2 + c**2 + d**2) end
def conj; Quaternion.new(a, -b, -c, -d) end
def +(n) Quaternion.new(a + n, b, c, d) end
def -(n) Quaternion.new(a - n, b, c, d) end
def -() Quaternion.new(-a, -b, -c, -d) end
def *(n) Quaternion.new(a * n, b * n, c * n, d * n) end
def ==(rhs : Quaternion) self.to_s == rhs.to_s end
def +(rhs : Quaternion)
Quaternion.new(a + rhs.a, b + rhs.b, c + rhs.c, d + rhs.d)
end
 
def -(rhs : Quaternion)
Quaternion.new(a - rhs.a, b - rhs.b, c - rhs.c, d - rhs.d)
end
 
def *(rhs : Quaternion)
Quaternion.new(
a * rhs.a - b * rhs.b - c * rhs.c - d * rhs.d,
a * rhs.b + b * rhs.a + c * rhs.d - d * rhs.c,
a * rhs.c - b * rhs.d + c * rhs.a + d * rhs.b,
a * rhs.d + b * rhs.c - c * rhs.b + d * rhs.a)
end
 
def to_s(io : IO) io << "(#{a} #{sgn(b)}i #{sgn(c)}j #{sgn(d)}k)\n" end
private def sgn(n) n.sign|1 == 1 ? "+ #{n}" : "- #{n.abs}" end
end
 
struct Number
def +(rhs : Quaternion)
Quaternion.new(rhs.a + self, rhs.b, rhs.c, rhs.d)
end
 
def -(rhs : Quaternion)
Quaternion.new(-rhs.a + self, -rhs.b, -rhs.c, -rhs.d)
end
 
def *(rhs : Quaternion)
Quaternion.new(rhs.a * self, rhs.b * self, rhs.c * self, rhs.d * self)
end
end
 
q0 = Quaternion.new(a: 1, b: 2, c: 3, d: 4)
q1 = Quaternion.new(2, 3, 4, 5)
q2 = Quaternion.new(3, 4, 5, 6)
r = 7
 
puts "q0 = #{q0}"
puts "q1 = #{q1}"
puts "q2 = #{q2}"
puts "r = #{r}"
puts
puts "normal of q0 = #{q0.norm}"
puts "-q0 = #{-q0}"
puts "conjugate of q0 = #{q0.conj}"
puts "q0 * (conjugate of q0) = #{q0 * q0.conj}"
puts "(conjugate of q0) * q0 = #{q0.conj * q0}"
puts
puts "r + q0 = #{r + q0}"
puts "q0 + r = #{q0 + r}"
puts
puts " q0 - r = #{q0 - r}"
puts "-q0 - r = #{-q0 - r}"
puts " r - q0 = #{r - q0}"
puts "-q0 + r = #{-q0 + r}"
puts
puts "r * q0 = #{r * q0}"
puts "q0 * r = #{q0 * r}"
puts
puts "q0 + q1 = #{q0 + q1}"
puts "q0 - q1 = #{q2 - q1}"
puts "q0 * q1 = #{q0 * q1}"
puts
puts " q0 + q1 * q2 = #{q0 + q1 * q2}"
puts "(q0 + q1) * q2 = #{(q0 + q1) * q2}"
puts
puts " q0 * q1 * q2 = #{q0 * q1 * q2}"
puts "(q0 * q1) * q2 = #{(q0 * q1) * q2}"
puts " q0 * (q1 * q2) = #{q0 * (q1 * q2)}"
puts
puts "q1 * q2 = #{q1 * q2}"
puts "q2 * q1 = #{q2 * q1}"
puts
puts "q1 * q2 != q2 * q1 => #{(q1 * q2) != (q2 * q1)}"
puts "q1 * q2 == q2 * q1 => #{(q1 * q2) == (q2 * q1)}"</syntaxhighlight>
{{out}}
<pre>q0 = (1 + 2i + 3j + 4k)
q1 = (2 + 3i + 4j + 5k)
q2 = (3 + 4i + 5j + 6k)
r = 7
 
normal of q0 = 5.477225575051661
-q0 = (-1 - 2i - 3j - 4k)
conjugate of q0 = (1 - 2i - 3j - 4k)
q0 * (conjugate of q0) = (30 + 0i + 0j + 0k)
(conjugate of q0) * q0 = (30 + 0i + 0j + 0k)
 
r + q0 = (8 + 2i + 3j + 4k)
q0 + r = (8 + 2i + 3j + 4k)
 
q0 - r = (-6 + 2i + 3j + 4k)
-q0 - r = (-8 - 2i - 3j - 4k)
r - q0 = (6 - 2i - 3j - 4k)
-q0 + r = (6 - 2i - 3j - 4k)
 
r * q0 = (7 + 14i + 21j + 28k)
q0 * r = (7 + 14i + 21j + 28k)
 
q0 + q1 = (3 + 5i + 7j + 9k)
q0 - q1 = (1 + 1i + 1j + 1k)
q0 * q1 = (-36 + 6i + 12j + 12k)
 
q0 + q1 * q2 = (-55 + 18i + 27j + 30k)
(q0 + q1) * q2 = (-100 + 24i + 42j + 42k)
 
q0 * q1 * q2 = (-264 - 114i - 132j - 198k)
(q0 * q1) * q2 = (-264 - 114i - 132j - 198k)
q0 * (q1 * q2) = (-264 - 114i - 132j - 198k)
 
q1 * q2 = (-56 + 16i + 24j + 26k)
q2 * q1 = (-56 + 18i + 20j + 28k)
 
q1 * q2 != q2 * q1 => true
q1 * q2 == q2 * q1 => false</pre>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.math, std.numeric, std.traits, std.conv, std.complex;
 
 
Line 1,702 ⟶ 2,582:
writeln(" exp(log(s)): ", exp(log(s)));
writeln(" log(exp(s)): ", log(exp(s)));
}</langsyntaxhighlight>
{{out}}
<pre>1. q - norm: 7.34847
Line 1,734 ⟶ 2,614:
exp(log(s)): [2, 0.33427, 0.445694, 0.557117]
log(exp(s)): [2, 0.33427, 0.445694, 0.557117]</pre>
 
 
=={{header|Dart}}==
{{trans|Kotlin}}
<syntaxhighlight lang="Dart">
import 'dart:math' as math;
 
class Quaternion {
final double a, b, c, d;
 
Quaternion(this.a, this.b, this.c, this.d);
 
Quaternion operator +(Object other) {
if (other is Quaternion) {
return Quaternion(a + other.a, b + other.b, c + other.c, d + other.d);
} else if (other is double) {
return Quaternion(a + other, b, c, d);
}
throw ArgumentError('Invalid type for addition: ${other.runtimeType}');
}
 
Quaternion operator *(Object other) {
if (other is Quaternion) {
return Quaternion(
a * other.a - b * other.b - c * other.c - d * other.d,
a * other.b + b * other.a + c * other.d - d * other.c,
a * other.c - b * other.d + c * other.a + d * other.b,
a * other.d + b * other.c - c * other.b + d * other.a,
);
} else if (other is double) {
return Quaternion(a * other, b * other, c * other, d * other);
}
throw ArgumentError('Invalid type for multiplication: ${other.runtimeType}');
}
 
Quaternion operator -() => Quaternion(-a, -b, -c, -d);
 
Quaternion conj() => Quaternion(a, -b, -c, -d);
 
double norm() => math.sqrt(a * a + b * b + c * c + d * d);
 
@override
String toString() => '($a, $b, $c, $d)';
}
 
void main() {
var q = Quaternion(1.0, 2.0, 3.0, 4.0);
var q1 = Quaternion(2.0, 3.0, 4.0, 5.0);
var q2 = Quaternion(3.0, 4.0, 5.0, 6.0);
var r = 7.0;
print("q = $q");
print("q1 = $q1");
print("q2 = $q2");
print("r = $r\n");
print("norm(q) = ${q.norm().toStringAsFixed(6)}");
print("-q = ${-q}");
print("conj(q) = ${q.conj()}\n");
print("r + q = ${q + r}");
print("q + r = ${q + r}");
print("q1 + q2 = ${q1 + q2}\n");
print("r * q = ${q * r}");
print("q * r = ${q * r}");
var q3 = q1 * q2;
var q4 = q2 * q1;
print("q1 * q2 = $q3");
print("q2 * q1 = $q4\n");
print("q1 * q2 != q2 * q1 = ${q3 != q4}");
}
</syntaxhighlight>
{{out}}
<pre>
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
 
norm(q) = 5.477226
-q = (-1.0, -2.0, -3.0, -4.0)
conj(q) = (1.0, -2.0, -3.0, -4.0)
 
r + q = (8.0, 2.0, 3.0, 4.0)
q + r = (8.0, 2.0, 3.0, 4.0)
q1 + q2 = (5.0, 7.0, 9.0, 11.0)
 
r * q = (7.0, 14.0, 21.0, 28.0)
q * r = (7.0, 14.0, 21.0, 28.0)
q1 * q2 = (-56.0, 16.0, 24.0, 26.0)
q2 * q1 = (-56.0, 18.0, 20.0, 28.0)
 
q1 * q2 != q2 * q1 = true
 
</pre>
 
 
=={{header|Delphi}}==
 
<langsyntaxhighlight Delphilang="delphi">unit Quaternions;
 
interface
Line 1,865 ⟶ 2,838:
end;
 
end.</langsyntaxhighlight>
 
Test program
<langsyntaxhighlight Delphilang="delphi">program QuaternionTest;
 
{$APPTYPE CONSOLE}
Line 1,899 ⟶ 2,872:
writeln('q1 * q2 = ', (q1 * q2).ToString);
writeln('q2 * q1 = ', (q2 * q1).ToString);
end.</langsyntaxhighlight>
 
{{out}}
Line 1,924 ⟶ 2,897:
=={{header|E}}==
 
<langsyntaxhighlight lang="e">interface Quaternion guards QS {}
def makeQuaternion(a, b, c, d) {
return def quaternion implements QS {
Line 1,984 ⟶ 2,957:
to d() { return d }
}
}</langsyntaxhighlight>
 
<langsyntaxhighlight lang="e">? def q1 := makeQuaternion(2,3,4,5)
# value: (2 + 3i + 4j + 5k)
 
Line 2,002 ⟶ 2,975:
 
? q1+(-2)
# value: (0 + 3i + 4j + 5k)</langsyntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func qnorm q[] .
for i to 4
s += q[i] * q[i]
.
return sqrt s
.
func[] qneg q[] .
for i to 4
q[i] = -q[i]
.
return q[]
.
func[] qconj q[] .
for i = 2 to 4
q[i] = -q[i]
.
return q[]
.
func[] qaddreal q[] r .
q[1] += r
return q[]
.
func[] qadd q[] q2[] .
for i to 4
q[i] += q2[i]
.
return q[]
.
func[] qmulreal q[] r .
for i to 4
q[i] *= r
.
return q[]
.
func[] qmul q1[] q2[] .
res[] &= q1[1] * q2[1] - q1[2] * q2[2] - q1[3] * q2[3] - q1[4] * q2[4]
res[] &= q1[1] * q2[2] + q1[2] * q2[1] + q1[3] * q2[4] - q1[4] * q2[3]
res[] &= q1[1] * q2[3] - q1[2] * q2[4] + q1[3] * q2[1] + q1[4] * q2[2]
res[] &= q1[1] * q2[4] + q1[2] * q2[3] - q1[3] * q2[2] + q1[4] * q2[1]
return res[]
.
q[] = [ 1 2 3 4 ]
q1[] = [ 2 3 4 5 ]
q2[] = [ 3 4 5 6 ]
r = 7
#
print "q = " & q[]
print "q1 = " & q1[]
print "q2 = " & q2[]
print "r = " & r
print "norm(q) = " & qnorm q[]
print "neg(q) = " & qneg q[]
print "conjugate(q) = " & qconj q[]
print "q+r = " & qaddreal q[] r
print "q1+q2 = " & qadd q1[] q2[]
print "qr = " & qmulreal q[] r
print "q1q2 = " & qmul q1[] q2[]
print "q2q1 = " & qmul q2[] q1[]
if q1[] <> q2[]
print "q1 != q2"
.
</syntaxhighlight>
 
{{out}}
<pre>
q = [ 1 2 3 4 ]
q1 = [ 2 3 4 5 ]
q2 = [ 3 4 5 6 ]
r = 7
norm(q) = 5.48
neg(q) = [ -1 -2 -3 -4 ]
conjugate(q) = [ 1 -2 -3 -4 ]
q+r = [ 8 2 3 4 ]
q1+q2 = [ 5 7 9 11 ]
qr = [ 7 14 21 28 ]
q1q2 = [ -56 16 24 26 ]
q2q1 = [ -56 18 20 28 ]
q1 != q2
</pre>
 
=={{header|Eero}}==
<langsyntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
interface Quaternion : Number
Line 2,106 ⟶ 3,161:
Log( 'q2 * q1 = %@', q2 * q1 )
 
return 0</langsyntaxhighlight>
 
{{out}}
Line 2,124 ⟶ 3,179:
2013-09-04 16:40:29.822 a.out[2170:507] q1 * q2 = (-56.0, 16.0, 24.0, 26.0)
2013-09-04 16:40:29.822 a.out[2170:507] q2 * q1 = (-56.0, 18.0, 20.0, 28.0)</pre>
 
=={{header|Elena}}==
{{trans|C#}}
ELENA 6.x :
<syntaxhighlight lang="elena">import system'math;
import extensions;
import extensions'text;
struct Quaternion
{
real A : rprop;
real B : rprop;
real C : rprop;
real D : rprop;
constructor new(a, b, c, d)
<= new(cast real(a), cast real(b), cast real(c), cast real(d));
constructor new(real a, real b, real c, real d)
{
A := a;
B := b;
C := c;
D := d
}
constructor(real r)
{
A := r;
B := 0.0r;
C := 0.0r;
D := 0.0r
}
real Norm = (A*A + B*B + C*C + D*D).sqrt();
Quaternion Negative = Quaternion.new(A.Negative,B.Negative,C.Negative,D.Negative);
Quaternion Conjugate = Quaternion.new(A,B.Negative,C.Negative,D.Negative);
Quaternion add(Quaternion q)
= Quaternion.new(A + q.A, B + q.B, C + q.C, D + q.D);
Quaternion multiply(Quaternion q)
= Quaternion.new(
A * q.A - B * q.B - C * q.C - D * q.D,
A * q.B + B * q.A + C * q.D - D * q.C,
A * q.C - B * q.D + C * q.A + D * q.B,
A * q.D + B * q.C - C * q.B + D * q.A);
Quaternion add(real r)
<= add(Quaternion.new(r,0,0,0));
Quaternion multiply(real r)
<= multiply(Quaternion.new(r,0,0,0));
bool equal(Quaternion q)
= (A == q.A) && (B == q.B) && (C == q.C) && (D == q.D);
string toPrintable()
= new StringWriter().printFormatted("Q({0}, {1}, {2}, {3})",A,B,C,D);
}
public program()
{
auto q := Quaternion.new(1,2,3,4);
auto q1 := Quaternion.new(2,3,4,5);
auto q2 := Quaternion.new(3,4,5,6);
real r := 7;
console.printLine("q = ", q);
console.printLine("q1 = ", q1);
console.printLine("q2 = ", q2);
console.printLine("r = ", r);
console.printLine("q.Norm() = ", q.Norm);
console.printLine("q1.Norm() = ", q1.Norm);
console.printLine("q2.Norm() = ", q2.Norm);
console.printLine("-q = ", q.Negative);
console.printLine("q.Conjugate() = ", q.Conjugate);
console.printLine("q + r = ", q + r);
console.printLine("q1 + q2 = ", q1 + q2);
console.printLine("q2 + q1 = ", q2 + q1);
console.printLine("q * r = ", q * r);
console.printLine("q1 * q2 = ", q1 * q2);
console.printLine("q2 * q1 = ", q2 * q1);
console.printLineFormatted("q1*q2 {0} q2*q1", ((q1 * q2) == (q2 * q1)).iif("==","!="))
}</syntaxhighlight>
{{out}}
<pre>
q = Q(1.0, 2.0, 3.0, 4.0)
q1 = Q(2.0, 3.0, 4.0, 5.0)
q2 = Q(3.0, 4.0, 5.0, 6.0)
r = 7.0
q.Norm() = 5.477225575052
q1.Norm() = 7.34846922835
q2.Norm() = 9.273618495496
-q = Q(-1.0, -2.0, -3.0, -4.0)
q.Conjugate() = Q(1.0, -2.0, -3.0, -4.0)
q + r = Q(8.0, 2.0, 3.0, 4.0)
q1 + q2 = Q(5.0, 7.0, 9.0, 11.0)
q2 + q1 = Q(5.0, 7.0, 9.0, 11.0)
q * r = Q(7.0, 14.0, 21.0, 28.0)
q1 * q2 = Q(-56.0, 16.0, 24.0, 26.0)
q2 * q1 = Q(-56.0, 18.0, 20.0, 28.0)
q1*q2 != q2*q1
</pre>
 
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
PROGRAM QUATERNION
 
Line 2,231 ⟶ 3,397:
PRINTQ(R.)
END PROGRAM
</syntaxhighlight>
</lang>
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">function norm(sequence q)
return sqrt(power(q[1],2)+power(q[2],2)+power(q[3],2)+power(q[4],2))
end function
Line 2,281 ⟶ 3,447:
printf(1, "q1 + q2 = %s\n", {quats(add(q1,q2))})
printf(1, "q1 * q2 = %s\n", {quats(mul(q1,q2))})
printf(1, "q2 * q1 = %s\n", {quats(mul(q2,q1))})</langsyntaxhighlight>
 
{{out}}
Line 2,295 ⟶ 3,461:
Mainly a {{trans|C#}} On the minus side we have no way to define a conversion to Quaternion from any suitable (numeric) type.
On the plus side we can avoid the stuff to make the equality structual (from the referential equality default) by just declaring it as an attribute to the type and let the compiler handle the details.
<langsyntaxhighlight lang="fsharp">open System
 
[<Struct; StructuralEquality; NoComparison>]
Line 2,357 ⟶ 3,523:
printfn "q1*q2 %s q2*q1" (if (q1 * q2) = (q2 * q1) then "=" else "<>")
printfn "q %s Q(1.,2.,3.,4.)" (if q = Quaternion(1., 2., 3., 4.) then "=" else "<>")
0</langsyntaxhighlight>
{{out}}
<pre>q = Q(1.000000, 2.000000, 3.000000, 4.000000)
Line 2,376 ⟶ 3,542:
q1*q2 <> q2*q1
q = Q(1.,2.,3.,4.)</pre>
 
=={{header|Factor}}==
The <code>math.quaternions</code> vocabulary provides words for treating sequences like quaternions. <code>norm</code> and <code>vneg</code> come from the <code>math.vectors</code> vocabulary. Oddly, I wasn't able to find a word for adding a real to a quaternion, so I wrote one.
<syntaxhighlight lang="factor">USING: generalizations io kernel locals math.quaternions
math.vectors prettyprint sequences ;
IN: rosetta-code.quaternion-type
 
: show ( quot -- )
[ unparse 2 tail but-last "= " append write ] [ call . ] bi
; inline
 
: 2show ( quots -- )
[ 2curry show ] map-compose [ call ] each ; inline
 
: q+n ( q n -- q+n ) n>q q+ ;
 
[let
{ 1 2 3 4 } 7 { 2 3 4 5 } { 3 4 5 6 } :> ( q r q1 q2 )
q [ norm ]
q [ vneg ]
q [ qconjugate ]
[ curry show ] 2tri@
{
[ q r [ q+n ] ]
[ q r [ q*n ] ]
[ q1 q2 [ q+ ] ]
[ q1 q2 [ q* ] ]
[ q2 q1 [ q* ] ]
} 2show
]</syntaxhighlight>
{{out}}
<pre>
{ 1 2 3 4 } norm = 5.477225575051661
{ 1 2 3 4 } vneg = { -1 -2 -3 -4 }
{ 1 2 3 4 } qconjugate = { 1 -2 -3 -4 }
{ 1 2 3 4 } 7 q+n = { 8 2 3 4 }
{ 1 2 3 4 } 7 q*n = { 7 14 21 28 }
{ 2 3 4 5 } { 3 4 5 6 } q+ = { 5 7 9 11 }
{ 2 3 4 5 } { 3 4 5 6 } q* = { -56 16 24 26 }
{ 3 4 5 6 } { 2 3 4 5 } q* = { -56 18 20 28 }
</pre>
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: quaternions 4 * floats ;
 
: qvariable create 1 quaternions allot ;
Line 2,454 ⟶ 3,661:
m1 q1 q2 q* m1 q. \ ( -56. 16. 24. 26. )
m2 q2 q1 q* m2 q. \ ( -56. 18. 20. 28. )
m1 m2 q= . \ 0 (false)</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">module Q_mod
implicit none
 
Line 2,618 ⟶ 3,825:
write(*, "(a, 4f8.3)") " q2 * q1 = ", q2 * q1
 
end program</langsyntaxhighlight>
{{out}}
<pre> q = 1.000 2.000 3.000 4.000
Line 2,634 ⟶ 3,841:
q1 * q2 = -56.000 16.000 24.000 26.000
q2 * q1 = -56.000 18.000 20.000 28.000</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">
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
</syntaxhighlight>
{{out}}
<pre>
q = (1, 2, 3, 4)
q1 = (2, 3, 4, 5)
q2 = (3, 4, 5, 6)
r = 7
norm(q) = 5.477225575051661
neg(q) = (-1, -2, -3, -4)
conj(q) = (1, -2, -3, -4)
r + q = (8, 2, 3, 4)
q1 + q2 = (5, 7, 9, 11)
q2 + q1 = (5, 7, 9, 11)
r * q = (7, 14, 21, 28)
q1 * q2 = (-56, 16, 24, 26)
q2 * q1 = (-56, 18, 20, 28)
</pre>
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap"># GAP has built-in support for quaternions
 
A := QuaternionAlgebra(Rationals);
Line 2,744 ⟶ 4,049:
 
1/q;
# (1/30)*e+(-1/15)*i+(-1/10)*j+(-2/15)*k</langsyntaxhighlight>
 
=={{header|Go}}==
Line 2,752 ⟶ 4,057:
The three inputs are reused repeatedly without being modified.
The output is also reused repeatedly, being overwritten for each operation.
<langsyntaxhighlight lang="go">package main
 
import (
Line 2,830 ⟶ 4,135:
q1.r*q2.k+q1.i*q2.j-q1.j*q2.i+q1.k*q2.r
return z
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,851 ⟶ 4,156:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Monad (join)
import Control.Arrow
import Data.List
 
data Quaternion a = Q Double Double Double Double
Q a a a a
deriving (Show, Ord, Eq)
deriving (Show, Eq)
 
realQ :: Quaternion a -> Doublea
realQ (Q r _ _ _) = r
 
imagQ :: Quaternion a -> [Doublea]
imagQ (Q _ i j k) = [i, j, k]
 
quaternionFromScalar :: (Num a) => a -> Quaternion a
quaternionFromScalar s = Q s 0 0 0
 
listFromQ (Q:: Quaternion a b c d) =-> [a,b,c,d]
listFromQ (Q a b c d) = [a, b, c, d]
 
quaternionFromList :: [a] -> Quaternion a
quaternionFromList [a, b, c, d] = Q a b c d
 
addQ, subQ, mulQnormQ :: Quaternion(RealFloat -a) => Quaternion a -> Quaterniona
normQ = sqrt . sum . join (zipWith (*)) . listFromQ
addQ (Q a b c d) (Q p q r s) = Q (a+p) (b+q) (c+r) (d+s)
 
conjQ :: (Num a) => Quaternion a -> Quaternion a
subQ (Q a b c d) (Q p q r s) = Q (a-p) (b-q) (c-r) (d-s)
 
mulQ (Q a b c d) (Q p q r s) =
Q (a*p - b*q - c*r - d*s)
(a*q + b*p + c*s - d*r)
(a*r - b*s + c*p + d*q)
(a*s + b*r - c*q + d*p)
normQ = sqrt. sum. join (zipWith (*)). listFromQ
 
conjQ, negQ :: Quaternion -> Quaternion
conjQ (Q a b c d) = Q a (-b) (-c) (-d)
 
instance (RealFloat a) => Num (Quaternion a) where
negQ (Q a b c d) = Q (-a) (-b) (-c) (-d)</lang>
(Q a b c d) + (Q p q r s) = Q (a + p) (b + q) (c + r) (d + s)
To use with the Examples:
(Q a b c d) - (Q p q r s) = Q (a - p) (b - q) (c - r) (d - s)
<lang haskell>[q,q1,q2] = map quaternionFromList [[1..4],[2..5],[3..6]]
(Q a b c d) * (Q p q r s) =
-- a*b == b*a
Q
test :: Quaternion -> Quaternion -> Bool
test (a * p - b =* q a- `mulQ`c b* ==r b- `mulQ`d a</lang>* s)
(a * q + b * p + c * s - d * r)
Examples:
<pre>*Main> mulQ (Q 0 1(a 0* 0)r $- mulQb (Q* 0s 0+ 1c 0)* (Qp 0+ 0d 0* 1q) -- i*j*k
(a * s + b * r - c * q + d * p)
Q (-1.0) 0.0 0.0 0.0
negate (Q a b c d) = Q (-a) (-b) (-c) (-d)
 
abs q = quaternionFromScalar (normQ q)
*Main> test q1 q2
signum (Q 0 0 0 0) = 0
False
signum q@(Q a b c d) = Q (a/n) (b/n) (c/n) (d/n) where n = normQ q
 
fromInteger n = quaternionFromScalar (fromInteger n)
*Main> mulQ q1 q2
Q (-56.0) 16.0 24.0 26.0
 
*Main> flip mulQ q1 q2
Q (-56.0) 18.0 20.0 28.0
 
main :: IO ()
*Main> imagQ q
main = do
[2.0,3.0,4.0]</pre>
let q, q1, q2 :: Quaternion Double
q = Q 1 2 3 4
q1 = Q 2 3 4 5
q2 = Q 3 4 5 6
print $ (Q 0 1 0 0) * (Q 0 0 1 0) * (Q 0 0 0 1) -- i*j*k; prints "Q (-1.0) 0.0 0.0 0.0"
print $ q1 * q2 -- prints "Q (-56.0) 16.0 24.0 26.0"
print $ q2 * q1 -- prints "Q (-56.0) 18.0 20.0 28.0"
print $ q1 * q2 == q2 * q1 -- prints "False"
print $ imagQ q -- prints "[2.0,3.0,4.0]"</syntaxhighlight>
 
==Icon and {{header|Unicon}}==
Line 2,911 ⟶ 4,214:
Using Unicon's class system.
 
<syntaxhighlight lang="unicon">
<lang Unicon>
class Quaternion(a, b, c, d)
 
Line 2,955 ⟶ 4,258:
self.d := if /d then 0 else d
end
</syntaxhighlight>
</lang>
 
To test the above:
 
<syntaxhighlight lang="unicon">
<lang Unicon>
procedure main ()
q := Quaternion (1,2,3,4)
Line 2,976 ⟶ 4,279:
write ("q2*q1 = " || q2.multiply(q1).string ())
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,990 ⟶ 4,293:
q2*q1 = -56+18i+20j+28k
</pre>
 
=={{header|Idris}}==
 
With [[wp:Dependent_type|dependent types]] we can implement the more general [[wp:Cayley-Dickson_construction|Cayley-Dickson construction]]. Here the dependent type <code>CD n a</code> is implemented. It depends on a natural number <code>n</code>, which is the number of iterations carried out, and the base type <code>a</code>. So the real numbers are just <code>CD 0 Double</code>, the complex numbers <code>CD 1 Double</code> and the quaternions <code>CD 2 Double</code>
 
<syntaxhighlight lang="idris">
module CayleyDickson
 
data CD : Nat -> Type -> Type where
CDBase : a -> CD 0 a
CDProd : CD n a -> CD n a -> CD (S n) a
 
pairTy : Nat -> Type -> Type
pairTy Z a = a
pairTy (S n) a = let b = pairTy n a in (b, b)
 
fromPair : (n : Nat) -> pairTy n a -> CD n a
fromPair Z x = CDBase x
fromPair (S m) (x, y) = CDProd (fromPair m x) $ fromPair m y
 
toPair : CD n a -> pairTy n a
toPair (CDBase x) = x
toPair (CDProd x v) = (toPair x, toPair v)
 
first : CD n a -> a
first (CDBase x) = x
first (CDProd x v) = first x
fromBase : Num a => (n : Nat) -> a -> CD n a
fromBase Z x = CDBase x
fromBase (S m) x = CDProd (fromBase m x) $ fromBase m 0
 
multSclr : Num a => CD n a -> a -> CD n a
multSclr (CDBase x) y = CDBase $ x * y
multSclr (CDProd x v) y = CDProd (multSclr x y) $ multSclr v y
 
divSclr : Fractional a => CD n a -> a -> CD n a
divSclr (CDBase x) y = CDBase $ x / y
divSclr (CDProd x v) y = CDProd (divSclr x y) $ divSclr v y
 
plusCD : Num a => CD n a -> CD n a -> CD n a
plusCD (CDBase x) (CDBase y) = CDBase $ x + y
plusCD (CDProd x v) (CDProd y w) = CDProd (plusCD x y) $ plusCD v w
 
negCD : Neg a => CD n a -> CD n a
negCD (CDBase x) = CDBase $ negate x
negCD (CDProd x v) = CDProd (negCD x) $ negCD v
 
minusCD : Neg a => CD n a -> CD n a -> CD n a
minusCD (CDBase x) (CDBase y) = CDBase $ x - y
minusCD (CDProd x v) (CDProd y w) = CDProd (minusCD x y) $ minusCD v w
 
conjCD : Neg a => CD n a -> CD n a
conjCD (CDBase x) = CDBase x
conjCD (CDProd x v) = CDProd (conjCD x) $ negCD v
 
multCD : Neg a => CD n a -> CD n a -> CD n a
multCD (CDBase x) (CDBase y) = CDBase $ x * y
multCD (CDProd x v) (CDProd y w) = CDProd (minusCD (multCD x y) (multCD (conjCD w) v)) $ plusCD (multCD w x) $ multCD v $ conjCD y
 
absSqrCD : Neg a => CD n a -> CD n a
absSqrCD x = multCD x $ conjCD x
 
sqrLnCD : Neg a => CD n a -> a
sqrLnCD = first . absSqrCD
 
recipCD : Neg a => Fractional a => CD n a -> CD n a
recipCD x = conjCD $ divSclr x $ sqrLnCD x
 
divCD : Neg a => Fractional a => CD n a -> CD n a -> CD n a
divCD x y = multCD x $ recipCD y
 
absCD : CD n Double -> Double
absCD x = sqrt $ sqrLnCD x
 
showComps : Show a => CD n a -> String
showComps (CDBase x) = show x
showComps (CDProd x v) = showComps x ++ ", " ++ showComps v
 
Eq a => Eq (CD n a) where
(CDBase x) == (CDBase y) = x == y
(CDProd x v) == (CDProd y w) = x == y && v == w
 
Show a => Show (CD n a) where
show x = "(" ++ showComps x ++ ")"
 
Neg a => Num (CD n a) where
(+) = plusCD
(*) = multCD
fromInteger m {n} = fromBase n $ fromInteger m
 
Neg a => Neg (CD n a) where
negate = negCD
(-) = minusCD
 
(Neg a, Fractional a) => Fractional (CD n a) where
(/) = divCD
recip = recipCD
 
Abs (CD n Double) where
abs {n} = fromBase n . absCD
</syntaxhighlight>
 
To test it:
 
<syntaxhighlight lang="idris">
import CayleyDickson
 
main : IO ()
main =
do
let q = fromPair 2 ((1, 2), (3, 4))
let q1 = fromPair 2 ((2, 3), (4, 5))
let q2 = fromPair 2 ((3, 4), (5, 6))
printLn $ q1 * q2
printLn $ q2 * q1
printLn $ q1 * q2 == q2 * q1
</syntaxhighlight>
 
=={{header|J}}==
Line 2,995 ⟶ 4,416:
Derived from the [[j:System/Requests/Quaternions|j wiki]]:
 
<langsyntaxhighlight lang="j"> NB. utilities
ip=: +/ .* NB. inner product
T=. (_1^#:0 10 9 12)*0 7 16 23 A.=i.4
Line 3,005 ⟶ 4,426:
conj=: 1 _1 _1 _1 * toQ NB. + y
add=: +&toQ NB. x + y
mul=: (ip T ip ])&toQ NB. x * y</langsyntaxhighlight>
 
T is a rank 3 tensor which allows us to express quaternion product ab as the inner product ATB if A and B are 4 element vectors representing the quaternions a and b. (Note also that once we have defined <code>mul</code> we no longer need to retain the definition of T, so we define T using =. instead of =:). The value of T is probably more interesting than its definition, so:
 
<langsyntaxhighlight Jlang="j"> T
1 0 0 0
0 1 0 0
Line 3,028 ⟶ 4,449:
0 0 _1 0
0 1 0 0
1 0 0 0</langsyntaxhighlight>
 
In other words, the last dimension of T corresponds to the structure of the right argument (columns, in the display of T), the first dimension of T corresponds to the structure of the left argument (tables, in the display of T) and the middle dimension of T corresponds to the structure of the result (rows, in the display of T).
Line 3,034 ⟶ 4,455:
Example use:
 
<syntaxhighlight lang="text"> q=: 1 2 3 4
q1=: 2 3 4 5
q2=: 3 4 5 6
Line 3,054 ⟶ 4,475:
_56 16 24 26
q2 mul q1
_56 18 20 28</langsyntaxhighlight>
 
Finally, note that when quaternions are used to represent [[wp:Quaternions_and_spatial_rotation|orientation or rotation]], we are typically only interested in unit length quaternions. As this is the typical application for quaternions, you will sometimes see quaternion multiplication expressed using "simplifications" which are only valid for unit length quaternions. But note also that in many of those contexts you also need to normalize the quaternion length after multiplication.
Line 3,061 ⟶ 4,482:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">public class Quaternion {
private final double a, b, c, d;
 
Line 3,163 ⟶ 4,584:
System.out.format("q1 \u00d7 q2 %s q2 \u00d7 q1%n", (q1q2.equals(q2q1) ? "=" : "\u2260"));
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,184 ⟶ 4,605:
Runs on Firefox 3+, limited support in other JS engines. More compatible JavaScript deserves its own entry.
 
<langsyntaxhighlight lang="javascript">var Quaternion = (function() {
// The Q() function takes an array argument and changes it
// prototype so that it becomes a Quaternion instance. This is
Line 3,241 ⟶ 4,662:
Quaternion.prototype = proto;
return Quaternion;
})();</langsyntaxhighlight>
 
Task/Example Usage:
 
<langsyntaxhighlight lang="javascript">var q = Quaternion(1,2,3,4);
var q1 = Quaternion(2,3,4,5);
var q2 = Quaternion(3,4,5,6);
Line 3,262 ⟶ 4,683:
console.log("7.a. q1.mul(q2) = "+q1.mul(q2));
console.log("7.b. q2.mul(q1) = "+q2.mul(q1));
console.log("8. q1.mul(q2) " + (q1.mul(q2).equals(q2.mul(q1)) ? "==" : "!=") + " q2.mul(q1)");</langsyntaxhighlight>
 
{{out}}
Line 3,281 ⟶ 4,702:
=={{header|jq}}==
 
Program file: quaternion.jq<langsyntaxhighlight lang="jq">def Quaternion(q0;q1;q2;q3): { "q0": q0, "q1": q1, "q2": q2, "q3": q3, "type": "Quaternion" };
 
# promotion of a real number to a quaternion
Line 3,372 ⟶ 4,793:
) ;
 
demo</langsyntaxhighlight>
Example usage and output:
<langsyntaxhighlight lang="sh"># jq -c -n -R -f quaternion.jq
Quaternion(1;0;0;0) => 1 + 0i + 0j + 0k
abs($q) => 5.477225575051661
Line 3,390 ⟶ 4,811:
times($q1;$q2) => -56 + 16i + 24j + 26k
times($q2; $q1) => -56 + 18i + 20j + 28k
times($q1; $q2) != times($q2; $q1) => true</langsyntaxhighlight>
 
=={{header|Julia}}==
https://github.com/andrioni/Quaternions.jl/blob/master/src/Quaternions.jl has a more complete implementation.
This is derived from the [https://github.com/JuliaLang/julia/blob/release-0.2/examples/quaternion.jl quaternion example file] included with Julia 0.2, which implements a quaternion type complete with arithmetic, type conversions / promotion rules, polymorphism over arbitrary real numeric types, and pretty-printing.
<langsyntaxhighlight lang="julia">import Base: convert, promote_rule, show, conj, abs, +, -, *
 
immutable Quaternion{T<:Real} <: Number
Line 3,435 ⟶ 4,856:
z.q0*w.q2 - z.q1*w.q3 + z.q2*w.q0 + z.q3*w.q1,
z.q0*w.q3 + z.q1*w.q2 - z.q2*w.q1 + z.q3*w.q0)
</syntaxhighlight>
</lang>
 
Example usage and output:
<langsyntaxhighlight lang="julia">julia> q = Quaternion(1,0,0,0)
julia> q = Quaternion (1, 2, 3, 4)
q1 = Quaternion(2, 3, 4, 5)
Line 3,463 ⟶ 4,884:
 
julia> q1*q2, q2*q1, q1*q2 != q2*q1
(-56 + 16i + 24j + 26k,-56 + 18i + 20j + 28k,true)</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.1.2
 
data class Quaternion(val a: Double, val b: Double, val c: Double, val d: Double) {
operator fun plus(other: Quaternion): Quaternion {
return Quaternion (this.a + other.a, this.b + other.b,
this.c + other.c, this.d + other.d)
}
 
operator fun plus(r: Double) = Quaternion(a + r, b, c, d)
 
operator fun times(other: Quaternion): Quaternion {
return Quaternion(
this.a * other.a - this.b * other.b - this.c * other.c - this.d * other.d,
this.a * other.b + this.b * other.a + this.c * other.d - this.d * other.c,
this.a * other.c - this.b * other.d + this.c * other.a + this.d * other.b,
this.a * other.d + this.b * other.c - this.c * other.b + this.d * other.a
)
}
 
operator fun times(r: Double) = Quaternion(a * r, b * r, c * r, d * r)
 
operator fun unaryMinus() = Quaternion(-a, -b, -c, -d)
 
fun conj() = Quaternion(a, -b, -c, -d)
 
fun norm() = Math.sqrt(a * a + b * b + c * c + d * d)
 
override fun toString() = "($a, $b, $c, $d)"
}
 
// extension functions for Double type
operator fun Double.plus(q: Quaternion) = q + this
operator fun Double.times(q: Quaternion) = q * this
 
fun main(args: Array<String>) {
val q = Quaternion(1.0, 2.0, 3.0, 4.0)
val q1 = Quaternion(2.0, 3.0, 4.0, 5.0)
val q2 = Quaternion(3.0, 4.0, 5.0, 6.0)
val r = 7.0
println("q = $q")
println("q1 = $q1")
println("q2 = $q2")
println("r = $r\n")
println("norm(q) = ${"%f".format(q.norm())}")
println("-q = ${-q}")
println("conj(q) = ${q.conj()}\n")
println("r + q = ${r + q}")
println("q + r = ${q + r}")
println("q1 + q2 = ${q1 + q2}\n")
println("r * q = ${r * q}")
println("q * r = ${q * r}")
val q3 = q1 * q2
val q4 = q2 * q1
println("q1 * q2 = $q3")
println("q2 * q1 = $q4\n")
println("q1 * q2 != q2 * q1 = ${q3 != q4}")
}</syntaxhighlight>
 
{{out}}
<pre>
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
 
norm(q) = 5.477226
-q = (-1.0, -2.0, -3.0, -4.0)
conj(q) = (1.0, -2.0, -3.0, -4.0)
 
r + q = (8.0, 2.0, 3.0, 4.0)
q + r = (8.0, 2.0, 3.0, 4.0)
q1 + q2 = (5.0, 7.0, 9.0, 11.0)
 
r * q = (7.0, 14.0, 21.0, 28.0)
q * r = (7.0, 14.0, 21.0, 28.0)
q1 * q2 = (-56.0, 16.0, 24.0, 26.0)
q2 * q1 = (-56.0, 18.0, 20.0, 28.0)
 
q1 * q2 != q2 * q1 = true
</pre>
 
=={{header|Liberty BASIC}}==
Quaternions saved as a space-separated string of four numbers.
<syntaxhighlight lang="lb">
<lang lb>
 
q$ = q$( 1 , 2 , 3 , 4 )
Line 3,580 ⟶ 5,083:
add2$ =q$( ar +br, ai +bi, aj +bj, ak +bk)
end function
</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">Quaternion = {}
 
function Quaternion.new( a, b, c, d )
Line 3,645 ⟶ 5,148:
function Quaternion.print( p )
print( string.format( "%f + %fi + %fj + %fk\n", p.a, p.b, p.c, p.d ) )
end</langsyntaxhighlight>
Examples:
<langsyntaxhighlight lang="lua">q1 = Quaternion.new( 1, 2, 3, 4 )
q2 = Quaternion.new( 5, 6, 7, 8 )
r = 12
Line 3,659 ⟶ 5,162:
io.write( "q1*r = " ); Quaternion.print( q1*r )
io.write( "q1*q2 = " ); Quaternion.print( q1*q2 )
io.write( "q2*q1 = " ); Quaternion.print( q2*q1 )</syntaxhighlight>
 
{{out}}
<pre>norm(q1) = 5.4772255750517
-q1 = -1.000000 -2.000000i -3.000000j -4.000000k
conj(q1) = 1.000000 -2.000000i -3.000000j -4.000000k
Line 3,669 ⟶ 5,173:
q1*r = 12.000000 + 24.000000i + 36.000000j + 48.000000k
q1*q2 = -60.000000 + 12.000000i + 30.000000j + 24.000000k
q2*q1 = -60.000000 + 20.000000i + 14.000000j + 32.000000k</langpre>
 
=={{header|M2000 Interpreter}}==
We can define Quaternions using a class, using operators for specific tasks, as negate, add, multiplication and equality with rounding to 13 decimal place (thats what doing "==" operator for doubles)
<syntaxhighlight lang="m2000 interpreter">
Module CheckIt {
class Quaternion {
\\ by default are double
a,b,c,d
Property ToString$ {
Value {
link parent a,b,c, d to a,b,c,d
value$=format$("{0} + {1}i + {2}j + {3}k",a,b,c,d)
}
}
Property Norm { Value}
Operator "==" {
read n
push .a==n.a and .b==n.b and .c==n.c and .d==n.d
}
Module CalcNorm {
.[Norm]<=sqrt(.a**2+.b**2+.c**2+.d**2)
}
Operator Unary {
.a-! : .b-! : .c-! :.d-!
}
Function Conj {
q=this
for q {
.b-! : .c-! :.d-!
}
=q
}
Function Add {
q=this
for q {
.a+=Number : .CalcNorm
}
=q
}
Operator "+" {
Read q2
For this, q2 {
.a+=..a :.b+=..b:.c+=..c:.d+=..d
.CalcNorm
}
}
Function Mul(r) {
q=this
for q {
.a*=r:.b*=r:.c*=r:.d*=r:.CalcNorm
}
=q
}
Operator "*" {
Read q2
For This, q2 {
Push .a*..a-.b*..b-.c*..c-.d*..d
Push .a*..b+.b*..a+.c*..d-.d*..c
Push .a*..c-.b*..d+.c*..a+.d*..b
.d<=.a*..d+.b*..c-.c*..b+.d*..a
Read .c, .b, .a
.CalcNorm
}
}
class:
module Quaternion {
if match("NNNN") then {
Read .a,.b,.c,.d
.CalcNorm
}
}
}
\\ variables
r=7
q=Quaternion(1,2,3,4)
q1=Quaternion(2,3,4,5)
q2=Quaternion(3,4,5,6)
\\ perform negate, conjugate, multiply by real, add a real, multiply quanterions, multiply in reverse order
qneg=-q
qconj=q.conj()
qmul=q.Mul(r)
qadd=q.Add(r)
q1q2=q1*q2
q2q1=q2*q1
Print "q = ";q.ToString$
Print "Normal q = ";q.Norm
Print "Neg q = ";qneg.ToString$
Print "Conj q = ";qconj.ToString$
Print "Mul q 7 = ";qmul.ToString$
Print "Add q 7 = ";qadd.ToString$
Print "q1 = ";q1.ToString$
Print "q2 = ";q2.ToString$
Print "q1 * q2 = ";q1q2.ToString$
Print "q2 * q1 = ";q2q1.ToString$
Print q1==q1 ' true
Print q1q2==q2q1 ' false
\\ multiplication and equality in one expression
Print q1 * q2 == q2 * q1 ' false
Print q1 * q2 == q1 * q2 ' true
}
CheckIt
</syntaxhighlight>
{{out}}
<pre>
q = 1 + 2i + 3j + 4k
Normal q = 5.47722557505166
Neg q = -1 + -2i + -3j + -4k
Conj q = 1 + -2i + -3j + -4k
Mul q 7 = 7 + 14i + 21j + 28k
Add q 7 = 8 + 2i + 3j + 4k
q1 = 2 + 3i + 4j + 5k
q2 = 3 + 4i + 5j + 6k
q1 * q2 = -56 + 16i + 24j + 26k
q2 * q1 = -56 + 18i + 20j + 28k
True
False
false
True</pre>
 
=={{header|Maple}}==
<syntaxhighlight lang="maple">
with(ArrayTools);
 
module Quaternion()
option object;
local real := 0;
local i := 0;
local j := 0;
local k := 0;
 
export getReal::static := proc(self::Quaternion, $)
return self:-real;
end proc;
 
export getI::static := proc(self::Quaternion, $)
return self:-i;
end proc;
 
export getJ::static := proc(self::Quaternion, $)
return self:-j;
end proc;
 
export getK::static := proc(self::Quaternion, $)
return self:-k;
end proc;
 
export Norm::static := proc(self::Quaternion, $)
return sqrt(self:-real^2 + self:-i^2 + self:-j^2 + self:-k^2);
end proc;
 
# NegativeQuaternion returns the additive inverse of the quaternion
export NegativeQuaternion::static := proc(self::Quaternion, $)
return Quaternion(- self:-real, - self:-i, - self:-j, - self:-k);
end proc;
 
export Conjugate::static := proc(self::Quaternion, $)
return Quaternion(self:-real, - self:-i, - self:-j, - self:-k);
end proc;
 
# quaternion addition
export `+`::static := overload ([
proc(self::Quaternion, x::Quaternion) option overload;
return Quaternion(self:-real + getReal(x), self:-i + getI(x), self:-j + getJ(x), self:-k + getK(x));
end proc,
proc(self::Quaternion, x::algebraic) option overload;
return Quaternion(self:-real + x, self:-i, self:-j, self:-k);
end proc,
proc(x::algebraic, self::Quaternion) option overload;
return Quaternion(x + self:-real, self:-i, self:-j, self:-k);
end
]);
 
# convert quaternion to additive inverse
export `-`::static := overload([
proc(self::Quaternion) option overload;
return Quaternion(-self:-real, -self:-i, -self:-j, -self:-k);
end
]);
 
# quaternion multiplication is non-abelian so the `.` operator needs to be used
export `.`::static := overload([
proc(self::Quaternion, x::Quaternion) option overload;
return Quaternion(self:-real * getReal(x) - self:-i * getI(x) - self:-j * getJ(x) - self:-k * getK(x),
self:-real * getI(x) + self:-i * getReal(x) + self:-j * getK(x) - self:-k * getJ(x),
self:-real * getJ(x) + self:-j * getReal(x) - self:-i * getK(x) + self:-k * getI(x),
self:-real * getK(x) + self:-k * getReal(x) + self:-i * getJ(x) - self:-j * getI(x));
end proc,
proc(self::Quaternion, x::algebraic) option overload;
return Quaternion(self:-real * x, self:-i * x, self:-j * x, self:-k * x);
end proc,
proc(x::algebraic, self::Quaternion) option overload;
return Quaternion(self:-real * x, self:-i * x, self:-j * x, self:-k * x);
end
]);
 
# redirect division to `.` operator
export `*`::static := overload([
proc(self::Quaternion, x::Quaternion) option overload;
use `*` = `.` in return self * x; end use
end proc,
proc(self::Quaternion, x::algebraic) option overload;
use `*` = `.` in return x * self; end use
end proc,
proc(x::algebraic, self::Quaternion) option overload;
use `*` = `.` in return x * self; end use
end
]);
 
# convert quaternion to multiplicative inverse
export `/`::static := overload([
proc(self::Quaternion) option overload;
return Conjugate(self) . (1/(Norm(self)^2));
end proc
]);
 
# QuaternionCommutator computes the commutator of self and x
export QuaternionCommutator::static := proc(x::Quaternion, y::Quaternion, $)
return (x . y) - (y . x);
end proc;
 
# display quaternion
export ModulePrint::static := proc(self::Quaternion, $);
return cat(self:-real, " + ", self:-i, "i + ", self:-j, "j + ", self:-k, "k"):
end proc;
 
export ModuleApply::static := proc()
Object(Quaternion, _passed);
end proc;
 
export ModuleCopy::static := proc(new::Quaternion, proto::Quaternion, R::algebraic, imag::algebraic, J::algebraic, K::algebraic, $)
new:-real := R;
new:-i := imag;
new:-j := J;
new:-k := K;
end proc;
end module:
 
q := Quaternion(1, 2, 3, 4):
q1 := Quaternion(2, 3, 4, 5):
q2 := Quaternion(3, 4, 5, 6):
r := 7:
 
quats := Array([q, q1, q2]):
print("q, q1, q2"):
seq(quats[i], i = 1..3);
print("norms"):
seq(Norm(quats[i]), i = 1..3);
print("negative"):
seq(NegativeQuaternion(quats[i]), i = 1..3);
print("conjugate"):
seq(Conjugate(quats[i]), i = 1..3);
print("addition of real number 7"):
seq(quats[i] + r, i = 1..3);
print("multiplication by real number 7"):
seq(quats[i] . r, i = 1..3);
print("division by real number 7"):
seq(quats[i] / 7, i = 1..3);
print("add quaternions q1 and q2"):
q1 + q2;
print("multiply quaternions q1 and q2");
q1 . q2;
print("multiply quaternions q2 and q1"):
q2 . q1;
print("quaternion commutator of q1 and q2"):
QuaternionCommutator(q1,q2);
print("divide q1 by q2"):
q1 / q2;
</syntaxhighlight>
{{out}}<pre>
"q, q1, q2"
 
1 + 2i + 3j + 4k, 2 + 3i + 4j + 5k, 3 + 4i + 5j + 6k
 
"norms"
 
1/2 1/2 1/2
30 , 3 6 , 86
 
"negative"
 
-1 + -2i + -3j + -4k, -2 + -3i + -4j + -5k, -3 + -4i + -5j + -6k
 
"conjugate"
 
1 + -2i + -3j + -4k, 2 + -3i + -4j + -5k, 3 + -4i + -5j + -6k
 
"addition of real number 7"
 
8 + 2i + 3j + 4k, 9 + 3i + 4j + 5k, 10 + 4i + 5j + 6k
 
"multiplication by real number 7"
 
7 + 14i + 21j + 28k, 14 + 21i + 28j + 35k, 21 + 28i + 35j + 42k
 
"division by real number 7"
 
1/7 + 2/7i + 3/7j + 4/7k, 2/7 + 3/7i + 4/7j + 5/7k, 3/7 + 4/7i + 5/7j + 6/7k
 
"add quaternions q1 and q2"
 
5 + 7i + 9j + 11k
 
"multiply quaternions q1 and q2"
 
-56 + 16i + 24j + 26k
 
"multiply quaternions q2 and q1"
 
-56 + 18i + 20j + 28k
 
"quaternion commutator of q1 and q2"
 
0 + -2i + 4j + -2k
 
"divide q1 by q2"
 
34/43 + 1/43i + 0j + 2/43k
 
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica"><<Quaternions`
q=Quaternion[1,2,3,4]
q1=Quaternion[2,3,4,5]
Line 3,702 ⟶ 5,527:
q2**q1
->Quaternion[-56,18,20,28]
</syntaxhighlight>
</lang>
 
=={{header|Mercury}}==
Line 3,708 ⟶ 5,533:
A possible implementation of quaternions in Mercury (the simplest representation) would look like this. Note that this is a full module implementation, complete with boilerplate, and that it works by giving an explicit conversion function for floats, converting a float into a quaternion representation of that float. Thus the float value <code>7.0</code> gets turned into the quaternion representation <code>q(7.0, 0.0, 0.0, 0.0)</code> through the function call <code>r(7.0)</code>.
 
<langsyntaxhighlight Mercurylang="mercury">:- module quaternion.
 
:- interface.
Line 3,745 ⟶ 5,570:
W0*I1 + I0*W1 + J0*K1 - K0*J1,
W0*J1 - I0*K1 + J0*W1 + K0*I1,
W0*K1 + I0*J1 - J0*I1 + K0*W1 ).</langsyntaxhighlight>
 
The following test module puts the module through its paces.
 
<langsyntaxhighlight Mercurylang="mercury">:- module test_quaternion.
 
:- interface.
Line 3,824 ⟶ 5,649:
to_string(q(I, J, K, W)) = string.format("q(%f, %f, %f, %f)",
[f(I), f(J), f(K), f(W)]).
:- end_module test_quaternion.</langsyntaxhighlight>
 
The output of the above code follows:
Line 3,862 ⟶ 5,687:
Q1 * Q2 = q(-56.000000, 16.000000, 24.000000, 26.000000)
Q2 * Q1 = q(-56.000000, 18.000000, 20.000000, 28.000000)
 
=={{header|Nim}}==
 
For simplicity, we have limited the type of quaternion fields to floats (i.e. float64). An implementation could use a generic type in order to allow other field types such as float32.
 
<syntaxhighlight lang="nim">import math, tables
 
type Quaternion* = object
a, b, c, d: float
 
func initQuaternion*(a, b, c, d = 0.0): Quaternion =
Quaternion(a: a, b: b, c: c, d: d)
 
func `-`*(q: Quaternion): Quaternion =
initQuaternion(-q.a, -q.b, -q.c, -q.d)
 
func `+`*(q: Quaternion; r: float): Quaternion =
initQuaternion(q.a + r, q.b, q.c, q.d)
 
func `+`*(r: float; q: Quaternion): Quaternion =
initQuaternion(q.a + r, q.b, q.c, q.d)
 
func `+`*(q1, q2: Quaternion): Quaternion =
initQuaternion(q1.a + q2.a, q1.b + q2.b, q1.c + q2.c, q1.d + q2.d)
 
func `*`*(q: Quaternion; r: float): Quaternion =
initQuaternion(q.a * r, q.b * r, q.c * r, q.d * r)
 
func `*`*(r: float; q: Quaternion): Quaternion =
initQuaternion(q.a * r, q.b * r, q.c * r, q.d * r)
 
func `*`*(q1, q2: Quaternion): Quaternion =
initQuaternion(q1.a * q2.a - q1.b * q2.b - q1.c * q2.c - q1.d * q2.d,
q1.a * q2.b + q1.b * q2.a + q1.c * q2.d - q1.d * q2.c,
q1.a * q2.c - q1.b * q2.d + q1.c * q2.a + q1.d * q2.b,
q1.a * q2.d + q1.b * q2.c - q1.c * q2.b + q1.d * q2.a)
 
func conjugate*(q: Quaternion): Quaternion =
initQuaternion(q.a, -q.b, -q.c, -q.d)
 
func norm*(q: Quaternion): float =
sqrt(q.a * q.a + q.b * q.b + q.c * q.c + q.d * q.d)
 
func `==`*(q: Quaternion; r: float): bool =
if q.b != 0 or q.c != 0 or q.d != 0: false
else: q.a == r
 
func `$`(q: Quaternion): string =
## Return the representation of a quaternion.
const Letter = {"a": "", "b": "i", "c": "j", "d": "k"}.toTable
if q == 0: return "0"
for name, value in q.fieldPairs:
if value != 0:
var val = value
if result.len != 0:
result.add if value >= 0: '+' else: '-'
val = abs(val)
result.add $val & Letter[name]
 
 
when isMainModule:
let
q = initQuaternion(1, 2, 3, 4)
q1 = initQuaternion(2, 3, 4, 5)
q2 = initQuaternion(3, 4, 5, 6)
r = 7.0
 
echo "∥q∥ = ", norm(q)
echo "-q = ", -q
echo "q* = ", conjugate(q)
echo "q + r = ", q + r
echo "r + q = ", r + q
echo "q1 + q2 = ", q1 + q2
echo "qr = ", q * r
echo "rq = ", r * q
echo "q1 * q2 = ", q1 * q2
echo "q2 * q1 = ", q2 * q1</syntaxhighlight>
 
{{out}}
<pre>∥q∥ = 5.477225575051661
-q = -1.0-2.0i-3.0j-4.0k
q* = 1.0-2.0i-3.0j-4.0k
q + r = 8.0+2.0i+3.0j+4.0k
r + q = 8.0+2.0i+3.0j+4.0k
q1 + q2 = 5.0+7.0i+9.0j+11.0k
qr = 7.0+14.0i+21.0j+28.0k
rq = 7.0+14.0i+21.0j+28.0k
q1 * q2 = -56.0+16.0i+24.0j+26.0k
q2 * q1 = -56.0+18.0i+20.0j+28.0k</pre>
 
As can be seen, <code>q1 * q2 != q2 * q1</code>.
 
=={{header|OCaml}}==
 
This implementation was build strictly to the specs without looking (too much) at other implementations. The implementation as a record type with only floats is said (on the ocaml mailing list) to be especially efficient. Put this into a file quaternion.ml:
<langsyntaxhighlight lang="ocaml">
type quaternion = {a: float; b: float; c: float; d: float}
 
Line 3,931 ⟶ 5,847:
pf "8. instead q2 * q1 = %s \n" (qstring (multq q2 q1));
pf "\n";
</syntaxhighlight>
</lang>
 
using this file on the command line will produce:
Line 3,947 ⟶ 5,863:
</pre>
For completeness, and since data types are of utmost importance in OCaml, here the types produced by pasting the code into the toplevel (''ocaml'' is the toplevel):
<langsyntaxhighlight lang="ocaml">
type quaternion = { a : float; b : float; c : float; d : float; }
val norm : quaternion -> float = <fun>
Line 3,959 ⟶ 5,875:
val qmake : float -> float -> float -> float -> quaternion = <fun>
val qstring : quaternion -> string = <fun>
</syntaxhighlight>
</lang>
 
=={{header|Octave}}==
Line 3,966 ⟶ 5,882:
Such a package can be install with the command:
 
<syntaxhighlight lang="text">pkg install -forge quaternion</langsyntaxhighlight>
 
Here is a sample interactive session solving the task:
 
<syntaxhighlight lang="text">> q = quaternion (1, 2, 3, 4)
q = 1 + 2i + 3j + 4k
> q1 = quaternion (2, 3, 4, 5)
Line 3,993 ⟶ 5,909:
ans = -56 + 16i + 24j + 26k
> q1 == q2
ans = 0</langsyntaxhighlight>
 
 
=={{header|Oforth}}==
Line 4,000 ⟶ 5,915:
neg is defined as "0 self -" into Number class, so no need to define it (if #- is defined).
 
<langsyntaxhighlight Oforthlang="oforth">160 Number Class newPriority: Quaternion(a, b, c, d)
 
Quaternion method: _a @a ;
Line 4,023 ⟶ 5,938:
q _a @b * q _b @a * + q _c @d * + q _d @c * -,
q _a @c * q _b @d * - q _c @a * + q _d @b * +,
q _a @d * q _b @c * + q _c @b * - q _d @a * + ) ;</langsyntaxhighlight>
 
Usage :
 
<langsyntaxhighlight Oforthlang="oforth">: test
| q q1 q2 r |
 
Line 4,046 ⟶ 5,961:
System.Out "q * r = " << q r * << cr
System.Out "q1 * q2 = " << q1 q2 * << cr
q1 q2 * q2 q1 * == ifFalse: [ "q1q2 and q2q1 are different quaternions" println ] ;</langsyntaxhighlight>
 
{{out}}
Line 4,062 ⟶ 5,977:
q1q2 and q2q1 are different quaternions
</pre>
 
=={{header|Ol}}==
 
See also [[#Scheme|the entry for Scheme]].
 
<syntaxhighlight lang="scheme">
;;
;; This program is written to run without modification both in Otus
;; Lisp and in any of many Scheme dialects. I assume the presence of
;; "case-lambda", but not of "let-values". The program has worked
;; (without modification) in Otus Lisp 2.4, Guile >= 2.0 (but not in
;; Guile version 1.8), CHICKEN Scheme 5.3.0, Chez Scheme 9.5.8, Gauche
;; Scheme 0.9.12, Ypsilon 0.9.6-update3.
;;
;; Here a quaternion is represented as a linked list of four real
;; numbers. Such a representation probably has the greatest
;; portability between Scheme dialects. However, this representation
;; can be replaced, simply by redefining the procedures "quaternion?",
;; "quaternion-components", "quaternion->list", and "quaternion".
;;
 
(define (quaternion? q) ; Can q be used as a quaternion?
(and (pair? q)
(let ((a (car q))
(q (cdr q)))
(and (real? a) (pair? q)
(let ((b (car q))
(q (cdr q)))
(and (real? b) (pair? q)
(let ((c (car q))
(q (cdr q)))
(and (real? c) (pair? q)
(let ((d (car q))
(q (cdr q)))
(and (real? d) (null? q)))))))))))
 
(define (quaternion-components q) ; Extract the basis components.
(let ((a (car q))
(q (cdr q)))
(let ((b (car q))
(q (cdr q)))
(let ((c (car q))
(q (cdr q)))
(let ((d (car q)))
(values a b c d))))))
 
(define (quaternion->list q) ; Get a list of the basis components.
q)
 
(define quaternion ; Make a quaternion.
(case-lambda
((a b c d)
;; Make the quaternion from basis components.
(list a b c d))
((q)
;; Make the quaternion from a scalar or from another quaternion.
;; WARNING: in the latter case, the quaternion is NOT
;; copied. This is not a problem, if you avoid things like
;; "set-car!" and "set-cdr!".
(if (real? q)
(list q 0 0 0)
q))))
 
(define (quaternion-norm q) ; The euclidean norm of a quaternion.
(let ((q (quaternion q)))
(call-with-values (lambda () (quaternion-components q))
(lambda (a b c d)
(sqrt (+ (* a a) (* b b) (* c c) (* d d)))))))
 
(define (quaternion-conjugate q) ; Conjugate a quaternion.
(let ((q (quaternion q)))
(call-with-values (lambda () (quaternion-components q))
(lambda (a b c d)
(quaternion a (- b) (- c) (- d))))))
 
(define quaternion+ ; Add quaternions.
(let ((quaternion-add
(lambda (q1 q2)
(let ((q1 (quaternion q1))
(q2 (quaternion q2)))
(call-with-values
(lambda () (quaternion-components q1))
(lambda (a1 b1 c1 d1)
(call-with-values
(lambda () (quaternion-components q2))
(lambda (a2 b2 c2 d2)
(quaternion (+ a1 a2) (+ b1 b2)
(+ c1 c2) (+ d1 d2))))))))))
(case-lambda
(() (quaternion 0))
((q . q*)
(let loop ((accum q)
(q* q*))
(if (pair? q*)
(loop (quaternion-add accum (car q*)) (cdr q*))
accum))))))
 
(define quaternion- ; Negate or subtract quaternions.
(let ((quaternion-sub
(lambda (q1 q2)
(let ((q1 (quaternion q1))
(q2 (quaternion q2)))
(call-with-values
(lambda () (quaternion-components q1))
(lambda (a1 b1 c1 d1)
(call-with-values
(lambda () (quaternion-components q2))
(lambda (a2 b2 c2 d2)
(quaternion (- a1 a2) (- b1 b2)
(- c1 c2) (- d1 d2))))))))))
(case-lambda
((q)
(let ((q (quaternion q)))
(call-with-values (lambda () (quaternion-components q))
(lambda (a b c d)
(quaternion (- a) (- b) (- c) (- d))))))
((q . q*)
(let loop ((accum q)
(q* q*))
(if (pair? q*)
(loop (quaternion-sub accum (car q*)) (cdr q*))
accum))))))
 
(define quaternion* ; Multiply quaternions.
(let ((quaternion-mul
(lambda (q1 q2)
(let ((q1 (quaternion q1))
(q2 (quaternion q2)))
(call-with-values
(lambda () (quaternion-components q1))
(lambda (a1 b1 c1 d1)
(call-with-values
(lambda () (quaternion-components q2))
(lambda (a2 b2 c2 d2)
(quaternion (- (* a1 a2) (* b1 b2)
(* c1 c2) (* d1 d2))
(- (+ (* a1 b2) (* b1 a2) (* c1 d2))
(* d1 c2))
(- (+ (* a1 c2) (* c1 a2) (* d1 b2))
(* b1 d2))
(- (+ (* a1 d2) (* b1 c2) (* d1 a2))
(* c1 b2)))))))))))
(case-lambda
(() (quaternion 1))
((q . q*)
(let loop ((accum q)
(q* q*))
(if (pair? q*)
(loop (quaternion-mul accum (car q*)) (cdr q*))
accum))))))
 
(define quaternion=? ; Are the quaternions equal?
(let ((=? (lambda (q1 q2)
(let ((q1 (quaternion q1))
(q2 (quaternion q2)))
(call-with-values
(lambda () (quaternion-components q1))
(lambda (a1 b1 c1 d1)
(call-with-values
(lambda () (quaternion-components q2))
(lambda (a2 b2 c2 d2)
(and (= a1 a2) (= b1 b2)
(= c1 c2) (= d1 d2))))))))))
(lambda (q . q*)
(let loop ((q* q*))
(if (pair? q*)
(and (=? q (car q*))
(loop (cdr q*)))
#t)))))
 
(define q (quaternion 1 2 3 4))
(define q1 (quaternion 2 3 4 5))
(define q2 (quaternion 3 4 5 6))
(define r 7)
 
(display "q = ") (display (quaternion->list q)) (newline)
(display "q1 = ") (display (quaternion->list q1)) (newline)
(display "q2 = ") (display (quaternion->list q2)) (newline)
(display "r = ") (display r) (newline)
(newline)
(display "(quaternion? q) = ") (display (quaternion? q)) (newline)
(display "(quaternion? q1) = ") (display (quaternion? q1)) (newline)
(display "(quaternion? q2) = ") (display (quaternion? q2)) (newline)
(display "(quaternion? r) = ") (display (quaternion? r)) (newline)
(newline)
(display "(quaternion-norm q) = ")
(display (quaternion-norm q)) (newline)
(display "(quaternion-norm q1) = ")
(display (quaternion-norm q1)) (newline)
(display "(quaternion-norm q2) = ")
(display (quaternion-norm q2)) (newline)
(newline)
(display "(quaternion- q) = ")
(display (quaternion->list (quaternion- q))) (newline)
(display "(quaternion- q1 q2) = ")
(display (quaternion->list (quaternion- q1 q2))) (newline)
(display "(quaternion- q q1 q2) = ")
(display (quaternion->list (quaternion- q q1 q2))) (newline)
(newline)
(display "(quaternion-conjugate q) = ")
(display (quaternion->list (quaternion-conjugate q))) (newline)
(newline)
(display "(quaternion+) = ")
(display (quaternion->list (quaternion+))) (newline)
(display "(quaternion+ q) = ")
(display (quaternion->list (quaternion+ q))) (newline)
(display "(quaternion+ r q) = ")
(display (quaternion->list (quaternion+ r q))) (newline)
(display "(quaternion+ q r) = ")
(display (quaternion->list (quaternion+ q r))) (newline)
(display "(quaternion+ q1 q2) = ")
(display (quaternion->list (quaternion+ q1 q2))) (newline)
(display "(quaternion+ q q1 q2) = ")
(display (quaternion->list (quaternion+ q q1 q2))) (newline)
(newline)
(display "(quaternion*) = ")
(display (quaternion->list (quaternion*))) (newline)
(display "(quaternion* q) = ")
(display (quaternion->list (quaternion* q))) (newline)
(display "(quaternion* r q) = ")
(display (quaternion->list (quaternion* r q))) (newline)
(display "(quaternion* q r) = ")
(display (quaternion->list (quaternion* q r))) (newline)
(display "(quaternion* q1 q2) = ")
(display (quaternion->list (quaternion* q1 q2))) (newline)
(display "(quaternion* q q1 q2) = ")
(display (quaternion->list (quaternion* q q1 q2))) (newline)
(newline)
(display "(quaternion=? q) = ")
(display (quaternion=? q)) (newline)
(display "(quaternion=? q q) = ")
(display (quaternion=? q q)) (newline)
(display "(quaternion=? q1 q2) = ")
(display (quaternion=? q1 q2)) (newline)
(display "(quaternion=? q q q) = ")
(display (quaternion=? q q q)) (newline)
(display "(quaternion=? q1 q1 q2) = ")
(display (quaternion=? q1 q1 q2)) (newline)
(newline)
(display "(quaternion* q1 q2) = ")
(display (quaternion->list (quaternion* q1 q2))) (newline)
(display "(quaternion* q2 q1) = ")
(display (quaternion->list (quaternion* q2 q1))) (newline)
(display "(quaternion=? (quaternion* q1 q2)") (newline)
(display " (quaternion* q2 q1)) = ")
(display (quaternion=? (quaternion* q1 q2)
(quaternion* q2 q1))) (newline)
</syntaxhighlight>
 
{{out}}
<pre>$ ol quaternions_task.scm
q = (1 2 3 4)
q1 = (2 3 4 5)
q2 = (3 4 5 6)
r = 7
 
(quaternion? q) = #true
(quaternion? q1) = #true
(quaternion? q2) = #true
(quaternion? r) = #false
 
(quaternion-norm q) = 116161/21208
(quaternion-norm q1) = 898285873/122241224
(quaternion-norm q2) = 6216793393/670374072
 
(quaternion- q) = (-1 -2 -3 -4)
(quaternion- q1 q2) = (-1 -1 -1 -1)
(quaternion- q q1 q2) = (-4 -5 -6 -7)
 
(quaternion-conjugate q) = (1 -2 -3 -4)
 
(quaternion+) = (0 0 0 0)
(quaternion+ q) = (1 2 3 4)
(quaternion+ r q) = (8 2 3 4)
(quaternion+ q r) = (8 2 3 4)
(quaternion+ q1 q2) = (5 7 9 11)
(quaternion+ q q1 q2) = (6 9 12 15)
 
(quaternion*) = (1 0 0 0)
(quaternion* q) = (1 2 3 4)
(quaternion* r q) = (7 14 21 28)
(quaternion* q r) = (7 14 21 28)
(quaternion* q1 q2) = (-56 16 24 26)
(quaternion* q q1 q2) = (-264 -114 -132 -198)
 
(quaternion=? q) = #true
(quaternion=? q q) = #true
(quaternion=? q1 q2) = #false
(quaternion=? q q q) = #true
(quaternion=? q1 q1 q2) = #false
 
(quaternion* q1 q2) = (-56 16 24 26)
(quaternion* q2 q1) = (-56 18 20 28)
(quaternion=? (quaternion* q1 q2)
(quaternion* q2 q1)) = #false</pre>
 
=={{header|ooRexx}}==
Note, this example uses operator overloads to perform the math operation. The operator overloads only work if the left-hand-side of the operation is a quaterion instance. Thus something like "7 + q1" would not work because this would get passed to the "+" of the string class. For those situations, the best solution would be an addition method on the .Quaternion class itself that took the appropriate action. I've chosen not to implement those to keep the example shorter.
<syntaxhighlight lang="oorexx">
<lang ooRexx>
q = .quaternion~new(1, 2, 3, 4)
q1 = .quaternion~new(2, 3, 4, 5)
Line 4,217 ⟶ 6,427:
::requires rxmath LIBRARY
 
</syntaxhighlight>
</lang>
{{out}}
 
<pre>
q = 1 + 2i + 3j + 4k
Line 4,239 ⟶ 6,449:
{{works with|PARI/GP|version 2.4.2 and above}}<!-- Needs closures -->
Here is a simple solution in GP. I think it's possible to implement this type directly in Pari by abusing t_COMPLEX, but I haven't attempted this.
<langsyntaxhighlight lang="parigp">q.norm={
if(type(q) != "t_VEC" || #q != 4, error("incorrect type"));
sqrt(q[1]^2+q[2]^2+q[3]^2+q[4]^2)
Line 4,273 ⟶ 6,483:
)
)
};</langsyntaxhighlight>
Usage:
<langsyntaxhighlight lang="parigp">r=7;q=[1,2,3,4];q1=[2,3,4,5];q2=[3,4,5,6];
q.norm
-q
Line 4,284 ⟶ 6,494:
q.mult(r) \\ or r*q or q*r
q1.mult(q2)
q1.mult(q2) != q2.mult(q1)</langsyntaxhighlight>
 
=={{header|Pascal}}==
Line 4,290 ⟶ 6,500:
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">package Quaternion;
use List::Util 'reduce';
use List::MoreUtils 'pairwise';
Line 4,359 ⟶ 6,569:
print "a conjugate is ", $a->conjugate, "\n";
print "a * b = ", $a * $b, "\n";
print "b * a = ", $b * $a, "\n";</langsyntaxhighlight>
 
=={{header|Perl 6}}==
<lang perl6>class Quaternion {
has Real ( $.r, $.i, $.j, $.k );
multi method new ( Real $r, Real $i, Real $j, Real $k ) {
self.bless: :$r, :$i, :$j, :$k;
}
multi qu(*@r) is export { Quaternion.new: |@r }
sub postfix:<j>(Real $x) is export { qu 0, 0, $x, 0 }
sub postfix:<k>(Real $x) is export { qu 0, 0, 0, $x }
method Str () { "$.r + {$.i}i + {$.j}j + {$.k}k" }
method reals () { $.r, $.i, $.j, $.k }
method conj () { qu $.r, -$.i, -$.j, -$.k }
method norm () { sqrt [+] self.reals X** 2 }
multi infix:<eqv> ( Quaternion $a, Quaternion $b ) is export { $a.reals eqv $b.reals }
multi infix:<+> ( Quaternion $a, Real $b ) is export { qu $b+$a.r, $a.i, $a.j, $a.k }
multi infix:<+> ( Real $a, Quaternion $b ) is export { qu $a+$b.r, $b.i, $b.j, $b.k }
multi infix:<+> ( Quaternion $a, Complex $b ) is export { qu $b.re + $a.r, $b.im + $a.i, $a.j, $a.k }
multi infix:<+> ( Complex $a, Quaternion $b ) is export { qu $a.re + $b.r, $a.im + $b.i, $b.j, $b.k }
multi infix:<+> ( Quaternion $a, Quaternion $b ) is export { qu $a.reals Z+ $b.reals }
multi prefix:<-> ( Quaternion $a ) is export { qu $a.reals X* -1 }
multi infix:<*> ( Quaternion $a, Real $b ) is export { qu $a.reals X* $b }
multi infix:<*> ( Real $a, Quaternion $b ) is export { qu $b.reals X* $a }
multi infix:<*> ( Quaternion $a, Complex $b ) is export { $a * qu $b.reals, 0, 0 }
multi infix:<*> ( Complex $a, Quaternion $b ) is export { $b R* qu $a.reals, 0, 0 }
multi infix:<*> ( Quaternion $a, Quaternion $b ) is export {
my @a_rijk = $a.reals;
my ( $r, $i, $j, $k ) = $b.reals;
return qu [+]( @a_rijk Z* $r, -$i, -$j, -$k ), # real
[+]( @a_rijk Z* $i, $r, $k, -$j ), # i
[+]( @a_rijk Z* $j, -$k, $r, $i ), # j
[+]( @a_rijk Z* $k, $j, -$i, $r ); # k
}
}
import Quaternion;
my $q = 1 + 2i + 3j + 4k;
my $q1 = 2 + 3i + 4j + 5k;
my $q2 = 3 + 4i + 5j + 6k;
my $r = 7;
say "1) q norm = {$q.norm}";
say "2) -q = {-$q}";
say "3) q conj = {$q.conj}";
say "4) q + r = {$q + $r}";
say "5) q1 + q2 = {$q1 + $q2}";
say "6) q * r = {$q * $r}";
say "7) q1 * q2 = {$q1 * $q2}";
say "8) q1q2 { $q1 * $q2 eqv $q2 * $q1 ?? '==' !! '!=' } q2q1";</lang>
{{out}}
<pre>1) q norm = 5.47722557505166
2) -q = -1 + -2i + -3j + -4k
3) q conj = 1 + -2i + -3j + -4k
4) q + r = 8 + 2i + 3j + 4k
5) q1 + q2 = 5 + 7i + 9j + 11k
6) q * r = 7 + 14i + 21j + 28k
7) q1 * q2 = -56 + 16i + 24j + 26k
8) q1q2 != q2q1</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
{{Trans|Euphoria}}
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<lang Phix>function norm(sequence q)
<span style="color: #008080;">function</span> <span style="color: #000000;">norm</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">q</span><span style="color: #0000FF;">)</span>
return sqrt(sum(sq_power(q,2)))
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)))</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">conjugate</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">q</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">q</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">q</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..</span><span style="color: #000000;">4</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_uminus</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..</span><span style="color: #000000;">4</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">q</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">negative</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">q</span><span style="color: #0000FF;">)</span>
function conj(sequence q)
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sq_uminus</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">)</span>
q[2..4] = sq_uminus(q[2..4])
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
return q
end function
<span style="color: #008080;">function</span> <span style="color: #000000;">add</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">q1</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000000;">q2</span><span style="color: #0000FF;">)</span>
function add(object q1, object q2)
<span style="color: #008080;">if</span> <span style="color: #004080;">atom</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q1</span><span style="color: #0000FF;">)!=</span><span style="color: #004080;">atom</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
if atom(q1)!=atom(q2) then
<span style="color: #008080;">if</span> <span style="color: #004080;">atom</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
if atom(q1) then
<span style="color: #000000;">q1</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">q1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}</span>
q1 = {q1,0,0,0}
<span style="color: #008080;">else</span>
<span style="color: #000000;">q2</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">q2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}</span>
q2 = {q2,0,0,0}
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q2</span><span style="color: #0000FF;">)</span>
return sq_add(q1,q2)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
end function
<span style="color: #008080;">function</span> <span style="color: #000000;">mul</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">q1</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000000;">q2</span><span style="color: #0000FF;">)</span>
function mul(object q1, object q2)
<span style="color: #008080;">if</span> <span style="color: #004080;">sequence</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #004080;">sequence</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
if sequence(q1) and sequence(q2) then
<span style="color: #004080;">atom</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">r1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k1</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">q1</span><span style="color: #0000FF;">,</span>
return { q1[1]*q2[1] - q1[2]*q2[2] - q1[3]*q2[3] - q1[4]*q2[4],
<span style="color: #0000FF;">{</span><span style="color: #000000;">r2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k2</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">q2</span>
q1[1]*q2[2] + q1[2]*q2[1] + q1[3]*q2[4] - q1[4]*q2[3],
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span> <span style="color: #000000;">r1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">r2</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">i1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">i2</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">j1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">j2</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">k1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">k2</span><span style="color: #0000FF;">,</span>
q1[1]*q2[3] - q1[2]*q2[4] + q1[3]*q2[1] + q1[4]*q2[2],
<span style="color: #000000;">r1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">i2</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">i1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">r2</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">j1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">k2</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">k1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">j2</span><span style="color: #0000FF;">,</span>
q1[1]*q2[4] + q1[2]*q2[3] - q1[3]*q2[2] + q1[4]*q2[1] }
<span style="color: #000000;">r1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">j2</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">i1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">k2</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">j1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">r2</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">k1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">i2</span><span style="color: #0000FF;">,</span>
else
<span style="color: #000000;">r1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">k2</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">i1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">j2</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">j1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">i2</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">k1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">r2</span> <span style="color: #0000FF;">}</span>
return sq_mul(q1,q2)
<span style="color: #008080;">else</span>
end if
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q2</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function quats(sequence q)
return sprintf("%g + %gi + %gj + %gk",q)
<span style="color: #008080;">function</span> <span style="color: #000000;">quats</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">q</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%g%+gi%+gj%+gk"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
constant
q = {1, 2, 3, 4},
<span style="color: #008080;">constant</span>
q1 = {2, 3, 4, 5},
<span style="color: #000000;">q</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">},</span>
q2 = {3, 4, 5, 6},
<span style="color: #000000;">q1</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">},</span>
r = 7
<span style="color: #000000;">q2</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">}</span>
printf(1, "q = %s\n", {quats(q)})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">" q = %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">quats</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">)})</span>
printf(1, "r = %g\n", r)
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">" q1 = %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">quats</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q1</span><span style="color: #0000FF;">)})</span>
printf(1, "norm(q) = %g\n", norm(q))
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">" q2 = %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">quats</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q2</span><span style="color: #0000FF;">)})</span>
printf(1, "-q = %s\n", {quats(-q)})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
printf(1, "conj(q) = %s\n", {quats(conj(q))})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"1. norm(q) = %g\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">norm</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">))</span>
printf(1, "q + r = %s\n", {quats(add(q,r))})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"2. negative(q) = %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">quats</span><span style="color: #0000FF;">(</span><span style="color: #000000;">negative</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">))})</span>
printf(1, "q * r = %s\n", {quats(mul(q,r))})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"3. conjugate(q) = %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">quats</span><span style="color: #0000FF;">(</span><span style="color: #000000;">conjugate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">))})</span>
printf(1, "q1 = %s\n", {quats(q1)})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
printf(1, "q2 = %s\n", {quats(q2)})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"4.a q + 7 = %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">quats</span><span style="color: #0000FF;">(</span><span style="color: #000000;">add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">))})</span>
printf(1, "q1 + q2 = %s\n", {quats(add(q1,q2))})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">" .b 7 + q = %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">quats</span><span style="color: #0000FF;">(</span><span style="color: #000000;">add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q</span><span style="color: #0000FF;">))})</span>
printf(1, "q2 + q1 = %s\n", {quats(add(q2,q1))})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
printf(1, "q1 * q2 = %s\n", {quats(mul(q1,q2))})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"5.a q1 + q2 = %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">quats</span><span style="color: #0000FF;">(</span><span style="color: #000000;">add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q2</span><span style="color: #0000FF;">))})</span>
printf(1, "q2 * q1 = %s\n", {quats(mul(q2,q1))})</lang>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">" .b q2 + q1 = %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">quats</span><span style="color: #0000FF;">(</span><span style="color: #000000;">add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q1</span><span style="color: #0000FF;">))})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"6.a q * 49 = %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">quats</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">,</span><span style="color: #000000;">49</span><span style="color: #0000FF;">))})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">" .b 49 * q = %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">quats</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">49</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q</span><span style="color: #0000FF;">))})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"7.a q1 * q2 = %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">quats</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q2</span><span style="color: #0000FF;">))})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">" .b q2 * q1 = %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">quats</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q1</span><span style="color: #0000FF;">))})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"8.a 4.a === 4.b: %t\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">equal</span><span style="color: #0000FF;">(</span><span style="color: #000000;">add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">),</span><span style="color: #000000;">add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q</span><span style="color: #0000FF;">))})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">" .b 5.a === 5.b: %t\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">equal</span><span style="color: #0000FF;">(</span><span style="color: #000000;">add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q2</span><span style="color: #0000FF;">),</span><span style="color: #000000;">add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q1</span><span style="color: #0000FF;">))})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">" .c 6.a === 6.b: %t\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">equal</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">,</span><span style="color: #000000;">49</span><span style="color: #0000FF;">),</span><span style="color: #000000;">mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">49</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q</span><span style="color: #0000FF;">))})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">" .d 7.a === 7.b: %t\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">equal</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q2</span><span style="color: #0000FF;">),</span><span style="color: #000000;">mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">q1</span><span style="color: #0000FF;">))})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
q   q = 1  1+ 2i + 3j + 4k
 q1 = 2+3i+4j+5k
r = 7
 q2 = 3+4i+5j+6k
norm(q) = 5.47723
 
-q = -1 + -2i + -3j + -4k
1.  norm(q) = 5.47723
conj(q) = 1 + -2i + -3j + -4k
2.  negative(q) = -1-2i-3j-4k
q + r = 8 + 2i + 3j + 4k
3.  conjugate(q) = 1-2i-3j-4k
q * r = 7 + 14i + 21j + 28k
 
q1 = 2 + 3i + 4j + 5k
4.a   q + 7  = 8+2i+3j+4k
q2 = 3 + 4i + 5j + 6k
 .b   7 + q  = 8+2i+3j+4k
q1 + q2 = 5 + 7i + 9j + 11k
 
q2 + q1 = 5 + 7i + 9j + 11k
5.a  q1 + q2 = 5+7i+9j+11k
q1 * q2 = -56 + 16i + 24j + 26k
 .b  q2 + q1 = 5+7i+9j+11k
q2 * q1 = -56 + 18i + 20j + 28k
 
6.a   q * 49 = 49+98i+147j+196k
 .b  49 * q  = 49+98i+147j+196k
 
7.a  q1 * q2 = -56+16i+24j+26k
 .b  q2 * q1 = -56+18i+20j+28k
 
8.a  4.a === 4.b: true
 .b  5.a === 5.b: true
 .c  6.a === 6.b: true
 .d  7.a === 7.b: false
</pre>
 
=={{header|Picat}}==
{{trans|Prolog}}
A quaternion is represented as a complex term <code>qx/4</code>.
<syntaxhighlight lang="picat">go =>
test,
nl.
 
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),
printf("abs(%w) is %w\n", Name, Norm), fail.
test :- data(q, Qx), negate(Qx, Nqx),
printf("negate(%w) is %w\n", q, Nqx), fail.
test :- data(q, Qx), conjugate(Qx, Nqx),
printf("conjugate(%w) is %w\n", q, Nqx), fail.
test :- data(q1, Q1), data(q2, Q2), add(Q1, Q2, Qx),
printf("q1+q2 is %w\n", Qx), fail.
test :- data(q1, Q1), data(q2, Q2), add(Q2, Q1, Qx),
printf("q2+q1 is %w\n", Qx), fail.
test :- data(q, Qx), data(r, R), mul(Qx, R, Nqx),
printf("q*r is %w\n", Nqx), fail.
test :- data(q, Qx), data(r, R), mul(R, Qx, Nqx),
printf("r*q is %w\n", Nqx), fail.
test :- data(q1, Q1), data(q2, Q2), mul(Q1, Q2, Qx),
printf("q1*q2 is %w\n", Qx), fail.
test :- data(q1, Q1), data(q2, Q2), mul(Q2, Q1, Qx),
printf("q2*q1 is %w\n", Qx), fail.
test.</syntaxhighlight>
 
{{out}}
<pre>abs(q) is 5.477225575051661
abs(q1) is 7.348469228349535
abs(q2) is 9.273618495495704
negate(q) is qx(-1,-2,-3,-4)
conjugate(q) is qx(1,-2,-3,-4)
q1+q2 is qx(5,7,9,11)
q2+q1 is qx(5,7,9,11)
q*r is qx(7,14,21,28)
r*q is qx(7,14,21,28)
q1*q2 is qx(-56,16,24,26)
q2*q1 is qx(-56,18,20,28)</pre>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(scl 6)
 
(def 'quatCopy copy)
Line 4,532 ⟶ 6,774:
(mapcar '((R S) (pack (format R *Scl) S))
Q
'(" + " "i + " "j + " "k") ) )</langsyntaxhighlight>
Test:
<langsyntaxhighlight PicoLisplang="picolisp">(setq
Q (1.0 2.0 3.0 4.0)
Q1 (2.0 3.0 4.0 5.0)
Line 4,555 ⟶ 6,797:
(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"))</langsyntaxhighlight>
{{out}}
<pre>R = 7.000000
Line 4,575 ⟶ 6,817:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">*process source attributes xref or(!);
qu: Proc Options(main);
/**********************************************************************
Line 4,710 ⟶ 6,952:
End;
 
quatEqual: procedure(qp,qq) Returns(Char(12) Var);
End;</lang>
Dcl (qp,qq) type quat;
Dcl i Bin Fixed(15);
Do i=1 To 4;
If qp.x(i)^=qq.x(i) Then
Return('not equal');
End;
Return('equal');
End;
 
End;</syntaxhighlight>
{{out}}
<pre>
Line 4,731 ⟶ 6,983:
task C: q1=q1 --> equal
</pre>
 
 
=={{header|PowerShell}}==
===Implementation===
<syntaxhighlight lang="powershell">
<lang PowerShell>
class Quaternion {
[Double]$w
Line 4,794 ⟶ 7,045:
"`$q1 * `$q2: $([Quaternion]::show([Quaternion]::mul($q1,$q2)))"
"`$q2 * `$q1: $([Quaternion]::show([Quaternion]::mul($q2,$q1)))"
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 4,807 ⟶ 7,058:
</pre>
===Library===
<syntaxhighlight lang="powershell">
<lang PowerShell>
function show([System.Numerics.Quaternion]$c) {
function st([Double]$r) {
Line 4,830 ⟶ 7,081:
"`$q1 * `$q2: $(show ([System.Numerics.Quaternion]::Multiply($q1,$q2)))"
"`$q2 * `$q1: $(show ([System.Numerics.Quaternion]::Multiply($q2,$q1)))"
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 4,844 ⟶ 7,095:
 
=={{header|Prolog}}==
<langsyntaxhighlight Prologlang="prolog">% 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.
Line 4,865 ⟶ 7,116:
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.</langsyntaxhighlight>
 
'''Test:'''
<langsyntaxhighlight Prologlang="prolog">data(q, qx(1,2,3,4)).
data(q1, qx(2,3,4,5)).
data(q2, qx(3,4,5,6)).
Line 4,891 ⟶ 7,142:
test :- data(q1, Q1), data(q2, Q2), mul(Q2, Q1, Qx),
writef('q2*q1 is %w\n', [Qx]), fail.
test.</langsyntaxhighlight>
{{out}}
<pre> ?- test.
Line 4,907 ⟶ 7,158:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Structure Quaternion
a.f
b.f
Line 4,991 ⟶ 7,242:
EndIf
ProcedureReturn 1 ;true
EndProcedure</langsyntaxhighlight>
Implementation & test
<langsyntaxhighlight PureBasiclang="purebasic">Procedure.s ShowQ(*x.Quaternion, NN = 0)
ProcedureReturn "{" + StrF(*x\a, NN) + "," + StrF(*x\b, NN) + "," + StrF(*x\c, NN) + "," + StrF(*x\d, NN) + "}"
EndProcedure
Line 5,021 ⟶ 7,272:
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</langsyntaxhighlight>
Result
<pre>Q0 = {1,2,3,4}
Line 5,038 ⟶ 7,289:
=={{header|Python}}==
This example extends Pythons [http://docs.python.org/library/collections.html?highlight=namedtuple#collections.namedtuple namedtuples] to add extra functionality.
<langsyntaxhighlight lang="python">from collections import namedtuple
import math
 
Line 5,119 ⟶ 7,370:
q1 = Q(2, 3, 4, 5)
q2 = Q(3, 4, 5, 6)
r = 7</langsyntaxhighlight>
 
'''Continued shell session'''
Run the above with the -i flag to python on the command line, or run with idle then continue in the shell as follows:
<langsyntaxhighlight lang="python">>>> q
Quaternion(real=1.0, i=2.0, j=3.0, k=4.0)
>>> q1
Line 5,178 ⟶ 7,429:
>>> q1 * q1.reciprocal()
Quaternion(real=0.9999999999999999, i=0.0, j=0.0, k=0.0)
>>> </langsyntaxhighlight>
 
 
 
=={{header|R}}==
Line 5,186 ⟶ 7,435:
Using the quaternions package.
 
<syntaxhighlight lang="r">
<lang R>
library(quaternions)
 
Line 5,221 ⟶ 7,470:
## q1*q2 != q2*q1
 
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
<langsyntaxhighlight Racketlang="racket">#lang racket
(struct quaternion (a b c d)
Line 5,298 ⟶ 7,547:
(multiply q2 q1)
(equal? (multiply q1 q2)
(multiply q2 q1)))</langsyntaxhighlight>
 
{{out}}
Line 5,327 ⟶ 7,576:
#f
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>class Quaternion {
has Real ( $.r, $.i, $.j, $.k );
multi method new ( Real $r, Real $i, Real $j, Real $k ) {
self.bless: :$r, :$i, :$j, :$k;
}
multi qu(*@r) is export { Quaternion.new: |@r }
sub postfix:<j>(Real $x) is export { qu 0, 0, $x, 0 }
sub postfix:<k>(Real $x) is export { qu 0, 0, 0, $x }
method Str () { "$.r + {$.i}i + {$.j}j + {$.k}k" }
method reals () { $.r, $.i, $.j, $.k }
method conj () { qu $.r, -$.i, -$.j, -$.k }
method norm () { sqrt [+] self.reals X** 2 }
multi infix:<eqv> ( Quaternion $a, Quaternion $b ) is export { $a.reals eqv $b.reals }
multi infix:<+> ( Quaternion $a, Real $b ) is export { qu $b+$a.r, $a.i, $a.j, $a.k }
multi infix:<+> ( Real $a, Quaternion $b ) is export { qu $a+$b.r, $b.i, $b.j, $b.k }
multi infix:<+> ( Quaternion $a, Complex $b ) is export { qu $b.re + $a.r, $b.im + $a.i, $a.j, $a.k }
multi infix:<+> ( Complex $a, Quaternion $b ) is export { qu $a.re + $b.r, $a.im + $b.i, $b.j, $b.k }
multi infix:<+> ( Quaternion $a, Quaternion $b ) is export { qu $a.reals Z+ $b.reals }
multi prefix:<-> ( Quaternion $a ) is export { qu $a.reals X* -1 }
multi infix:<*> ( Quaternion $a, Real $b ) is export { qu $a.reals X* $b }
multi infix:<*> ( Real $a, Quaternion $b ) is export { qu $b.reals X* $a }
multi infix:<*> ( Quaternion $a, Complex $b ) is export { $a * qu $b.reals, 0, 0 }
multi infix:<*> ( Complex $a, Quaternion $b ) is export { $b R* qu $a.reals, 0, 0 }
multi infix:<*> ( Quaternion $a, Quaternion $b ) is export {
my @a_rijk = $a.reals;
my ( $r, $i, $j, $k ) = $b.reals;
return qu [+]( @a_rijk Z* $r, -$i, -$j, -$k ), # real
[+]( @a_rijk Z* $i, $r, $k, -$j ), # i
[+]( @a_rijk Z* $j, -$k, $r, $i ), # j
[+]( @a_rijk Z* $k, $j, -$i, $r ); # k
}
}
import Quaternion;
my $q = 1 + 2i + 3j + 4k;
my $q1 = 2 + 3i + 4j + 5k;
my $q2 = 3 + 4i + 5j + 6k;
my $r = 7;
say "1) q norm = {$q.norm}";
say "2) -q = {-$q}";
say "3) q conj = {$q.conj}";
say "4) q + r = {$q + $r}";
say "5) q1 + q2 = {$q1 + $q2}";
say "6) q * r = {$q * $r}";
say "7) q1 * q2 = {$q1 * $q2}";
say "8) q1q2 { $q1 * $q2 eqv $q2 * $q1 ?? '==' !! '!=' } q2q1";</syntaxhighlight>
{{out}}
<pre>1) q norm = 5.47722557505166
2) -q = -1 + -2i + -3j + -4k
3) q conj = 1 + -2i + -3j + -4k
4) q + r = 8 + 2i + 3j + 4k
5) q1 + q2 = 5 + 7i + 9j + 11k
6) q * r = 7 + 14i + 21j + 28k
7) q1 * q2 = -56 + 16i + 24j + 26k
8) q1q2 != q2q1</pre>
 
=={{header|Red}}==
<syntaxhighlight lang="red">
quaternion: context [
quaternion!: make typeset! [block! hash! vector!]
multiply: function [q [integer! float! quaternion!] p [integer! float! quaternion!]][
case [
number? q [collect [forall p [keep p/1 * q]]]
number? p [collect [forall q [keep q/1 * p]]]
'else [
reduce [
(q/1 * p/1) - (q/2 * p/2) - (q/3 * p/3) - (q/4 * p/4)
(q/1 * p/2) + (q/2 * p/1) + (q/3 * p/4) - (q/4 * p/3)
(q/1 * p/3) + (q/3 * p/1) + (q/4 * p/2) - (q/2 * p/4)
(q/1 * p/4) + (q/4 * p/1) + (q/2 * p/3) - (q/3 * p/2)
]
]
]
]
add: func [q [integer! float! quaternion!] p [integer! float! quaternion!]][
case [
number? q [head change copy p p/1 + q]
number? p [head change copy q q/1 + p]
'else [collect [forall q [keep q/1 + p/(index? q)]]]
]
]
negate: func [q [quaternion!]][collect [forall q [keep 0 - q/1]]]
conjugate: func [q [quaternion!]][collect [keep q/1 q: next q forall q [keep 0 - q/1]]]
norm: func [q [quaternion!]][sqrt first multiply q conjugate copy q]
normalize: function [q [quaternion!]][n: norm q collect [forall q [keep q/1 / n]]]
inverse: func [q [quaternion!]][(conjugate q) / ((norm q) ** 2)]
]
 
set [q q1 q2 r] [[1 2 3 4] [2 3 4 5] [3 4 5 6] 7]
 
print [{
1. The norm of a quaternion:
`quaternion/norm q` =>} quaternion/norm q {
 
2. The negative of a quaternion:
`quaternion/negate q` =>} mold quaternion/negate q {
 
3. The conjugate of a quaternion:
<code>quaternion/conjugate q</code> =>} mold quaternion/conjugate q {
 
4. Addition of a real number `r` and a quaternion `q`:
`quaternion/add r q` =>} mold quaternion/add r q {
`quaternion/add q r` =>} mold quaternion/add q r {
 
5. Addition of two quaternions:
`quaternion/add q1 q2` =>} mold quaternion/add q1 q2 {
 
6. Multiplication of a real number and a quaternion:
`quaternion/multiply q r` =>} mold quaternion/multiply q r {
`quaternion/multiply r q` =>} mold quaternion/multiply r q {
 
7. Multiplication of two quaternions `q1` and `q2` is given by:
`quaternion/multiply q1 q2` =>} mold quaternion/multiply q1 q2 {
 
8. Show that, for the two quaternions `q1` and `q2`:
`equal? quaternion/multiply q1 q2 mold quaternion/multiply q2 q1` =>}
equal? quaternion/multiply q1 q2 quaternion/multiply q2 q1]
</syntaxhighlight>
 
Output:
 
1. The norm of a quaternion: <br>
<code>quaternion/norm q</code> => <code>5.477225575051661</code>
 
2. The negative of a quaternion: <br>
<code>quaternion/negate q</code> => <code>[-1 -2 -3 -4]</code>
 
3. The conjugate of a quaternion:<br>
<code>quaternion/conjugate q</code> => <code>[1 -2 -3 -4]</code>
 
4. Addition of a real number <code>r</code> and a quaternion <code>q</code>:<br>
<code>quaternion/add r q</code> => <code>[8 2 3 4]</code> <br>
<code>quaternion/add q r</code> => <code>[8 2 3 4]</code>
 
5. Addition of two quaternions: <br>
<code>quaternion/add q1 q2</code> => <code>[5 7 9 11]</code>
 
6. Multiplication of a real number and a quaternion: <br>
<code>quaternion/multiply q r</code> => <code>[7 14 21 28]</code> <br>
<code>quaternion/multiply r q</code> => <code>[7 14 21 28]</code>
 
7. Multiplication of two quaternions <code>q1</code> and <code>q2</code> is given by:<br>
<code>quaternion/multiply q1 q2</code> => <code>[-56 16 24 26]</code>
 
8. Show that, for the two quaternions <code>q1</code> and <code>q2</code>:<br>
<code>equal? quaternion/multiply q1 q2 mold quaternion/multiply q2 q1</code> => <code>false</code>
 
=={{header|REXX}}==
The REXX language has no native quaternion support, but subroutines can be easily written.
<langsyntaxhighlight lang="rexx">/*REXX pgmprogram performs some operations on quaternion type numbers and showsdisplays results*/
q = 1 2 3 4 ; q1 = 2 3 4 5
r = 7 ; q2 = 3 4 5 6
call qShow q , 'q'
call qShow q1 , 'q1'
call qShow q2 , 'q2'
call qShow r , 'r'
call qShow qNorm(q) , 'norm q' , "task 1:"
call qShow qNeg(q) , 'negative q' , "task 2:"
call qShow qConj(q) , 'conjugate q' , "task 3:"
call qShow qAdd( r, q ) , 'addition r+q' , "task 4:"
call qShow qAdd(q1, q2 ) , 'addition q1+q2' , "task 5:"
call qShow qMul( q, r ) , 'multiplication q*r' , "task 6:"
call qShow qMul(q1, q2 ) , 'multiplication q1*q2' , "task 7:"
call qShow qMul(q2, q1 ) , 'multiplication q2*q1' , "task 8:"
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────*/
qConj: procedure; parse arg x; call qXY; return return x.1 (-x.2) (-x.3) (-x.4)
qNeg: procedure; parse arg x; call qXY; return return -x.1 (-x.2) (-x.3) (-x.4)
qNorm: procedure; parse arg x; call qXY; return sqrt(x.1**2 +x.2**2 +x.3**2 +x.4**2)
/*──────────────────────────────────────────────────────────────────────────────*/
qAdd: procedure; parse arg x,y; call qXY 2; return x.1+y.1 x.2+y.2 x.3+y.3 x.4+y.4
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────*/
qMul: procedure; parse arg x,y; call qXY y
return x.1*y.1 -x.2*y.2 -x.3*y.3 -x.4*y.4 x.1*y.2 +x.2*y.1 +x.3*y.4 -x.4*y.3 ,
x.1*y.3 -x.2*y.4 +x.3*y.1 +x.4*y.2 x.1*y.4 +x.2*y.3 -x.3*y.2 +x.4*y.1
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────*/
qNormqShow: procedure; parse arg x; call qXY; return sqrt(x.1**2+x.2**2+x.3**2+x.4**2) $=
do m=1 for 4; _= x.m; if _==0 then iterate; if _>=0 then _= '+'_
/*──────────────────────────────────────────────────────────────────────────────*/
if m\==1 then _= _ || substr('∙ijk', m, 1); $= strip($ || _, , "+")
qShow: procedure; parse arg x; call qXY; $=
do m=1 for 4; _=x.m; if _==0 then iterate; if _>=0 then _='+'_
if m\==1 then _=_ || substr('~ijk',m,1); $=strip($ || _,,'+')
end /*m*/
say left(arg(3), 9) right(arg(2), 20) ' ──► ' $; return $
/*──────────────────────────────────────────────────────────────────────────────────────*/
return $
qXY: do n=1 for 4; x.n= word( word(x, n) 0, 1)/1; end /*n*/
/*──────────────────────────────────────────────────────────────────────────────*/
qXY: if arg()==1 then do nm=1 for 4; xy.nm= word( word(xy,n m) 0, 1)/1; end /*nm*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
if arg()==1 then do m=1 for 4; y.m=word(word(y,m) 0,1)/1; end /*m*/
sqrt: procedure; parse arg x; if x=0 then return 0; d= digits(); i=; m.=9; h=d+6
return
numeric digits; numeric form; if x<0 then parse value -x 'i' with x i
/*──────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse argvalue format(x;, 2, 1, if, x=0) 'E0' then return 0;with d=digits();g "E" i=; _ m.; g=9 g *.5'e'_ % 2
numeric digits do j=0 while h>9; numeric form; h m.j=d+6h; if x<0 then do; x=-x; i h='i' h % 2 + 1; end /*j*/
parse value format(x,2,1,,0) 'E0'do k=j+5 with to g0 'E' _by .-1; numeric digits m.k; g= (g + x/g)* .5'e'_%2; end /*k*/
numeric digits d; do j=0 while h>9; m.j=h;return (g/1)i h=h%2+1; /*make complex if X<0 end /*j*/</syntaxhighlight>
{{out|output|text=&nbsp; when using the internal default inputs:}}
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
numeric digits d; return (g/1)i /*make complex if X < 0. */</lang>
'''output''' &nbsp; when using the default input:
<pre>
q ──► 1+2i+3j+4k
Line 5,390 ⟶ 7,792:
task 8: multiplication q2*q1 ──► -56+18i+20j+28k
</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
By considering quaternions as arrays, negation and addition can be directly achieved by resp. <code>NEG</code> and <code>+</code> instructions. Other operations need specific RPL words:
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ 0 1 4 '''FOR''' q OVER q GET SQ + '''NEXT''' √ SWAP DROP
≫ '<span style="color:blue">QNORM</span>' STO
≪ NEG 1 DUP2 GET NEG PUT ≫ '<span style="color:blue">QCONJ</span>' STO
DUP TYPE 3 == ≪ SWAP ≫ IFT
OVER 1 GET + 1 SWAP PUT
≫ '<span style="color:blue">QRADD</span>' STO
ARRY→ DROP 5 ROLL ARRY→ DROP → a2 b2 c2 d2 a1 b1 c1 d1
≪ 'a1*a2 − b1*b2 − c1*c2 − d1*d2' EVAL
'a1*b2 + b1*a2 + c1*d2 − d1*c2' EVAL
'a1*c2 − b1*d2 + c1*a2 + d1*b2' EVAL
'a1*d2 + b1*c2 − c1*b2 + d1*a2' EVAL
{ 4 } →ARRY
≫ ≫ '<span style="color:blue">QMULT</span>' STO
|
<span style="color:blue">QNORM</span> ''( [ a b c d ] -- √(a²+b²+c²+d²) )''
<span style="color:blue">QCONJ</span> ''( [ a b c d ] -- [ a -b -c -d ] )''
<span style="color:blue">QRADD</span> ''( [ a b c d ] r -- [ a+r b c d ] )''
switch arguments if quaternion is at stack level 1
replace a by a+r
<span style="color:blue">QMULT</span> ''( [Q1] [Q2] -- [Q1 x Q2] )''
put the 2 quaternions in local variables
do the math in stack
convert stack to a quaternion
|}
 
[1 2 3 4] <span style="color:blue">QNORM</span>
[1 2 3 4] NEG
[1 2 3 4] <span style="color:blue">QCONJ</span>
[1 2 3 4] 7 <span style="color:blue">QRADD</span>
[2 3 4 5] [3 4 5 6] +
[1 2 3 4] 7 *
[2 3 4 5] [3 4 5 6] <span style="color:blue">QMULT</span>
[3 4 5 6] [2 3 4 5] <span style="color:blue">QMULT</span>
 
{{out}}
<pre>
8: 5.47722557505
7: [ -1 -2 -3 -4 ]
6: [ 1 -2 -3 -4 ]
5: [ 8 2 3 4 ]
4: [ 5 7 9 11 ]
3: [ 7 14 21 28 ]
2: [ -56 16 24 26 ]
1: [ -56 18 20 28 ]
</pre>
=== Quaternion multiplication through Cayley-Dickson construction===
This is a shorter and faster version of the <code>QMULT</code> word. {{trans|Ruby}}
{| class="wikitable"
! RPL code
! Comment
|-
|
ARRY→ DROP R→C ROT ROT R→C ROT
ARRY→ DROP R→C ROT ROT R→C → d c b a
≪ a c * d CONJ b * - C→R
d a * b c CONJ * + C→R
{ 4 } →ARRY
≫ ≫ '<span style="color:blue">QMULT</span>' STO
|
<span style="color:blue">QMULT</span> ''( [Q1] [Q2] -- [Q1 x Q2] )''
convert the 2 quaternions into 2 pairs of complex numbers
and store them locally
(a,b)(c,d) = (ac - conj(d).b, // (a,b) and (c,d) are pairs
da + b.conj(c)) // of complex numbers
convert stack to a quaternion
|}
Output is the same.
===Using the matrix form===
This efficient implementation is based on an article of [https://edspi31415.blogspot.com/2015/06/hp-prime-and-hp-50g-quaternions.html?fbclid=IwAR1KTjHt4xVt2FoMqL-82MJ1SS3SBg8jNoF-8uNcqg2Y5bLD2oiyxVfO88Y Eddie's Math and Calculator Blog].
 
« ARRY→ DROP → a b c d
« a b R→C c d R→C
c NEG d R→C
3 PICK CONJ
{ 2 2 } →ARRY
» » '<span style="color:blue">→QTM</span>' STO <span style="color:grey">''@ ( [ a b c d ] → [[ a+bi c+di ][ -c+di a-bi ]] )''</span>
« DUP 1 GET RE LASTARG IM
ROT 2 GET RE LASTARG IM
{ 4 } →ARRY
» '<span style="color:blue">QTM→</span>' STO <span style="color:grey">''@ ( [[ a+bi c+di ][ -c+di a-bi ]] → [ a b c d ] )''</span>
« <span style="color:blue">→QTM</span> SWAP <span style="color:blue">QTM→</span> SWAP * <span style="color:blue">QTM→</span>
» '<span style="color:blue">QMULT</span>' STO <span style="color:grey">''@ ( q1 q2 → q1*q2 ) ''</span>
« <span style="color:blue">→QTM</span> DET √ ABS
» '<span style="color:blue">QNORM</span>' STO <span style="color:grey">''@ ( q → qnorm(q) ) ''</span>
« DUP INV SWAP <span style="color:blue">QNORM</span> SQ *
» '<span style="color:blue">QCONJ</span>' STO <span style="color:grey">''@ ( q → conj(q) ) ''</span>
 
Quaternions' matrix form allows to quickly develop additional operations:
 
« DUP <span style="color:blue">QNORM</span> /
» '<span style="color:blue">QSIGN</span>' STO <span style="color:grey">''@ ( q → q/norm(q) ) ''</span>
« <span style="color:blue">→QTM</span> INV <span style="color:blue">QTM→</span>
» '<span style="color:blue">QINV</span>' STO <span style="color:grey">''@ ( q → q^(-1) ) ''</span>
« <span style="color:blue">QINV QMULT</span>
» '<span style="color:blue">QDIV</span>' STO <span style="color:grey">''@ ( q1 q2 → q1/q2 )''</span>
 
=={{header|Ruby}}==
{{works with|Ruby|1.9}}
 
<langsyntaxhighlight lang="ruby">class Quaternion
def initialize(*parts)
raise ArgumentError, "wrong number of arguments (#{parts.size} for 4)" unless parts.size == 4
Line 5,470 ⟶ 7,998:
puts "%20s = %s" % [exp, eval(exp)]
end
end</langsyntaxhighlight>
{{out}}
<pre>
Line 5,490 ⟶ 8,018:
q - r = Quaternion[-6, 2, 3, 4]
r - q = Quaternion[6, -2, -3, -4]
</pre>
 
=={{header|Rust}}==
 
<syntaxhighlight lang="rust">use std::fmt::{Display, Error, Formatter};
use std::ops::{Add, Mul, Neg};
 
#[derive(Clone,Copy,Debug)]
struct Quaternion {
a: f64,
b: f64,
c: f64,
d: f64
}
 
impl Quaternion {
pub fn new(a: f64, b: f64, c: f64, d: f64) -> Quaternion {
Quaternion {
a: a,
b: b,
c: c,
d: d
}
}
 
pub fn norm(&self) -> f64 {
(self.a.powi(2) + self.b.powi(2) + self.c.powi(2) + self.d.powi(2)).sqrt()
}
 
pub fn conjugate(&self) -> Quaternion {
Quaternion {
a: self.a,
b: -self.b,
c: -self.c,
d: -self.d
}
}
}
 
impl Add for Quaternion {
type Output = Quaternion;
 
#[inline]
fn add(self, other: Quaternion) -> Self::Output {
Quaternion {
a: self.a + other.a,
b: self.b + other.b,
c: self.c + other.c,
d: self.d + other.d
}
}
}
 
impl Add<f64> for Quaternion {
type Output = Quaternion;
 
#[inline]
fn add(self, other: f64) -> Self::Output {
Quaternion {
a: self.a + other,
b: self.b,
c: self.c,
d: self.d
}
}
}
 
impl Add<Quaternion> for f64 {
type Output = Quaternion;
 
#[inline]
fn add(self, other: Quaternion) -> Self::Output {
Quaternion {
a: other.a + self,
b: other.b,
c: other.c,
d: other.d
}
}
}
 
impl Display for Quaternion {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(f, "({} + {}i + {}j + {}k)", self.a, self.b, self.c, self.d)
}
}
 
impl Mul for Quaternion {
type Output = Quaternion;
 
#[inline]
fn mul(self, rhs: Quaternion) -> Self::Output {
Quaternion {
a: self.a * rhs.a - self.b * rhs.b - self.c * rhs.c - self.d * rhs.d,
b: self.a * rhs.b + self.b * rhs.a + self.c * rhs.d - self.d * rhs.c,
c: self.a * rhs.c - self.b * rhs.d + self.c * rhs.a + self.d * rhs.b,
d: self.a * rhs.d + self.b * rhs.c - self.c * rhs.b + self.d * rhs.a,
}
}
}
 
impl Mul<f64> for Quaternion {
type Output = Quaternion;
 
#[inline]
fn mul(self, other: f64) -> Self::Output {
Quaternion {
a: self.a * other,
b: self.b * other,
c: self.c * other,
d: self.d * other
}
}
}
 
impl Mul<Quaternion> for f64 {
type Output = Quaternion;
 
#[inline]
fn mul(self, other: Quaternion) -> Self::Output {
Quaternion {
a: other.a * self,
b: other.b * self,
c: other.c * self,
d: other.d * self
}
}
}
 
impl Neg for Quaternion {
type Output = Quaternion;
 
#[inline]
fn neg(self) -> Self::Output {
Quaternion {
a: -self.a,
b: -self.b,
c: -self.c,
d: -self.d
}
}
}
 
fn main() {
let q0 = Quaternion { a: 1., b: 2., c: 3., d: 4. };
let q1 = Quaternion::new(2., 3., 4., 5.);
let q2 = Quaternion::new(3., 4., 5., 6.);
let r: f64 = 7.;
 
println!("q0 = {}", q0);
println!("q1 = {}", q1);
println!("q2 = {}", q2);
println!("r = {}", r);
println!();
println!("-q0 = {}", -q0);
println!("conjugate of q0 = {}", q0.conjugate());
println!();
println!("r + q0 = {}", r + q0);
println!("q0 + r = {}", q0 + r);
println!();
println!("r * q0 = {}", r * q0);
println!("q0 * r = {}", q0 * r);
println!();
println!("q0 + q1 = {}", q0 + q1);
println!("q0 * q1 = {}", q0 * q1);
println!();
println!("q0 * (conjugate of q0) = {}", q0 * q0.conjugate());
println!();
println!(" q0 + q1 * q2 = {}", q0 + q1 * q2);
println!("(q0 + q1) * q2 = {}", (q0 + q1) * q2);
println!();
println!(" q0 * q1 * q2 = {}", q0 *q1 * q2);
println!("(q0 * q1) * q2 = {}", (q0 * q1) * q2);
println!(" q0 * (q1 * q2) = {}", q0 * (q1 * q2));
println!();
println!("normal of q0 = {}", q0.norm());
}</syntaxhighlight>
{{out}}
<pre>
q0 = (1 + 2i + 3j + 4k)
q1 = (2 + 3i + 4j + 5k)
q2 = (3 + 4i + 5j + 6k)
r = 7
 
-q0 = (-1 + -2i + -3j + -4k)
conjugate of q0 = (1 + -2i + -3j + -4k)
 
r + q0 = (8 + 2i + 3j + 4k)
q0 + r = (8 + 2i + 3j + 4k)
 
r * q0 = (7 + 14i + 21j + 28k)
q0 * r = (7 + 14i + 21j + 28k)
 
q0 + q1 = (3 + 5i + 7j + 9k)
q0 * q1 = (-36 + 6i + 12j + 12k)
 
q0 * (conjugate of q0) = (30 + 0i + 0j + 0k)
 
q0 + q1 * q2 = (-55 + 18i + 27j + 30k)
(q0 + q1) * q2 = (-100 + 24i + 42j + 42k)
 
q0 * q1 * q2 = (-264 + -114i + -132j + -198k)
(q0 * q1) * q2 = (-264 + -114i + -132j + -198k)
q0 * (q1 * q2) = (-264 + -114i + -132j + -198k)
 
normal of q0 = 5.477225575051661
</pre>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">case class Quaternion(re: Double = 0.0, i: Double = 0.0, j: Double = 0.0, k: Double = 0.0) {
lazy val im = (i, j, k)
private lazy val norm2 = re*re + i*i + j*j + k*k
lazy val norm = math.sqrt(norm2)
def negative =new Quaternion(-re, -i, -j, -k)
def conjugate =new Quaternion(re, -i, -j, -k)
def reciprocal =new Quaternion(re/norm2, -i/norm2, -j/norm2, -k/norm2)
def +(q: Quaternion) =new Quaternion(re+q.re, i+q.i, j+q.j, k+q.k)
def -(q: Quaternion) =new Quaternion(re-q.re, i-q.i, j-q.j, k-q.k)
def *(q: Quaternion) =new Quaternion(
re*q.re - i*q.i - j*q.j - k*q.k,
re*q.i + i*q.re + j*q.k - k*q.j,
Line 5,510 ⟶ 8,244:
re*q.k + i*q.j - j*q.i + k*q.re
)
def /(q: Quaternion) = this * q.reciprocal
def unary_- = negative
def unary_~ = conjugate
override def toString = "Q(%.2f, %.2fi, %.2fj, %.2fk)".formatLocal(java.util.Locale.ENGLISH, re, i, j, k)
override def equals(x:Any):Boolean=x match {
case Quaternion(re, i, j, k) => (Double.doubleToLongBits(this.re)==Double.doubleToLongBits(re)) &&
Double.doubleToLongBits(this.i)==Double.doubleToLongBits(i) &&
Double.doubleToLongBits(this.j)==Double.doubleToLongBits(j) &&
Double.doubleToLongBits(this.k)==Double.doubleToLongBits(k)
case _ => false
}
override def toString()="Q(%.2f, %.2fi, %.2fj, %.2fk)".formatLocal(Locale.ENGLISH, re,i,j,k)
}
 
object Quaternion {
import scala.language.implicitConversions
implicit def number2Quaternion[T <% Number](n:T):Quaternion = apply(n.doubleValue)
import Numeric.Implicits._
}</lang>
 
implicit def number2Quaternion[T:Numeric](n: T) = Quaternion(n.toDouble)
}</syntaxhighlight>
Demonstration:
<langsyntaxhighlight lang="scala">val q0=Quaternion(1.0, 2.0, 3.0, 4.0);
val q1=Quaternion(2.0, 3.0, 4.0, 5.0);
val q2=Quaternion(3.0, 4.0, 5.0, 6.0);
Line 5,564 ⟶ 8,293:
println("q2/q1 = "+ q2/q1)
println("q1/r = "+ q1/r)
println("r/q1 = "+ r/q1)</langsyntaxhighlight>
{{out}}
<pre>q0 = Q(1.00, 2.00i, 3.00j, 4.00k)
Line 5,595 ⟶ 8,324:
q1/r = Q(0.29, 0.43i, 0.57j, 0.71k)
r/q1 = Q(0.26, -0.39i, -0.52j, -0.65k)</pre>
 
=={{header|Scheme}}==
For the source code, see [[#Ol|the entry for Otus Lisp]]. However, with most Scheme implementations the output will look different:
 
{{out}}
<pre>$ ypsilon quaternions_task.scm
q = (1 2 3 4)
q1 = (2 3 4 5)
q2 = (3 4 5 6)
r = 7
 
(quaternion? q) = #t
(quaternion? q1) = #t
(quaternion? q2) = #t
(quaternion? r) = #f
 
(quaternion-norm q) = 5.477225575051661
(quaternion-norm q1) = 7.3484692283495345
(quaternion-norm q2) = 9.273618495495704
 
(quaternion- q) = (-1 -2 -3 -4)
(quaternion- q1 q2) = (-1 -1 -1 -1)
(quaternion- q q1 q2) = (-4 -5 -6 -7)
 
(quaternion-conjugate q) = (1 -2 -3 -4)
 
(quaternion+) = (0 0 0 0)
(quaternion+ q) = (1 2 3 4)
(quaternion+ r q) = (8 2 3 4)
(quaternion+ q r) = (8 2 3 4)
(quaternion+ q1 q2) = (5 7 9 11)
(quaternion+ q q1 q2) = (6 9 12 15)
 
(quaternion*) = (1 0 0 0)
(quaternion* q) = (1 2 3 4)
(quaternion* r q) = (7 14 21 28)
(quaternion* q r) = (7 14 21 28)
(quaternion* q1 q2) = (-56 16 24 26)
(quaternion* q q1 q2) = (-264 -114 -132 -198)
 
(quaternion=? q) = #t
(quaternion=? q q) = #t
(quaternion=? q1 q2) = #f
(quaternion=? q q q) = #t
(quaternion=? q1 q1 q2) = #f
 
(quaternion* q1 q2) = (-56 16 24 26)
(quaternion* q2 q1) = (-56 18 20 28)
(quaternion=? (quaternion* q1 q2)
(quaternion* q2 q1)) = #f</pre>
 
=={{header|Seed7}}==
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
include "math.s7i";
 
 
# Define the quaternion number data type.
const type: quaternion is new object struct
var float: a is 0.0;
var float: b is 0.0;
var float: c is 0.0;
var float: d is 0.0;
end struct;
 
 
# Create a quaternion number from its real and imaginary parts.
const func quaternion: quaternion
(in float: a, in float: b, in float: c, in float: d) is func
result
var quaternion: aQuaternion is quaternion.value;
begin
aQuaternion.a := a;
aQuaternion.b := b;
aQuaternion.c := c;
aQuaternion.d := d;
end func;
 
 
# Helper function for str().
const func string: signed (in float: number, in string: part) is func
result
var string: stri is str(number) & part;
begin
if number > 0.0 then
stri := "+" & stri;
elsif number = 0.0 then
stri := "";
end if;
end func;
 
 
# Convert a quaternion number to a string.
const func string: str (in quaternion: number) is func
result
var string: stri is "";
begin
if number.a <> 0.0 then
stri &:= str(number.a);
end if;
stri &:= signed(number.b, "i");
stri &:= signed(number.c, "j");
stri &:= signed(number.d, "k");
end func;
 
 
# Compute the norm of a quaternion number.
const func float: norm (in quaternion: number) is func
result
var float: qnorm is 0.0;
begin
qnorm := sqrt(
number.a ** 2.0 + number.b ** 2.0 +
number.c ** 2.0 + number.d ** 2.0
);
end func;
 
 
# Compute the negative of a quaternion number.
const func quaternion: - (in quaternion: number) is func
result
var quaternion: negatedNumber is quaternion.value;
begin
negatedNumber.a := -number.a;
negatedNumber.b := -number.b;
negatedNumber.c := -number.c;
negatedNumber.d := -number.d;
end func;
 
 
# Compute the conjugate of a quaternion number.
const func quaternion: conjugate (in quaternion: number) is func
result
var quaternion: conjugateNumber is quaternion.value;
begin
conjugateNumber.a := number.a;
conjugateNumber.b := -number.b;
conjugateNumber.c := -number.c;
conjugateNumber.d := -number.d;
end func;
 
 
# Add a float to a quaternion number.
const func quaternion: (in quaternion: number) + (in float: real) is func
result
var quaternion: sum is quaternion.value;
begin
sum.a := number.a + real;
sum.b := number.b;
sum.c := number.c;
sum.d := number.d;
end func;
 
 
# Add a quaternion number to a float.
const func quaternion: (in float: real) + (in quaternion: number) is
return number + real;
 
 
# Add two quaternion numbers.
const func quaternion: (in quaternion: number1) + (in quaternion: number2) is func
result
var quaternion: sum is quaternion.value;
begin
sum.a := number1.a + number2.a;
sum.b := number1.b + number2.b;
sum.c := number1.c + number2.c;
sum.d := number1.d + number2.d;
end func;
 
 
# Multiply a float and a quaternion number.
const func quaternion: (in float: real) * (in quaternion: number) is func
result
var quaternion: product is quaternion.value;
begin
product.a := number.a * real;
product.b := number.b * real;
product.c := number.c * real;
product.d := number.d * real;
end func;
 
 
# Multiply a quaternion number and a float.
const func quaternion: (in quaternion: number) * (in float: real) is
return real * number;
 
 
# Multiply two quaternion numbers.
const func quaternion: (in quaternion: x) * (in quaternion: y) is func
result
var quaternion: product is quaternion.value;
begin
product.a := x.a * y.a - x.b * y.b - x.c * y.c - x.d * y.d;
product.b := x.a * y.b + x.b * y.a + x.c * y.d - x.d * y.c;
product.c := x.a * y.c - x.b * y.d + x.c * y.a + x.d * y.b;
product.d := x.a * y.d + x.b * y.c - x.c * y.b + x.d * y.a;
end func;
 
 
# Allow quaternions to be written using write(), writeln() etc.
enable_output(quaternion);
 
 
# Demonstrate quaternion numbers.
const proc: main is func
local
const quaternion: q is quaternion(1.0, 2.0, 3.0, 4.0);
const quaternion: q1 is quaternion(2.0, 3.0, 4.0, 5.0);
const quaternion: q2 is quaternion(3.0, 4.0, 5.0, 6.0);
const float: r is 7.0;
begin
writeln(" q = " <& q);
writeln("q1 = " <& q1);
writeln("q2 = " <& q2);
writeln(" r = " <& r <& "\n");
writeln("norm(q) = " <& norm(q));
writeln("-q = " <& -q);
writeln("conjugate(q) = " <& conjugate(q));
writeln("q + r = " <& q + r);
writeln("r + q = " <& r + q);
writeln("q1 + q2 = " <& q1 + q2);
writeln("q2 + q1 = " <& q2 + q1);
writeln("q * r = " <& q * r);
writeln("r * q = " <& r * q);
writeln("q1 * q2 = " <& q1 * q2);
writeln("q2 * q1 = " <& q2 * q1);
end func;</syntaxhighlight>
{{out}}
<pre>
q = 1.0+2.0i+3.0j+4.0k
q1 = 2.0+3.0i+4.0j+5.0k
q2 = 3.0+4.0i+5.0j+6.0k
r = 7.0
 
norm(q) = 5.47722557505166
-q = -1.0-2.0i-3.0j-4.0k
conjugate(q) = 1.0-2.0i-3.0j-4.0k
q + r = 8.0+2.0i+3.0j+4.0k
r + q = 8.0+2.0i+3.0j+4.0k
q1 + q2 = 5.0+7.0i+9.0j+11.0k
q2 + q1 = 5.0+7.0i+9.0j+11.0k
q * r = 7.0+14.0i+21.0j+28.0k
r * q = 7.0+14.0i+21.0j+28.0k
q1 * q2 = -56.0+16.0i+24.0j+26.0k
q2 * q1 = -56.0+18.0i+20.0j+28.0k
</pre>
 
=={{header|Sidef}}==
{{trans|Raku}}
<syntaxhighlight lang="ruby">class Quaternion(r, i, j, k) {
 
func qu(*r) { Quaternion(r...) }
 
method to_s { "#{r} + #{i}i + #{j}j + #{k}k" }
method reals { [r, i, j, k] }
method conj { qu(r, -i, -j, -k) }
method norm { self.reals.map { _*_ }.sum.sqrt }
 
method ==(Quaternion b) { self.reals == b.reals }
 
method +(Number b) { qu(b+r, i, j, k) }
method +(Quaternion b) { qu((self.reals ~Z+ b.reals)...) }
 
method neg { qu(self.reals.map{ .neg }...) }
 
method *(Number b) { qu((self.reals»*»b)...) }
method *(Quaternion b) {
var (r,i,j,k) = b.reals...
qu(sum(self.reals ~Z* [r, -i, -j, -k]),
sum(self.reals ~Z* [i, r, k, -j]),
sum(self.reals ~Z* [j, -k, r, i]),
sum(self.reals ~Z* [k, j, -i, r]))
}
}
 
var q = Quaternion(1, 2, 3, 4)
var q1 = Quaternion(2, 3, 4, 5)
var q2 = Quaternion(3, 4, 5, 6)
var r = 7
 
say "1) q norm = #{q.norm}"
say "2) -q = #{-q}"
say "3) q conj = #{q.conj}"
say "4) q + r = #{q + r}"
say "5) q1 + q2 = #{q1 + q2}"
say "6) q * r = #{q * r}"
say "7) q1 * q2 = #{q1 * q2}"
say "8) q1q2 #{ q1*q2 == q2*q1 ? '==' : '!=' } q2q1"</syntaxhighlight>
{{out}}
<pre>
1) q norm = 5.47722557505166113456969782800802133952744694997983
2) -q = -1 + -2i + -3j + -4k
3) q conj = 1 + -2i + -3j + -4k
4) q + r = 8 + 2i + 3j + 4k
5) q1 + q2 = 5 + 7i + 9j + 11k
6) q * r = 7 + 14i + 21j + 28k
7) q1 * q2 = -56 + 16i + 24j + 26k
8) q1q2 != q2q1
</pre>
 
=={{header|Swift}}==
<syntaxhighlight lang="swift">import Foundation
 
struct Quaternion {
var a, b, c, d: Double
static let i = Quaternion(a: 0, b: 1, c: 0, d: 0)
static let j = Quaternion(a: 0, b: 0, c: 1, d: 0)
static let k = Quaternion(a: 0, b: 0, c: 0, d: 1)
}
extension Quaternion: Equatable {
static func ==(lhs: Quaternion, rhs: Quaternion) -> Bool {
return (lhs.a, lhs.b, lhs.c, lhs.d) == (rhs.a, rhs.b, rhs.c, rhs.d)
}
}
extension Quaternion: ExpressibleByIntegerLiteral {
init(integerLiteral: Double) {
a = integerLiteral
b = 0
c = 0
d = 0
}
}
extension Quaternion: Numeric {
var magnitude: Double {
return norm
}
init?<T>(exactly: T) { // stub to satisfy protocol requirements
return nil
}
public static func + (lhs: Quaternion, rhs: Quaternion) -> Quaternion {
return Quaternion(
a: lhs.a + rhs.a,
b: lhs.b + rhs.b,
c: lhs.c + rhs.c,
d: lhs.d + rhs.d
)
}
public static func - (lhs: Quaternion, rhs: Quaternion) -> Quaternion {
return Quaternion(
a: lhs.a - rhs.a,
b: lhs.b - rhs.b,
c: lhs.c - rhs.c,
d: lhs.d - rhs.d
)
}
public static func * (lhs: Quaternion, rhs: Quaternion) -> Quaternion {
return Quaternion(
a: lhs.a*rhs.a - lhs.b*rhs.b - lhs.c*rhs.c - lhs.d*rhs.d,
b: lhs.a*rhs.b + lhs.b*rhs.a + lhs.c*rhs.d - lhs.d*rhs.c,
c: lhs.a*rhs.c - lhs.b*rhs.d + lhs.c*rhs.a + lhs.d*rhs.b,
d: lhs.a*rhs.d + lhs.b*rhs.c - lhs.c*rhs.b + lhs.d*rhs.a
)
}
public static func += (lhs: inout Quaternion, rhs: Quaternion) {
lhs = Quaternion(
a: lhs.a + rhs.a,
b: lhs.b + rhs.b,
c: lhs.c + rhs.c,
d: lhs.d + rhs.d
)
}
public static func -= (lhs: inout Quaternion, rhs: Quaternion) {
lhs = Quaternion(
a: lhs.a - rhs.a,
b: lhs.b - rhs.b,
c: lhs.c - rhs.c,
d: lhs.d - rhs.d
)
}
public static func *= (lhs: inout Quaternion, rhs: Quaternion) {
lhs = Quaternion(
a: lhs.a*rhs.a - lhs.b*rhs.b - lhs.c*rhs.c - lhs.d*rhs.d,
b: lhs.a*rhs.b + lhs.b*rhs.a + lhs.c*rhs.d - lhs.d*rhs.c,
c: lhs.a*rhs.c - lhs.b*rhs.d + lhs.c*rhs.a + lhs.d*rhs.b,
d: lhs.a*rhs.d + lhs.b*rhs.c - lhs.c*rhs.b + lhs.d*rhs.a
)
}
}
extension Quaternion: CustomStringConvertible {
var description: String {
let formatter = NumberFormatter()
formatter.positivePrefix = "+"
let f: (Double) -> String = { formatter.string(from: $0 as NSNumber)! }
return [f(a), f(b), "i", f(c), "j", f(d), "k"].joined()
}
}
extension Quaternion {
var norm: Double {
return sqrt(a*a + b*b + c*c + d*d)
}
var conjugate: Quaternion {
return Quaternion(a: a, b: -b, c: -c, d: -d)
}
public static func + (lhs: Double, rhs: Quaternion) -> Quaternion {
var result = rhs
result.a += lhs
return result
}
public static func + (lhs: Quaternion, rhs: Double) -> Quaternion {
var result = lhs
result.a += rhs
return result
}
public static func * (lhs: Double, rhs: Quaternion) -> Quaternion {
return Quaternion(a: lhs*rhs.a, b: lhs*rhs.b, c: lhs*rhs.c, d: lhs*rhs.d)
}
public static func * (lhs: Quaternion, rhs: Double) -> Quaternion {
return Quaternion(a: lhs.a*rhs, b: lhs.b*rhs, c: lhs.c*rhs, d: lhs.d*rhs)
}
public static prefix func - (x: Quaternion) -> Quaternion {
return Quaternion(a: -x.a, b: -x.b, c: -x.c, d: -x.d)
}
}
 
let q: Quaternion = 1 + 2 * .i + 3 * .j + 4 * .k // 1+2i+3j+4k
let q1: Quaternion = 2 + 3 * .i + 4 * .j + 5 * .k // 2+3i+4j+5k
let q2: Quaternion = 3 + 4 * .i + 5 * .j + 6 * .k // 3+4i+5j+6k
let r: Double = 7
 
print("""
q = \(q)
q1 = \(q1)
q2 = \(q2)
r = \(r)
-q = \(-q)
‖q‖ = \(q.norm)
conjugate of q = \(q.conjugate)
r + q = q + r = \(r+q) = \(q+r)
q₁ + q₂ = \(q1 + q2) = \(q2 + q1)
qr = rq = \(q*r) = \(r*q)
q₁q₂ = \(q1 * q2)
q₂q₁ = \(q2 * q1)
q₁q₂ ≠ q₂q₁ is \(q1*q2 != q2*q1)
""")</syntaxhighlight>
 
{{out}}
<pre>
q = +1+2i+3j+4k
q1 = +2+3i+4j+5k
q2 = +3+4i+5j+6k
r = 7.0
-q = -1-2i-3j-4k
‖q‖ = 5.477225575051661
conjugate of q = +1-2i-3j-4k
r + q = q + r = +8+2i+3j+4k = +8+2i+3j+4k
q₁ + q₂ = +5+7i+9j+11k = +5+7i+9j+11k
qr = rq = +7+14i+21j+28k = +7+14i+21j+28k
q₁q₂ = -56+16i+24j+26k
q₂q₁ = -56+18i+20j+28k
q₁q₂ ≠ q₂q₁ is true
</pre>
 
=={{header|Tcl}}==
{{works with|Tcl|8.6}} or {{libheader|TclOO}}
<langsyntaxhighlight lang="tcl">package require TclOO
 
# Support class that provides C++-like RAII lifetimes
Line 5,696 ⟶ 8,879:
 
export - + * ==
}</langsyntaxhighlight>
Demonstration code:
<langsyntaxhighlight lang="tcl">set q [Q new 1 2 3 4]
set q1 [Q new 2 3 4 5]
set q2 [Q new 3 4 5 6]
Line 5,719 ⟶ 8,902:
puts "q1 * q2 = [[$q1 * $q2] p]"
puts "q2 * q1 = [[$q2 * $q1] p]"
puts "equal(q1*q2, q2*q1) = [[$q1 * $q2] == [$q2 * $q1]]"</langsyntaxhighlight>
{{out}}
<pre>
Line 5,738 ⟶ 8,921:
q2 * q1 = Q(-56.0,18.0,20.0,28.0)
equal(q1*q2, q2*q1) = 0
</pre>
 
=={{header|VBA}}==
<syntaxhighlight lang="vb">Option Base 1
Private Function norm(q As Variant) As Double
norm = Sqr(WorksheetFunction.SumSq(q))
End Function
 
Private Function negative(q) As Variant
Dim res(4) As Double
For i = 1 To 4
res(i) = -q(i)
Next i
negative = res
End Function
 
Private Function conj(q As Variant) As Variant
Dim res(4) As Double
res(1) = q(1)
For i = 2 To 4
res(i) = -q(i)
Next i
conj = res
End Function
 
Private Function addr(r As Double, q As Variant) As Variant
Dim res As Variant
res = q
res(1) = r + q(1)
addr = res
End Function
 
Private Function add(q1 As Variant, q2 As Variant) As Variant
add = WorksheetFunction.MMult(Array(1, 1), Array(q1, q2))
End Function
 
Private Function multr(r As Double, q As Variant) As Variant
multr = WorksheetFunction.MMult(r, q)
End Function
 
Private Function mult(q1 As Variant, q2 As Variant)
Dim res(4) As Double
res(1) = q1(1) * q2(1) - q1(2) * q2(2) - q1(3) * q2(3) - q1(4) * q2(4)
res(2) = q1(1) * q2(2) + q1(2) * q2(1) + q1(3) * q2(4) - q1(4) * q2(3)
res(3) = q1(1) * q2(3) - q1(2) * q2(4) + q1(3) * q2(1) + q1(4) * q2(2)
res(4) = q1(1) * q2(4) + q1(2) * q2(3) - q1(3) * q2(2) + q1(4) * q2(1)
mult = res
End Function
 
Private Sub quats(q As Variant)
Debug.Print q(1); IIf(q(2) < 0, " - " & Abs(q(2)), " + " & q(2));
Debug.Print IIf(q(3) < 0, "i - " & Abs(q(3)), "i + " & q(3));
Debug.Print IIf(q(4) < 0, "j - " & Abs(q(4)), "j + " & q(4)); "k"
End Sub
 
Public Sub quaternions()
q = [{ 1, 2, 3, 4}]
q1 = [{2, 3, 4, 5}]
q2 = [{3, 4, 5, 6}]
Dim r_ As Double
r_ = 7#
Debug.Print "q = ";: quats q
Debug.Print "q1 = ";: quats q1
Debug.Print "q2 = ";: quats q2
Debug.Print "r = "; r_
Debug.Print "norm(q) = "; norm(q)
Debug.Print "negative(q) = ";: quats negative(q)
Debug.Print "conjugate(q) = ";: quats conj(q)
Debug.Print "r + q = ";: quats addr(r_, q)
Debug.Print "q1 + q2 = ";: quats add(q1, q2)
Debug.Print "q * r = ";: quats multr(r_, q)
Debug.Print "q1 * q2 = ";: quats mult(q1, q2)
Debug.Print "q2 * q1 = ";: quats mult(q2, q1)
End Sub</syntaxhighlight>{{out}}
<pre>q = 1 + 2i + 3j + 4k
q1 = 2 + 3i + 4j + 5k
q2 = 3 + 4i + 5j + 6k
r = 7
norm(q) = 5,47722557505166
negative(q) = -1 - 2i - 3j - 4k
conjugate(q) = 1 - 2i - 3j - 4k
r + q = 8 + 2i + 3j + 4k
q1 + q2 = 5 + 7i + 9j + 11k
q * r = 7 + 14i + 21j + 28k
q1 * q2 = -56 + 16i + 24j + 26k
q2 * q1 = -56 + 18i + 20j + 28k</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
'''Compiler:''' Roslyn Visual Basic (language version >= 14, e.g. with Visual Studio 2015)
{{works with|.NET Core|2.1}}
 
<syntaxhighlight lang="vbnet">Option Compare Binary
Option Explicit On
Option Infer On
Option Strict On
 
Structure Quaternion
Implements IEquatable(Of Quaternion), IStructuralEquatable
 
Public ReadOnly A, B, C, D As Double
 
Public Sub New(a As Double, b As Double, c As Double, d As Double)
Me.A = a
Me.B = b
Me.C = c
Me.D = d
End Sub
 
Public ReadOnly Property Norm As Double
Get
Return Math.Sqrt((Me.A ^ 2) + (Me.B ^ 2) + (Me.C ^ 2) + (Me.D ^ 2))
End Get
End Property
 
Public ReadOnly Property Conjugate As Quaternion
Get
Return New Quaternion(Me.A, -Me.B, -Me.C, -Me.D)
End Get
End Property
 
Public Overrides Function Equals(obj As Object) As Boolean
If TypeOf obj IsNot Quaternion Then Return False
Return Me.Equals(DirectCast(obj, Quaternion))
End Function
 
Public Overloads Function Equals(other As Quaternion) As Boolean Implements IEquatable(Of Quaternion).Equals
Return other = Me
End Function
 
Public Overloads Function Equals(other As Object, comparer As IEqualityComparer) As Boolean Implements IStructuralEquatable.Equals
If TypeOf other IsNot Quaternion Then Return False
Dim q = DirectCast(other, Quaternion)
Return comparer.Equals(Me.A, q.A) AndAlso
comparer.Equals(Me.B, q.B) AndAlso
comparer.Equals(Me.C, q.C) AndAlso
comparer.Equals(Me.D, q.D)
End Function
 
Public Overrides Function GetHashCode() As Integer
Return HashCode.Combine(Me.A, Me.B, Me.C, Me.D)
End Function
 
Public Overloads Function GetHashCode(comparer As IEqualityComparer) As Integer Implements IStructuralEquatable.GetHashCode
Return HashCode.Combine(
comparer.GetHashCode(Me.A),
comparer.GetHashCode(Me.B),
comparer.GetHashCode(Me.C),
comparer.GetHashCode(Me.D))
End Function
 
Public Overrides Function ToString() As String
Return $"Q({Me.A}, {Me.B}, {Me.C}, {Me.D})"
End Function
 
#Region "Operators"
Public Shared Operator =(left As Quaternion, right As Quaternion) As Boolean
Return left.A = right.A AndAlso
left.B = right.B AndAlso
left.C = right.C AndAlso
left.D = right.D
End Operator
 
Public Shared Operator <>(left As Quaternion, right As Quaternion) As Boolean
Return Not left = right
End Operator
 
Public Shared Operator +(q1 As Quaternion, q2 As Quaternion) As Quaternion
Return New Quaternion(q1.A + q2.A, q1.B + q2.B, q1.C + q2.C, q1.D + q2.D)
End Operator
 
Public Shared Operator -(q As Quaternion) As Quaternion
Return New Quaternion(-q.A, -q.B, -q.C, -q.D)
End Operator
 
Public Shared Operator *(q1 As Quaternion, q2 As Quaternion) As Quaternion
Return New Quaternion(
(q1.A * q2.A) - (q1.B * q2.B) - (q1.C * q2.C) - (q1.D * q2.D),
(q1.A * q2.B) + (q1.B * q2.A) + (q1.C * q2.D) - (q1.D * q2.C),
(q1.A * q2.C) - (q1.B * q2.D) + (q1.C * q2.A) + (q1.D * q2.B),
(q1.A * q2.D) + (q1.B * q2.C) - (q1.C * q2.B) + (q1.D * q2.A))
End Operator
 
Public Shared Widening Operator CType(d As Double) As Quaternion
Return New Quaternion(d, 0, 0, 0)
End Operator
#End Region
End Structure</syntaxhighlight>
 
Demonstration:
<syntaxhighlight lang="vbnet">Module Program
Sub Main()
Dim q As New Quaternion(1, 2, 3, 4),
q1 As New Quaternion(2, 3, 4, 5),
q2 As New Quaternion(3, 4, 5, 6),
r As Double = 7
 
Console.WriteLine($"q = {q}")
Console.WriteLine($"q1 = {q1}")
Console.WriteLine($"q2 = {q2}")
Console.WriteLine($"r = {r}")
Console.WriteLine($"q.Norm = {q.Norm}")
Console.WriteLine($"q1.Norm = {q1.Norm}")
Console.WriteLine($"q2.Norm = {q2.Norm}")
Console.WriteLine($"-q = {-q}")
Console.WriteLine($"q.Conjugate = {q.Conjugate}")
Console.WriteLine($"q + r = {q + r}")
Console.WriteLine($"q1 + q2 = {q1 + q2}")
Console.WriteLine($"q2 + q1 = {q2 + q1}")
Console.WriteLine($"q * r = {q * r}")
Console.WriteLine($"q1 * q2 = {q1 * q2}")
Console.WriteLine($"q2 * q1 = {q2 * q1}")
Console.WriteLine($"q1*q2 {If((q1 * q2) = (q2 * q1), "=", "!=")} q2*q1")
End Sub
End Module</syntaxhighlight>
 
{{out}}
<pre>q = Q(1, 2, 3, 4)
q1 = Q(2, 3, 4, 5)
q2 = Q(3, 4, 5, 6)
r = 7
q.Norm = 5.47722557505166
q1.Norm = 7.34846922834953
q2.Norm = 9.2736184954957
-q = Q(-1, -2, -3, -4)
q.Conjugate = Q(1, -2, -3, -4)
q + r = Q(8, 2, 3, 4)
q1 + q2 = Q(5, 7, 9, 11)
q2 + q1 = Q(5, 7, 9, 11)
q * r = Q(7, 14, 21, 28)
q1 * q2 = Q(-56, 16, 24, 26)
q2 * q1 = Q(-56, 18, 20, 28)
q1*q2 != q2*q1</pre>
 
=={{header|Wren}}==
<syntaxhighlight lang="wren">class Quaternion {
construct new(a, b, c, d ) {
_a = a
_b = b
_c = c
_d = d
}
 
a { _a }
b { _b }
c { _c }
d { _d }
 
norm { (a*a + b*b + c*c + d*d).sqrt }
 
- { Quaternion.new(-a, -b, -c, -d) }
 
conj { Quaternion.new(a, -b, -c, -d) }
 
+ (q) {
if (q is Num) return Quaternion.new(a + q, b, c, d)
return Quaternion.new(a + q.a, b + q.b, c + q.c, d + q.d)
}
 
* (q) {
if (q is Num) return Quaternion.new(a * q, b * q, c * q, d * q)
return Quaternion.new(a*q.a - b*q.b - c*q.c - d*q.d,
a*q.b + b*q.a + c*q.d - d*q.c,
a*q.c - b*q.d + c*q.a + d*q.b,
a*q.d + b*q.c - c*q.b + d*q.a)
}
 
== (q) { a == q.a && b == q.b && c == q.c && d == q.d }
!= (q) { !(this == q) }
 
toString { "(%(a), %(b), %(c), %(d))" }
 
static realAdd(r, q) { q + r }
 
static realMul(r, q) { q * r }
}
 
var q = Quaternion.new(1, 2, 3, 4)
var q1 = Quaternion.new(2, 3, 4, 5)
var q2 = Quaternion.new(3, 4, 5, 6)
var q3 = q1 * q2
var q4 = q2 * q1
var r = 7
 
System.print("q = %(q)")
System.print("q1 = %(q1)")
System.print("q2 = %(q2)")
System.print("r = %(r)")
System.print("norm(q) = %(q.norm)")
System.print("-q = %(-q)")
System.print("conj(q) = %(q.conj)")
System.print("r + q = %(Quaternion.realAdd(r, q))")
System.print("q + r = %(q + r))")
System.print("q1 + q2 = %(q1 + q2)")
System.print("q2 + q1 = %(q2 + q1)")
System.print("rq = %(Quaternion.realMul(r, q))")
System.print("qr = %(q * r)")
System.print("q1q2 = %(q3)")
System.print("q2q1 = %(q4)")
System.print("q1q2 ≠ q2q1 = %(q3 != q4)")</syntaxhighlight>
 
{{out}}
<pre>
q = (1, 2, 3, 4)
q1 = (2, 3, 4, 5)
q2 = (3, 4, 5, 6)
r = 7
norm(q) = 5.4772255750517
-q = (-1, -2, -3, -4)
conj(q) = (1, -2, -3, -4)
r + q = (8, 2, 3, 4)
q + r = (8, 2, 3, 4))
q1 + q2 = (5, 7, 9, 11)
q2 + q1 = (5, 7, 9, 11)
rq = (7, 14, 21, 28)
qr = (7, 14, 21, 28)
q1q2 = (-56, 16, 24, 26)
q2q1 = (-56, 18, 20, 28)
q1q2 ≠ q2q1 = true
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">proc QPrint(Q); \Display quaternion
real Q;
[RlOut(0, Q(0)); Text(0, " + "); RlOut(0, Q(1)); Text(0, "i + ");
RlOut(0, Q(2)); Text(0, "j + "); RlOut(0, Q(3)); Text(0, "k");
CrLf(0);
];
func real QNorm(Q); \Return norm of a quaternion
real Q;
return sqrt( Q(0)*Q(0) + Q(1)*Q(1) + Q(2)*Q(2) + Q(3)*Q(3) );
 
func real QNeg(Q, R); \Return negative of a quaternion: Q:= -R
real Q, R;
[Q(0):= -R(0); Q(1):= -R(1); Q(2):= -R(2); Q(3):= -R(3);
return Q;
];
func real QConj(Q, R); \Return conjugate of a quaternion: Q:= conj R
real Q, R;
[Q(0):= R(0); Q(1):= -R(1); Q(2):= -R(2); Q(3):= -R(3);
return Q;
];
func real QRAdd(Q, R, Real); \Return quaternion plus real: Q:= R + Real
real Q, R, Real;
[Q(0):= R(0) + Real; Q(1):= R(1); Q(2):= R(2); Q(3):= R(3);
return Q;
];
func real QAdd(Q, R, S); \Return quaternion sum: Q:= R + S
real Q, R, S;
[Q(0):= R(0) + S(0); Q(1):= R(1) + S(1); Q(2):= R(2) + S(2); Q(3):= R(3) + S(3);
return Q;
];
func real QRMul(Q, R, Real); \Return quaternion times real: Q:= R + Real
real Q, R, Real;
[Q(0):= R(0) * Real; Q(1):= R(1) * Real; Q(2):= R(2) * Real; Q(3):= R(3) * Real;
return Q;
];
func real QMul(Q, R, S); \Return quaternion product: Q:= R * S
real Q, R, S;
[Q(0):= R(0)*S(0) - R(1)*S(1) - R(2)*S(2) - R(3)*S(3);
Q(1):= R(0)*S(1) + R(1)*S(0) + R(2)*S(3) - R(3)*S(2);
Q(2):= R(0)*S(2) - R(1)*S(3) + R(2)*S(0) + R(3)*S(1);
Q(3):= R(0)*S(3) + R(1)*S(2) - R(2)*S(1) + R(3)*S(0);
return Q;
];
 
real Q, Q1, Q2, R, Q0(4),;
[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;
Format(3, 1);
Text(0, "q = "); QPrint(Q);
Text(0, "q1 = "); QPrint(Q1);
Text(0, "q2 = "); QPrint(Q2);
Text(0, "norm(q) = "); RlOut(0, QNorm(Q)); CrLf(0);
Text(0, "-q = "); QPrint(QNeg(Q0, Q));
Text(0, "conj(q) = "); QPrint(QConj(Q0, Q));
Text(0, "r + q = "); QPrint(QRAdd(Q0, Q, R));
Text(0, "q1 + q2 = "); QPrint(QAdd (Q0, Q1, Q2));
Text(0, "r * q = "); QPrint(QRMul(Q0, Q, R));
Text(0, "q1 * q2 = "); QPrint(QMul (Q0, Q1, Q2));
Text(0, "q2 * q1 = "); QPrint(QMul (Q0, Q2, Q1));
]</syntaxhighlight>
 
{{out}}
<pre>
q = 1.0 + 2.0i + 3.0j + 4.0k
q1 = 2.0 + 3.0i + 4.0j + 5.0k
q2 = 3.0 + 4.0i + 5.0j + 6.0k
norm(q) = 5.5
-q = -1.0 + -2.0i + -3.0j + -4.0k
conj(q) = 1.0 + -2.0i + -3.0j + -4.0k
r + q = 8.0 + 2.0i + 3.0j + 4.0k
q1 + q2 = 5.0 + 7.0i + 9.0j + 11.0k
r * q = 7.0 + 14.0i + 21.0j + 28.0k
q1 * q2 = -56.0 + 16.0i + 24.0j + 26.0k
q2 * q1 = -56.0 + 18.0i + 20.0j + 28.0k
</pre>
 
=={{header|zkl}}==
{{trans|D}}
<langsyntaxhighlight lang="zkl">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) )
Line 5,794 ⟶ 9,375:
(iversor*inorm.sin() + inorm.cos()) * r.exp();
}
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl"> // Demo code
r:=7;
q:=Quat(2,3,4,5); q1:=Quat(2,3,4,5); q2:=Quat(3,4,5,6);
Line 5,833 ⟶ 9,414:
println(" s.log(): ", s.log());
println(" s.log().exp(): ", s.log().exp());
println(" s.exp().log(): ", s.exp().log());</langsyntaxhighlight>
{{out}}
<pre>
1,983

edits