1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- ADA.NUMERICS.GENERIC_COMPLEX_ARRAYS --
9 -- Copyright (C) 2006-2024, 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 -- 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
37 pragma Assertion_Policy
(Pre
=> Ignore
,
40 Loop_Invariant
=> Ignore
,
43 with System
.Generic_Array_Operations
; use System
.Generic_Array_Operations
;
45 package body Ada
.Numerics
.Generic_Complex_Arrays
is
47 -- Operations that are defined in terms of operations on the type Real,
48 -- such as addition, subtraction and scaling, are computed in the canonical
49 -- way looping over all elements.
51 package Ops
renames System
.Generic_Array_Operations
;
53 subtype Real
is Real_Arrays
.Real
;
54 -- Work around visibility bug ???
56 function Is_Non_Zero
(X
: Complex
) return Boolean is (X
/= (0.0, 0.0));
57 -- Needed by Back_Substitute
59 procedure Back_Substitute
is new Ops
.Back_Substitute
61 Matrix
=> Complex_Matrix
,
62 Is_Non_Zero
=> Is_Non_Zero
);
64 procedure Forward_Eliminate
is new Ops
.Forward_Eliminate
67 Matrix
=> Complex_Matrix
,
71 procedure Transpose
is new Ops
.Transpose
73 Matrix
=> Complex_Matrix
);
75 -- Helper function that raises a Constraint_Error is the argument is
76 -- not a square matrix, and otherwise returns its length.
78 function Length
is new Square_Matrix_Length
(Complex
, Complex_Matrix
);
80 -- Instant a generic square root implementation here, in order to avoid
81 -- instantiating a complete copy of Generic_Elementary_Functions.
82 -- Speed of the square root is not a big concern here.
84 function Sqrt
is new Ops
.Sqrt
(Real
'Base);
86 -- Instantiating the following subprograms directly would lead to
87 -- name clashes, so use a local package.
89 package Instantiations
is
95 function "*" is new Vector_Scalar_Elementwise_Operation
96 (Left_Scalar
=> Complex
,
97 Right_Scalar
=> Complex
,
98 Result_Scalar
=> Complex
,
99 Left_Vector
=> Complex_Vector
,
100 Result_Vector
=> Complex_Vector
,
103 function "*" is new Vector_Scalar_Elementwise_Operation
104 (Left_Scalar
=> Complex
,
105 Right_Scalar
=> Real
'Base,
106 Result_Scalar
=> Complex
,
107 Left_Vector
=> Complex_Vector
,
108 Result_Vector
=> Complex_Vector
,
111 function "*" is new Scalar_Vector_Elementwise_Operation
112 (Left_Scalar
=> Complex
,
113 Right_Scalar
=> Complex
,
114 Result_Scalar
=> Complex
,
115 Right_Vector
=> Complex_Vector
,
116 Result_Vector
=> Complex_Vector
,
119 function "*" is new Scalar_Vector_Elementwise_Operation
120 (Left_Scalar
=> Real
'Base,
121 Right_Scalar
=> Complex
,
122 Result_Scalar
=> Complex
,
123 Right_Vector
=> Complex_Vector
,
124 Result_Vector
=> Complex_Vector
,
127 function "*" is new Inner_Product
128 (Left_Scalar
=> Complex
,
129 Right_Scalar
=> Real
'Base,
130 Result_Scalar
=> Complex
,
131 Left_Vector
=> Complex_Vector
,
132 Right_Vector
=> Real_Vector
,
135 function "*" is new Inner_Product
136 (Left_Scalar
=> Real
'Base,
137 Right_Scalar
=> Complex
,
138 Result_Scalar
=> Complex
,
139 Left_Vector
=> Real_Vector
,
140 Right_Vector
=> Complex_Vector
,
143 function "*" is new Inner_Product
144 (Left_Scalar
=> Complex
,
145 Right_Scalar
=> Complex
,
146 Result_Scalar
=> Complex
,
147 Left_Vector
=> Complex_Vector
,
148 Right_Vector
=> Complex_Vector
,
151 function "*" is new Outer_Product
152 (Left_Scalar
=> Complex
,
153 Right_Scalar
=> Complex
,
154 Result_Scalar
=> Complex
,
155 Left_Vector
=> Complex_Vector
,
156 Right_Vector
=> Complex_Vector
,
157 Matrix
=> Complex_Matrix
);
159 function "*" is new Outer_Product
160 (Left_Scalar
=> Real
'Base,
161 Right_Scalar
=> Complex
,
162 Result_Scalar
=> Complex
,
163 Left_Vector
=> Real_Vector
,
164 Right_Vector
=> Complex_Vector
,
165 Matrix
=> Complex_Matrix
);
167 function "*" is new Outer_Product
168 (Left_Scalar
=> Complex
,
169 Right_Scalar
=> Real
'Base,
170 Result_Scalar
=> Complex
,
171 Left_Vector
=> Complex_Vector
,
172 Right_Vector
=> Real_Vector
,
173 Matrix
=> Complex_Matrix
);
175 function "*" is new Matrix_Scalar_Elementwise_Operation
176 (Left_Scalar
=> Complex
,
177 Right_Scalar
=> Complex
,
178 Result_Scalar
=> Complex
,
179 Left_Matrix
=> Complex_Matrix
,
180 Result_Matrix
=> Complex_Matrix
,
183 function "*" is new Matrix_Scalar_Elementwise_Operation
184 (Left_Scalar
=> Complex
,
185 Right_Scalar
=> Real
'Base,
186 Result_Scalar
=> Complex
,
187 Left_Matrix
=> Complex_Matrix
,
188 Result_Matrix
=> Complex_Matrix
,
191 function "*" is new Scalar_Matrix_Elementwise_Operation
192 (Left_Scalar
=> Complex
,
193 Right_Scalar
=> Complex
,
194 Result_Scalar
=> Complex
,
195 Right_Matrix
=> Complex_Matrix
,
196 Result_Matrix
=> Complex_Matrix
,
199 function "*" is new Scalar_Matrix_Elementwise_Operation
200 (Left_Scalar
=> Real
'Base,
201 Right_Scalar
=> Complex
,
202 Result_Scalar
=> Complex
,
203 Right_Matrix
=> Complex_Matrix
,
204 Result_Matrix
=> Complex_Matrix
,
207 function "*" is new Matrix_Vector_Product
208 (Left_Scalar
=> Real
'Base,
209 Right_Scalar
=> Complex
,
210 Result_Scalar
=> Complex
,
211 Matrix
=> Real_Matrix
,
212 Right_Vector
=> Complex_Vector
,
213 Result_Vector
=> Complex_Vector
,
216 function "*" is new Matrix_Vector_Product
217 (Left_Scalar
=> Complex
,
218 Right_Scalar
=> Real
'Base,
219 Result_Scalar
=> Complex
,
220 Matrix
=> Complex_Matrix
,
221 Right_Vector
=> Real_Vector
,
222 Result_Vector
=> Complex_Vector
,
225 function "*" is new Matrix_Vector_Product
226 (Left_Scalar
=> Complex
,
227 Right_Scalar
=> Complex
,
228 Result_Scalar
=> Complex
,
229 Matrix
=> Complex_Matrix
,
230 Right_Vector
=> Complex_Vector
,
231 Result_Vector
=> Complex_Vector
,
234 function "*" is new Vector_Matrix_Product
235 (Left_Scalar
=> Real
'Base,
236 Right_Scalar
=> Complex
,
237 Result_Scalar
=> Complex
,
238 Left_Vector
=> Real_Vector
,
239 Matrix
=> Complex_Matrix
,
240 Result_Vector
=> Complex_Vector
,
243 function "*" is new Vector_Matrix_Product
244 (Left_Scalar
=> Complex
,
245 Right_Scalar
=> Real
'Base,
246 Result_Scalar
=> Complex
,
247 Left_Vector
=> Complex_Vector
,
248 Matrix
=> Real_Matrix
,
249 Result_Vector
=> Complex_Vector
,
252 function "*" is new Vector_Matrix_Product
253 (Left_Scalar
=> Complex
,
254 Right_Scalar
=> Complex
,
255 Result_Scalar
=> Complex
,
256 Left_Vector
=> Complex_Vector
,
257 Matrix
=> Complex_Matrix
,
258 Result_Vector
=> Complex_Vector
,
261 function "*" is new Matrix_Matrix_Product
262 (Left_Scalar
=> Complex
,
263 Right_Scalar
=> Complex
,
264 Result_Scalar
=> Complex
,
265 Left_Matrix
=> Complex_Matrix
,
266 Right_Matrix
=> Complex_Matrix
,
267 Result_Matrix
=> Complex_Matrix
,
270 function "*" is new Matrix_Matrix_Product
271 (Left_Scalar
=> Real
'Base,
272 Right_Scalar
=> Complex
,
273 Result_Scalar
=> Complex
,
274 Left_Matrix
=> Real_Matrix
,
275 Right_Matrix
=> Complex_Matrix
,
276 Result_Matrix
=> Complex_Matrix
,
279 function "*" is new Matrix_Matrix_Product
280 (Left_Scalar
=> Complex
,
281 Right_Scalar
=> Real
'Base,
282 Result_Scalar
=> Complex
,
283 Left_Matrix
=> Complex_Matrix
,
284 Right_Matrix
=> Real_Matrix
,
285 Result_Matrix
=> Complex_Matrix
,
292 function "+" is new Vector_Elementwise_Operation
293 (X_Scalar
=> Complex
,
294 Result_Scalar
=> Complex
,
295 X_Vector
=> Complex_Vector
,
296 Result_Vector
=> Complex_Vector
,
299 function "+" is new Vector_Vector_Elementwise_Operation
300 (Left_Scalar
=> Complex
,
301 Right_Scalar
=> Complex
,
302 Result_Scalar
=> Complex
,
303 Left_Vector
=> Complex_Vector
,
304 Right_Vector
=> Complex_Vector
,
305 Result_Vector
=> Complex_Vector
,
308 function "+" is new Vector_Vector_Elementwise_Operation
309 (Left_Scalar
=> Real
'Base,
310 Right_Scalar
=> Complex
,
311 Result_Scalar
=> Complex
,
312 Left_Vector
=> Real_Vector
,
313 Right_Vector
=> Complex_Vector
,
314 Result_Vector
=> Complex_Vector
,
317 function "+" is new Vector_Vector_Elementwise_Operation
318 (Left_Scalar
=> Complex
,
319 Right_Scalar
=> Real
'Base,
320 Result_Scalar
=> Complex
,
321 Left_Vector
=> Complex_Vector
,
322 Right_Vector
=> Real_Vector
,
323 Result_Vector
=> Complex_Vector
,
326 function "+" is new Matrix_Elementwise_Operation
327 (X_Scalar
=> Complex
,
328 Result_Scalar
=> Complex
,
329 X_Matrix
=> Complex_Matrix
,
330 Result_Matrix
=> Complex_Matrix
,
333 function "+" is new Matrix_Matrix_Elementwise_Operation
334 (Left_Scalar
=> Complex
,
335 Right_Scalar
=> Complex
,
336 Result_Scalar
=> Complex
,
337 Left_Matrix
=> Complex_Matrix
,
338 Right_Matrix
=> Complex_Matrix
,
339 Result_Matrix
=> Complex_Matrix
,
342 function "+" is new Matrix_Matrix_Elementwise_Operation
343 (Left_Scalar
=> Real
'Base,
344 Right_Scalar
=> Complex
,
345 Result_Scalar
=> Complex
,
346 Left_Matrix
=> Real_Matrix
,
347 Right_Matrix
=> Complex_Matrix
,
348 Result_Matrix
=> Complex_Matrix
,
351 function "+" is new Matrix_Matrix_Elementwise_Operation
352 (Left_Scalar
=> Complex
,
353 Right_Scalar
=> Real
'Base,
354 Result_Scalar
=> Complex
,
355 Left_Matrix
=> Complex_Matrix
,
356 Right_Matrix
=> Real_Matrix
,
357 Result_Matrix
=> Complex_Matrix
,
364 function "-" is new Vector_Elementwise_Operation
365 (X_Scalar
=> Complex
,
366 Result_Scalar
=> Complex
,
367 X_Vector
=> Complex_Vector
,
368 Result_Vector
=> Complex_Vector
,
371 function "-" is new Vector_Vector_Elementwise_Operation
372 (Left_Scalar
=> Complex
,
373 Right_Scalar
=> Complex
,
374 Result_Scalar
=> Complex
,
375 Left_Vector
=> Complex_Vector
,
376 Right_Vector
=> Complex_Vector
,
377 Result_Vector
=> Complex_Vector
,
380 function "-" is new Vector_Vector_Elementwise_Operation
381 (Left_Scalar
=> Real
'Base,
382 Right_Scalar
=> Complex
,
383 Result_Scalar
=> Complex
,
384 Left_Vector
=> Real_Vector
,
385 Right_Vector
=> Complex_Vector
,
386 Result_Vector
=> Complex_Vector
,
389 function "-" is new Vector_Vector_Elementwise_Operation
390 (Left_Scalar
=> Complex
,
391 Right_Scalar
=> Real
'Base,
392 Result_Scalar
=> Complex
,
393 Left_Vector
=> Complex_Vector
,
394 Right_Vector
=> Real_Vector
,
395 Result_Vector
=> Complex_Vector
,
398 function "-" is new Matrix_Elementwise_Operation
399 (X_Scalar
=> Complex
,
400 Result_Scalar
=> Complex
,
401 X_Matrix
=> Complex_Matrix
,
402 Result_Matrix
=> Complex_Matrix
,
405 function "-" is new Matrix_Matrix_Elementwise_Operation
406 (Left_Scalar
=> Complex
,
407 Right_Scalar
=> Complex
,
408 Result_Scalar
=> Complex
,
409 Left_Matrix
=> Complex_Matrix
,
410 Right_Matrix
=> Complex_Matrix
,
411 Result_Matrix
=> Complex_Matrix
,
414 function "-" is new Matrix_Matrix_Elementwise_Operation
415 (Left_Scalar
=> Real
'Base,
416 Right_Scalar
=> Complex
,
417 Result_Scalar
=> Complex
,
418 Left_Matrix
=> Real_Matrix
,
419 Right_Matrix
=> Complex_Matrix
,
420 Result_Matrix
=> Complex_Matrix
,
423 function "-" is new Matrix_Matrix_Elementwise_Operation
424 (Left_Scalar
=> Complex
,
425 Right_Scalar
=> Real
'Base,
426 Result_Scalar
=> Complex
,
427 Left_Matrix
=> Complex_Matrix
,
428 Right_Matrix
=> Real_Matrix
,
429 Result_Matrix
=> Complex_Matrix
,
436 function "/" is new Vector_Scalar_Elementwise_Operation
437 (Left_Scalar
=> Complex
,
438 Right_Scalar
=> Complex
,
439 Result_Scalar
=> Complex
,
440 Left_Vector
=> Complex_Vector
,
441 Result_Vector
=> Complex_Vector
,
444 function "/" is new Vector_Scalar_Elementwise_Operation
445 (Left_Scalar
=> Complex
,
446 Right_Scalar
=> Real
'Base,
447 Result_Scalar
=> Complex
,
448 Left_Vector
=> Complex_Vector
,
449 Result_Vector
=> Complex_Vector
,
452 function "/" is new Matrix_Scalar_Elementwise_Operation
453 (Left_Scalar
=> Complex
,
454 Right_Scalar
=> Complex
,
455 Result_Scalar
=> Complex
,
456 Left_Matrix
=> Complex_Matrix
,
457 Result_Matrix
=> Complex_Matrix
,
460 function "/" is new Matrix_Scalar_Elementwise_Operation
461 (Left_Scalar
=> Complex
,
462 Right_Scalar
=> Real
'Base,
463 Result_Scalar
=> Complex
,
464 Left_Matrix
=> Complex_Matrix
,
465 Result_Matrix
=> Complex_Matrix
,
472 function "abs" is new L2_Norm
473 (X_Scalar
=> Complex
,
474 Result_Real
=> Real
'Base,
475 X_Vector
=> Complex_Vector
);
481 function Argument
is new Vector_Elementwise_Operation
482 (X_Scalar
=> Complex
,
483 Result_Scalar
=> Real
'Base,
484 X_Vector
=> Complex_Vector
,
485 Result_Vector
=> Real_Vector
,
486 Operation
=> Argument
);
488 function Argument
is new Vector_Scalar_Elementwise_Operation
489 (Left_Scalar
=> Complex
,
490 Right_Scalar
=> Real
'Base,
491 Result_Scalar
=> Real
'Base,
492 Left_Vector
=> Complex_Vector
,
493 Result_Vector
=> Real_Vector
,
494 Operation
=> Argument
);
496 function Argument
is new Matrix_Elementwise_Operation
497 (X_Scalar
=> Complex
,
498 Result_Scalar
=> Real
'Base,
499 X_Matrix
=> Complex_Matrix
,
500 Result_Matrix
=> Real_Matrix
,
501 Operation
=> Argument
);
503 function Argument
is new Matrix_Scalar_Elementwise_Operation
504 (Left_Scalar
=> Complex
,
505 Right_Scalar
=> Real
'Base,
506 Result_Scalar
=> Real
'Base,
507 Left_Matrix
=> Complex_Matrix
,
508 Result_Matrix
=> Real_Matrix
,
509 Operation
=> Argument
);
511 ----------------------------
512 -- Compose_From_Cartesian --
513 ----------------------------
515 function Compose_From_Cartesian
is new Vector_Elementwise_Operation
516 (X_Scalar
=> Real
'Base,
517 Result_Scalar
=> Complex
,
518 X_Vector
=> Real_Vector
,
519 Result_Vector
=> Complex_Vector
,
520 Operation
=> Compose_From_Cartesian
);
522 function Compose_From_Cartesian
is
523 new Vector_Vector_Elementwise_Operation
524 (Left_Scalar
=> Real
'Base,
525 Right_Scalar
=> Real
'Base,
526 Result_Scalar
=> Complex
,
527 Left_Vector
=> Real_Vector
,
528 Right_Vector
=> Real_Vector
,
529 Result_Vector
=> Complex_Vector
,
530 Operation
=> Compose_From_Cartesian
);
532 function Compose_From_Cartesian
is new Matrix_Elementwise_Operation
533 (X_Scalar
=> Real
'Base,
534 Result_Scalar
=> Complex
,
535 X_Matrix
=> Real_Matrix
,
536 Result_Matrix
=> Complex_Matrix
,
537 Operation
=> Compose_From_Cartesian
);
539 function Compose_From_Cartesian
is
540 new Matrix_Matrix_Elementwise_Operation
541 (Left_Scalar
=> Real
'Base,
542 Right_Scalar
=> Real
'Base,
543 Result_Scalar
=> Complex
,
544 Left_Matrix
=> Real_Matrix
,
545 Right_Matrix
=> Real_Matrix
,
546 Result_Matrix
=> Complex_Matrix
,
547 Operation
=> Compose_From_Cartesian
);
549 ------------------------
550 -- Compose_From_Polar --
551 ------------------------
553 function Compose_From_Polar
is
554 new Vector_Vector_Elementwise_Operation
555 (Left_Scalar
=> Real
'Base,
556 Right_Scalar
=> Real
'Base,
557 Result_Scalar
=> Complex
,
558 Left_Vector
=> Real_Vector
,
559 Right_Vector
=> Real_Vector
,
560 Result_Vector
=> Complex_Vector
,
561 Operation
=> Compose_From_Polar
);
563 function Compose_From_Polar
is
564 new Vector_Vector_Scalar_Elementwise_Operation
565 (X_Scalar
=> Real
'Base,
566 Y_Scalar
=> Real
'Base,
567 Z_Scalar
=> Real
'Base,
568 Result_Scalar
=> Complex
,
569 X_Vector
=> Real_Vector
,
570 Y_Vector
=> Real_Vector
,
571 Result_Vector
=> Complex_Vector
,
572 Operation
=> Compose_From_Polar
);
574 function Compose_From_Polar
is
575 new Matrix_Matrix_Elementwise_Operation
576 (Left_Scalar
=> Real
'Base,
577 Right_Scalar
=> Real
'Base,
578 Result_Scalar
=> Complex
,
579 Left_Matrix
=> Real_Matrix
,
580 Right_Matrix
=> Real_Matrix
,
581 Result_Matrix
=> Complex_Matrix
,
582 Operation
=> Compose_From_Polar
);
584 function Compose_From_Polar
is
585 new Matrix_Matrix_Scalar_Elementwise_Operation
586 (X_Scalar
=> Real
'Base,
587 Y_Scalar
=> Real
'Base,
588 Z_Scalar
=> Real
'Base,
589 Result_Scalar
=> Complex
,
590 X_Matrix
=> Real_Matrix
,
591 Y_Matrix
=> Real_Matrix
,
592 Result_Matrix
=> Complex_Matrix
,
593 Operation
=> Compose_From_Polar
);
599 function Conjugate
is new Vector_Elementwise_Operation
600 (X_Scalar
=> Complex
,
601 Result_Scalar
=> Complex
,
602 X_Vector
=> Complex_Vector
,
603 Result_Vector
=> Complex_Vector
,
604 Operation
=> Conjugate
);
606 function Conjugate
is new Matrix_Elementwise_Operation
607 (X_Scalar
=> Complex
,
608 Result_Scalar
=> Complex
,
609 X_Matrix
=> Complex_Matrix
,
610 Result_Matrix
=> Complex_Matrix
,
611 Operation
=> Conjugate
);
617 function Im
is new Vector_Elementwise_Operation
618 (X_Scalar
=> Complex
,
619 Result_Scalar
=> Real
'Base,
620 X_Vector
=> Complex_Vector
,
621 Result_Vector
=> Real_Vector
,
624 function Im
is new Matrix_Elementwise_Operation
625 (X_Scalar
=> Complex
,
626 Result_Scalar
=> Real
'Base,
627 X_Matrix
=> Complex_Matrix
,
628 Result_Matrix
=> Real_Matrix
,
635 function Modulus
is new Vector_Elementwise_Operation
636 (X_Scalar
=> Complex
,
637 Result_Scalar
=> Real
'Base,
638 X_Vector
=> Complex_Vector
,
639 Result_Vector
=> Real_Vector
,
640 Operation
=> Modulus
);
642 function Modulus
is new Matrix_Elementwise_Operation
643 (X_Scalar
=> Complex
,
644 Result_Scalar
=> Real
'Base,
645 X_Matrix
=> Complex_Matrix
,
646 Result_Matrix
=> Real_Matrix
,
647 Operation
=> Modulus
);
653 function Re
is new Vector_Elementwise_Operation
654 (X_Scalar
=> Complex
,
655 Result_Scalar
=> Real
'Base,
656 X_Vector
=> Complex_Vector
,
657 Result_Vector
=> Real_Vector
,
660 function Re
is new Matrix_Elementwise_Operation
661 (X_Scalar
=> Complex
,
662 Result_Scalar
=> Real
'Base,
663 X_Matrix
=> Complex_Matrix
,
664 Result_Matrix
=> Real_Matrix
,
671 procedure Set_Im
is new Update_Vector_With_Vector
672 (X_Scalar
=> Complex
,
673 Y_Scalar
=> Real
'Base,
674 X_Vector
=> Complex_Vector
,
675 Y_Vector
=> Real_Vector
,
678 procedure Set_Im
is new Update_Matrix_With_Matrix
679 (X_Scalar
=> Complex
,
680 Y_Scalar
=> Real
'Base,
681 X_Matrix
=> Complex_Matrix
,
682 Y_Matrix
=> Real_Matrix
,
689 procedure Set_Re
is new Update_Vector_With_Vector
690 (X_Scalar
=> Complex
,
691 Y_Scalar
=> Real
'Base,
692 X_Vector
=> Complex_Vector
,
693 Y_Vector
=> Real_Vector
,
696 procedure Set_Re
is new Update_Matrix_With_Matrix
697 (X_Scalar
=> Complex
,
698 Y_Scalar
=> Real
'Base,
699 X_Matrix
=> Complex_Matrix
,
700 Y_Matrix
=> Real_Matrix
,
707 function Solve
is new Matrix_Vector_Solution
708 (Complex
, (0.0, 0.0), Complex_Vector
, Complex_Matrix
);
710 function Solve
is new Matrix_Matrix_Solution
711 (Complex
, (0.0, 0.0), Complex_Matrix
);
717 function Unit_Matrix
is new System
.Generic_Array_Operations
.Unit_Matrix
719 Matrix
=> Complex_Matrix
,
723 function Unit_Vector
is new System
.Generic_Array_Operations
.Unit_Vector
725 Vector
=> Complex_Vector
,
735 (Left
: Complex_Vector
;
736 Right
: Complex_Vector
) return Complex
737 renames Instantiations
."*";
741 Right
: Complex_Vector
) return Complex
742 renames Instantiations
."*";
745 (Left
: Complex_Vector
;
746 Right
: Real_Vector
) return Complex
747 renames Instantiations
."*";
751 Right
: Complex_Vector
) return Complex_Vector
752 renames Instantiations
."*";
755 (Left
: Complex_Vector
;
756 Right
: Complex
) return Complex_Vector
757 renames Instantiations
."*";
761 Right
: Complex_Vector
) return Complex_Vector
762 renames Instantiations
."*";
765 (Left
: Complex_Vector
;
766 Right
: Real
'Base) return Complex_Vector
767 renames Instantiations
."*";
770 (Left
: Complex_Matrix
;
771 Right
: Complex_Matrix
) return Complex_Matrix
772 renames Instantiations
."*";
775 (Left
: Complex_Vector
;
776 Right
: Complex_Vector
) return Complex_Matrix
777 renames Instantiations
."*";
780 (Left
: Complex_Vector
;
781 Right
: Complex_Matrix
) return Complex_Vector
782 renames Instantiations
."*";
785 (Left
: Complex_Matrix
;
786 Right
: Complex_Vector
) return Complex_Vector
787 renames Instantiations
."*";
791 Right
: Complex_Matrix
) return Complex_Matrix
792 renames Instantiations
."*";
795 (Left
: Complex_Matrix
;
796 Right
: Real_Matrix
) return Complex_Matrix
797 renames Instantiations
."*";
801 Right
: Complex_Vector
) return Complex_Matrix
802 renames Instantiations
."*";
805 (Left
: Complex_Vector
;
806 Right
: Real_Vector
) return Complex_Matrix
807 renames Instantiations
."*";
811 Right
: Complex_Matrix
) return Complex_Vector
812 renames Instantiations
."*";
815 (Left
: Complex_Vector
;
816 Right
: Real_Matrix
) return Complex_Vector
817 renames Instantiations
."*";
821 Right
: Complex_Vector
) return Complex_Vector
822 renames Instantiations
."*";
825 (Left
: Complex_Matrix
;
826 Right
: Real_Vector
) return Complex_Vector
827 renames Instantiations
."*";
831 Right
: Complex_Matrix
) return Complex_Matrix
832 renames Instantiations
."*";
835 (Left
: Complex_Matrix
;
836 Right
: Complex
) return Complex_Matrix
837 renames Instantiations
."*";
841 Right
: Complex_Matrix
) return Complex_Matrix
842 renames Instantiations
."*";
845 (Left
: Complex_Matrix
;
846 Right
: Real
'Base) return Complex_Matrix
847 renames Instantiations
."*";
853 function "+" (Right
: Complex_Vector
) return Complex_Vector
854 renames Instantiations
."+";
857 (Left
: Complex_Vector
;
858 Right
: Complex_Vector
) return Complex_Vector
859 renames Instantiations
."+";
863 Right
: Complex_Vector
) return Complex_Vector
864 renames Instantiations
."+";
867 (Left
: Complex_Vector
;
868 Right
: Real_Vector
) return Complex_Vector
869 renames Instantiations
."+";
871 function "+" (Right
: Complex_Matrix
) return Complex_Matrix
872 renames Instantiations
."+";
875 (Left
: Complex_Matrix
;
876 Right
: Complex_Matrix
) return Complex_Matrix
877 renames Instantiations
."+";
881 Right
: Complex_Matrix
) return Complex_Matrix
882 renames Instantiations
."+";
885 (Left
: Complex_Matrix
;
886 Right
: Real_Matrix
) return Complex_Matrix
887 renames Instantiations
."+";
894 (Right
: Complex_Vector
) return Complex_Vector
895 renames Instantiations
."-";
898 (Left
: Complex_Vector
;
899 Right
: Complex_Vector
) return Complex_Vector
900 renames Instantiations
."-";
904 Right
: Complex_Vector
) return Complex_Vector
905 renames Instantiations
."-";
908 (Left
: Complex_Vector
;
909 Right
: Real_Vector
) return Complex_Vector
910 renames Instantiations
."-";
912 function "-" (Right
: Complex_Matrix
) return Complex_Matrix
913 renames Instantiations
."-";
916 (Left
: Complex_Matrix
;
917 Right
: Complex_Matrix
) return Complex_Matrix
918 renames Instantiations
."-";
922 Right
: Complex_Matrix
) return Complex_Matrix
923 renames Instantiations
."-";
926 (Left
: Complex_Matrix
;
927 Right
: Real_Matrix
) return Complex_Matrix
928 renames Instantiations
."-";
935 (Left
: Complex_Vector
;
936 Right
: Complex
) return Complex_Vector
937 renames Instantiations
."/";
940 (Left
: Complex_Vector
;
941 Right
: Real
'Base) return Complex_Vector
942 renames Instantiations
."/";
945 (Left
: Complex_Matrix
;
946 Right
: Complex
) return Complex_Matrix
947 renames Instantiations
."/";
950 (Left
: Complex_Matrix
;
951 Right
: Real
'Base) return Complex_Matrix
952 renames Instantiations
."/";
958 function "abs" (Right
: Complex_Vector
) return Real
'Base
959 renames Instantiations
."abs";
965 function Argument
(X
: Complex_Vector
) return Real_Vector
966 renames Instantiations
.Argument
;
970 Cycle
: Real
'Base) return Real_Vector
971 renames Instantiations
.Argument
;
973 function Argument
(X
: Complex_Matrix
) return Real_Matrix
974 renames Instantiations
.Argument
;
978 Cycle
: Real
'Base) return Real_Matrix
979 renames Instantiations
.Argument
;
981 ----------------------------
982 -- Compose_From_Cartesian --
983 ----------------------------
985 function Compose_From_Cartesian
(Re
: Real_Vector
) return Complex_Vector
986 renames Instantiations
.Compose_From_Cartesian
;
988 function Compose_From_Cartesian
990 Im
: Real_Vector
) return Complex_Vector
991 renames Instantiations
.Compose_From_Cartesian
;
993 function Compose_From_Cartesian
(Re
: Real_Matrix
) return Complex_Matrix
994 renames Instantiations
.Compose_From_Cartesian
;
996 function Compose_From_Cartesian
998 Im
: Real_Matrix
) return Complex_Matrix
999 renames Instantiations
.Compose_From_Cartesian
;
1001 ------------------------
1002 -- Compose_From_Polar --
1003 ------------------------
1005 function Compose_From_Polar
1006 (Modulus
: Real_Vector
;
1007 Argument
: Real_Vector
) return Complex_Vector
1008 renames Instantiations
.Compose_From_Polar
;
1010 function Compose_From_Polar
1011 (Modulus
: Real_Vector
;
1012 Argument
: Real_Vector
;
1013 Cycle
: Real
'Base) return Complex_Vector
1014 renames Instantiations
.Compose_From_Polar
;
1016 function Compose_From_Polar
1017 (Modulus
: Real_Matrix
;
1018 Argument
: Real_Matrix
) return Complex_Matrix
1019 renames Instantiations
.Compose_From_Polar
;
1021 function Compose_From_Polar
1022 (Modulus
: Real_Matrix
;
1023 Argument
: Real_Matrix
;
1024 Cycle
: Real
'Base) return Complex_Matrix
1025 renames Instantiations
.Compose_From_Polar
;
1031 function Conjugate
(X
: Complex_Vector
) return Complex_Vector
1032 renames Instantiations
.Conjugate
;
1034 function Conjugate
(X
: Complex_Matrix
) return Complex_Matrix
1035 renames Instantiations
.Conjugate
;
1041 function Determinant
(A
: Complex_Matrix
) return Complex
is
1042 M
: Complex_Matrix
:= A
;
1043 B
: Complex_Matrix
(A
'Range (1), 1 .. 0);
1046 Forward_Eliminate
(M
, B
, R
);
1054 procedure Eigensystem
1055 (A
: Complex_Matrix
;
1056 Values
: out Real_Vector
;
1057 Vectors
: out Complex_Matrix
)
1059 N
: constant Natural := Length
(A
);
1061 -- For a Hermitian matrix C, we convert the eigenvalue problem to a
1062 -- real symmetric one: if C = A + i * B, then the (N, N) complex
1063 -- eigenvalue problem:
1064 -- (A + i * B) * (u + i * v) = Lambda * (u + i * v)
1066 -- is equivalent to the (2 * N, 2 * N) real eigenvalue problem:
1067 -- [ A, B ] [ u ] = Lambda * [ u ]
1068 -- [ -B, A ] [ v ] [ v ]
1070 -- Note that the (2 * N, 2 * N) matrix above is symmetric, as
1071 -- Transpose (A) = A and Transpose (B) = -B if C is Hermitian.
1073 -- We solve this eigensystem using the real-valued algorithms. The final
1074 -- result will have every eigenvalue twice, so in the sorted output we
1075 -- just pick every second value, with associated eigenvector u + i * v.
1077 M
: Real_Matrix
(1 .. 2 * N
, 1 .. 2 * N
);
1078 Vals
: Real_Vector
(1 .. 2 * N
);
1079 Vecs
: Real_Matrix
(1 .. 2 * N
, 1 .. 2 * N
);
1082 for J
in 1 .. N
loop
1083 for K
in 1 .. N
loop
1085 C
: constant Complex
:=
1086 (A
(A
'First (1) + (J
- 1), A
'First (2) + (K
- 1)));
1089 M
(J
+ N
, K
+ N
) := Re
(C
);
1090 M
(J
+ N
, K
) := Im
(C
);
1091 M
(J
, K
+ N
) := -Im
(C
);
1096 Eigensystem
(M
, Vals
, Vecs
);
1098 for J
in 1 .. N
loop
1100 Col
: constant Integer := Values
'First + (J
- 1);
1102 Values
(Col
) := Vals
(2 * J
);
1104 for K
in 1 .. N
loop
1106 Row
: constant Integer := Vectors
'First (2) + (K
- 1);
1109 := (Vecs
(J
* 2, Col
), Vecs
(J
* 2, Col
+ N
));
1120 function Eigenvalues
(A
: Complex_Matrix
) return Real_Vector
is
1121 -- See Eigensystem for a description of the algorithm
1123 N
: constant Natural := Length
(A
);
1124 R
: Real_Vector
(A
'Range (1));
1126 M
: Real_Matrix
(1 .. 2 * N
, 1 .. 2 * N
);
1127 Vals
: Real_Vector
(1 .. 2 * N
);
1129 for J
in 1 .. N
loop
1130 for K
in 1 .. N
loop
1132 C
: constant Complex
:=
1133 (A
(A
'First (1) + (J
- 1), A
'First (2) + (K
- 1)));
1136 M
(J
+ N
, K
+ N
) := Re
(C
);
1137 M
(J
+ N
, K
) := Im
(C
);
1138 M
(J
, K
+ N
) := -Im
(C
);
1143 Vals
:= Eigenvalues
(M
);
1145 for J
in 1 .. N
loop
1146 R
(A
'First (1) + (J
- 1)) := Vals
(2 * J
);
1156 function Im
(X
: Complex_Vector
) return Real_Vector
1157 renames Instantiations
.Im
;
1159 function Im
(X
: Complex_Matrix
) return Real_Matrix
1160 renames Instantiations
.Im
;
1166 function Inverse
(A
: Complex_Matrix
) return Complex_Matrix
is
1167 (Solve
(A
, Unit_Matrix
(Length
(A
),
1168 First_1
=> A
'First (2),
1169 First_2
=> A
'First (1))));
1175 function Modulus
(X
: Complex_Vector
) return Real_Vector
1176 renames Instantiations
.Modulus
;
1178 function Modulus
(X
: Complex_Matrix
) return Real_Matrix
1179 renames Instantiations
.Modulus
;
1185 function Re
(X
: Complex_Vector
) return Real_Vector
1186 renames Instantiations
.Re
;
1188 function Re
(X
: Complex_Matrix
) return Real_Matrix
1189 renames Instantiations
.Re
;
1196 (X
: in out Complex_Matrix
;
1198 renames Instantiations
.Set_Im
;
1201 (X
: in out Complex_Vector
;
1203 renames Instantiations
.Set_Im
;
1210 (X
: in out Complex_Matrix
;
1212 renames Instantiations
.Set_Re
;
1215 (X
: in out Complex_Vector
;
1217 renames Instantiations
.Set_Re
;
1224 (A
: Complex_Matrix
;
1225 X
: Complex_Vector
) return Complex_Vector
1226 renames Instantiations
.Solve
;
1229 (A
: Complex_Matrix
;
1230 X
: Complex_Matrix
) return Complex_Matrix
1231 renames Instantiations
.Solve
;
1238 (X
: Complex_Matrix
) return Complex_Matrix
1240 R
: Complex_Matrix
(X
'Range (2), X
'Range (1));
1250 function Unit_Matrix
1252 First_1
: Integer := 1;
1253 First_2
: Integer := 1) return Complex_Matrix
1254 renames Instantiations
.Unit_Matrix
;
1260 function Unit_Vector
1263 First
: Integer := 1) return Complex_Vector
1264 renames Instantiations
.Unit_Vector
;
1266 end Ada
.Numerics
.Generic_Complex_Arrays
;