| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- ADA.NUMERICS.GENERIC_REAL_ARRAYS -- |
| 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 |
-- This version of Generic_Real_Arrays avoids the use of BLAS and LAPACK. One |
| 33 |
-- reason for this is new Ada 2012 requirements that prohibit algorithms such |
| 34 |
-- as Strassen's algorithm, which may be used by some BLAS implementations. In |
| 35 |
-- addition, some platforms lacked suitable compilers to compile the reference |
| 36 |
-- BLAS/LAPACK implementation. Finally, on some platforms there are more |
| 37 |
-- floating point types than supported by BLAS/LAPACK. |
| 38 |
|
| 39 |
-- Preconditions, postconditions, ghost code, loop invariants and assertions |
| 40 |
-- in this unit are meant for analysis only, not for run-time checking, as it |
| 41 |
-- would be too costly otherwise. This is enforced by setting the assertion |
| 42 |
-- policy to Ignore. |
| 43 |
|
| 44 |
pragma Assertion_Policy (Pre => Ignore, |
| 45 |
Post => Ignore, |
| 46 |
Ghost => Ignore, |
| 47 |
Loop_Invariant => Ignore, |
| 48 |
Assert => Ignore); |
| 49 |
|
| 50 |
with Ada.Containers.Generic_Anonymous_Array_Sort; use Ada.Containers; |
| 51 |
|
| 52 |
with System; use System; |
| 53 |
with System.Generic_Array_Operations; use System.Generic_Array_Operations; |
| 54 |
|
| 55 |
package body Ada.Numerics.Generic_Real_Arrays is |
| 56 |
|
| 57 |
package Ops renames System.Generic_Array_Operations; |
| 58 |
|
| 59 |
function Is_Non_Zero (X : Real'Base) return Boolean is (X /= 0.0); |
| 60 |
|
| 61 |
procedure Back_Substitute is new Ops.Back_Substitute |
| 62 |
(Scalar => Real'Base, |
| 63 |
Matrix => Real_Matrix, |
| 64 |
Is_Non_Zero => Is_Non_Zero); |
| 65 |
|
| 66 |
function Diagonal is new Ops.Diagonal |
| 67 |
(Scalar => Real'Base, |
| 68 |
Vector => Real_Vector, |
| 69 |
Matrix => Real_Matrix); |
| 70 |
|
| 71 |
procedure Forward_Eliminate is new Ops.Forward_Eliminate |
| 72 |
(Scalar => Real'Base, |
| 73 |
Real => Real'Base, |
| 74 |
Matrix => Real_Matrix, |
| 75 |
Zero => 0.0, |
| 76 |
One => 1.0); |
| 77 |
|
| 78 |
procedure Swap_Column is new Ops.Swap_Column |
| 79 |
(Scalar => Real'Base, |
| 80 |
Matrix => Real_Matrix); |
| 81 |
|
| 82 |
procedure Transpose is new Ops.Transpose |
| 83 |
(Scalar => Real'Base, |
| 84 |
Matrix => Real_Matrix); |
| 85 |
|
| 86 |
function Is_Symmetric (A : Real_Matrix) return Boolean is |
| 87 |
(Transpose (A) = A); |
| 88 |
-- Return True iff A is symmetric, see RM G.3.1 (90) |
| 89 |
|
| 90 |
function Is_Tiny (Value, Compared_To : Real) return Boolean is |
| 91 |
(abs Compared_To + 100.0 * abs Value = abs Compared_To); |
| 92 |
-- Return True iff the Value is much smaller in magnitude than the least |
| 93 |
-- significant digit of Compared_To. |
| 94 |
|
| 95 |
procedure Jacobi |
| 96 |
(A : Real_Matrix; |
| 97 |
Values : out Real_Vector; |
| 98 |
Vectors : out Real_Matrix; |
| 99 |
Compute_Vectors : Boolean); |
| 100 |
-- Perform Jacobi's eigensystem algorithm on real symmetric matrix A |
| 101 |
|
| 102 |
function Length is new Square_Matrix_Length (Real'Base, Real_Matrix); |
| 103 |
-- Helper function that raises a Constraint_Error is the argument is |
| 104 |
-- not a square matrix, and otherwise returns its length. |
| 105 |
|
| 106 |
procedure Rotate (X, Y : in out Real; Sin, Tau : Real); |
| 107 |
-- Perform a Givens rotation of angle Theta given by sin and sin/(1 + cos) |
| 108 |
|
| 109 |
procedure Sort_Eigensystem |
| 110 |
(Values : in out Real_Vector; |
| 111 |
Vectors : in out Real_Matrix; |
| 112 |
Compute_Vectors : Boolean); |
| 113 |
-- Sort Values and associated Vectors by decreasing absolute value |
| 114 |
|
| 115 |
procedure Swap (Left, Right : in out Real); |
| 116 |
-- Exchange Left and Right |
| 117 |
|
| 118 |
function Sqrt is new Ops.Sqrt (Real); |
| 119 |
-- Instant a generic square root implementation here, in order to avoid |
| 120 |
-- instantiating a complete copy of Generic_Elementary_Functions. |
| 121 |
-- Speed of the square root is not a big concern here. |
| 122 |
|
| 123 |
------------ |
| 124 |
-- Rotate -- |
| 125 |
------------ |
| 126 |
|
| 127 |
procedure Rotate (X, Y : in out Real; Sin, Tau : Real) is |
| 128 |
Old_X : constant Real := X; |
| 129 |
Old_Y : constant Real := Y; |
| 130 |
begin |
| 131 |
X := Old_X - Sin * (Old_Y + Old_X * Tau); |
| 132 |
Y := Old_Y + Sin * (Old_X - Old_Y * Tau); |
| 133 |
end Rotate; |
| 134 |
|
| 135 |
---------- |
| 136 |
-- Swap -- |
| 137 |
---------- |
| 138 |
|
| 139 |
procedure Swap (Left, Right : in out Real) is |
| 140 |
Temp : constant Real := Left; |
| 141 |
begin |
| 142 |
Left := Right; |
| 143 |
Right := Temp; |
| 144 |
end Swap; |
| 145 |
|
| 146 |
-- Instantiating the following subprograms directly would lead to |
| 147 |
-- name clashes, so use a local package. |
| 148 |
|
| 149 |
package Instantiations is |
| 150 |
|
| 151 |
function "+" is new |
| 152 |
Vector_Elementwise_Operation |
| 153 |
(X_Scalar => Real'Base, |
| 154 |
Result_Scalar => Real'Base, |
| 155 |
X_Vector => Real_Vector, |
| 156 |
Result_Vector => Real_Vector, |
| 157 |
Operation => "+"); |
| 158 |
|
| 159 |
function "+" is new |
| 160 |
Matrix_Elementwise_Operation |
| 161 |
(X_Scalar => Real'Base, |
| 162 |
Result_Scalar => Real'Base, |
| 163 |
X_Matrix => Real_Matrix, |
| 164 |
Result_Matrix => Real_Matrix, |
| 165 |
Operation => "+"); |
| 166 |
|
| 167 |
function "+" is new |
| 168 |
Vector_Vector_Elementwise_Operation |
| 169 |
(Left_Scalar => Real'Base, |
| 170 |
Right_Scalar => Real'Base, |
| 171 |
Result_Scalar => Real'Base, |
| 172 |
Left_Vector => Real_Vector, |
| 173 |
Right_Vector => Real_Vector, |
| 174 |
Result_Vector => Real_Vector, |
| 175 |
Operation => "+"); |
| 176 |
|
| 177 |
function "+" is new |
| 178 |
Matrix_Matrix_Elementwise_Operation |
| 179 |
(Left_Scalar => Real'Base, |
| 180 |
Right_Scalar => Real'Base, |
| 181 |
Result_Scalar => Real'Base, |
| 182 |
Left_Matrix => Real_Matrix, |
| 183 |
Right_Matrix => Real_Matrix, |
| 184 |
Result_Matrix => Real_Matrix, |
| 185 |
Operation => "+"); |
| 186 |
|
| 187 |
function "-" is new |
| 188 |
Vector_Elementwise_Operation |
| 189 |
(X_Scalar => Real'Base, |
| 190 |
Result_Scalar => Real'Base, |
| 191 |
X_Vector => Real_Vector, |
| 192 |
Result_Vector => Real_Vector, |
| 193 |
Operation => "-"); |
| 194 |
|
| 195 |
function "-" is new |
| 196 |
Matrix_Elementwise_Operation |
| 197 |
(X_Scalar => Real'Base, |
| 198 |
Result_Scalar => Real'Base, |
| 199 |
X_Matrix => Real_Matrix, |
| 200 |
Result_Matrix => Real_Matrix, |
| 201 |
Operation => "-"); |
| 202 |
|
| 203 |
function "-" is new |
| 204 |
Vector_Vector_Elementwise_Operation |
| 205 |
(Left_Scalar => Real'Base, |
| 206 |
Right_Scalar => Real'Base, |
| 207 |
Result_Scalar => Real'Base, |
| 208 |
Left_Vector => Real_Vector, |
| 209 |
Right_Vector => Real_Vector, |
| 210 |
Result_Vector => Real_Vector, |
| 211 |
Operation => "-"); |
| 212 |
|
| 213 |
function "-" is new |
| 214 |
Matrix_Matrix_Elementwise_Operation |
| 215 |
(Left_Scalar => Real'Base, |
| 216 |
Right_Scalar => Real'Base, |
| 217 |
Result_Scalar => Real'Base, |
| 218 |
Left_Matrix => Real_Matrix, |
| 219 |
Right_Matrix => Real_Matrix, |
| 220 |
Result_Matrix => Real_Matrix, |
| 221 |
Operation => "-"); |
| 222 |
|
| 223 |
function "*" is new |
| 224 |
Scalar_Vector_Elementwise_Operation |
| 225 |
(Left_Scalar => Real'Base, |
| 226 |
Right_Scalar => Real'Base, |
| 227 |
Result_Scalar => Real'Base, |
| 228 |
Right_Vector => Real_Vector, |
| 229 |
Result_Vector => Real_Vector, |
| 230 |
Operation => "*"); |
| 231 |
|
| 232 |
function "*" is new |
| 233 |
Scalar_Matrix_Elementwise_Operation |
| 234 |
(Left_Scalar => Real'Base, |
| 235 |
Right_Scalar => Real'Base, |
| 236 |
Result_Scalar => Real'Base, |
| 237 |
Right_Matrix => Real_Matrix, |
| 238 |
Result_Matrix => Real_Matrix, |
| 239 |
Operation => "*"); |
| 240 |
|
| 241 |
function "*" is new |
| 242 |
Vector_Scalar_Elementwise_Operation |
| 243 |
(Left_Scalar => Real'Base, |
| 244 |
Right_Scalar => Real'Base, |
| 245 |
Result_Scalar => Real'Base, |
| 246 |
Left_Vector => Real_Vector, |
| 247 |
Result_Vector => Real_Vector, |
| 248 |
Operation => "*"); |
| 249 |
|
| 250 |
function "*" is new |
| 251 |
Matrix_Scalar_Elementwise_Operation |
| 252 |
(Left_Scalar => Real'Base, |
| 253 |
Right_Scalar => Real'Base, |
| 254 |
Result_Scalar => Real'Base, |
| 255 |
Left_Matrix => Real_Matrix, |
| 256 |
Result_Matrix => Real_Matrix, |
| 257 |
Operation => "*"); |
| 258 |
|
| 259 |
function "*" is new |
| 260 |
Outer_Product |
| 261 |
(Left_Scalar => Real'Base, |
| 262 |
Right_Scalar => Real'Base, |
| 263 |
Result_Scalar => Real'Base, |
| 264 |
Left_Vector => Real_Vector, |
| 265 |
Right_Vector => Real_Vector, |
| 266 |
Matrix => Real_Matrix); |
| 267 |
|
| 268 |
function "*" is new |
| 269 |
Inner_Product |
| 270 |
(Left_Scalar => Real'Base, |
| 271 |
Right_Scalar => Real'Base, |
| 272 |
Result_Scalar => Real'Base, |
| 273 |
Left_Vector => Real_Vector, |
| 274 |
Right_Vector => Real_Vector, |
| 275 |
Zero => 0.0); |
| 276 |
|
| 277 |
function "*" is new |
| 278 |
Matrix_Vector_Product |
| 279 |
(Left_Scalar => Real'Base, |
| 280 |
Right_Scalar => Real'Base, |
| 281 |
Result_Scalar => Real'Base, |
| 282 |
Matrix => Real_Matrix, |
| 283 |
Right_Vector => Real_Vector, |
| 284 |
Result_Vector => Real_Vector, |
| 285 |
Zero => 0.0); |
| 286 |
|
| 287 |
function "*" is new |
| 288 |
Vector_Matrix_Product |
| 289 |
(Left_Scalar => Real'Base, |
| 290 |
Right_Scalar => Real'Base, |
| 291 |
Result_Scalar => Real'Base, |
| 292 |
Left_Vector => Real_Vector, |
| 293 |
Matrix => Real_Matrix, |
| 294 |
Result_Vector => Real_Vector, |
| 295 |
Zero => 0.0); |
| 296 |
|
| 297 |
function "*" is new |
| 298 |
Matrix_Matrix_Product |
| 299 |
(Left_Scalar => Real'Base, |
| 300 |
Right_Scalar => Real'Base, |
| 301 |
Result_Scalar => Real'Base, |
| 302 |
Left_Matrix => Real_Matrix, |
| 303 |
Right_Matrix => Real_Matrix, |
| 304 |
Result_Matrix => Real_Matrix, |
| 305 |
Zero => 0.0); |
| 306 |
|
| 307 |
function "/" is new |
| 308 |
Vector_Scalar_Elementwise_Operation |
| 309 |
(Left_Scalar => Real'Base, |
| 310 |
Right_Scalar => Real'Base, |
| 311 |
Result_Scalar => Real'Base, |
| 312 |
Left_Vector => Real_Vector, |
| 313 |
Result_Vector => Real_Vector, |
| 314 |
Operation => "/"); |
| 315 |
|
| 316 |
function "/" is new |
| 317 |
Matrix_Scalar_Elementwise_Operation |
| 318 |
(Left_Scalar => Real'Base, |
| 319 |
Right_Scalar => Real'Base, |
| 320 |
Result_Scalar => Real'Base, |
| 321 |
Left_Matrix => Real_Matrix, |
| 322 |
Result_Matrix => Real_Matrix, |
| 323 |
Operation => "/"); |
| 324 |
|
| 325 |
function "abs" is new |
| 326 |
L2_Norm |
| 327 |
(X_Scalar => Real'Base, |
| 328 |
Result_Real => Real'Base, |
| 329 |
X_Vector => Real_Vector, |
| 330 |
"abs" => "+"); |
| 331 |
-- While the L2_Norm by definition uses the absolute values of the |
| 332 |
-- elements of X_Vector, for real values the subsequent squaring |
| 333 |
-- makes this unnecessary, so we substitute the "+" identity function |
| 334 |
-- instead. |
| 335 |
|
| 336 |
function "abs" is new |
| 337 |
Vector_Elementwise_Operation |
| 338 |
(X_Scalar => Real'Base, |
| 339 |
Result_Scalar => Real'Base, |
| 340 |
X_Vector => Real_Vector, |
| 341 |
Result_Vector => Real_Vector, |
| 342 |
Operation => "abs"); |
| 343 |
|
| 344 |
function "abs" is new |
| 345 |
Matrix_Elementwise_Operation |
| 346 |
(X_Scalar => Real'Base, |
| 347 |
Result_Scalar => Real'Base, |
| 348 |
X_Matrix => Real_Matrix, |
| 349 |
Result_Matrix => Real_Matrix, |
| 350 |
Operation => "abs"); |
| 351 |
|
| 352 |
function Solve is new |
| 353 |
Matrix_Vector_Solution (Real'Base, 0.0, Real_Vector, Real_Matrix); |
| 354 |
|
| 355 |
function Solve is new |
| 356 |
Matrix_Matrix_Solution (Real'Base, 0.0, Real_Matrix); |
| 357 |
|
| 358 |
function Unit_Matrix is new |
| 359 |
Generic_Array_Operations.Unit_Matrix |
| 360 |
(Scalar => Real'Base, |
| 361 |
Matrix => Real_Matrix, |
| 362 |
Zero => 0.0, |
| 363 |
One => 1.0); |
| 364 |
|
| 365 |
function Unit_Vector is new |
| 366 |
Generic_Array_Operations.Unit_Vector |
| 367 |
(Scalar => Real'Base, |
| 368 |
Vector => Real_Vector, |
| 369 |
Zero => 0.0, |
| 370 |
One => 1.0); |
| 371 |
|
| 372 |
end Instantiations; |
| 373 |
|
| 374 |
--------- |
| 375 |
-- "+" -- |
| 376 |
--------- |
| 377 |
|
| 378 |
function "+" (Right : Real_Vector) return Real_Vector |
| 379 |
renames Instantiations."+"; |
| 380 |
|
| 381 |
function "+" (Right : Real_Matrix) return Real_Matrix |
| 382 |
renames Instantiations."+"; |
| 383 |
|
| 384 |
function "+" (Left, Right : Real_Vector) return Real_Vector |
| 385 |
renames Instantiations."+"; |
| 386 |
|
| 387 |
function "+" (Left, Right : Real_Matrix) return Real_Matrix |
| 388 |
renames Instantiations."+"; |
| 389 |
|
| 390 |
--------- |
| 391 |
-- "-" -- |
| 392 |
--------- |
| 393 |
|
| 394 |
function "-" (Right : Real_Vector) return Real_Vector |
| 395 |
renames Instantiations."-"; |
| 396 |
|
| 397 |
function "-" (Right : Real_Matrix) return Real_Matrix |
| 398 |
renames Instantiations."-"; |
| 399 |
|
| 400 |
function "-" (Left, Right : Real_Vector) return Real_Vector |
| 401 |
renames Instantiations."-"; |
| 402 |
|
| 403 |
function "-" (Left, Right : Real_Matrix) return Real_Matrix |
| 404 |
renames Instantiations."-"; |
| 405 |
|
| 406 |
--------- |
| 407 |
-- "*" -- |
| 408 |
--------- |
| 409 |
|
| 410 |
-- Scalar multiplication |
| 411 |
|
| 412 |
function "*" (Left : Real'Base; Right : Real_Vector) return Real_Vector |
| 413 |
renames Instantiations."*"; |
| 414 |
|
| 415 |
function "*" (Left : Real_Vector; Right : Real'Base) return Real_Vector |
| 416 |
renames Instantiations."*"; |
| 417 |
|
| 418 |
function "*" (Left : Real'Base; Right : Real_Matrix) return Real_Matrix |
| 419 |
renames Instantiations."*"; |
| 420 |
|
| 421 |
function "*" (Left : Real_Matrix; Right : Real'Base) return Real_Matrix |
| 422 |
renames Instantiations."*"; |
| 423 |
|
| 424 |
-- Vector multiplication |
| 425 |
|
| 426 |
function "*" (Left, Right : Real_Vector) return Real'Base |
| 427 |
renames Instantiations."*"; |
| 428 |
|
| 429 |
function "*" (Left, Right : Real_Vector) return Real_Matrix |
| 430 |
renames Instantiations."*"; |
| 431 |
|
| 432 |
function "*" (Left : Real_Vector; Right : Real_Matrix) return Real_Vector |
| 433 |
renames Instantiations."*"; |
| 434 |
|
| 435 |
function "*" (Left : Real_Matrix; Right : Real_Vector) return Real_Vector |
| 436 |
renames Instantiations."*"; |
| 437 |
|
| 438 |
-- Matrix Multiplication |
| 439 |
|
| 440 |
function "*" (Left, Right : Real_Matrix) return Real_Matrix |
| 441 |
renames Instantiations."*"; |
| 442 |
|
| 443 |
--------- |
| 444 |
-- "/" -- |
| 445 |
--------- |
| 446 |
|
| 447 |
function "/" (Left : Real_Vector; Right : Real'Base) return Real_Vector |
| 448 |
renames Instantiations."/"; |
| 449 |
|
| 450 |
function "/" (Left : Real_Matrix; Right : Real'Base) return Real_Matrix |
| 451 |
renames Instantiations."/"; |
| 452 |
|
| 453 |
----------- |
| 454 |
-- "abs" -- |
| 455 |
----------- |
| 456 |
|
| 457 |
function "abs" (Right : Real_Vector) return Real'Base |
| 458 |
renames Instantiations."abs"; |
| 459 |
|
| 460 |
function "abs" (Right : Real_Vector) return Real_Vector |
| 461 |
renames Instantiations."abs"; |
| 462 |
|
| 463 |
function "abs" (Right : Real_Matrix) return Real_Matrix |
| 464 |
renames Instantiations."abs"; |
| 465 |
|
| 466 |
----------------- |
| 467 |
-- Determinant -- |
| 468 |
----------------- |
| 469 |
|
| 470 |
function Determinant (A : Real_Matrix) return Real'Base is |
| 471 |
M : Real_Matrix := A; |
| 472 |
B : Real_Matrix (A'Range (1), 1 .. 0); |
| 473 |
R : Real'Base; |
| 474 |
begin |
| 475 |
Forward_Eliminate (M, B, R); |
| 476 |
return R; |
| 477 |
end Determinant; |
| 478 |
|
| 479 |
----------------- |
| 480 |
-- Eigensystem -- |
| 481 |
----------------- |
| 482 |
|
| 483 |
procedure Eigensystem |
| 484 |
(A : Real_Matrix; |
| 485 |
Values : out Real_Vector; |
| 486 |
Vectors : out Real_Matrix) |
| 487 |
is |
| 488 |
begin |
| 489 |
Jacobi (A, Values, Vectors, Compute_Vectors => True); |
| 490 |
Sort_Eigensystem (Values, Vectors, Compute_Vectors => True); |
| 491 |
end Eigensystem; |
| 492 |
|
| 493 |
----------------- |
| 494 |
-- Eigenvalues -- |
| 495 |
----------------- |
| 496 |
|
| 497 |
function Eigenvalues (A : Real_Matrix) return Real_Vector is |
| 498 |
begin |
| 499 |
return Values : Real_Vector (A'Range (1)) do |
| 500 |
declare |
| 501 |
Vectors : Real_Matrix (1 .. 0, 1 .. 0); |
| 502 |
begin |
| 503 |
Jacobi (A, Values, Vectors, Compute_Vectors => False); |
| 504 |
Sort_Eigensystem (Values, Vectors, Compute_Vectors => False); |
| 505 |
end; |
| 506 |
end return; |
| 507 |
end Eigenvalues; |
| 508 |
|
| 509 |
------------- |
| 510 |
-- Inverse -- |
| 511 |
------------- |
| 512 |
|
| 513 |
function Inverse (A : Real_Matrix) return Real_Matrix is |
| 514 |
(Solve (A, Unit_Matrix (Length (A), |
| 515 |
First_1 => A'First (2), |
| 516 |
First_2 => A'First (1)))); |
| 517 |
|
| 518 |
------------ |
| 519 |
-- Jacobi -- |
| 520 |
------------ |
| 521 |
|
| 522 |
procedure Jacobi |
| 523 |
(A : Real_Matrix; |
| 524 |
Values : out Real_Vector; |
| 525 |
Vectors : out Real_Matrix; |
| 526 |
Compute_Vectors : Boolean) |
| 527 |
is |
| 528 |
-- This subprogram uses Carl Gustav Jacob Jacobi's cyclic iterative |
| 529 |
-- method for computing eigenvalues and eigenvectors and is based on |
| 530 |
-- Rutishauser's implementation. |
| 531 |
|
| 532 |
-- The given real symmetric matrix is transformed iteratively to |
| 533 |
-- diagonal form through a sequence of appropriately chosen elementary |
| 534 |
-- orthogonal transformations, called Jacobi rotations. |
| 535 |
|
| 536 |
-- The Jacobi method produces a systematic decrease of the sum of the |
| 537 |
-- squares of off-diagonal elements. Convergence to zero is quadratic, |
| 538 |
-- both for this implementation, as for the classic method that doesn't |
| 539 |
-- use row-wise scanning for pivot selection. |
| 540 |
|
| 541 |
-- The numerical stability and accuracy of Jacobi's method make it the |
| 542 |
-- best choice here, even though for large matrices other methods will |
| 543 |
-- be significantly more efficient in both time and space. |
| 544 |
|
| 545 |
-- While the eigensystem computation is absolutely foolproof for all |
| 546 |
-- real symmetric matrices, in presence of invalid values, or similar |
| 547 |
-- exceptional situations, it may not be. In such cases, the results |
| 548 |
-- cannot be trusted and Constraint_Error is raised. |
| 549 |
|
| 550 |
-- Note: this implementation needs temporary storage for 2 * N + N**2 |
| 551 |
-- values of type Real. |
| 552 |
|
| 553 |
Max_Iterations : constant := 50; |
| 554 |
N : constant Natural := Length (A); |
| 555 |
|
| 556 |
subtype Square_Matrix is Real_Matrix (1 .. N, 1 .. N); |
| 557 |
|
| 558 |
-- In order to annihilate the M (Row, Col) element, the rotation angle |
| 559 |
-- Theta is chosen as follows: |
| 560 |
|
| 561 |
-- Cot (2.0 * Theta) = (Diag (Col) - Diag (Row)) / (2.0 * M (Row, Col)) |
| 562 |
|
| 563 |
-- If C = Cot (2.0 * Theta), then Tan (Theta) is computed as the smaller |
| 564 |
-- root (in modulus) of: |
| 565 |
|
| 566 |
-- X**2 + 2 * C * X - 1 = 0 |
| 567 |
|
| 568 |
-- or else as 0.5 / C, if C is large. |
| 569 |
|
| 570 |
function Compute_Tan (C : Real) return Real is |
| 571 |
(Real'Copy_Sign (1.0 / (abs C + Sqrt (1.0 + C**2)), C)); |
| 572 |
|
| 573 |
function Compute_Tan (P, H : Real) return Real is |
| 574 |
(if Is_Tiny (P, Compared_To => H) |
| 575 |
then P / H |
| 576 |
else Compute_Tan (C => H / (2.0 * P))); |
| 577 |
pragma Annotate |
| 578 |
(CodePeer, False_Positive, "divide by zero", "H, P /= 0"); |
| 579 |
|
| 580 |
function Sum_Strict_Upper (M : Square_Matrix) return Real; |
| 581 |
-- Return the sum of the absolute value of all the elements in the |
| 582 |
-- strict upper triangle of M. |
| 583 |
|
| 584 |
---------------------- |
| 585 |
-- Sum_Strict_Upper -- |
| 586 |
---------------------- |
| 587 |
|
| 588 |
function Sum_Strict_Upper (M : Square_Matrix) return Real is |
| 589 |
Sum : Real := 0.0; |
| 590 |
|
| 591 |
begin |
| 592 |
for Row in 1 .. N - 1 loop |
| 593 |
for Col in Row + 1 .. N loop |
| 594 |
Sum := Sum + abs M (Row, Col); |
| 595 |
end loop; |
| 596 |
end loop; |
| 597 |
|
| 598 |
return Sum; |
| 599 |
end Sum_Strict_Upper; |
| 600 |
|
| 601 |
-- Local variables |
| 602 |
|
| 603 |
M : Square_Matrix := A; -- Work space for solving eigensystem |
| 604 |
Diag : Real_Vector (1 .. N); |
| 605 |
Diag_Adj : Real_Vector (1 .. N); |
| 606 |
Sum : Real; |
| 607 |
Threshold : Real; |
| 608 |
|
| 609 |
-- The vector Diag_Adj indicates the amount of change in each value, |
| 610 |
-- while Diag tracks the value itself and Values holds the values as |
| 611 |
-- they were at the beginning. As the changes typically will be small |
| 612 |
-- compared to the absolute value of Diag, at the end of each iteration |
| 613 |
-- Diag is computed as Diag + Diag_Adj thus avoiding accumulating |
| 614 |
-- rounding errors. This technique is due to Rutishauser. |
| 615 |
|
| 616 |
begin |
| 617 |
if Compute_Vectors |
| 618 |
and then (Vectors'Length (1) /= N or else Vectors'Length (2) /= N) |
| 619 |
then |
| 620 |
raise Constraint_Error with "incompatible matrix dimensions"; |
| 621 |
|
| 622 |
elsif Values'Length /= N then |
| 623 |
raise Constraint_Error with "incompatible vector length"; |
| 624 |
|
| 625 |
elsif not Is_Symmetric (M) then |
| 626 |
raise Constraint_Error with "matrix not symmetric"; |
| 627 |
end if; |
| 628 |
|
| 629 |
Values := Diagonal (M); |
| 630 |
|
| 631 |
-- Note: Only the locally declared matrix M and vectors (Diag, Diag_Adj) |
| 632 |
-- have lower bound equal to 1. The Vectors matrix may have |
| 633 |
-- different bounds, so take care indexing elements. Assignment |
| 634 |
-- as a whole is fine as sliding is automatic in that case. |
| 635 |
|
| 636 |
Vectors := (if Compute_Vectors |
| 637 |
then Unit_Matrix (N) |
| 638 |
else [1 .. 0 => [1 .. 0 => 0.0]]); |
| 639 |
|
| 640 |
Sweep : for Iteration in 1 .. Max_Iterations loop |
| 641 |
|
| 642 |
-- During the first three iterations, perform the rotation only for |
| 643 |
-- elements that are not much smaller than the average off-diagonal |
| 644 |
-- element. After this, rotate for any non-zero elements. After the |
| 645 |
-- fifth iteration, additionally zero out off-diagonal elements that |
| 646 |
-- are very small compared to elements on the diagonal with the same |
| 647 |
-- column or row index. |
| 648 |
|
| 649 |
Sum := Sum_Strict_Upper (M); |
| 650 |
|
| 651 |
exit Sweep when Sum = 0.0; |
| 652 |
|
| 653 |
Threshold := (if Iteration < 4 then 0.2 * Sum / Real (N**2) else 0.0); |
| 654 |
|
| 655 |
-- Iterate over all off-diagonal elements, rotating any that have an |
| 656 |
-- absolute value that exceeds the threshold. |
| 657 |
|
| 658 |
Diag := Values; |
| 659 |
Diag_Adj := [others => 0.0]; -- Accumulates adjustments to Diag |
| 660 |
|
| 661 |
for Row in 1 .. N - 1 loop |
| 662 |
for Col in Row + 1 .. N loop |
| 663 |
|
| 664 |
-- If, before the rotation, M (Row, Col) is tiny compared to |
| 665 |
-- Diag (Row) and Diag (Col), rotation is skipped. This is |
| 666 |
-- meaningful, as it produces no larger error than would be |
| 667 |
-- produced anyhow if the rotation had been performed. |
| 668 |
-- Suppress this optimization in the first four iterations, so |
| 669 |
-- that this procedure can be used for computing eigenvectors |
| 670 |
-- of perturbed diagonal matrices. |
| 671 |
|
| 672 |
if Iteration > 4 |
| 673 |
and then Is_Tiny (M (Row, Col), Compared_To => Diag (Row)) |
| 674 |
and then Is_Tiny (M (Row, Col), Compared_To => Diag (Col)) |
| 675 |
then |
| 676 |
M (Row, Col) := 0.0; |
| 677 |
|
| 678 |
elsif abs M (Row, Col) > Threshold then |
| 679 |
Perform_Rotation : declare |
| 680 |
Tan : constant Real := |
| 681 |
Compute_Tan (M (Row, Col), Diag (Col) - Diag (Row)); |
| 682 |
Cos : constant Real := 1.0 / Sqrt (1.0 + Tan**2); |
| 683 |
Sin : constant Real := Tan * Cos; |
| 684 |
Tau : constant Real := Sin / (1.0 + Cos); |
| 685 |
Adj : constant Real := Tan * M (Row, Col); |
| 686 |
|
| 687 |
begin |
| 688 |
Diag_Adj (Row) := Diag_Adj (Row) - Adj; |
| 689 |
Diag_Adj (Col) := Diag_Adj (Col) + Adj; |
| 690 |
Diag (Row) := Diag (Row) - Adj; |
| 691 |
Diag (Col) := Diag (Col) + Adj; |
| 692 |
|
| 693 |
M (Row, Col) := 0.0; |
| 694 |
|
| 695 |
for J in 1 .. Row - 1 loop -- 1 <= J < Row |
| 696 |
Rotate (M (J, Row), M (J, Col), Sin, Tau); |
| 697 |
end loop; |
| 698 |
|
| 699 |
for J in Row + 1 .. Col - 1 loop -- Row < J < Col |
| 700 |
Rotate (M (Row, J), M (J, Col), Sin, Tau); |
| 701 |
end loop; |
| 702 |
|
| 703 |
for J in Col + 1 .. N loop -- Col < J <= N |
| 704 |
Rotate (M (Row, J), M (Col, J), Sin, Tau); |
| 705 |
end loop; |
| 706 |
|
| 707 |
for J in Vectors'Range (1) loop |
| 708 |
Rotate (Vectors (J, Row - 1 + Vectors'First (2)), |
| 709 |
Vectors (J, Col - 1 + Vectors'First (2)), |
| 710 |
Sin, Tau); |
| 711 |
end loop; |
| 712 |
end Perform_Rotation; |
| 713 |
end if; |
| 714 |
end loop; |
| 715 |
end loop; |
| 716 |
|
| 717 |
Values := Values + Diag_Adj; |
| 718 |
end loop Sweep; |
| 719 |
|
| 720 |
-- All normal matrices with valid values should converge perfectly |
| 721 |
|
| 722 |
if Sum /= 0.0 then |
| 723 |
raise Constraint_Error with "eigensystem solution does not converge"; |
| 724 |
end if; |
| 725 |
end Jacobi; |
| 726 |
|
| 727 |
----------- |
| 728 |
-- Solve -- |
| 729 |
----------- |
| 730 |
|
| 731 |
function Solve (A : Real_Matrix; X : Real_Vector) return Real_Vector |
| 732 |
renames Instantiations.Solve; |
| 733 |
|
| 734 |
function Solve (A, X : Real_Matrix) return Real_Matrix |
| 735 |
renames Instantiations.Solve; |
| 736 |
|
| 737 |
---------------------- |
| 738 |
-- Sort_Eigensystem -- |
| 739 |
---------------------- |
| 740 |
|
| 741 |
procedure Sort_Eigensystem |
| 742 |
(Values : in out Real_Vector; |
| 743 |
Vectors : in out Real_Matrix; |
| 744 |
Compute_Vectors : Boolean) |
| 745 |
is |
| 746 |
procedure Swap (Left, Right : Integer); |
| 747 |
-- Swap Values (Left) with Values (Right), and also swap the |
| 748 |
-- corresponding eigenvectors. Note that lowerbounds may differ. |
| 749 |
|
| 750 |
function Less (Left, Right : Integer) return Boolean is |
| 751 |
(Values (Left) > Values (Right)); |
| 752 |
-- Sort by decreasing eigenvalue, see RM G.3.1 (76). |
| 753 |
|
| 754 |
procedure Sort is new Generic_Anonymous_Array_Sort (Integer); |
| 755 |
-- Sorts eigenvalues and eigenvectors by decreasing value |
| 756 |
|
| 757 |
procedure Swap (Left, Right : Integer) is |
| 758 |
begin |
| 759 |
Swap (Values (Left), Values (Right)); |
| 760 |
if Compute_Vectors then |
| 761 |
Swap_Column (Vectors, Left - Values'First + Vectors'First (2), |
| 762 |
Right - Values'First + Vectors'First (2)); |
| 763 |
end if; |
| 764 |
end Swap; |
| 765 |
|
| 766 |
begin |
| 767 |
Sort (Values'First, Values'Last); |
| 768 |
end Sort_Eigensystem; |
| 769 |
|
| 770 |
--------------- |
| 771 |
-- Transpose -- |
| 772 |
--------------- |
| 773 |
|
| 774 |
function Transpose (X : Real_Matrix) return Real_Matrix is |
| 775 |
begin |
| 776 |
return R : Real_Matrix (X'Range (2), X'Range (1)) do |
| 777 |
Transpose (X, R); |
| 778 |
end return; |
| 779 |
end Transpose; |
| 780 |
|
| 781 |
----------------- |
| 782 |
-- Unit_Matrix -- |
| 783 |
----------------- |
| 784 |
|
| 785 |
function Unit_Matrix |
| 786 |
(Order : Positive; |
| 787 |
First_1 : Integer := 1; |
| 788 |
First_2 : Integer := 1) return Real_Matrix |
| 789 |
renames Instantiations.Unit_Matrix; |
| 790 |
|
| 791 |
----------------- |
| 792 |
-- Unit_Vector -- |
| 793 |
----------------- |
| 794 |
|
| 795 |
function Unit_Vector |
| 796 |
(Index : Integer; |
| 797 |
Order : Positive; |
| 798 |
First : Integer := 1) return Real_Vector |
| 799 |
renames Instantiations.Unit_Vector; |
| 800 |
|
| 801 |
end Ada.Numerics.Generic_Real_Arrays; |