1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- ADA.NUMERICS.GENERIC_COMPLEX_ELEMENTARY_FUNCTIONS --
9 -- Copyright (C) 1992-2016, 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 with Ada
.Numerics
.Generic_Elementary_Functions
;
34 package body Ada
.Numerics
.Generic_Complex_Elementary_Functions
is
36 package Elementary_Functions
is new
37 Ada
.Numerics
.Generic_Elementary_Functions
(Real
'Base);
38 use Elementary_Functions
;
40 PI
: constant := 3.14159_26535_89793_23846_26433_83279_50288_41971
;
41 PI_2
: constant := PI
/ 2.0;
42 Sqrt_Two
: constant := 1.41421_35623_73095_04880_16887_24209_69807_85696
;
43 Log_Two
: constant := 0.69314_71805_59945_30941_72321_21458_17656_80755
;
45 subtype T
is Real
'Base;
47 Epsilon
: constant T
:= 2.0 ** (1 - T
'Model_Mantissa);
48 Square_Root_Epsilon
: constant T
:= Sqrt_Two
** (1 - T
'Model_Mantissa);
49 Inv_Square_Root_Epsilon
: constant T
:= Sqrt_Two
** (T
'Model_Mantissa - 1);
50 Root_Root_Epsilon
: constant T
:= Sqrt_Two
**
51 ((1 - T
'Model_Mantissa) / 2);
52 Log_Inverse_Epsilon_2
: constant T
:= T
(T
'Model_Mantissa - 1) / 2.0;
54 Complex_Zero
: constant Complex
:= (0.0, 0.0);
55 Complex_One
: constant Complex
:= (1.0, 0.0);
56 Complex_I
: constant Complex
:= (0.0, 1.0);
57 Half_Pi
: constant Complex
:= (PI_2
, 0.0);
63 function "**" (Left
: Complex
; Right
: Complex
) return Complex
is
66 and then Im
(Right
) = 0.0
67 and then Re
(Left
) = 0.0
68 and then Im
(Left
) = 0.0
73 and then Im
(Left
) = 0.0
74 and then Re
(Right
) < 0.0
76 raise Constraint_Error
;
78 elsif Re
(Left
) = 0.0 and then Im
(Left
) = 0.0 then
81 elsif Right
= (0.0, 0.0) then
84 elsif Re
(Right
) = 0.0 and then Im
(Right
) = 0.0 then
87 elsif Re
(Right
) = 1.0 and then Im
(Right
) = 0.0 then
91 return Exp
(Right
* Log
(Left
));
95 function "**" (Left
: Real
'Base; Right
: Complex
) return Complex
is
97 if Re
(Right
) = 0.0 and then Im
(Right
) = 0.0 and then Left
= 0.0 then
100 elsif Left
= 0.0 and then Re
(Right
) < 0.0 then
101 raise Constraint_Error
;
103 elsif Left
= 0.0 then
104 return Compose_From_Cartesian
(Left
, 0.0);
106 elsif Re
(Right
) = 0.0 and then Im
(Right
) = 0.0 then
109 elsif Re
(Right
) = 1.0 and then Im
(Right
) = 0.0 then
110 return Compose_From_Cartesian
(Left
, 0.0);
113 return Exp
(Log
(Left
) * Right
);
117 function "**" (Left
: Complex
; Right
: Real
'Base) return Complex
is
120 and then Re
(Left
) = 0.0
121 and then Im
(Left
) = 0.0
123 raise Argument_Error
;
125 elsif Re
(Left
) = 0.0
126 and then Im
(Left
) = 0.0
129 raise Constraint_Error
;
131 elsif Re
(Left
) = 0.0 and then Im
(Left
) = 0.0 then
134 elsif Right
= 0.0 then
137 elsif Right
= 1.0 then
141 return Exp
(Right
* Log
(Left
));
149 function Arccos
(X
: Complex
) return Complex
is
153 if X
= Complex_One
then
156 elsif abs Re
(X
) < Square_Root_Epsilon
and then
157 abs Im
(X
) < Square_Root_Epsilon
161 elsif abs Re
(X
) > Inv_Square_Root_Epsilon
or else
162 abs Im
(X
) > Inv_Square_Root_Epsilon
164 return -2.0 * Complex_I
* Log
(Sqrt
((1.0 + X
) / 2.0) +
165 Complex_I
* Sqrt
((1.0 - X
) / 2.0));
168 Result
:= -Complex_I
* Log
(X
+ Complex_I
* Sqrt
(1.0 - X
* X
));
171 and then abs Re
(X
) <= 1.00
173 Set_Im
(Result
, Im
(X
));
183 function Arccosh
(X
: Complex
) return Complex
is
187 if X
= Complex_One
then
190 elsif abs Re
(X
) < Square_Root_Epsilon
and then
191 abs Im
(X
) < Square_Root_Epsilon
193 Result
:= Compose_From_Cartesian
(-Im
(X
), -PI_2
+ Re
(X
));
195 elsif abs Re
(X
) > Inv_Square_Root_Epsilon
or else
196 abs Im
(X
) > Inv_Square_Root_Epsilon
198 Result
:= Log_Two
+ Log
(X
);
201 Result
:= 2.0 * Log
(Sqrt
((1.0 + X
) / 2.0) +
202 Sqrt
((X
- 1.0) / 2.0));
205 if Re
(Result
) <= 0.0 then
216 function Arccot
(X
: Complex
) return Complex
is
220 if abs Re
(X
) < Square_Root_Epsilon
and then
221 abs Im
(X
) < Square_Root_Epsilon
225 elsif abs Re
(X
) > 1.0 / Epsilon
or else
226 abs Im
(X
) > 1.0 / Epsilon
228 Xt
:= Complex_One
/ X
;
231 Set_Re
(Xt
, PI
- Re
(Xt
));
238 Xt
:= Complex_I
* Log
((X
- Complex_I
) / (X
+ Complex_I
)) / 2.0;
240 if Re
(Xt
) < 0.0 then
251 function Arccoth
(X
: Complex
) return Complex
is
255 if X
= (0.0, 0.0) then
256 return Compose_From_Cartesian
(0.0, PI_2
);
258 elsif abs Re
(X
) < Square_Root_Epsilon
259 and then abs Im
(X
) < Square_Root_Epsilon
261 return PI_2
* Complex_I
+ X
;
263 elsif abs Re
(X
) > 1.0 / Epsilon
or else
264 abs Im
(X
) > 1.0 / Epsilon
269 return PI
* Complex_I
;
272 elsif Im
(X
) = 0.0 and then Re
(X
) = 1.0 then
273 raise Constraint_Error
;
275 elsif Im
(X
) = 0.0 and then Re
(X
) = -1.0 then
276 raise Constraint_Error
;
280 R
:= Log
((1.0 + X
) / (X
- 1.0)) / 2.0;
283 when Constraint_Error
=>
284 R
:= (Log
(1.0 + X
) - Log
(X
- 1.0)) / 2.0;
288 Set_Im
(R
, PI
+ Im
(R
));
302 function Arcsin
(X
: Complex
) return Complex
is
306 -- For very small argument, sin (x) = x
308 if abs Re
(X
) < Square_Root_Epsilon
and then
309 abs Im
(X
) < Square_Root_Epsilon
313 elsif abs Re
(X
) > Inv_Square_Root_Epsilon
or else
314 abs Im
(X
) > Inv_Square_Root_Epsilon
316 Result
:= -Complex_I
* (Log
(Complex_I
* X
) + Log
(2.0 * Complex_I
));
318 if Im
(Result
) > PI_2
then
319 Set_Im
(Result
, PI
- Im
(X
));
321 elsif Im
(Result
) < -PI_2
then
322 Set_Im
(Result
, -(PI
+ Im
(X
)));
328 Result
:= -Complex_I
* Log
(Complex_I
* X
+ Sqrt
(1.0 - X
* X
));
331 Set_Re
(Result
, Re
(X
));
334 and then abs Re
(X
) <= 1.00
336 Set_Im
(Result
, Im
(X
));
346 function Arcsinh
(X
: Complex
) return Complex
is
350 if abs Re
(X
) < Square_Root_Epsilon
and then
351 abs Im
(X
) < Square_Root_Epsilon
355 elsif abs Re
(X
) > Inv_Square_Root_Epsilon
or else
356 abs Im
(X
) > Inv_Square_Root_Epsilon
358 Result
:= Log_Two
+ Log
(X
); -- may have wrong sign
360 if (Re
(X
) < 0.0 and then Re
(Result
) > 0.0)
361 or else (Re
(X
) > 0.0 and then Re
(Result
) < 0.0)
363 Set_Re
(Result
, -Re
(Result
));
369 Result
:= Log
(X
+ Sqrt
(1.0 + X
* X
));
372 Set_Re
(Result
, Re
(X
));
373 elsif Im
(X
) = 0.0 then
374 Set_Im
(Result
, Im
(X
));
384 function Arctan
(X
: Complex
) return Complex
is
386 if abs Re
(X
) < Square_Root_Epsilon
and then
387 abs Im
(X
) < Square_Root_Epsilon
392 return -Complex_I
* (Log
(1.0 + Complex_I
* X
)
393 - Log
(1.0 - Complex_I
* X
)) / 2.0;
401 function Arctanh
(X
: Complex
) return Complex
is
403 if abs Re
(X
) < Square_Root_Epsilon
and then
404 abs Im
(X
) < Square_Root_Epsilon
408 return (Log
(1.0 + X
) - Log
(1.0 - X
)) / 2.0;
416 function Cos
(X
: Complex
) return Complex
is
419 Compose_From_Cartesian
420 (Cos
(Re
(X
)) * Cosh
(Im
(X
)),
421 -(Sin
(Re
(X
)) * Sinh
(Im
(X
))));
428 function Cosh
(X
: Complex
) return Complex
is
431 Compose_From_Cartesian
432 (Cosh
(Re
(X
)) * Cos
(Im
(X
)),
433 Sinh
(Re
(X
)) * Sin
(Im
(X
)));
440 function Cot
(X
: Complex
) return Complex
is
442 if abs Re
(X
) < Square_Root_Epsilon
and then
443 abs Im
(X
) < Square_Root_Epsilon
445 return Complex_One
/ X
;
447 elsif Im
(X
) > Log_Inverse_Epsilon_2
then
450 elsif Im
(X
) < -Log_Inverse_Epsilon_2
then
454 return Cos
(X
) / Sin
(X
);
461 function Coth
(X
: Complex
) return Complex
is
463 if abs Re
(X
) < Square_Root_Epsilon
and then
464 abs Im
(X
) < Square_Root_Epsilon
466 return Complex_One
/ X
;
468 elsif Re
(X
) > Log_Inverse_Epsilon_2
then
471 elsif Re
(X
) < -Log_Inverse_Epsilon_2
then
475 return Cosh
(X
) / Sinh
(X
);
483 function Exp
(X
: Complex
) return Complex
is
484 EXP_RE_X
: constant Real
'Base := Exp
(Re
(X
));
487 return Compose_From_Cartesian
(EXP_RE_X
* Cos
(Im
(X
)),
488 EXP_RE_X
* Sin
(Im
(X
)));
491 function Exp
(X
: Imaginary
) return Complex
is
492 ImX
: constant Real
'Base := Im
(X
);
495 return Compose_From_Cartesian
(Cos
(ImX
), Sin
(ImX
));
502 function Log
(X
: Complex
) return Complex
is
508 if Re
(X
) = 0.0 and then Im
(X
) = 0.0 then
509 raise Constraint_Error
;
511 elsif abs (1.0 - Re
(X
)) < Root_Root_Epsilon
512 and then abs Im
(X
) < Root_Root_Epsilon
515 Set_Re
(Z
, Re
(Z
) - 1.0);
517 return (1.0 - (1.0 / 2.0 -
518 (1.0 / 3.0 - (1.0 / 4.0) * Z
) * Z
) * Z
) * Z
;
522 ReX
:= Log
(Modulus
(X
));
525 when Constraint_Error
=>
526 ReX
:= Log
(Modulus
(X
/ 2.0)) - Log_Two
;
529 ImX
:= Arctan
(Im
(X
), Re
(X
));
532 ImX
:= ImX
- 2.0 * PI
;
535 return Compose_From_Cartesian
(ReX
, ImX
);
542 function Sin
(X
: Complex
) return Complex
is
544 if abs Re
(X
) < Square_Root_Epsilon
546 abs Im
(X
) < Square_Root_Epsilon
552 Compose_From_Cartesian
553 (Sin
(Re
(X
)) * Cosh
(Im
(X
)),
554 Cos
(Re
(X
)) * Sinh
(Im
(X
)));
561 function Sinh
(X
: Complex
) return Complex
is
563 if abs Re
(X
) < Square_Root_Epsilon
and then
564 abs Im
(X
) < Square_Root_Epsilon
569 return Compose_From_Cartesian
(Sinh
(Re
(X
)) * Cos
(Im
(X
)),
570 Cosh
(Re
(X
)) * Sin
(Im
(X
)));
578 function Sqrt
(X
: Complex
) return Complex
is
579 ReX
: constant Real
'Base := Re
(X
);
580 ImX
: constant Real
'Base := Im
(X
);
581 XR
: constant Real
'Base := abs Re
(X
);
582 YR
: constant Real
'Base := abs Im
(X
);
588 -- Deal with pure real case, see (RM G.1.2(39))
593 Compose_From_Cartesian
601 Compose_From_Cartesian
602 (0.0, Real
'Copy_Sign (Sqrt
(-ReX
), ImX
));
606 R_X
:= Sqrt
(YR
/ 2.0);
609 return Compose_From_Cartesian
(R_X
, R_X
);
611 return Compose_From_Cartesian
(R_X
, -R_X
);
615 R
:= Sqrt
(XR
** 2 + YR
** 2);
617 -- If the square of the modulus overflows, try rescaling the
618 -- real and imaginary parts. We cannot depend on an exception
619 -- being raised on all targets.
621 if R
> Real
'Base'Last then
622 raise Constraint_Error;
625 -- We are solving the system
627 -- XR = R_X ** 2 - Y_R ** 2 (1)
628 -- YR = 2.0 * R_X * R_Y (2)
630 -- The symmetric solution involves square roots for both R_X and
631 -- R_Y, but it is more accurate to use the square root with the
632 -- larger argument for either R_X or R_Y, and equation (2) for the
636 R_Y := Sqrt (0.5 * (R - ReX));
637 R_X := YR / (2.0 * R_Y);
640 R_X := Sqrt (0.5 * (R + ReX));
641 R_Y := YR / (2.0 * R_X);
645 if Im (X) < 0.0 then -- halve angle, Sqrt of magnitude
648 return Compose_From_Cartesian (R_X, R_Y);
651 when Constraint_Error =>
653 -- Rescale and try again
655 R := Modulus (Compose_From_Cartesian (Re (X / 4.0), Im (X / 4.0)));
656 R_X := 2.0 * Sqrt (0.5 * R + 0.5 * Re (X / 4.0));
657 R_Y := 2.0 * Sqrt (0.5 * R - 0.5 * Re (X / 4.0));
659 if Im (X) < 0.0 then -- halve angle, Sqrt of magnitude
663 return Compose_From_Cartesian (R_X, R_Y);
670 function Tan (X : Complex) return Complex is
672 if abs Re (X) < Square_Root_Epsilon and then
673 abs Im (X) < Square_Root_Epsilon
677 elsif Im (X) > Log_Inverse_Epsilon_2 then
680 elsif Im (X) < -Log_Inverse_Epsilon_2 then
684 return Sin (X) / Cos (X);
692 function Tanh (X : Complex) return Complex is
694 if abs Re (X) < Square_Root_Epsilon and then
695 abs Im (X) < Square_Root_Epsilon
699 elsif Re (X) > Log_Inverse_Epsilon_2 then
702 elsif Re (X) < -Log_Inverse_Epsilon_2 then
706 return Sinh (X) / Cosh (X);
710 end Ada.Numerics.Generic_Complex_Elementary_Functions;