1 // asinh().
2 
3 // General includes.
4 #include "base/cl_sysdep.h"
5 
6 // Specification.
7 #include "cln/complex.h"
8 
9 
10 // Implementation.
11 
12 #include "complex/cl_C.h"
13 #include "cln/real.h"
14 
15 namespace cln {
16 
17 // Methode:
18 // Wert und Branch Cuts nach der Formel CLTL2, S. 313:
19 //   arsinh(z) = log(z+sqrt(1+z^2))
20 // z=x+iy, Ergebnis u+iv.
21 // Falls x=0 und y=0: u=0, v=0.
22 // Falls x=0: arsinh(iy) = i arcsin(y).
23 //   y rational ->
24 //     Bei y=1: u = 0, v = pi/2.
25 //     Bei y=1/2: u = 0, v = pi/6.
26 //     Bei y=0: u = 0, v = 0.
27 //     Bei y=-1/2: u = 0, v = -pi/6.
28 //     Bei y=-1: u = 0, v = -pi/2.
29 //     Sonst y in Float umwandeln.
30 //   e := Exponent aus (decode-float y), d := (float-digits y)
31 //   Bei y=0.0 oder e<=-d/2 liefere u = 0, v = y
32 //     (denn bei e<=-d/2 ist y^2/3 < y^2/2 < 2^(-d)/2 = 2^(-d-1), also
33 //     1 <= asin(y)/y < 1+y^2/3 < 1+2^(-d-1) < 1+2^(-d),
34 //     also ist asin(y)/y, auf d Bits gerundet, gleich 1.0).
35 //   Berechne 1-y^2.
36 //   Bei y>1 liefere  u = ln(y+sqrt(y^2-1)), v = pi/2.
37 //   Bei y<-1 liefere  u = -ln(|y|+sqrt(|y|^2-1)), v = -pi/2.
38 //   Bei |y|<=1 liefere  u = 0, v = atan(X=sqrt(1-y^2),Y=y).
39 // Falls y=0:
40 //   x rational -> x in Float umwandeln.
41 //   |x|<1/2: u = atanh(x/sqrt(1+x^2)),
42 //   x>=1/2: u = ln(x+sqrt(1+x^2)),
43 //   x<=-1/2: u = -ln(-x+sqrt(1+x^2)).
44 //   v = 0.
45 // Sonst:
46 //   z in Bild(sqrt) -> log(sqrt(1+z^2)+z) = (!) = 2 artanh(z/(1+sqrt(1+z^2))).
47 //   z nicht in Bild(sqrt) ->
48 //     arsinh(z) = -arsinh(-z).
49 //     (Denn arsinh(z)+arsinh(-z) == log((z+sqrt(1+z^2))(-z+sqrt(1+z^2)))
50 //           = log((1+z^2)-z^2) = log(1) = 0 mod 2 pi i, und links ist
51 //      der Imaginärteil betragsmäßig <=pi.)
52 //     Also arsinh(z) = -arsinh(-z) = - 2 artanh(-z/(1+sqrt(1+z^2)))
53 //          = (wegen -artanh(-w) = artanh(w)) = 2 artanh(z/(1+sqrt(1+z^2))).
54 // Real- und Imaginärteil des Ergebnisses sind Floats, außer wenn z reell oder
55 // rein imaginär ist.
56 
57 // Um für zwei Zahlen u,v mit u^2-v^2=1 und u,v beide in Bild(sqrt)
58 // (d.h. Realteil>0.0 oder Realteil=0.0 und Imaginärteil>=0.0)
59 // log(u+v) zu berechnen:
60 //               log(u+v) = 2 artanh(v/(u+1))                            (!)
61 // (Beweis: 2 artanh(v/(u+1)) = log(1+(v/(u+1))) - log(1-(v/(u+1)))
62 //  = log((1+u+v)/(u+1)) - log((1+u-v)/(u+1)) == log((1+u+v)/(1+u-v))
63 //  = log(u+v) mod 2 pi i, und beider Imaginärteil ist > -pi und <= pi.)
64 
_asinh(const cl_N & z)65 inline const cl_C_R _asinh (const cl_N& z)
66 {
67 	if (realp(z)) {
68 		DeclareType(cl_R,z);
69 		return asinh(z,0);
70 	} else {
71 		DeclareType(cl_C,z);
72 		return asinh(realpart(z),imagpart(z));
73 	}
74 }
75 
asinh(const cl_N & z)76 const cl_N asinh (const cl_N& z)
77 {
78 	var cl_C_R u_v = _asinh(z);
79 	var cl_R& u = u_v.realpart;
80 	var cl_R& v = u_v.imagpart;
81 	return complex(u,v);
82 }
83 
84 }  // namespace cln
85