1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
|
------------------------------------------------------------------------------
-- --
-- GNAT RUNTIME COMPONENTS --
-- --
-- A D A . N U M E R I C S . G E N E R I C _ C O M P L E X _ T Y P E S --
-- --
-- B o d y --
-- --
-- $Revision: 1.16 $
-- --
-- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
-- --
-- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 2, or (at your option) any later ver- --
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
-- for more details. You should have received a copy of the GNU General --
-- Public License distributed with GNAT; see file COPYING. If not, write --
-- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
-- MA 02111-1307, USA. --
-- --
-- As a special exception, if other files instantiate generics from this --
-- unit, or you link this unit with other files to produce an executable, --
-- this unit does not by itself cause the resulting executable to be --
-- covered by the GNU General Public License. This exception does not --
-- however invalidate any other reasons why the executable file might be --
-- covered by the GNU Public License. --
-- --
-- GNAT was originally developed by the GNAT team at New York University. --
-- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
-- --
------------------------------------------------------------------------------
with Ada.Numerics.Aux; use Ada.Numerics.Aux;
package body Ada.Numerics.Generic_Complex_Types is
subtype R is Real'Base;
Two_Pi : constant R := R (2.0) * Pi;
Half_Pi : constant R := Pi / R (2.0);
---------
-- "*" --
---------
function "*" (Left, Right : Complex) return Complex is
X : R;
Y : R;
begin
X := Left.Re * Right.Re - Left.Im * Right.Im;
Y := Left.Re * Right.Im + Left.Im * Right.Re;
-- If either component overflows, try to scale.
if abs (X) > R'Last then
X := R' (4.0) * (R'(Left.Re / 2.0) * R'(Right.Re / 2.0)
- R'(Left.Im / 2.0) * R'(Right.Im / 2.0));
end if;
if abs (Y) > R'Last then
Y := R' (4.0) * (R'(Left.Re / 2.0) * R'(Right.Im / 2.0)
- R'(Left.Im / 2.0) * R'(Right.Re / 2.0));
end if;
return (X, Y);
end "*";
function "*" (Left, Right : Imaginary) return Real'Base is
begin
return -R (Left) * R (Right);
end "*";
function "*" (Left : Complex; Right : Real'Base) return Complex is
begin
return Complex'(Left.Re * Right, Left.Im * Right);
end "*";
function "*" (Left : Real'Base; Right : Complex) return Complex is
begin
return (Left * Right.Re, Left * Right.Im);
end "*";
function "*" (Left : Complex; Right : Imaginary) return Complex is
begin
return Complex'(-(Left.Im * R (Right)), Left.Re * R (Right));
end "*";
function "*" (Left : Imaginary; Right : Complex) return Complex is
begin
return Complex'(-(R (Left) * Right.Im), R (Left) * Right.Re);
end "*";
function "*" (Left : Imaginary; Right : Real'Base) return Imaginary is
begin
return Left * Imaginary (Right);
end "*";
function "*" (Left : Real'Base; Right : Imaginary) return Imaginary is
begin
return Imaginary (Left * R (Right));
end "*";
----------
-- "**" --
----------
function "**" (Left : Complex; Right : Integer) return Complex is
Result : Complex := (1.0, 0.0);
Factor : Complex := Left;
Exp : Integer := Right;
begin
-- We use the standard logarithmic approach, Exp gets shifted right
-- testing successive low order bits and Factor is the value of the
-- base raised to the next power of 2. For positive exponents we
-- multiply the result by this factor, for negative exponents, we
-- divide by this factor.
if Exp >= 0 then
-- For a positive exponent, if we get a constraint error during
-- this loop, it is an overflow, and the constraint error will
-- simply be passed on to the caller.
while Exp /= 0 loop
if Exp rem 2 /= 0 then
Result := Result * Factor;
end if;
Factor := Factor * Factor;
Exp := Exp / 2;
end loop;
return Result;
else -- Exp < 0 then
-- For the negative exponent case, a constraint error during this
-- calculation happens if Factor gets too large, and the proper
-- response is to return 0.0, since what we essentially have is
-- 1.0 / infinity, and the closest model number will be zero.
begin
while Exp /= 0 loop
if Exp rem 2 /= 0 then
Result := Result * Factor;
end if;
Factor := Factor * Factor;
Exp := Exp / 2;
end loop;
return R ' (1.0) / Result;
exception
when Constraint_Error =>
return (0.0, 0.0);
end;
end if;
end "**";
function "**" (Left : Imaginary; Right : Integer) return Complex is
M : R := R (Left) ** Right;
begin
case Right mod 4 is
when 0 => return (M, 0.0);
when 1 => return (0.0, M);
when 2 => return (-M, 0.0);
when 3 => return (0.0, -M);
when others => raise Program_Error;
end case;
end "**";
---------
-- "+" --
---------
function "+" (Right : Complex) return Complex is
begin
return Right;
end "+";
function "+" (Left, Right : Complex) return Complex is
begin
return Complex'(Left.Re + Right.Re, Left.Im + Right.Im);
end "+";
function "+" (Right : Imaginary) return Imaginary is
begin
return Right;
end "+";
function "+" (Left, Right : Imaginary) return Imaginary is
begin
return Imaginary (R (Left) + R (Right));
end "+";
function "+" (Left : Complex; Right : Real'Base) return Complex is
begin
return Complex'(Left.Re + Right, Left.Im);
end "+";
function "+" (Left : Real'Base; Right : Complex) return Complex is
begin
return Complex'(Left + Right.Re, Right.Im);
end "+";
function "+" (Left : Complex; Right : Imaginary) return Complex is
begin
return Complex'(Left.Re, Left.Im + R (Right));
end "+";
function "+" (Left : Imaginary; Right : Complex) return Complex is
begin
return Complex'(Right.Re, R (Left) + Right.Im);
end "+";
function "+" (Left : Imaginary; Right : Real'Base) return Complex is
begin
return Complex'(Right, R (Left));
end "+";
function "+" (Left : Real'Base; Right : Imaginary) return Complex is
begin
return Complex'(Left, R (Right));
end "+";
---------
-- "-" --
---------
function "-" (Right : Complex) return Complex is
begin
return (-Right.Re, -Right.Im);
end "-";
function "-" (Left, Right : Complex) return Complex is
begin
return (Left.Re - Right.Re, Left.Im - Right.Im);
end "-";
function "-" (Right : Imaginary) return Imaginary is
begin
return Imaginary (-R (Right));
end "-";
function "-" (Left, Right : Imaginary) return Imaginary is
begin
return Imaginary (R (Left) - R (Right));
end "-";
function "-" (Left : Complex; Right : Real'Base) return Complex is
begin
return Complex'(Left.Re - Right, Left.Im);
end "-";
function "-" (Left : Real'Base; Right : Complex) return Complex is
begin
return Complex'(Left - Right.Re, -Right.Im);
end "-";
function "-" (Left : Complex; Right : Imaginary) return Complex is
begin
return Complex'(Left.Re, Left.Im - R (Right));
end "-";
function "-" (Left : Imaginary; Right : Complex) return Complex is
begin
return Complex'(-Right.Re, R (Left) - Right.Im);
end "-";
function "-" (Left : Imaginary; Right : Real'Base) return Complex is
begin
return Complex'(-Right, R (Left));
end "-";
function "-" (Left : Real'Base; Right : Imaginary) return Complex is
begin
return Complex'(Left, -R (Right));
end "-";
---------
-- "/" --
---------
function "/" (Left, Right : Complex) return Complex is
a : constant R := Left.Re;
b : constant R := Left.Im;
c : constant R := Right.Re;
d : constant R := Right.Im;
begin
if c = 0.0 and then d = 0.0 then
raise Constraint_Error;
else
return Complex'(Re => ((a * c) + (b * d)) / (c ** 2 + d ** 2),
Im => ((b * c) - (a * d)) / (c ** 2 + d ** 2));
end if;
end "/";
function "/" (Left, Right : Imaginary) return Real'Base is
begin
return R (Left) / R (Right);
end "/";
function "/" (Left : Complex; Right : Real'Base) return Complex is
begin
return Complex'(Left.Re / Right, Left.Im / Right);
end "/";
function "/" (Left : Real'Base; Right : Complex) return Complex is
a : constant R := Left;
c : constant R := Right.Re;
d : constant R := Right.Im;
begin
return Complex'(Re => (a * c) / (c ** 2 + d ** 2),
Im => -(a * d) / (c ** 2 + d ** 2));
end "/";
function "/" (Left : Complex; Right : Imaginary) return Complex is
a : constant R := Left.Re;
b : constant R := Left.Im;
d : constant R := R (Right);
begin
return (b / d, -a / d);
end "/";
function "/" (Left : Imaginary; Right : Complex) return Complex is
b : constant R := R (Left);
c : constant R := Right.Re;
d : constant R := Right.Im;
begin
return (Re => b * d / (c ** 2 + d ** 2),
Im => b * c / (c ** 2 + d ** 2));
end "/";
function "/" (Left : Imaginary; Right : Real'Base) return Imaginary is
begin
return Imaginary (R (Left) / Right);
end "/";
function "/" (Left : Real'Base; Right : Imaginary) return Imaginary is
begin
return Imaginary (-Left / R (Right));
end "/";
---------
-- "<" --
---------
function "<" (Left, Right : Imaginary) return Boolean is
begin
return R (Left) < R (Right);
end "<";
----------
-- "<=" --
----------
function "<=" (Left, Right : Imaginary) return Boolean is
begin
return R (Left) <= R (Right);
end "<=";
---------
-- ">" --
---------
function ">" (Left, Right : Imaginary) return Boolean is
begin
return R (Left) > R (Right);
end ">";
----------
-- ">=" --
----------
function ">=" (Left, Right : Imaginary) return Boolean is
begin
return R (Left) >= R (Right);
end ">=";
-----------
-- "abs" --
-----------
function "abs" (Right : Imaginary) return Real'Base is
begin
return abs R (Right);
end "abs";
--------------
-- Argument --
--------------
function Argument (X : Complex) return Real'Base is
a : constant R := X.Re;
b : constant R := X.Im;
arg : R;
begin
if b = 0.0 then
if a >= 0.0 then
return 0.0;
else
return R'Copy_Sign (Pi, b);
end if;
elsif a = 0.0 then
if b >= 0.0 then
return Half_Pi;
else
return -Half_Pi;
end if;
else
arg := R (Atan (Double (abs (b / a))));
if a > 0.0 then
if b > 0.0 then
return arg;
else -- b < 0.0
return -arg;
end if;
else -- a < 0.0
if b >= 0.0 then
return Pi - arg;
else -- b < 0.0
return -(Pi - arg);
end if;
end if;
end if;
exception
when Constraint_Error =>
if b > 0.0 then
return Half_Pi;
else
return -Half_Pi;
end if;
end Argument;
function Argument (X : Complex; Cycle : Real'Base) return Real'Base is
begin
if Cycle > 0.0 then
return Argument (X) * Cycle / Two_Pi;
else
raise Argument_Error;
end if;
end Argument;
----------------------------
-- Compose_From_Cartesian --
----------------------------
function Compose_From_Cartesian (Re, Im : Real'Base) return Complex is
begin
return (Re, Im);
end Compose_From_Cartesian;
function Compose_From_Cartesian (Re : Real'Base) return Complex is
begin
return (Re, 0.0);
end Compose_From_Cartesian;
function Compose_From_Cartesian (Im : Imaginary) return Complex is
begin
return (0.0, R (Im));
end Compose_From_Cartesian;
------------------------
-- Compose_From_Polar --
------------------------
function Compose_From_Polar (
Modulus, Argument : Real'Base)
return Complex
is
begin
if Modulus = 0.0 then
return (0.0, 0.0);
else
return (Modulus * R (Cos (Double (Argument))),
Modulus * R (Sin (Double (Argument))));
end if;
end Compose_From_Polar;
function Compose_From_Polar (
Modulus, Argument, Cycle : Real'Base)
return Complex
is
Arg : Real'Base;
begin
if Modulus = 0.0 then
return (0.0, 0.0);
elsif Cycle > 0.0 then
if Argument = 0.0 then
return (Modulus, 0.0);
elsif Argument = Cycle / 4.0 then
return (0.0, Modulus);
elsif Argument = Cycle / 2.0 then
return (-Modulus, 0.0);
elsif Argument = 3.0 * Cycle / R (4.0) then
return (0.0, -Modulus);
else
Arg := Two_Pi * Argument / Cycle;
return (Modulus * R (Cos (Double (Arg))),
Modulus * R (Sin (Double (Arg))));
end if;
else
raise Argument_Error;
end if;
end Compose_From_Polar;
---------------
-- Conjugate --
---------------
function Conjugate (X : Complex) return Complex is
begin
return Complex'(X.Re, -X.Im);
end Conjugate;
--------
-- Im --
--------
function Im (X : Complex) return Real'Base is
begin
return X.Im;
end Im;
function Im (X : Imaginary) return Real'Base is
begin
return R (X);
end Im;
-------------
-- Modulus --
-------------
function Modulus (X : Complex) return Real'Base is
Re2, Im2 : R;
begin
begin
Re2 := X.Re ** 2;
-- To compute (a**2 + b**2) ** (0.5) when a**2 may be out of bounds,
-- compute a * (1 + (b/a) **2) ** (0.5). On a machine where the
-- squaring does not raise constraint_error but generates infinity,
-- we can use an explicit comparison to determine whether to use
-- the scaling expression.
if Re2 > R'Last then
raise Constraint_Error;
end if;
exception
when Constraint_Error =>
return abs (X.Re)
* R (Sqrt (Double (R (1.0) + (X.Im / X.Re) ** 2)));
end;
begin
Im2 := X.Im ** 2;
if Im2 > R'Last then
raise Constraint_Error;
end if;
exception
when Constraint_Error =>
return abs (X.Im)
* R (Sqrt (Double (R (1.0) + (X.Re / X.Im) ** 2)));
end;
-- Now deal with cases of underflow. If only one of the squares
-- underflows, return the modulus of the other component. If both
-- squares underflow, use scaling as above.
if Re2 = 0.0 then
if X.Re = 0.0 then
return abs (X.Im);
elsif Im2 = 0.0 then
if X.Im = 0.0 then
return abs (X.Re);
else
if abs (X.Re) > abs (X.Im) then
return
abs (X.Re)
* R (Sqrt (Double (R (1.0) + (X.Im / X.Re) ** 2)));
else
return
abs (X.Im)
* R (Sqrt (Double (R (1.0) + (X.Re / X.Im) ** 2)));
end if;
end if;
else
return abs (X.Im);
end if;
elsif Im2 = 0.0 then
return abs (X.Re);
-- in all other cases, the naive computation will do.
else
return R (Sqrt (Double (Re2 + Im2)));
end if;
end Modulus;
--------
-- Re --
--------
function Re (X : Complex) return Real'Base is
begin
return X.Re;
end Re;
------------
-- Set_Im --
------------
procedure Set_Im (X : in out Complex; Im : in Real'Base) is
begin
X.Im := Im;
end Set_Im;
procedure Set_Im (X : out Imaginary; Im : in Real'Base) is
begin
X := Imaginary (Im);
end Set_Im;
------------
-- Set_Re --
------------
procedure Set_Re (X : in out Complex; Re : in Real'Base) is
begin
X.Re := Re;
end Set_Re;
end Ada.Numerics.Generic_Complex_Types;
|