-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix_Lib.adb
316 lines (272 loc) · 11 KB
/
Matrix_Lib.adb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
-------------------------------------------------------------------------------
-- --
-- Matrix Lib --
-- --
-- Matrix_Lib.adb --
-- --
-- BODY --
-- --
-- Copyright (C) 1996 Ulrik Hørlyk Hjort --
-- --
-- Matrix Lib is free software; you can redistribute it --
-- and/or modify it under terms of the GNU General Public License --
-- as published by the Free Software Foundation; either version 2, --
-- or (at your option) any later version. --
-- Matrix Lib is distributed in the hope that it will be --
-- useful, but WITHOUT ANY WARRANTY; without even the implied warranty --
-- of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
-- See the GNU General Public License for more details. --
-- You should have received a copy of the GNU General --
-- Public License distributed with Yolk. If not, write to the Free --
-- Software Foundation, 51 Franklin Street, Fifth Floor, Boston, --
-- MA 02110 - 1301, USA. --
-- --
-------------------------------------------------------------------------------
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Number_Theory_Tools; use Number_Theory_Tools;
with Permutations_Generic;
package body Matrix_Lib is
package Permutations_Package is new Permutations_Generic(Natural); use Permutations_Package;
----------------------------------------------------------------------------
--
----------------------------------------------------------------------------
function "+" (Left, Right : Matrix) return Matrix is
RetVal : Matrix(Left'Range(1), Left'Range(2)) := (others =>(others => 0.0));
begin
for I in Left'Range(1) loop
for J in Left'Range(2) loop
RetVal(I,J) :=Left(I,J) + Right(I,J);
end loop;
end loop;
return RetVal;
end "+";
----------------------------------------------------------------------------
--
----------------------------------------------------------------------------
function "-" (Left, Right : Matrix) return Matrix is
RetVal : Matrix(Left'Range(1), Left'Range(2)) := (others =>(others => 0.0));
begin
for I in Left'Range(1) loop
for J in Left'Range(2) loop
RetVal(I,J) :=Left(I,J) - Right(I,J);
end loop;
end loop;
return RetVal;
end "-";
----------------------------------------------------------------------------
--
----------------------------------------------------------------------------
function "*" (Left, Right : Matrix) return Matrix is
RetVal : Matrix(Left'Range(1), Right'Range(2)) := (others =>(others => 0.0));
begin
if Left'Length(2) /= Right'Length(1) then
raise Constraint_Error;
end if;
for I in Left'Range(1) loop
for J in Right'Range(2) loop
for K in Left'Range(2) loop
RetVal(I,J) := RetVal(I,J) + Left(I, K)*Right(K, J);
end loop;
end loop;
end loop;
return RetVal;
end "*";
----------------------------------------------------------------------------
--
----------------------------------------------------------------------------
procedure Print(M : Matrix) is
begin
for I in M'Range(1) loop
for J in M'Range(2) loop
Put(Item => M(I,J), Aft => 1 ,EXP => 0); Put(" ");
end loop;
New_Line;
end loop;
New_Line;
end Print;
----------------------------------------------------------------------------
--
----------------------------------------------------------------------------
procedure Forward_Substitution(A : in out Matrix) is
M : constant Natural := A'Last(1);
N : constant Natural := A'Last(2);
Maxi : Natural;
Element : Float;
begin
for I in A'Range(1) loop
Maxi := I;
for J in (I + 1) .. M loop
if A(J,I) > A(Maxi,I) then
Maxi := J;
end if;
end loop;
-- Interchange rows:
for J in A'Range(2) loop
Element := A(Maxi,J);
A(Maxi,J) := A(I,J);
A(I,J) := Element;
end loop;
for J in reverse I .. N loop
for K in (I + 1) .. M loop
A(K,J) := A(K,J) - (A(K,I)/A(I,I) * A(I,J));
end loop;
end loop;
end loop;
end Forward_Substitution;
----------------------------------------------------------------------------
--
----------------------------------------------------------------------------
procedure Reverse_Elimination(A : in out Matrix) is
N : constant Natural := A'Last(2);
begin
for I in reverse A'Range(1) loop
A(I,N) := A(I,N) / A(I,I);
A(I,I) := 1.0;
for J in reverse 0 .. (I-1) loop
A(J,N) := A(J,N) - (A(J,I) * A(I,N));
A(J,I) := 0.0;
end loop;
end loop;
end Reverse_Elimination;
----------------------------------------------------------------------------
--
-- Gaussian eliminination of the matrix A
--
----------------------------------------------------------------------------
procedure Gaussian_Elimination(A : in out Matrix) is
begin
Forward_Substitution(A);
Reverse_Elimination(A);
end Gaussian_Elimination;
-------------------------------------------------------------------------------
-- Inverse matrix I of A calculated by Gaussian elimination by solving the
-- equation system:
--
-- AX = Bi, where Bi is the i row in the identity matrix of same size as A
-- and B is size (A_row, 1)
--
-- For each solution Bi, Ii = Bi
--
--
-- Returns the inverse matrix to A
-------------------------------------------------------------------------------
function Get_Inverse_By_Gaussian_Elimination(A : Matrix) return Matrix is
Inverse : Matrix(A'Range(1), A'Range(2)) := (others => (others => 0.0));
M : Matrix(A'Range(1), A'First(2) .. A'Last(2) + 1) := (others => (others => 0.0));
begin
for K in A'Range(2) loop
-- Copy A into M:
for I in A'Range(1) loop
for J in A'Range(2) loop
M(I,J) := A(I,J);
end loop;
M(I,M'Last(2)) := 0.0;
end loop;
-- Create next unit column:
M(K,M'Last(2)) := 1.0;
Gaussian_Elimination(M);
-- Insert next column in inverse matrix:
for L in A'Range(1) loop
Inverse(L,K) := M(L,M'Last(2));
end loop;
end loop;
return Inverse;
end Get_Inverse_By_Gaussian_Elimination;
----------------------------------------------------------------------------
--
-- Solve the equation system AX=0
--
-- X is returned in Column
----------------------------------------------------------------------------
procedure Solve_Equation_System(A : in Matrix; Solution : out Column) is
A_Copy : Matrix := A;
begin
Gaussian_Elimination(A_Copy);
for I in Solution'Range loop
Solution(I) := A_Copy(I,A_Copy'Last(2));
end loop;
end Solve_Equation_System;
----------------------------------------------------------------------------
--
-- Calculate the determinant for the n*n matrix A by the Leibniz formula
-- as the sum of all permutations P over the set S in A'Range(1):
--
-- Det = SUM(parity(P) PROD(AiPi))
-- P in S i = 1 .. n
--
----------------------------------------------------------------------------
function Determinant(A : in Matrix) return Float is
Permutations_List : Permutations_T(0 .. Natural(Factorial(A'Length(1))), A'Range(1));
Number_Of_Permutations : Natural := 0;
Initial : List_T(A'Range(1));
Permutation : List_T(A'Range(1));
Sign : Float;
Product : Float := 1.0;
Sum : Float := 0.0;
begin
for I in A'Range(1) loop
Initial(I) := I;
Permutation(I) := I;
end loop;
Permute(Permutation,0, Permutations_List,Number_Of_Permutations);
for I in Permutations_List'First .. Permutations_List'Last-1 loop
Permutation := Get_Permutation(I, Permutations_List);
Sign := Float(Parity(Initial,Permutation));
Product := 1.0 * sign;
for J in Permutation'Range loop
Product := Product * A(J,Permutation(J));
end loop;
Sum := Sum + Product;
end loop;
return Sum;
end Determinant;
-------------------------------------------------------------------------------
--
-- Returns the cofactor Crow,column
--
-------------------------------------------------------------------------------
function Get_Minor(A : Matrix; Row : Natural; Column : Natural) return Matrix is
Minor : Matrix(A'First(1) .. A'Last(1)-1, A'First(2) .. A'Last(2)-1) := (others =>(others =>0.0));
Row_Number : Natural := 0;
Column_Number : Natural := 0;
begin
for I in A'Range(1) loop
if I /= Row then
Column_Number := 0;
for J in A'Range(1) loop
if J /= Column then
Minor(Row_Number,Column_Number) := A(I,J);
Column_Number := Column_Number + 1;
end if;
end loop;
Row_Number := Row_Number + 1;
end if;
end loop;
return Minor;
end Get_Minor;
-------------------------------------------------------------------------------
--
-- Calculate the inverse of matrix A
--
-------------------------------------------------------------------------------
function Get_Inverse(A : Matrix) return Matrix is
Inverse : Matrix(A'Range(1), A'Range(2)) := (others =>(others =>0.0));
Minor : Matrix(A'First(1) .. A'Last(1)-1, A'First(2) .. A'Last(2)-1) := (others =>(others =>0.0));
Det : Float := Determinant(A);
begin
if Det /= 0.0 then
Det := 1.0 / Det;
for J in A'Range(1) loop
for I in A'Range(1) loop
Minor := Get_Minor(A,J,I);
Inverse(I,J) := Det * Determinant(Minor);
if (I+J) mod 2 = 1 then
Inverse(I,J) := - Inverse(I,J);
end if;
end loop;
end loop;
end if;
return Inverse;
end Get_Inverse;
end Matrix_Lib;