| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 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 -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2006-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 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. -- |
| 17 |
-- -- |
| 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. -- |
| 21 |
-- -- |
| 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/>. -- |
| 26 |
-- -- |
| 27 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
-- Preconditions, postconditions, ghost code, loop invariants and assertions |
| 33 |
-- in this unit are meant for analysis only, not for run-time checking, as it |
| 34 |
-- would be too costly otherwise. This is enforced by setting the assertion |
| 35 |
-- policy to Ignore, here for non-generic code, and inside the generic for |
| 36 |
-- generic code. |
| 37 |
|
| 38 |
pragma Assertion_Policy (Pre => Ignore, |
| 39 |
Post => Ignore, |
| 40 |
Ghost => Ignore, |
| 41 |
Loop_Invariant => Ignore, |
| 42 |
Assert => Ignore); |
| 43 |
|
| 44 |
with Ada.Numerics; use Ada.Numerics; |
| 45 |
|
| 46 |
package body System.Generic_Array_Operations |
| 47 |
with SPARK_Mode |
| 48 |
is |
| 49 |
pragma Warnings |
| 50 |
(Off, "aspect * not enforced on inlined subprogram", |
| 51 |
Reason => "Contracts in this unit are never executed"); |
| 52 |
|
| 53 |
function Check_Unit_Last |
| 54 |
(Index : Integer; |
| 55 |
Order : Positive; |
| 56 |
First : Integer) return Integer |
| 57 |
with |
| 58 |
Pre => Index >= First |
| 59 |
and then First <= Integer'Last - Order + 1 |
| 60 |
and then Index <= First + (Order - 1), |
| 61 |
Post => Check_Unit_Last'Result = First + (Order - 1); |
| 62 |
|
| 63 |
pragma Inline_Always (Check_Unit_Last); |
| 64 |
-- Compute index of last element returned by Unit_Vector or Unit_Matrix. |
| 65 |
-- A separate function is needed to allow raising Constraint_Error before |
| 66 |
-- declaring the function result variable. The result variable needs to be |
| 67 |
-- declared first, to allow front-end inlining. |
| 68 |
|
| 69 |
pragma Warnings (On, "aspect * not enforced on inlined subprogram"); |
| 70 |
|
| 71 |
-------------- |
| 72 |
-- Diagonal -- |
| 73 |
-------------- |
| 74 |
|
| 75 |
function Diagonal (A : Matrix) return Vector is |
| 76 |
pragma Assertion_Policy (Pre => Ignore, |
| 77 |
Post => Ignore, |
| 78 |
Ghost => Ignore, |
| 79 |
Loop_Invariant => Ignore, |
| 80 |
Assert => Ignore); |
| 81 |
|
| 82 |
N : constant Natural := Natural'Min (A'Length (1), A'Length (2)); |
| 83 |
begin |
| 84 |
return R : Vector (A'First (1) .. A'First (1) + (N - 1)) |
| 85 |
with Relaxed_Initialization |
| 86 |
do |
| 87 |
for J in 0 .. N - 1 loop |
| 88 |
R (R'First + J) := A (A'First (1) + J, A'First (2) + J); |
| 89 |
|
| 90 |
pragma Loop_Invariant |
| 91 |
(for all JJ in R'First .. R'First + J => R (JJ)'Initialized); |
| 92 |
end loop; |
| 93 |
end return; |
| 94 |
end Diagonal; |
| 95 |
|
| 96 |
-------------------------- |
| 97 |
-- Square_Matrix_Length -- |
| 98 |
-------------------------- |
| 99 |
|
| 100 |
function Square_Matrix_Length (A : Matrix) return Natural is |
| 101 |
begin |
| 102 |
if A'Length (1) /= A'Length (2) then |
| 103 |
raise Constraint_Error with "matrix is not square"; |
| 104 |
else |
| 105 |
return A'Length (1); |
| 106 |
end if; |
| 107 |
end Square_Matrix_Length; |
| 108 |
|
| 109 |
--------------------- |
| 110 |
-- Check_Unit_Last -- |
| 111 |
--------------------- |
| 112 |
|
| 113 |
function Check_Unit_Last |
| 114 |
(Index : Integer; |
| 115 |
Order : Positive; |
| 116 |
First : Integer) return Integer |
| 117 |
is |
| 118 |
begin |
| 119 |
-- Order the tests carefully to avoid overflow |
| 120 |
|
| 121 |
if Index < First |
| 122 |
or else First > Integer'Last - Order + 1 |
| 123 |
or else Index > First + (Order - 1) |
| 124 |
then |
| 125 |
raise Constraint_Error; |
| 126 |
end if; |
| 127 |
|
| 128 |
return First + (Order - 1); |
| 129 |
end Check_Unit_Last; |
| 130 |
|
| 131 |
--------------------- |
| 132 |
-- Back_Substitute -- |
| 133 |
--------------------- |
| 134 |
|
| 135 |
procedure Back_Substitute (M, N : in out Matrix) is |
| 136 |
pragma Assertion_Policy (Pre => Ignore, |
| 137 |
Post => Ignore, |
| 138 |
Ghost => Ignore, |
| 139 |
Loop_Invariant => Ignore, |
| 140 |
Assert => Ignore); |
| 141 |
pragma Assert (M'First (1) = N'First (1) |
| 142 |
and then |
| 143 |
M'Last (1) = N'Last (1)); |
| 144 |
|
| 145 |
procedure Sub_Row |
| 146 |
(M : in out Matrix; |
| 147 |
Target : Integer; |
| 148 |
Source : Integer; |
| 149 |
Factor : Scalar) |
| 150 |
with |
| 151 |
Pre => Target in M'Range (1) |
| 152 |
and then Source in M'Range (1); |
| 153 |
-- Elementary row operation that subtracts Factor * M (Source, <>) from |
| 154 |
-- M (Target, <>) |
| 155 |
|
| 156 |
------------- |
| 157 |
-- Sub_Row -- |
| 158 |
------------- |
| 159 |
|
| 160 |
procedure Sub_Row |
| 161 |
(M : in out Matrix; |
| 162 |
Target : Integer; |
| 163 |
Source : Integer; |
| 164 |
Factor : Scalar) |
| 165 |
is |
| 166 |
begin |
| 167 |
for J in M'Range (2) loop |
| 168 |
M (Target, J) := M (Target, J) - Factor * M (Source, J); |
| 169 |
end loop; |
| 170 |
end Sub_Row; |
| 171 |
|
| 172 |
-- Local declarations |
| 173 |
|
| 174 |
Max_Col : Integer := M'Last (2); |
| 175 |
|
| 176 |
-- Start of processing for Back_Substitute |
| 177 |
|
| 178 |
begin |
| 179 |
Do_Rows : for Row in reverse M'Range (1) loop |
| 180 |
|
| 181 |
pragma Loop_Invariant (Max_Col <= M'Last (2)); |
| 182 |
|
| 183 |
Find_Non_Zero : for Col in reverse M'First (2) .. Max_Col loop |
| 184 |
if Is_Non_Zero (M (Row, Col)) then |
| 185 |
|
| 186 |
-- Found first non-zero element, so subtract a multiple of this |
| 187 |
-- element from all higher rows, to reduce all other elements |
| 188 |
-- in this column to zero. |
| 189 |
|
| 190 |
declare |
| 191 |
-- We can't use a for loop, as we'd need to iterate to |
| 192 |
-- Row - 1, but that expression will overflow if M'First |
| 193 |
-- equals Integer'First, which is true for aggregates |
| 194 |
-- without explicit bounds.. |
| 195 |
|
| 196 |
J : Integer := M'First (1); |
| 197 |
NZ : constant Scalar := M (Row, Col); |
| 198 |
|
| 199 |
begin |
| 200 |
while J < Row loop |
| 201 |
pragma Loop_Invariant (J in M'Range (1)); |
| 202 |
|
| 203 |
Sub_Row (N, J, Row, (M (J, Col) / NZ)); |
| 204 |
Sub_Row (M, J, Row, (M (J, Col) / NZ)); |
| 205 |
J := J + 1; |
| 206 |
end loop; |
| 207 |
end; |
| 208 |
|
| 209 |
-- Avoid potential overflow in the subtraction below |
| 210 |
|
| 211 |
exit Do_Rows when Col = M'First (2); |
| 212 |
|
| 213 |
Max_Col := Col - 1; |
| 214 |
|
| 215 |
exit Find_Non_Zero; |
| 216 |
end if; |
| 217 |
end loop Find_Non_Zero; |
| 218 |
end loop Do_Rows; |
| 219 |
end Back_Substitute; |
| 220 |
|
| 221 |
----------------------- |
| 222 |
-- Forward_Eliminate -- |
| 223 |
----------------------- |
| 224 |
|
| 225 |
procedure Forward_Eliminate |
| 226 |
(M : in out Matrix; |
| 227 |
N : in out Matrix; |
| 228 |
Det : out Scalar) |
| 229 |
is |
| 230 |
pragma Assertion_Policy (Pre => Ignore, |
| 231 |
Post => Ignore, |
| 232 |
Ghost => Ignore, |
| 233 |
Loop_Invariant => Ignore, |
| 234 |
Assert => Ignore); |
| 235 |
pragma Assert (M'First (1) = N'First (1) |
| 236 |
and then |
| 237 |
M'Last (1) = N'Last (1)); |
| 238 |
|
| 239 |
-- The following are variations of the elementary matrix row operations: |
| 240 |
-- row switching, row multiplication and row addition. Because in this |
| 241 |
-- algorithm the addition factor is always a negated value, we chose to |
| 242 |
-- use row subtraction instead. Similarly, instead of multiplying by |
| 243 |
-- a reciprocal, we divide. |
| 244 |
|
| 245 |
procedure Sub_Row |
| 246 |
(M : in out Matrix; |
| 247 |
Target : Integer; |
| 248 |
Source : Integer; |
| 249 |
Factor : Scalar) |
| 250 |
with |
| 251 |
Pre => Target in M'Range (1) |
| 252 |
and then Source in M'Range (1); |
| 253 |
-- Subtrace Factor * M (Source, <>) from M (Target, <>) |
| 254 |
|
| 255 |
procedure Divide_Row |
| 256 |
(M, N : in out Matrix; |
| 257 |
Row : Integer; |
| 258 |
Scale : Scalar) |
| 259 |
with |
| 260 |
Pre => Row in M'Range (1) |
| 261 |
and then M'First (1) = N'First (1) |
| 262 |
and then M'Last (1) = N'Last (1) |
| 263 |
and then Scale /= Zero; |
| 264 |
-- Divide M (Row) and N (Row) by Scale, and update Det |
| 265 |
|
| 266 |
procedure Switch_Row |
| 267 |
(M, N : in out Matrix; |
| 268 |
Row_1 : Integer; |
| 269 |
Row_2 : Integer) |
| 270 |
with |
| 271 |
Pre => Row_1 in M'Range (1) |
| 272 |
and then Row_2 in M'Range (1) |
| 273 |
and then M'First (1) = N'First (1) |
| 274 |
and then M'Last (1) = N'Last (1), |
| 275 |
Post => (for all J in M'Range (2) => |
| 276 |
M (Row_1, J) = M'Old (Row_2, J) |
| 277 |
and then M (Row_2, J) = M'Old (Row_1, J)) |
| 278 |
and then (for all J in N'Range (2) => |
| 279 |
N (Row_1, J) = N'Old (Row_2, J) |
| 280 |
and then N (Row_2, J) = N'Old (Row_1, J)); |
| 281 |
-- Exchange M (Row_1) and N (Row_1) with M (Row_2) and N (Row_2), |
| 282 |
-- negating Det in the process. |
| 283 |
|
| 284 |
------------- |
| 285 |
-- Sub_Row -- |
| 286 |
------------- |
| 287 |
|
| 288 |
procedure Sub_Row |
| 289 |
(M : in out Matrix; |
| 290 |
Target : Integer; |
| 291 |
Source : Integer; |
| 292 |
Factor : Scalar) |
| 293 |
is |
| 294 |
begin |
| 295 |
for J in M'Range (2) loop |
| 296 |
M (Target, J) := M (Target, J) - Factor * M (Source, J); |
| 297 |
end loop; |
| 298 |
end Sub_Row; |
| 299 |
|
| 300 |
---------------- |
| 301 |
-- Divide_Row -- |
| 302 |
---------------- |
| 303 |
|
| 304 |
procedure Divide_Row |
| 305 |
(M, N : in out Matrix; |
| 306 |
Row : Integer; |
| 307 |
Scale : Scalar) |
| 308 |
is |
| 309 |
begin |
| 310 |
Det := Det * Scale; |
| 311 |
|
| 312 |
for J in M'Range (2) loop |
| 313 |
M (Row, J) := M (Row, J) / Scale; |
| 314 |
end loop; |
| 315 |
|
| 316 |
for J in N'Range (2) loop |
| 317 |
N (Row, J) := N (Row, J) / Scale; |
| 318 |
pragma Annotate |
| 319 |
(CodePeer, False_Positive, "divide by zero", "Scale /= 0"); |
| 320 |
end loop; |
| 321 |
end Divide_Row; |
| 322 |
|
| 323 |
---------------- |
| 324 |
-- Switch_Row -- |
| 325 |
---------------- |
| 326 |
|
| 327 |
procedure Switch_Row |
| 328 |
(M, N : in out Matrix; |
| 329 |
Row_1 : Integer; |
| 330 |
Row_2 : Integer) |
| 331 |
is |
| 332 |
procedure Swap (X, Y : in out Scalar) |
| 333 |
with |
| 334 |
Post => X = Y'Old and then Y = X'Old; |
| 335 |
-- Exchange the values of X and Y |
| 336 |
|
| 337 |
---------- |
| 338 |
-- Swap -- |
| 339 |
---------- |
| 340 |
|
| 341 |
procedure Swap (X, Y : in out Scalar) is |
| 342 |
T : constant Scalar := X; |
| 343 |
begin |
| 344 |
X := Y; |
| 345 |
Y := T; |
| 346 |
end Swap; |
| 347 |
|
| 348 |
-- Start of processing for Switch_Row |
| 349 |
|
| 350 |
begin |
| 351 |
if Row_1 /= Row_2 then |
| 352 |
Det := Zero - Det; |
| 353 |
|
| 354 |
for J in M'Range (2) loop |
| 355 |
Swap (M (Row_1, J), M (Row_2, J)); |
| 356 |
pragma Annotate |
| 357 |
(GNATprove, False_Positive, |
| 358 |
"formal parameters ""X"" and ""Y"" might be aliased", |
| 359 |
"Row_1 /= Row_2"); |
| 360 |
|
| 361 |
pragma Loop_Invariant |
| 362 |
(for all JJ in M'First (2) .. J => |
| 363 |
M (Row_1, JJ) = M'Loop_Entry (Row_2, JJ) |
| 364 |
and then M (Row_2, JJ) = M'Loop_Entry (Row_1, JJ)); |
| 365 |
end loop; |
| 366 |
|
| 367 |
for J in N'Range (2) loop |
| 368 |
Swap (N (Row_1, J), N (Row_2, J)); |
| 369 |
pragma Annotate |
| 370 |
(GNATprove, False_Positive, |
| 371 |
"formal parameters ""X"" and ""Y"" might be aliased", |
| 372 |
"Row_1 /= Row_2"); |
| 373 |
|
| 374 |
pragma Loop_Invariant |
| 375 |
(for all JJ in N'First (2) .. J => |
| 376 |
N (Row_1, JJ) = N'Loop_Entry (Row_2, JJ) |
| 377 |
and then N (Row_2, JJ) = N'Loop_Entry (Row_1, JJ)); |
| 378 |
end loop; |
| 379 |
end if; |
| 380 |
end Switch_Row; |
| 381 |
|
| 382 |
-- Local declarations |
| 383 |
|
| 384 |
Row : Integer := M'First (1); |
| 385 |
|
| 386 |
-- Start of processing for Forward_Eliminate |
| 387 |
|
| 388 |
begin |
| 389 |
Det := One; |
| 390 |
|
| 391 |
for J in M'Range (2) loop |
| 392 |
pragma Loop_Invariant (Row >= M'First (1)); |
| 393 |
|
| 394 |
declare |
| 395 |
Max_Row : Integer := Row; |
| 396 |
Max_Abs : Real'Base := 0.0; |
| 397 |
|
| 398 |
begin |
| 399 |
-- Find best pivot in column J, starting in row Row |
| 400 |
|
| 401 |
for K in Row .. M'Last (1) loop |
| 402 |
pragma Loop_Invariant (Max_Row in M'Range (1)); |
| 403 |
pragma Loop_Invariant |
| 404 |
(if Max_Abs /= 0.0 then Max_Abs = abs M (Max_Row, J)); |
| 405 |
|
| 406 |
declare |
| 407 |
New_Abs : constant Real'Base := abs M (K, J); |
| 408 |
begin |
| 409 |
if Max_Abs < New_Abs then |
| 410 |
Max_Abs := New_Abs; |
| 411 |
Max_Row := K; |
| 412 |
end if; |
| 413 |
end; |
| 414 |
end loop; |
| 415 |
|
| 416 |
if Max_Abs > 0.0 then |
| 417 |
Switch_Row (M, N, Row, Max_Row); |
| 418 |
|
| 419 |
pragma Assert (Max_Abs = abs M (Row, J)); |
| 420 |
|
| 421 |
-- The temporaries below are necessary to force a copy of the |
| 422 |
-- value and avoid improper aliasing. |
| 423 |
|
| 424 |
declare |
| 425 |
Scale : constant Scalar := M (Row, J); |
| 426 |
begin |
| 427 |
Divide_Row (M, N, Row, Scale); |
| 428 |
end; |
| 429 |
|
| 430 |
for U in Row .. M'Last (1) when U /= Row loop |
| 431 |
declare |
| 432 |
Factor : constant Scalar := M (U, J); |
| 433 |
begin |
| 434 |
Sub_Row (N, U, Row, Factor); |
| 435 |
Sub_Row (M, U, Row, Factor); |
| 436 |
end; |
| 437 |
end loop; |
| 438 |
|
| 439 |
exit when Row >= M'Last (1); |
| 440 |
|
| 441 |
Row := Row + 1; |
| 442 |
|
| 443 |
else |
| 444 |
-- Set zero (note that we do not have literals) |
| 445 |
|
| 446 |
Det := Zero; |
| 447 |
end if; |
| 448 |
end; |
| 449 |
end loop; |
| 450 |
end Forward_Eliminate; |
| 451 |
|
| 452 |
------------------- |
| 453 |
-- Inner_Product -- |
| 454 |
------------------- |
| 455 |
|
| 456 |
function Inner_Product |
| 457 |
(Left : Left_Vector; |
| 458 |
Right : Right_Vector) return Result_Scalar |
| 459 |
is |
| 460 |
R : Result_Scalar := Zero; |
| 461 |
|
| 462 |
begin |
| 463 |
if Left'Length /= Right'Length then |
| 464 |
raise Constraint_Error with |
| 465 |
"vectors are of different length in inner product"; |
| 466 |
end if; |
| 467 |
|
| 468 |
for J in Left'Range loop |
| 469 |
R := R + Left (J) * Right (J - Left'First + Right'First); |
| 470 |
end loop; |
| 471 |
|
| 472 |
return R; |
| 473 |
end Inner_Product; |
| 474 |
|
| 475 |
------------- |
| 476 |
-- L2_Norm -- |
| 477 |
------------- |
| 478 |
|
| 479 |
function L2_Norm (X : X_Vector) return Result_Real'Base is |
| 480 |
pragma Assertion_Policy (Pre => Ignore, |
| 481 |
Post => Ignore, |
| 482 |
Ghost => Ignore, |
| 483 |
Loop_Invariant => Ignore, |
| 484 |
Assert => Ignore); |
| 485 |
Sum : Result_Real'Base := 0.0; |
| 486 |
|
| 487 |
begin |
| 488 |
for J in X'Range loop |
| 489 |
pragma Loop_Invariant (Sum >= 0.0); |
| 490 |
Sum := Sum + Result_Real'Base (abs X (J))**2; |
| 491 |
pragma Annotate |
| 492 |
(GNATprove, Intentional, "float overflow check might fail", |
| 493 |
"Intermediate computation might overflow in L2_Norm"); |
| 494 |
end loop; |
| 495 |
|
| 496 |
return Sqrt (Sum); |
| 497 |
end L2_Norm; |
| 498 |
|
| 499 |
---------------------------------- |
| 500 |
-- Matrix_Elementwise_Operation -- |
| 501 |
---------------------------------- |
| 502 |
|
| 503 |
function Matrix_Elementwise_Operation (X : X_Matrix) return Result_Matrix is |
| 504 |
pragma Assertion_Policy (Pre => Ignore, |
| 505 |
Post => Ignore, |
| 506 |
Ghost => Ignore, |
| 507 |
Loop_Invariant => Ignore, |
| 508 |
Assert => Ignore); |
| 509 |
begin |
| 510 |
return R : Result_Matrix (X'Range (1), X'Range (2)) |
| 511 |
with Relaxed_Initialization |
| 512 |
do |
| 513 |
for J in R'Range (1) loop |
| 514 |
for K in R'Range (2) loop |
| 515 |
R (J, K) := Operation (X (J, K)); |
| 516 |
|
| 517 |
pragma Loop_Invariant |
| 518 |
(for all JJ in R'First (1) .. J when JJ /= J => |
| 519 |
(for all KK in R'Range (2) => R (JJ, KK)'Initialized)); |
| 520 |
pragma Loop_Invariant |
| 521 |
(for all KK in R'First (2) .. K => R (J, KK)'Initialized); |
| 522 |
end loop; |
| 523 |
|
| 524 |
pragma Loop_Invariant |
| 525 |
(for all JJ in R'First (1) .. J when JJ /= J => |
| 526 |
(for all KK in R'Range (2) => R (JJ, KK)'Initialized)); |
| 527 |
pragma Loop_Invariant |
| 528 |
(for all KK in R'Range (2) => R (J, KK)'Initialized); |
| 529 |
end loop; |
| 530 |
end return; |
| 531 |
end Matrix_Elementwise_Operation; |
| 532 |
|
| 533 |
---------------------------------- |
| 534 |
-- Vector_Elementwise_Operation -- |
| 535 |
---------------------------------- |
| 536 |
|
| 537 |
function Vector_Elementwise_Operation (X : X_Vector) return Result_Vector is |
| 538 |
begin |
| 539 |
return R : Result_Vector (X'Range) do |
| 540 |
for J in R'Range loop |
| 541 |
R (J) := Operation (X (J)); |
| 542 |
end loop; |
| 543 |
end return; |
| 544 |
end Vector_Elementwise_Operation; |
| 545 |
|
| 546 |
----------------------------------------- |
| 547 |
-- Matrix_Matrix_Elementwise_Operation -- |
| 548 |
----------------------------------------- |
| 549 |
|
| 550 |
function Matrix_Matrix_Elementwise_Operation |
| 551 |
(Left : Left_Matrix; |
| 552 |
Right : Right_Matrix) return Result_Matrix |
| 553 |
is |
| 554 |
pragma Assertion_Policy (Pre => Ignore, |
| 555 |
Post => Ignore, |
| 556 |
Ghost => Ignore, |
| 557 |
Loop_Invariant => Ignore, |
| 558 |
Assert => Ignore); |
| 559 |
begin |
| 560 |
return R : Result_Matrix (Left'Range (1), Left'Range (2)) |
| 561 |
with Relaxed_Initialization |
| 562 |
do |
| 563 |
if Left'Length (1) /= Right'Length (1) |
| 564 |
or else |
| 565 |
Left'Length (2) /= Right'Length (2) |
| 566 |
then |
| 567 |
raise Constraint_Error with |
| 568 |
"matrices are of different dimension in elementwise operation"; |
| 569 |
end if; |
| 570 |
|
| 571 |
for J in R'Range (1) loop |
| 572 |
for K in R'Range (2) loop |
| 573 |
R (J, K) := |
| 574 |
Operation |
| 575 |
(Left (J, K), |
| 576 |
Right |
| 577 |
(J - R'First (1) + Right'First (1), |
| 578 |
K - R'First (2) + Right'First (2))); |
| 579 |
|
| 580 |
pragma Loop_Invariant |
| 581 |
(for all JJ in R'First (1) .. J when JJ /= J => |
| 582 |
(for all KK in R'Range (2) => R (JJ, KK)'Initialized)); |
| 583 |
pragma Loop_Invariant |
| 584 |
(for all KK in R'First (2) .. K => R (J, KK)'Initialized); |
| 585 |
end loop; |
| 586 |
|
| 587 |
pragma Loop_Invariant |
| 588 |
(for all JJ in R'First (1) .. J when JJ /= J => |
| 589 |
(for all KK in R'Range (2) => R (JJ, KK)'Initialized)); |
| 590 |
pragma Loop_Invariant |
| 591 |
(for all KK in R'Range (2) => R (J, KK)'Initialized); |
| 592 |
end loop; |
| 593 |
end return; |
| 594 |
end Matrix_Matrix_Elementwise_Operation; |
| 595 |
|
| 596 |
------------------------------------------------ |
| 597 |
-- Matrix_Matrix_Scalar_Elementwise_Operation -- |
| 598 |
------------------------------------------------ |
| 599 |
|
| 600 |
function Matrix_Matrix_Scalar_Elementwise_Operation |
| 601 |
(X : X_Matrix; |
| 602 |
Y : Y_Matrix; |
| 603 |
Z : Z_Scalar) return Result_Matrix |
| 604 |
is |
| 605 |
pragma Assertion_Policy (Pre => Ignore, |
| 606 |
Post => Ignore, |
| 607 |
Ghost => Ignore, |
| 608 |
Loop_Invariant => Ignore, |
| 609 |
Assert => Ignore); |
| 610 |
begin |
| 611 |
return R : Result_Matrix (X'Range (1), X'Range (2)) |
| 612 |
with Relaxed_Initialization |
| 613 |
do |
| 614 |
if X'Length (1) /= Y'Length (1) |
| 615 |
or else |
| 616 |
X'Length (2) /= Y'Length (2) |
| 617 |
then |
| 618 |
raise Constraint_Error with |
| 619 |
"matrices are of different dimension in elementwise operation"; |
| 620 |
end if; |
| 621 |
|
| 622 |
for J in R'Range (1) loop |
| 623 |
for K in R'Range (2) loop |
| 624 |
R (J, K) := |
| 625 |
Operation |
| 626 |
(X (J, K), |
| 627 |
Y (J - R'First (1) + Y'First (1), |
| 628 |
K - R'First (2) + Y'First (2)), |
| 629 |
Z); |
| 630 |
|
| 631 |
pragma Loop_Invariant |
| 632 |
(for all JJ in R'First (1) .. J when JJ /= J => |
| 633 |
(for all KK in R'Range (2) => R (JJ, KK)'Initialized)); |
| 634 |
pragma Loop_Invariant |
| 635 |
(for all KK in R'First (2) .. K => R (J, KK)'Initialized); |
| 636 |
end loop; |
| 637 |
|
| 638 |
pragma Loop_Invariant |
| 639 |
(for all JJ in R'First (1) .. J when JJ /= J => |
| 640 |
(for all KK in R'Range (2) => R (JJ, KK)'Initialized)); |
| 641 |
pragma Loop_Invariant |
| 642 |
(for all KK in R'Range (2) => R (J, KK)'Initialized); |
| 643 |
end loop; |
| 644 |
end return; |
| 645 |
end Matrix_Matrix_Scalar_Elementwise_Operation; |
| 646 |
|
| 647 |
----------------------------------------- |
| 648 |
-- Vector_Vector_Elementwise_Operation -- |
| 649 |
----------------------------------------- |
| 650 |
|
| 651 |
function Vector_Vector_Elementwise_Operation |
| 652 |
(Left : Left_Vector; |
| 653 |
Right : Right_Vector) return Result_Vector |
| 654 |
is |
| 655 |
begin |
| 656 |
return R : Result_Vector (Left'Range) do |
| 657 |
if Left'Length /= Right'Length then |
| 658 |
raise Constraint_Error with |
| 659 |
"vectors are of different length in elementwise operation"; |
| 660 |
end if; |
| 661 |
|
| 662 |
for J in R'Range loop |
| 663 |
R (J) := Operation (Left (J), Right (J - R'First + Right'First)); |
| 664 |
end loop; |
| 665 |
end return; |
| 666 |
end Vector_Vector_Elementwise_Operation; |
| 667 |
|
| 668 |
------------------------------------------------ |
| 669 |
-- Vector_Vector_Scalar_Elementwise_Operation -- |
| 670 |
------------------------------------------------ |
| 671 |
|
| 672 |
function Vector_Vector_Scalar_Elementwise_Operation |
| 673 |
(X : X_Vector; |
| 674 |
Y : Y_Vector; |
| 675 |
Z : Z_Scalar) return Result_Vector is |
| 676 |
begin |
| 677 |
return R : Result_Vector (X'Range) do |
| 678 |
if X'Length /= Y'Length then |
| 679 |
raise Constraint_Error with |
| 680 |
"vectors are of different length in elementwise operation"; |
| 681 |
end if; |
| 682 |
|
| 683 |
for J in R'Range loop |
| 684 |
R (J) := Operation (X (J), Y (J - X'First + Y'First), Z); |
| 685 |
end loop; |
| 686 |
end return; |
| 687 |
end Vector_Vector_Scalar_Elementwise_Operation; |
| 688 |
|
| 689 |
----------------------------------------- |
| 690 |
-- Matrix_Scalar_Elementwise_Operation -- |
| 691 |
----------------------------------------- |
| 692 |
|
| 693 |
function Matrix_Scalar_Elementwise_Operation |
| 694 |
(Left : Left_Matrix; |
| 695 |
Right : Right_Scalar) return Result_Matrix |
| 696 |
is |
| 697 |
pragma Assertion_Policy (Pre => Ignore, |
| 698 |
Post => Ignore, |
| 699 |
Ghost => Ignore, |
| 700 |
Loop_Invariant => Ignore, |
| 701 |
Assert => Ignore); |
| 702 |
begin |
| 703 |
return R : Result_Matrix (Left'Range (1), Left'Range (2)) |
| 704 |
with Relaxed_Initialization |
| 705 |
do |
| 706 |
for J in R'Range (1) loop |
| 707 |
for K in R'Range (2) loop |
| 708 |
R (J, K) := Operation (Left (J, K), Right); |
| 709 |
|
| 710 |
pragma Loop_Invariant |
| 711 |
(for all JJ in R'First (1) .. J when JJ /= J => |
| 712 |
(for all KK in R'Range (2) => R (JJ, KK)'Initialized)); |
| 713 |
pragma Loop_Invariant |
| 714 |
(for all KK in R'First (2) .. K => R (J, KK)'Initialized); |
| 715 |
end loop; |
| 716 |
|
| 717 |
pragma Loop_Invariant |
| 718 |
(for all JJ in R'First (1) .. J when JJ /= J => |
| 719 |
(for all KK in R'Range (2) => R (JJ, KK)'Initialized)); |
| 720 |
pragma Loop_Invariant |
| 721 |
(for all KK in R'Range (2) => R (J, KK)'Initialized); |
| 722 |
end loop; |
| 723 |
end return; |
| 724 |
end Matrix_Scalar_Elementwise_Operation; |
| 725 |
|
| 726 |
----------------------------------------- |
| 727 |
-- Vector_Scalar_Elementwise_Operation -- |
| 728 |
----------------------------------------- |
| 729 |
|
| 730 |
function Vector_Scalar_Elementwise_Operation |
| 731 |
(Left : Left_Vector; |
| 732 |
Right : Right_Scalar) return Result_Vector |
| 733 |
is |
| 734 |
begin |
| 735 |
return R : Result_Vector (Left'Range) do |
| 736 |
for J in R'Range loop |
| 737 |
R (J) := Operation (Left (J), Right); |
| 738 |
end loop; |
| 739 |
end return; |
| 740 |
end Vector_Scalar_Elementwise_Operation; |
| 741 |
|
| 742 |
----------------------------------------- |
| 743 |
-- Scalar_Matrix_Elementwise_Operation -- |
| 744 |
----------------------------------------- |
| 745 |
|
| 746 |
function Scalar_Matrix_Elementwise_Operation |
| 747 |
(Left : Left_Scalar; |
| 748 |
Right : Right_Matrix) return Result_Matrix |
| 749 |
is |
| 750 |
pragma Assertion_Policy (Pre => Ignore, |
| 751 |
Post => Ignore, |
| 752 |
Ghost => Ignore, |
| 753 |
Loop_Invariant => Ignore, |
| 754 |
Assert => Ignore); |
| 755 |
begin |
| 756 |
return R : Result_Matrix (Right'Range (1), Right'Range (2)) |
| 757 |
with Relaxed_Initialization |
| 758 |
do |
| 759 |
for J in R'Range (1) loop |
| 760 |
for K in R'Range (2) loop |
| 761 |
R (J, K) := Operation (Left, Right (J, K)); |
| 762 |
|
| 763 |
pragma Loop_Invariant |
| 764 |
(for all JJ in R'First (1) .. J when JJ /= J => |
| 765 |
(for all KK in R'Range (2) => R (JJ, KK)'Initialized)); |
| 766 |
pragma Loop_Invariant |
| 767 |
(for all KK in R'First (2) .. K => R (J, KK)'Initialized); |
| 768 |
end loop; |
| 769 |
|
| 770 |
pragma Loop_Invariant |
| 771 |
(for all JJ in R'First (1) .. J when JJ /= J => |
| 772 |
(for all KK in R'Range (2) => R (JJ, KK)'Initialized)); |
| 773 |
pragma Loop_Invariant |
| 774 |
(for all KK in R'Range (2) => R (J, KK)'Initialized); |
| 775 |
end loop; |
| 776 |
end return; |
| 777 |
end Scalar_Matrix_Elementwise_Operation; |
| 778 |
|
| 779 |
----------------------------------------- |
| 780 |
-- Scalar_Vector_Elementwise_Operation -- |
| 781 |
----------------------------------------- |
| 782 |
|
| 783 |
function Scalar_Vector_Elementwise_Operation |
| 784 |
(Left : Left_Scalar; |
| 785 |
Right : Right_Vector) return Result_Vector |
| 786 |
is |
| 787 |
begin |
| 788 |
return R : Result_Vector (Right'Range) do |
| 789 |
for J in R'Range loop |
| 790 |
R (J) := Operation (Left, Right (J)); |
| 791 |
end loop; |
| 792 |
end return; |
| 793 |
end Scalar_Vector_Elementwise_Operation; |
| 794 |
|
| 795 |
---------- |
| 796 |
-- Sqrt -- |
| 797 |
---------- |
| 798 |
|
| 799 |
function Sqrt (X : Real'Base) return Real'Base |
| 800 |
with SPARK_Mode => Off -- Not in SPARK due to use of Real'Exponent |
| 801 |
is |
| 802 |
Root, Next : Real'Base; |
| 803 |
|
| 804 |
begin |
| 805 |
-- Be defensive: any comparisons with NaN values will yield False. |
| 806 |
|
| 807 |
if not (X > 0.0) then |
| 808 |
if X = 0.0 then |
| 809 |
return X; |
| 810 |
else |
| 811 |
raise Argument_Error; |
| 812 |
end if; |
| 813 |
|
| 814 |
elsif X > Real'Base'Last then |
| 815 |
pragma Annotate |
| 816 |
(CodePeer, Intentional, |
| 817 |
"test always false", "test for infinity"); |
| 818 |
|
| 819 |
-- X is infinity, which is its own square root |
| 820 |
|
| 821 |
return X; |
| 822 |
end if; |
| 823 |
|
| 824 |
-- Compute an initial estimate based on: |
| 825 |
|
| 826 |
-- X = M * R**E and Sqrt (X) = Sqrt (M) * R**(E / 2.0), |
| 827 |
|
| 828 |
-- where M is the mantissa, R is the radix and E the exponent. |
| 829 |
|
| 830 |
-- By ignoring the mantissa and ignoring the case of an odd |
| 831 |
-- exponent, we get a final error that is at most R. In other words, |
| 832 |
-- the result has about a single bit precision. |
| 833 |
|
| 834 |
Root := Real'Base (Real'Machine_Radix) ** (Real'Exponent (X) / 2); |
| 835 |
|
| 836 |
-- Because of the poor initial estimate, use the Babylonian method of |
| 837 |
-- computing the square root, as it is stable for all inputs. Every step |
| 838 |
-- will roughly double the precision of the result. Just a few steps |
| 839 |
-- suffice in most cases. Eight iterations should give about 2**8 bits |
| 840 |
-- of precision. |
| 841 |
|
| 842 |
for J in 1 .. 8 loop |
| 843 |
pragma Assert (Root /= 0.0); |
| 844 |
|
| 845 |
Next := (Root + X / Root) / 2.0; |
| 846 |
exit when Root = Next; |
| 847 |
Root := Next; |
| 848 |
end loop; |
| 849 |
|
| 850 |
return Root; |
| 851 |
end Sqrt; |
| 852 |
|
| 853 |
--------------------------- |
| 854 |
-- Matrix_Matrix_Product -- |
| 855 |
--------------------------- |
| 856 |
|
| 857 |
function Matrix_Matrix_Product |
| 858 |
(Left : Left_Matrix; |
| 859 |
Right : Right_Matrix) return Result_Matrix |
| 860 |
is |
| 861 |
pragma Assertion_Policy (Pre => Ignore, |
| 862 |
Post => Ignore, |
| 863 |
Ghost => Ignore, |
| 864 |
Loop_Invariant => Ignore, |
| 865 |
Assert => Ignore); |
| 866 |
begin |
| 867 |
return R : Result_Matrix (Left'Range (1), Right'Range (2)) |
| 868 |
with Relaxed_Initialization |
| 869 |
do |
| 870 |
if Left'Length (2) /= Right'Length (1) then |
| 871 |
raise Constraint_Error with |
| 872 |
"incompatible dimensions in matrix multiplication"; |
| 873 |
end if; |
| 874 |
|
| 875 |
for J in R'Range (1) loop |
| 876 |
for K in R'Range (2) loop |
| 877 |
declare |
| 878 |
S : Result_Scalar := Zero; |
| 879 |
|
| 880 |
begin |
| 881 |
for M in Left'Range (2) loop |
| 882 |
S := S + Left (J, M) * |
| 883 |
Right |
| 884 |
(M - Left'First (2) + Right'First (1), K); |
| 885 |
end loop; |
| 886 |
|
| 887 |
R (J, K) := S; |
| 888 |
end; |
| 889 |
|
| 890 |
pragma Loop_Invariant |
| 891 |
(for all JJ in R'First (1) .. J when JJ /= J => |
| 892 |
(for all KK in R'Range (2) => R (JJ, KK)'Initialized)); |
| 893 |
pragma Loop_Invariant |
| 894 |
(for all KK in R'First (2) .. K => R (J, KK)'Initialized); |
| 895 |
end loop; |
| 896 |
|
| 897 |
pragma Loop_Invariant |
| 898 |
(for all JJ in R'First (1) .. J when JJ /= J => |
| 899 |
(for all KK in R'Range (2) => R (JJ, KK)'Initialized)); |
| 900 |
pragma Loop_Invariant |
| 901 |
(for all KK in R'Range (2) => R (J, KK)'Initialized); |
| 902 |
end loop; |
| 903 |
end return; |
| 904 |
end Matrix_Matrix_Product; |
| 905 |
|
| 906 |
---------------------------- |
| 907 |
-- Matrix_Vector_Solution -- |
| 908 |
---------------------------- |
| 909 |
|
| 910 |
function Matrix_Vector_Solution (A : Matrix; X : Vector) return Vector is |
| 911 |
pragma Assertion_Policy (Pre => Ignore, |
| 912 |
Post => Ignore, |
| 913 |
Ghost => Ignore, |
| 914 |
Loop_Invariant => Ignore, |
| 915 |
Assert => Ignore); |
| 916 |
|
| 917 |
procedure Ignore (M : Matrix) |
| 918 |
with |
| 919 |
Ghost, |
| 920 |
Depends => (null => M); |
| 921 |
|
| 922 |
procedure Ignore (M : Matrix) is null; |
| 923 |
-- Ghost procedure to document that the value of argument M is ignored, |
| 924 |
-- which prevents a warning being issued about the value not being used |
| 925 |
-- in the rest of the code. |
| 926 |
|
| 927 |
N : constant Natural := A'Length (1); |
| 928 |
MA : Matrix := A; |
| 929 |
MX : Matrix (A'Range (1), 1 .. 1) with Relaxed_Initialization; |
| 930 |
R : Vector (A'Range (2)) with Relaxed_Initialization; |
| 931 |
Det : Scalar; |
| 932 |
|
| 933 |
begin |
| 934 |
if A'Length (2) /= N then |
| 935 |
raise Constraint_Error with "matrix is not square"; |
| 936 |
end if; |
| 937 |
|
| 938 |
if X'Length /= N then |
| 939 |
raise Constraint_Error with "incompatible vector length"; |
| 940 |
end if; |
| 941 |
|
| 942 |
for J in 0 .. MX'Length (1) - 1 loop |
| 943 |
MX (MX'First (1) + J, 1) := X (X'First + J); |
| 944 |
|
| 945 |
pragma Loop_Invariant |
| 946 |
(for all JJ in MX'First (1) .. MX'First (1) + J => |
| 947 |
MX (JJ, 1)'Initialized); |
| 948 |
end loop; |
| 949 |
|
| 950 |
Forward_Eliminate (MA, MX, Det); |
| 951 |
|
| 952 |
if Det = Zero then |
| 953 |
raise Constraint_Error with "matrix is singular"; |
| 954 |
pragma Annotate |
| 955 |
(GNATprove, Intentional, "exception might be raised", |
| 956 |
"An exception should be raised on a singular matrix"); |
| 957 |
end if; |
| 958 |
|
| 959 |
Back_Substitute (MA, MX); |
| 960 |
Ignore (MA); |
| 961 |
|
| 962 |
for J in 0 .. R'Length - 1 loop |
| 963 |
R (R'First + J) := MX (MX'First (1) + J, 1); |
| 964 |
|
| 965 |
pragma Loop_Invariant |
| 966 |
(for all JJ in R'First .. R'First + J => R (JJ)'Initialized); |
| 967 |
end loop; |
| 968 |
|
| 969 |
return R; |
| 970 |
end Matrix_Vector_Solution; |
| 971 |
|
| 972 |
---------------------------- |
| 973 |
-- Matrix_Matrix_Solution -- |
| 974 |
---------------------------- |
| 975 |
|
| 976 |
function Matrix_Matrix_Solution (A, X : Matrix) return Matrix is |
| 977 |
pragma Assertion_Policy (Pre => Ignore, |
| 978 |
Post => Ignore, |
| 979 |
Ghost => Ignore, |
| 980 |
Loop_Invariant => Ignore, |
| 981 |
Assert => Ignore); |
| 982 |
|
| 983 |
procedure Ignore (M : Matrix) |
| 984 |
with |
| 985 |
Ghost, |
| 986 |
Depends => (null => M); |
| 987 |
|
| 988 |
procedure Ignore (M : Matrix) is null; |
| 989 |
-- Ghost procedure to document that the value of argument M is ignored, |
| 990 |
-- which prevents a warning being issued about the value not being used |
| 991 |
-- in the rest of the code. |
| 992 |
|
| 993 |
N : constant Natural := A'Length (1); |
| 994 |
MA : Matrix (A'Range (2), A'Range (2)) with Relaxed_Initialization; |
| 995 |
MB : Matrix (A'Range (2), X'Range (2)) with Relaxed_Initialization; |
| 996 |
Det : Scalar; |
| 997 |
|
| 998 |
begin |
| 999 |
if A'Length (2) /= N then |
| 1000 |
raise Constraint_Error with "matrix is not square"; |
| 1001 |
end if; |
| 1002 |
|
| 1003 |
if X'Length (1) /= N then |
| 1004 |
raise Constraint_Error with "matrices have unequal number of rows"; |
| 1005 |
end if; |
| 1006 |
|
| 1007 |
for J in 0 .. A'Length (1) - 1 loop |
| 1008 |
for K in MA'Range (2) loop |
| 1009 |
MA (MA'First (1) + J, K) := A (A'First (1) + J, K); |
| 1010 |
|
| 1011 |
pragma Loop_Invariant |
| 1012 |
(for all JJ in MA'First (1) .. MA'First (1) + J |
| 1013 |
when JJ /= MA'First (1) + J |
| 1014 |
=> |
| 1015 |
(for all KK in MA'Range (2) => |
| 1016 |
MA (JJ, KK)'Initialized)); |
| 1017 |
pragma Loop_Invariant |
| 1018 |
(for all KK in MA'First (2) .. K => |
| 1019 |
MA (MA'First (1) + J, KK)'Initialized); |
| 1020 |
end loop; |
| 1021 |
|
| 1022 |
for K in MB'Range (2) loop |
| 1023 |
MB (MB'First (1) + J, K) := X (X'First (1) + J, K); |
| 1024 |
|
| 1025 |
pragma Loop_Invariant |
| 1026 |
(for all JJ in MB'First (1) .. MB'First (1) + J |
| 1027 |
when JJ /= MB'First (1) + J |
| 1028 |
=> |
| 1029 |
(for all KK in MB'Range (2) => |
| 1030 |
MB (JJ, KK)'Initialized)); |
| 1031 |
pragma Loop_Invariant |
| 1032 |
(for all KK in MB'First (2) .. K => |
| 1033 |
MB (MB'First (1) + J, KK)'Initialized); |
| 1034 |
end loop; |
| 1035 |
|
| 1036 |
pragma Loop_Invariant |
| 1037 |
(for all JJ in MA'First (1) .. MA'First (1) + J => |
| 1038 |
(for all KK in MA'Range (2) => |
| 1039 |
MA (JJ, KK)'Initialized)); |
| 1040 |
pragma Loop_Invariant |
| 1041 |
(for all JJ in MB'First (1) .. MB'First (1) + J => |
| 1042 |
(for all KK in MB'Range (2) => |
| 1043 |
MB (JJ, KK)'Initialized)); |
| 1044 |
end loop; |
| 1045 |
|
| 1046 |
Forward_Eliminate (MA, MB, Det); |
| 1047 |
|
| 1048 |
if Det = Zero then |
| 1049 |
raise Constraint_Error with "matrix is singular"; |
| 1050 |
pragma Annotate |
| 1051 |
(GNATprove, Intentional, "exception might be raised", |
| 1052 |
"An exception should be raised on a singular matrix"); |
| 1053 |
end if; |
| 1054 |
|
| 1055 |
Back_Substitute (MA, MB); |
| 1056 |
Ignore (MA); |
| 1057 |
|
| 1058 |
return MB; |
| 1059 |
end Matrix_Matrix_Solution; |
| 1060 |
|
| 1061 |
--------------------------- |
| 1062 |
-- Matrix_Vector_Product -- |
| 1063 |
--------------------------- |
| 1064 |
|
| 1065 |
function Matrix_Vector_Product |
| 1066 |
(Left : Matrix; |
| 1067 |
Right : Right_Vector) return Result_Vector |
| 1068 |
is |
| 1069 |
begin |
| 1070 |
return R : Result_Vector (Left'Range (1)) do |
| 1071 |
if Left'Length (2) /= Right'Length then |
| 1072 |
raise Constraint_Error with |
| 1073 |
"incompatible dimensions in matrix-vector multiplication"; |
| 1074 |
end if; |
| 1075 |
|
| 1076 |
for J in Left'Range (1) loop |
| 1077 |
declare |
| 1078 |
S : Result_Scalar := Zero; |
| 1079 |
|
| 1080 |
begin |
| 1081 |
for K in Left'Range (2) loop |
| 1082 |
S := S + Left (J, K) |
| 1083 |
* Right (K - Left'First (2) + Right'First); |
| 1084 |
end loop; |
| 1085 |
|
| 1086 |
R (J) := S; |
| 1087 |
end; |
| 1088 |
end loop; |
| 1089 |
end return; |
| 1090 |
end Matrix_Vector_Product; |
| 1091 |
|
| 1092 |
------------------- |
| 1093 |
-- Outer_Product -- |
| 1094 |
------------------- |
| 1095 |
|
| 1096 |
function Outer_Product |
| 1097 |
(Left : Left_Vector; |
| 1098 |
Right : Right_Vector) return Matrix |
| 1099 |
is |
| 1100 |
pragma Assertion_Policy (Pre => Ignore, |
| 1101 |
Post => Ignore, |
| 1102 |
Ghost => Ignore, |
| 1103 |
Loop_Invariant => Ignore, |
| 1104 |
Assert => Ignore); |
| 1105 |
begin |
| 1106 |
return R : Matrix (Left'Range, Right'Range) |
| 1107 |
with Relaxed_Initialization |
| 1108 |
do |
| 1109 |
for J in R'Range (1) loop |
| 1110 |
for K in R'Range (2) loop |
| 1111 |
R (J, K) := Left (J) * Right (K); |
| 1112 |
|
| 1113 |
pragma Loop_Invariant |
| 1114 |
(for all JJ in R'First (1) .. J when JJ /= J => |
| 1115 |
(for all KK in R'Range (2) => R (JJ, KK)'Initialized)); |
| 1116 |
pragma Loop_Invariant |
| 1117 |
(for all KK in R'First (2) .. K => R (J, KK)'Initialized); |
| 1118 |
end loop; |
| 1119 |
|
| 1120 |
pragma Loop_Invariant |
| 1121 |
(for all JJ in R'First (1) .. J when JJ /= J => |
| 1122 |
(for all KK in R'Range (2) => R (JJ, KK)'Initialized)); |
| 1123 |
pragma Loop_Invariant |
| 1124 |
(for all KK in R'Range (2) => R (J, KK)'Initialized); |
| 1125 |
end loop; |
| 1126 |
end return; |
| 1127 |
end Outer_Product; |
| 1128 |
|
| 1129 |
----------------- |
| 1130 |
-- Swap_Column -- |
| 1131 |
----------------- |
| 1132 |
|
| 1133 |
procedure Swap_Column (A : in out Matrix; Left, Right : Integer) is |
| 1134 |
Temp : Scalar; |
| 1135 |
begin |
| 1136 |
for J in A'Range (1) loop |
| 1137 |
Temp := A (J, Left); |
| 1138 |
A (J, Left) := A (J, Right); |
| 1139 |
A (J, Right) := Temp; |
| 1140 |
end loop; |
| 1141 |
end Swap_Column; |
| 1142 |
|
| 1143 |
--------------- |
| 1144 |
-- Transpose -- |
| 1145 |
--------------- |
| 1146 |
|
| 1147 |
procedure Transpose (A : Matrix; R : out Matrix) is |
| 1148 |
pragma Assertion_Policy (Pre => Ignore, |
| 1149 |
Post => Ignore, |
| 1150 |
Ghost => Ignore, |
| 1151 |
Loop_Invariant => Ignore, |
| 1152 |
Assert => Ignore); |
| 1153 |
begin |
| 1154 |
for J in R'Range (1) loop |
| 1155 |
for K in R'Range (2) loop |
| 1156 |
R (J, K) := A (K - R'First (2) + A'First (1), |
| 1157 |
J - R'First (1) + A'First (2)); |
| 1158 |
|
| 1159 |
pragma Loop_Invariant |
| 1160 |
(for all JJ in R'First (1) .. J when JJ /= J => |
| 1161 |
(for all K in R'Range (2) => R (JJ, K)'Initialized)); |
| 1162 |
pragma Loop_Invariant |
| 1163 |
(for all KK in R'First (2) .. K => R (J, KK)'Initialized); |
| 1164 |
end loop; |
| 1165 |
|
| 1166 |
pragma Loop_Invariant |
| 1167 |
(for all JJ in R'First (1) .. J => |
| 1168 |
(for all K in R'Range (2) => R (JJ, K)'Initialized)); |
| 1169 |
end loop; |
| 1170 |
end Transpose; |
| 1171 |
|
| 1172 |
------------------------------- |
| 1173 |
-- Update_Matrix_With_Matrix -- |
| 1174 |
------------------------------- |
| 1175 |
|
| 1176 |
procedure Update_Matrix_With_Matrix (X : in out X_Matrix; Y : Y_Matrix) is |
| 1177 |
begin |
| 1178 |
if X'Length (1) /= Y'Length (1) |
| 1179 |
or else |
| 1180 |
X'Length (2) /= Y'Length (2) |
| 1181 |
then |
| 1182 |
raise Constraint_Error with |
| 1183 |
"matrices are of different dimension in update operation"; |
| 1184 |
end if; |
| 1185 |
|
| 1186 |
for J in X'Range (1) loop |
| 1187 |
for K in X'Range (2) loop |
| 1188 |
Update (X (J, K), Y (J - X'First (1) + Y'First (1), |
| 1189 |
K - X'First (2) + Y'First (2))); |
| 1190 |
end loop; |
| 1191 |
end loop; |
| 1192 |
end Update_Matrix_With_Matrix; |
| 1193 |
|
| 1194 |
------------------------------- |
| 1195 |
-- Update_Vector_With_Vector -- |
| 1196 |
------------------------------- |
| 1197 |
|
| 1198 |
procedure Update_Vector_With_Vector (X : in out X_Vector; Y : Y_Vector) is |
| 1199 |
begin |
| 1200 |
if X'Length /= Y'Length then |
| 1201 |
raise Constraint_Error with |
| 1202 |
"vectors are of different length in update operation"; |
| 1203 |
end if; |
| 1204 |
|
| 1205 |
for J in X'Range loop |
| 1206 |
Update (X (J), Y (J - X'First + Y'First)); |
| 1207 |
end loop; |
| 1208 |
end Update_Vector_With_Vector; |
| 1209 |
|
| 1210 |
----------------- |
| 1211 |
-- Unit_Matrix -- |
| 1212 |
----------------- |
| 1213 |
|
| 1214 |
function Unit_Matrix |
| 1215 |
(Order : Positive; |
| 1216 |
First_1 : Integer := 1; |
| 1217 |
First_2 : Integer := 1) return Matrix |
| 1218 |
is |
| 1219 |
begin |
| 1220 |
return R : Matrix (First_1 .. Check_Unit_Last (First_1, Order, First_1), |
| 1221 |
First_2 .. Check_Unit_Last (First_2, Order, First_2)) |
| 1222 |
do |
| 1223 |
R := [others => [others => Zero]]; |
| 1224 |
|
| 1225 |
for J in 0 .. Order - 1 loop |
| 1226 |
R (First_1 + J, First_2 + J) := One; |
| 1227 |
end loop; |
| 1228 |
end return; |
| 1229 |
end Unit_Matrix; |
| 1230 |
|
| 1231 |
----------------- |
| 1232 |
-- Unit_Vector -- |
| 1233 |
----------------- |
| 1234 |
|
| 1235 |
function Unit_Vector |
| 1236 |
(Index : Integer; |
| 1237 |
Order : Positive; |
| 1238 |
First : Integer := 1) return Vector |
| 1239 |
is |
| 1240 |
begin |
| 1241 |
return R : Vector (First .. Check_Unit_Last (Index, Order, First)) do |
| 1242 |
R := [others => Zero]; |
| 1243 |
R (Index) := One; |
| 1244 |
end return; |
| 1245 |
end Unit_Vector; |
| 1246 |
|
| 1247 |
--------------------------- |
| 1248 |
-- Vector_Matrix_Product -- |
| 1249 |
--------------------------- |
| 1250 |
|
| 1251 |
function Vector_Matrix_Product |
| 1252 |
(Left : Left_Vector; |
| 1253 |
Right : Matrix) return Result_Vector |
| 1254 |
is |
| 1255 |
begin |
| 1256 |
return R : Result_Vector (Right'Range (2)) do |
| 1257 |
if Left'Length /= Right'Length (1) then |
| 1258 |
raise Constraint_Error with |
| 1259 |
"incompatible dimensions in vector-matrix multiplication"; |
| 1260 |
end if; |
| 1261 |
|
| 1262 |
for J in Right'Range (2) loop |
| 1263 |
declare |
| 1264 |
S : Result_Scalar := Zero; |
| 1265 |
|
| 1266 |
begin |
| 1267 |
for K in Right'Range (1) loop |
| 1268 |
S := S + Left (K - Right'First (1) |
| 1269 |
+ Left'First) * Right (K, J); |
| 1270 |
end loop; |
| 1271 |
|
| 1272 |
R (J) := S; |
| 1273 |
end; |
| 1274 |
end loop; |
| 1275 |
end return; |
| 1276 |
end Vector_Matrix_Product; |
| 1277 |
|
| 1278 |
end System.Generic_Array_Operations; |