Skip to content

Commit

Permalink
Move routines into BBS.Numerical namespace and add some simple statis…
Browse files Browse the repository at this point in the history
…tics.
  • Loading branch information
BrentSeidel committed Jan 31, 2024
1 parent 85f3e96 commit 7d2a313
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 9 deletions.
2 changes: 1 addition & 1 deletion numerical.gpr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
with "..\BBS-Ada\bbs.gpr";
with "../BBS-Ada/bbs.gpr";

library project Numerical is

Expand Down
47 changes: 47 additions & 0 deletions src/BBS-Numerical-Statistics.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--with Ada.Text_IO;
--with Ada.Float_Text_IO;
with Ada.Numerics.Generic_Elementary_Functions;
package body BBS.Numerical.Statistics is
--
-- Compute the mean of an array of data
--
function mean(d : data_array) return F is
sum : F := 0.0;
begin
for i in d'Range loop
sum := sum + d(i);
end loop;
return sum/F(d'Length);
end;
--
-- Compute the limits of an array of data
--
procedure limits(d : data_array; min : out F; max : out F) is
begin
min := d(d'First);
max := d(d'First);
for i in (d'First + 1) .. d'Last loop
if d(i) > max then
max := d(i);
end if;
if d(i) < min then
min := d(i);
end if;
end loop;
end;
--
-- Compute the variance (and mean) of an array of data. Use this,
-- instead of mean() if you need both values.
--
procedure variance(d : data_array; var : out F; mean : out F) is
sum2 : F := 0.0;
sum : F := 0.0;
begin
for i in d'Range loop
sum := sum + d(i);
sum2 := sum2 + (d(i)*d(i));
end loop;
mean := sum/F(d'Length);
var := (sum2 - sum*sum/F(d'Length))/F(d'Length - 1);
end;
end;
18 changes: 18 additions & 0 deletions src/BBS-Numerical-Statistics.ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
generic
type F is digits <>;
package BBS.Numerical.statistics is
type data_array is array (Integer range <>) of F;
--
-- Compute the mean of an array of data
--
function mean(d : data_array) return F;
--
-- Compute the median of an array of data
--
procedure limits(d : data_array; min : out F; max : out F);
--
-- Compute the variance (and mean) of an array of data. Use this,
-- instead of mean() if you need both values.
--
procedure variance(d : data_array; var : out F; mean : out F);
end;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
with Ada.Numerics.Generic_Elementary_Functions;
package body BBS.complex_real is
package body BBS.Numerical.complex_real is
package elem is new Ada.Numerics.Generic_Elementary_Functions(f);
--
-- Basic manipulations
Expand Down Expand Up @@ -122,4 +122,4 @@ package body BBS.complex_real is
return (r => t1 * elem.Cos(self.i), i => t1 * elem.Sin(self.i));
end;
--
end BBS.complex_real;
end BBS.Numerical.complex_real;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
--
generic
type F is digits <>;
package BBS.complex_real is
package BBS.Numerical.complex_real is
type complex is tagged record
r : f'Base;
i : f'Base;
Expand Down Expand Up @@ -63,4 +63,4 @@ package BBS.complex_real is
i : complex renames j;
private

end BBS.complex_real;
end BBS.Numerical.complex_real;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package body BBS.integration_real is
package body BBS.Numerical.integration_real is
--
function trapezoid(test : test_func; lower, upper : f; steps : Positive) return f is
step_size : constant f := (upper - lower)/f(steps);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
generic
type F is digits <>;
package BBS.integration_real is
package BBS.Numerical.integration_real is
type test_func is access function (x : f) return f;
--
-- Integrate the provided function between the lower and upper limits using
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--with Ada.Text_IO;
--with Ada.Float_Text_IO;
with Ada.Numerics.Generic_Elementary_Functions;
package body BBS.roots_real is
package body BBS.Numerical.roots_real is
package elem is new Ada.Numerics.Generic_Elementary_Functions(f);
--
-- Bisection algorithm for finding a root.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
generic
type F is digits <>;
package BBS.roots_real is
package BBS.Numerical.roots_real is
type errors is (none, bad_args, no_solution);
type test_func is access function (x : f) return f;

Expand Down
7 changes: 7 additions & 0 deletions src/BBS-Numerical.ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--
-- Empty package to create a namespace. Common items may be added here
-- later.
--
package BBS.Numerical is
-- Currently empty.
end;

0 comments on commit 7d2a313

Please sign in to comment.