Skip to content

Commit

Permalink
Start basics of a quaternion library.
Browse files Browse the repository at this point in the history
  • Loading branch information
BrentSeidel committed May 7, 2024
1 parent 7d2a313 commit 6b4669d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/BBS-Numerical-quaternion.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
with Ada.Numerics.Generic_Elementary_Functions;
package body BBS.Numerical.quaternion is
package elem is new Ada.Numerics.Generic_Elementary_Functions(f);
--
-- Basic operations
--
function "+" (Left, Right : quaternion) return quaternion is
begin
return (r => Left.r + Right.r,
i => Left.i + Right.i,
j => Left.j + Right.j,
k => Left.k + Right.k);
end;
--
function "-" (Left, Right : quaternion) return quaternion is
begin
return (r => Left.r - Right.r,
i => Left.i - Right.i,
j => Left.j - Right.j,
k => Left.k - Right.k);
end;

function "*" (Left : f; Right : quaternion) return quaternion is
begin
return (r => Left * Right.r,
i => Left * Right.i,
j => Left * Right.j,
k => Left * Right.k);
end;
--
function "*" (Left : quaternion; Right : f) return quaternion is
begin
return (r => Left.r * Right,
i => Left.i * Right,
j => Left.j * Right,
k => Left.k * Right);
end;
--
function "/" (Left : quaternion; Right : f) return quaternion is
begin
return (r => Left.r / Right,
i => Left.i / Right,
j => Left.j / Right,
k => Left.k / Right);
end;
--
function magnitude(self : in quaternion) return f'Base is
begin
return elem.Sqrt(self.r * self.r + self.i * self.i +
self.j * self.j + self.k * self.k);
end;
--
end BBS.Numerical.quaternion;
26 changes: 26 additions & 0 deletions src/BBS-Numerical-quaternion.ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--
-- This is a simple package that supports basic functions on quaternions.
--
generic
type F is digits <>;
package BBS.Numerical.quaternion is
type quaternion is tagged record
r : f'Base; -- Scalar part
i : f'Base; -- i, j, and k are the vector part
j : f'Base;
k : f'Base;
end record;
--
-- Basic arithmatic operations
--
function "+" (Left, Right : quaternion) return quaternion;
function "-" (Left, Right : quaternion) return quaternion;

function "*" (Left : f; Right : quaternion) return quaternion;
function "*" (Left : quaternion; Right : f) return quaternion;

function "/" (Left : quaternion; Right : f) return quaternion;

function magnitude(self : in quaternion) return f'Base;

end BBS.Numerical.quaternion;

0 comments on commit 6b4669d

Please sign in to comment.