1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . G E N E R I C _ A R R A Y _ O P E R A T I O N S --
9 -- Copyright (C) 2006-2011, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 package System.Generic_Array_Operations is
33 pragma Pure (Generic_Array_Operations);
40 type Scalar is private;
41 type Matrix is array (Integer range <>, Integer range <>) of Scalar;
42 with function "-" (Left, Right : Scalar) return Scalar is <>;
43 with function "*" (Left, Right : Scalar) return Scalar is <>;
44 with function "/" (Left, Right : Scalar) return Scalar is <>;
45 with function Is_Non_Zero (X : Scalar) return Boolean is <>;
46 procedure Back_Substitute (M, N : in out Matrix);
53 type Scalar is private;
54 type Vector is array (Integer range <>) of Scalar;
55 type Matrix is array (Integer range <>, Integer range <>) of Scalar;
56 function Diagonal (A : Matrix) return Vector;
58 -----------------------
59 -- Forward_Eliminate --
60 -----------------------
62 -- Use elementary row operations to put square matrix M in row echolon
63 -- form. Identical row operations are performed on matrix N, must have the
64 -- same number of rows as M.
67 type Scalar is private;
68 type Real is digits <>;
69 type Matrix is array (Integer range <>, Integer range <>) of Scalar;
70 with function "abs" (Right : Scalar) return Real'Base is <>;
71 with function "-" (Left, Right : Scalar) return Scalar is <>;
72 with function "*" (Left, Right : Scalar) return Scalar is <>;
73 with function "/" (Left, Right : Scalar) return Scalar is <>;
76 procedure Forward_Eliminate
81 --------------------------
82 -- Square_Matrix_Length --
83 --------------------------
86 type Scalar is private;
87 type Matrix is array (Integer range <>, Integer range <>) of Scalar;
88 function Square_Matrix_Length (A : Matrix) return Natural;
89 -- If A is non-square, raise Constraint_Error, else return its dimension
91 ----------------------------------
92 -- Vector_Elementwise_Operation --
93 ----------------------------------
96 type X_Scalar is private;
97 type Result_Scalar is private;
98 type X_Vector is array (Integer range <>) of X_Scalar;
99 type Result_Vector is array (Integer range <>) of Result_Scalar;
100 with function Operation (X : X_Scalar) return Result_Scalar;
101 function Vector_Elementwise_Operation (X : X_Vector) return Result_Vector;
103 ----------------------------------
104 -- Matrix_Elementwise_Operation --
105 ----------------------------------
108 type X_Scalar is private;
109 type Result_Scalar is private;
110 type X_Matrix is array (Integer range <>, Integer range <>) of X_Scalar;
111 type Result_Matrix is array (Integer range <>, Integer range <>)
113 with function Operation (X : X_Scalar) return Result_Scalar;
114 function Matrix_Elementwise_Operation (X : X_Matrix) return Result_Matrix;
116 -----------------------------------------
117 -- Vector_Vector_Elementwise_Operation --
118 -----------------------------------------
121 type Left_Scalar is private;
122 type Right_Scalar is private;
123 type Result_Scalar is private;
124 type Left_Vector is array (Integer range <>) of Left_Scalar;
125 type Right_Vector is array (Integer range <>) of Right_Scalar;
126 type Result_Vector is array (Integer range <>) of Result_Scalar;
127 with function Operation
129 Right : Right_Scalar) return Result_Scalar;
130 function Vector_Vector_Elementwise_Operation
132 Right : Right_Vector) return Result_Vector;
134 ------------------------------------------------
135 -- Vector_Vector_Scalar_Elementwise_Operation --
136 ------------------------------------------------
139 type X_Scalar is private;
140 type Y_Scalar is private;
141 type Z_Scalar is private;
142 type Result_Scalar is private;
143 type X_Vector is array (Integer range <>) of X_Scalar;
144 type Y_Vector is array (Integer range <>) of Y_Scalar;
145 type Result_Vector is array (Integer range <>) of Result_Scalar;
146 with function Operation
149 Z : Z_Scalar) return Result_Scalar;
150 function Vector_Vector_Scalar_Elementwise_Operation
153 Z : Z_Scalar) return Result_Vector;
155 -----------------------------------------
156 -- Matrix_Matrix_Elementwise_Operation --
157 -----------------------------------------
160 type Left_Scalar is private;
161 type Right_Scalar is private;
162 type Result_Scalar is private;
163 type Left_Matrix is array (Integer range <>, Integer range <>)
165 type Right_Matrix is array (Integer range <>, Integer range <>)
167 type Result_Matrix is array (Integer range <>, Integer range <>)
169 with function Operation
171 Right : Right_Scalar) return Result_Scalar;
172 function Matrix_Matrix_Elementwise_Operation
174 Right : Right_Matrix) return Result_Matrix;
176 ------------------------------------------------
177 -- Matrix_Matrix_Scalar_Elementwise_Operation --
178 ------------------------------------------------
181 type X_Scalar is private;
182 type Y_Scalar is private;
183 type Z_Scalar is private;
184 type Result_Scalar is private;
185 type X_Matrix is array (Integer range <>, Integer range <>) of X_Scalar;
186 type Y_Matrix is array (Integer range <>, Integer range <>) of Y_Scalar;
187 type Result_Matrix is array (Integer range <>, Integer range <>)
189 with function Operation
192 Z : Z_Scalar) return Result_Scalar;
193 function Matrix_Matrix_Scalar_Elementwise_Operation
196 Z : Z_Scalar) return Result_Matrix;
198 -----------------------------------------
199 -- Vector_Scalar_Elementwise_Operation --
200 -----------------------------------------
203 type Left_Scalar is private;
204 type Right_Scalar is private;
205 type Result_Scalar is private;
206 type Left_Vector is array (Integer range <>) of Left_Scalar;
207 type Result_Vector is array (Integer range <>) of Result_Scalar;
208 with function Operation
210 Right : Right_Scalar) return Result_Scalar;
211 function Vector_Scalar_Elementwise_Operation
213 Right : Right_Scalar) return Result_Vector;
215 -----------------------------------------
216 -- Matrix_Scalar_Elementwise_Operation --
217 -----------------------------------------
220 type Left_Scalar is private;
221 type Right_Scalar is private;
222 type Result_Scalar is private;
223 type Left_Matrix is array (Integer range <>, Integer range <>)
225 type Result_Matrix is array (Integer range <>, Integer range <>)
227 with function Operation
229 Right : Right_Scalar) return Result_Scalar;
230 function Matrix_Scalar_Elementwise_Operation
232 Right : Right_Scalar) return Result_Matrix;
234 -----------------------------------------
235 -- Scalar_Vector_Elementwise_Operation --
236 -----------------------------------------
239 type Left_Scalar is private;
240 type Right_Scalar is private;
241 type Result_Scalar is private;
242 type Right_Vector is array (Integer range <>) of Right_Scalar;
243 type Result_Vector is array (Integer range <>) of Result_Scalar;
244 with function Operation
246 Right : Right_Scalar) return Result_Scalar;
247 function Scalar_Vector_Elementwise_Operation
249 Right : Right_Vector) return Result_Vector;
251 -----------------------------------------
252 -- Scalar_Matrix_Elementwise_Operation --
253 -----------------------------------------
256 type Left_Scalar is private;
257 type Right_Scalar is private;
258 type Result_Scalar is private;
259 type Right_Matrix is array (Integer range <>, Integer range <>)
261 type Result_Matrix is array (Integer range <>, Integer range <>)
263 with function Operation
265 Right : Right_Scalar) return Result_Scalar;
266 function Scalar_Matrix_Elementwise_Operation
268 Right : Right_Matrix) return Result_Matrix;
275 type Left_Scalar is private;
276 type Right_Scalar is private;
277 type Result_Scalar is private;
278 type Left_Vector is array (Integer range <>) of Left_Scalar;
279 type Right_Vector is array (Integer range <>) of Right_Scalar;
280 Zero : Result_Scalar;
283 Right : Right_Scalar) return Result_Scalar is <>;
285 (Left : Result_Scalar;
286 Right : Result_Scalar) return Result_Scalar is <>;
287 function Inner_Product
289 Right : Right_Vector) return Result_Scalar;
296 type X_Scalar is private;
297 type Result_Real is digits <>;
298 type X_Vector is array (Integer range <>) of X_Scalar;
299 with function "abs" (Right : X_Scalar) return Result_Real is <>;
300 with function Sqrt (X : Result_Real'Base) return Result_Real'Base is <>;
301 function L2_Norm (X : X_Vector) return Result_Real'Base;
308 type Left_Scalar is private;
309 type Right_Scalar is private;
310 type Result_Scalar is private;
311 type Left_Vector is array (Integer range <>) of Left_Scalar;
312 type Right_Vector is array (Integer range <>) of Right_Scalar;
313 type Matrix is array (Integer range <>, Integer range <>)
317 Right : Right_Scalar) return Result_Scalar is <>;
318 function Outer_Product
320 Right : Right_Vector) return Matrix;
322 ---------------------------
323 -- Matrix_Vector_Product --
324 ---------------------------
327 type Left_Scalar is private;
328 type Right_Scalar is private;
329 type Result_Scalar is private;
330 type Matrix is array (Integer range <>, Integer range <>)
332 type Right_Vector is array (Integer range <>) of Right_Scalar;
333 type Result_Vector is array (Integer range <>) of Result_Scalar;
334 Zero : Result_Scalar;
337 Right : Right_Scalar) return Result_Scalar is <>;
339 (Left : Result_Scalar;
340 Right : Result_Scalar) return Result_Scalar is <>;
341 function Matrix_Vector_Product
343 Right : Right_Vector) return Result_Vector;
345 ---------------------------
346 -- Vector_Matrix_Product --
347 ---------------------------
350 type Left_Scalar is private;
351 type Right_Scalar is private;
352 type Result_Scalar is private;
353 type Left_Vector is array (Integer range <>) of Left_Scalar;
354 type Matrix is array (Integer range <>, Integer range <>)
356 type Result_Vector is array (Integer range <>) of Result_Scalar;
357 Zero : Result_Scalar;
360 Right : Right_Scalar) return Result_Scalar is <>;
362 (Left : Result_Scalar;
363 Right : Result_Scalar) return Result_Scalar is <>;
364 function Vector_Matrix_Product
366 Right : Matrix) return Result_Vector;
368 ---------------------------
369 -- Matrix_Matrix_Product --
370 ---------------------------
373 type Left_Scalar is private;
374 type Right_Scalar is private;
375 type Result_Scalar is private;
376 type Left_Matrix is array (Integer range <>, Integer range <>)
378 type Right_Matrix is array (Integer range <>, Integer range <>)
380 type Result_Matrix is array (Integer range <>, Integer range <>)
382 Zero : Result_Scalar;
385 Right : Right_Scalar) return Result_Scalar is <>;
387 (Left : Result_Scalar;
388 Right : Result_Scalar) return Result_Scalar is <>;
389 function Matrix_Matrix_Product
391 Right : Right_Matrix) return Result_Matrix;
393 ----------------------------
394 -- Matrix_Vector_Solution --
395 ----------------------------
398 type Scalar is private;
399 type Vector is array (Integer range <>) of Scalar;
400 type Matrix is array (Integer range <>, Integer range <>) of Scalar;
401 with procedure Back_Substitute (M, N : in out Matrix) is <>;
402 with procedure Forward_Eliminate
405 Det : out Scalar) is <>;
406 function Matrix_Vector_Solution (A : Matrix; X : Vector) return Vector;
408 ----------------------------
409 -- Matrix_Matrix_Solution --
410 ----------------------------
413 type Scalar is private;
414 type Matrix is array (Integer range <>, Integer range <>) of Scalar;
415 with procedure Back_Substitute (M, N : in out Matrix) is <>;
416 with procedure Forward_Eliminate
419 Det : out Scalar) is <>;
420 function Matrix_Matrix_Solution (A : Matrix; X : Matrix) return Matrix;
427 type Real is digits <>;
428 function Sqrt (X : Real'Base) return Real'Base;
435 type Scalar is private;
436 type Matrix is array (Integer range <>, Integer range <>) of Scalar;
437 procedure Swap_Column (A : in out Matrix; Left, Right : Integer);
444 type Scalar is private;
445 type Matrix is array (Integer range <>, Integer range <>) of Scalar;
446 procedure Transpose (A : Matrix; R : out Matrix);
448 -------------------------------
449 -- Update_Vector_With_Vector --
450 -------------------------------
453 type X_Scalar is private;
454 type Y_Scalar is private;
455 type X_Vector is array (Integer range <>) of X_Scalar;
456 type Y_Vector is array (Integer range <>) of Y_Scalar;
457 with procedure Update (X : in out X_Scalar; Y : Y_Scalar);
458 procedure Update_Vector_With_Vector (X : in out X_Vector; Y : Y_Vector);
460 -------------------------------
461 -- Update_Matrix_With_Matrix --
462 -------------------------------
465 type X_Scalar is private;
466 type Y_Scalar is private;
467 type X_Matrix is array (Integer range <>, Integer range <>) of X_Scalar;
468 type Y_Matrix is array (Integer range <>, Integer range <>) of Y_Scalar;
469 with procedure Update (X : in out X_Scalar; Y : Y_Scalar);
470 procedure Update_Matrix_With_Matrix (X : in out X_Matrix; Y : Y_Matrix);
477 type Scalar is private;
478 type Matrix is array (Integer range <>, Integer range <>) of Scalar;
483 First_1 : Integer := 1;
484 First_2 : Integer := 1) return Matrix;
491 type Scalar is private;
492 type Vector is array (Integer range <>) of Scalar;
498 First : Integer := 1) return Vector;
500 end System.Generic_Array_Operations;