Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -1,38 +0,0 @@
|
|||
generic
|
||||
type Element_Type is private;
|
||||
Zero : Element_Type;
|
||||
One : Element_Type;
|
||||
with function "+" (Left, Right : Element_Type) return Element_Type is <>;
|
||||
with function "-" (Left, Right : Element_Type) return Element_Type is <>;
|
||||
with function "*" (Left, Right : Element_Type) return Element_Type is <>;
|
||||
with function "/" (Left, Right : Element_Type) return Element_Type is <>;
|
||||
package Matrices is
|
||||
type Vector is array (Positive range <>) of Element_Type;
|
||||
type Matrix is
|
||||
array (Positive range <>, Positive range <>) of Element_Type;
|
||||
|
||||
function "*" (Left, Right : Matrix) return Matrix;
|
||||
function Invert (Source : Matrix) return Matrix;
|
||||
function Reduced_Row_Echelon_Form (Source : Matrix) return Matrix;
|
||||
function Regression_Coefficients
|
||||
(Source : Vector;
|
||||
Regressors : Matrix)
|
||||
return Vector;
|
||||
function To_Column_Vector
|
||||
(Source : Matrix;
|
||||
Row : Positive := 1)
|
||||
return Vector;
|
||||
function To_Matrix
|
||||
(Source : Vector;
|
||||
Column_Vector : Boolean := True)
|
||||
return Matrix;
|
||||
function To_Row_Vector
|
||||
(Source : Matrix;
|
||||
Column : Positive := 1)
|
||||
return Vector;
|
||||
function Transpose (Source : Matrix) return Matrix;
|
||||
|
||||
Size_Mismatch : exception;
|
||||
Not_Square_Matrix : exception;
|
||||
Not_Invertible : exception;
|
||||
end Matrices;
|
||||
|
|
@ -1,189 +0,0 @@
|
|||
package body Matrices is
|
||||
function "*" (Left, Right : Matrix) return Matrix is
|
||||
Result : Matrix (Left'Range (1), Right'Range (2)) :=
|
||||
(others => (others => Zero));
|
||||
begin
|
||||
if Left'Length (2) /= Right'Length (1) then
|
||||
raise Size_Mismatch;
|
||||
end if;
|
||||
for I in Result'Range (1) loop
|
||||
for K in Result'Range (2) loop
|
||||
for J in Left'Range (2) loop
|
||||
Result (I, K) := Result (I, K) + Left (I, J) * Right (J, K);
|
||||
end loop;
|
||||
end loop;
|
||||
end loop;
|
||||
return Result;
|
||||
end "*";
|
||||
|
||||
function Invert (Source : Matrix) return Matrix is
|
||||
Expanded : Matrix (Source'Range (1),
|
||||
Source'First (2) .. Source'Last (2) * 2);
|
||||
Result : Matrix (Source'Range (1), Source'Range (2));
|
||||
begin
|
||||
-- Matrix has to be square.
|
||||
if Source'Length (1) /= Source'Length (2) then
|
||||
raise Not_Square_Matrix;
|
||||
end if;
|
||||
-- Copy Source into Expanded matrix and attach identity matrix to right
|
||||
for Row in Source'Range (1) loop
|
||||
for Col in Source'Range (2) loop
|
||||
Expanded (Row, Col) := Source (Row, Col);
|
||||
Expanded (Row, Source'Last (2) + Col) := Zero;
|
||||
end loop;
|
||||
Expanded (Row, Source'Last (2) + Row) := One;
|
||||
end loop;
|
||||
Expanded := Reduced_Row_Echelon_Form (Source => Expanded);
|
||||
-- Copy right side to Result (= inverted Source)
|
||||
for Row in Result'Range (1) loop
|
||||
for Col in Result'Range (2) loop
|
||||
Result (Row, Col) := Expanded (Row, Source'Last (2) + Col);
|
||||
end loop;
|
||||
end loop;
|
||||
return Result;
|
||||
end Invert;
|
||||
|
||||
function Reduced_Row_Echelon_Form (Source : Matrix) return Matrix is
|
||||
procedure Divide_Row
|
||||
(From : in out Matrix;
|
||||
Row : Positive;
|
||||
Divisor : Element_Type)
|
||||
is
|
||||
begin
|
||||
for Col in From'Range (2) loop
|
||||
From (Row, Col) := From (Row, Col) / Divisor;
|
||||
end loop;
|
||||
end Divide_Row;
|
||||
|
||||
procedure Subtract_Rows
|
||||
(From : in out Matrix;
|
||||
Subtrahend, Minuend : Positive;
|
||||
Factor : Element_Type)
|
||||
is
|
||||
begin
|
||||
for Col in From'Range (2) loop
|
||||
From (Minuend, Col) := From (Minuend, Col) -
|
||||
From (Subtrahend, Col) * Factor;
|
||||
end loop;
|
||||
end Subtract_Rows;
|
||||
|
||||
procedure Swap_Rows (From : in out Matrix; First, Second : Positive) is
|
||||
Temporary : Element_Type;
|
||||
begin
|
||||
for Col in From'Range (2) loop
|
||||
Temporary := From (First, Col);
|
||||
From (First, Col) := From (Second, Col);
|
||||
From (Second, Col) := Temporary;
|
||||
end loop;
|
||||
end Swap_Rows;
|
||||
|
||||
Result : Matrix := Source;
|
||||
Lead : Positive := Result'First (2);
|
||||
I : Positive;
|
||||
begin
|
||||
Rows : for Row in Result'Range (1) loop
|
||||
exit Rows when Lead > Result'Last (2);
|
||||
I := Row;
|
||||
while Result (I, Lead) = Zero loop
|
||||
I := I + 1;
|
||||
if I = Result'Last (1) then
|
||||
I := Row;
|
||||
Lead := Lead + 1;
|
||||
exit Rows when Lead = Result'Last (2);
|
||||
end if;
|
||||
end loop;
|
||||
if I /= Row then
|
||||
Swap_Rows (From => Result, First => I, Second => Row);
|
||||
end if;
|
||||
Divide_Row
|
||||
(From => Result,
|
||||
Row => Row,
|
||||
Divisor => Result (Row, Lead));
|
||||
for Other_Row in Result'Range (1) loop
|
||||
if Other_Row /= Row then
|
||||
Subtract_Rows
|
||||
(From => Result,
|
||||
Subtrahend => Row,
|
||||
Minuend => Other_Row,
|
||||
Factor => Result (Other_Row, Lead));
|
||||
end if;
|
||||
end loop;
|
||||
Lead := Lead + 1;
|
||||
end loop Rows;
|
||||
return Result;
|
||||
end Reduced_Row_Echelon_Form;
|
||||
|
||||
function Regression_Coefficients
|
||||
(Source : Vector;
|
||||
Regressors : Matrix)
|
||||
return Vector
|
||||
is
|
||||
Result : Matrix (Regressors'Range (2), 1 .. 1);
|
||||
begin
|
||||
if Source'Length /= Regressors'Length (1) then
|
||||
raise Size_Mismatch;
|
||||
end if;
|
||||
declare
|
||||
Regressors_T : constant Matrix := Transpose (Regressors);
|
||||
begin
|
||||
Result := Invert (Regressors_T * Regressors) *
|
||||
Regressors_T *
|
||||
To_Matrix (Source);
|
||||
end;
|
||||
return To_Row_Vector (Source => Result);
|
||||
end Regression_Coefficients;
|
||||
|
||||
function To_Column_Vector
|
||||
(Source : Matrix;
|
||||
Row : Positive := 1)
|
||||
return Vector
|
||||
is
|
||||
Result : Vector (Source'Range (2));
|
||||
begin
|
||||
for Column in Result'Range loop
|
||||
Result (Column) := Source (Row, Column);
|
||||
end loop;
|
||||
return Result;
|
||||
end To_Column_Vector;
|
||||
|
||||
function To_Matrix
|
||||
(Source : Vector;
|
||||
Column_Vector : Boolean := True)
|
||||
return Matrix
|
||||
is
|
||||
Result : Matrix (1 .. 1, Source'Range);
|
||||
begin
|
||||
for Column in Source'Range loop
|
||||
Result (1, Column) := Source (Column);
|
||||
end loop;
|
||||
if Column_Vector then
|
||||
return Transpose (Result);
|
||||
else
|
||||
return Result;
|
||||
end if;
|
||||
end To_Matrix;
|
||||
|
||||
function To_Row_Vector
|
||||
(Source : Matrix;
|
||||
Column : Positive := 1)
|
||||
return Vector
|
||||
is
|
||||
Result : Vector (Source'Range (1));
|
||||
begin
|
||||
for Row in Result'Range loop
|
||||
Result (Row) := Source (Row, Column);
|
||||
end loop;
|
||||
return Result;
|
||||
end To_Row_Vector;
|
||||
|
||||
function Transpose (Source : Matrix) return Matrix is
|
||||
Result : Matrix (Source'Range (2), Source'Range (1));
|
||||
begin
|
||||
for Row in Result'Range (1) loop
|
||||
for Column in Result'Range (2) loop
|
||||
Result (Row, Column) := Source (Column, Row);
|
||||
end loop;
|
||||
end loop;
|
||||
return Result;
|
||||
end Transpose;
|
||||
end Matrices;
|
||||
|
|
@ -1,68 +0,0 @@
|
|||
with Ada.Text_IO;
|
||||
with Matrices;
|
||||
procedure Multiple_Regression is
|
||||
package Float_Matrices is new Matrices (
|
||||
Element_Type => Float,
|
||||
Zero => 0.0,
|
||||
One => 1.0);
|
||||
subtype Vector is Float_Matrices.Vector;
|
||||
subtype Matrix is Float_Matrices.Matrix;
|
||||
use type Matrix;
|
||||
|
||||
procedure Output_Matrix (X : Matrix) is
|
||||
begin
|
||||
for Row in X'Range (1) loop
|
||||
for Col in X'Range (2) loop
|
||||
Ada.Text_IO.Put (Float'Image (X (Row, Col)) & ' ');
|
||||
end loop;
|
||||
Ada.Text_IO.New_Line;
|
||||
end loop;
|
||||
end Output_Matrix;
|
||||
|
||||
-- example from Ruby solution
|
||||
V : constant Vector := (1.0, 2.0, 3.0, 4.0, 5.0);
|
||||
M : constant Matrix :=
|
||||
((1 => 2.0),
|
||||
(1 => 1.0),
|
||||
(1 => 3.0),
|
||||
(1 => 4.0),
|
||||
(1 => 5.0));
|
||||
C : constant Vector :=
|
||||
Float_Matrices.Regression_Coefficients (Source => V, Regressors => M);
|
||||
-- Wikipedia example
|
||||
Weight : constant Vector (1 .. 15) :=
|
||||
(52.21, 53.12, 54.48, 55.84, 57.20,
|
||||
58.57, 59.93, 61.29, 63.11, 64.47,
|
||||
66.28, 68.10, 69.92, 72.19, 74.46);
|
||||
Height : Vector (1 .. 15) :=
|
||||
(1.47, 1.50, 1.52, 1.55, 1.57,
|
||||
1.60, 1.63, 1.65, 1.68, 1.70,
|
||||
1.73, 1.75, 1.78, 1.80, 1.83);
|
||||
Height_Matrix : Matrix (1 .. 15, 1 .. 3);
|
||||
begin
|
||||
Ada.Text_IO.Put_Line ("Example from Ruby solution:");
|
||||
Ada.Text_IO.Put_Line ("V:");
|
||||
Output_Matrix (Float_Matrices.To_Matrix (V));
|
||||
Ada.Text_IO.Put_Line ("M:");
|
||||
Output_Matrix (M);
|
||||
Ada.Text_IO.Put_Line ("C:");
|
||||
Output_Matrix (Float_Matrices.To_Matrix (C));
|
||||
Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put_Line ("Example from Wikipedia:");
|
||||
for I in Height'Range loop
|
||||
Height_Matrix (I, 1) := 1.0;
|
||||
Height_Matrix (I, 2) := Height (I);
|
||||
Height_Matrix (I, 3) := Height (I) ** 2;
|
||||
end loop;
|
||||
Ada.Text_IO.Put_Line ("Matrix:");
|
||||
Output_Matrix (Height_Matrix);
|
||||
declare
|
||||
Coefficients : constant Vector :=
|
||||
Float_Matrices.Regression_Coefficients
|
||||
(Source => Weight,
|
||||
Regressors => Height_Matrix);
|
||||
begin
|
||||
Ada.Text_IO.Put_Line ("Coefficients:");
|
||||
Output_Matrix (Float_Matrices.To_Matrix (Coefficients));
|
||||
end;
|
||||
end Multiple_Regression;
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
(let ((x1 '(0 1 2 3 4 5 6 7 8 9 10))
|
||||
(x2 '(0 1 1 3 3 7 6 7 3 9 8))
|
||||
(y '(1 6 17 34 57 86 121 162 209 262 321)))
|
||||
(apply #'calc-eval "fit(a*X1+b*X2+c,[X1,X2],[a,b,c],[$1 $2 $3])" nil
|
||||
(mapcar (lambda (items) (cons 'vec items)) (list x1 x2 y))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue