1 /********************************************************************************
2 * *
3 * S i n g l e - P r e c i s i o n C o m p l e x N u m b e r *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2006,2021 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #include "xincs.h"
22 #include "fxver.h"
23 #include "fxdefs.h"
24 #include "fxmath.h"
25 #include "FXArray.h"
26 #include "FXHash.h"
27 #include "FXStream.h"
28 #include "FXObject.h"
29 #include "FXComplexf.h"
30
31
32 using namespace FX;
33
34
35 /*******************************************************************************/
36
37 namespace FX {
38
39
40 // Complex square root
csqrt(const FXComplexf & c)41 FXComplexf csqrt(const FXComplexf& c){
42 FXfloat mag=abs(c);
43 FXfloat rr=Math::sqrt((mag+c.re)*0.5f);
44 FXfloat ii=Math::sqrt((mag-c.re)*0.5f);
45 return FXComplexf(rr,Math::copysign(ii,c.im));
46 }
47
48
49 // Complex sine
csin(const FXComplexf & c)50 FXComplexf csin(const FXComplexf& c){
51 return FXComplexf(Math::sin(c.re)*Math::cosh(c.im),Math::cos(c.re)*Math::sinh(c.im));
52 }
53
54
55 // Complex cosine
ccos(const FXComplexf & c)56 FXComplexf ccos(const FXComplexf& c){
57 return FXComplexf(Math::cos(c.re)*Math::cosh(c.im),-Math::sin(c.re)*Math::sinh(c.im));
58 }
59
60
61 // Complex tangent
ctan(const FXComplexf & c)62 FXComplexf ctan(const FXComplexf& c){
63 FXComplexf em=exp(FXComplexf(c.im,-c.re));
64 FXComplexf ep=exp(FXComplexf(-c.im,c.re));
65 FXComplexf t=(em-ep)/(em+ep);
66 return FXComplexf(-t.im,t.re);
67 }
68
69
70 // Complex hyperbolic sine
csinh(const FXComplexf & c)71 FXComplexf csinh(const FXComplexf& c){
72 return FXComplexf(Math::cos(c.im)*Math::sinh(c.re),Math::sin(c.im)*Math::cosh(c.re));
73 }
74
75
76 // Complex hyperbolic cosine
ccosh(const FXComplexf & c)77 FXComplexf ccosh(const FXComplexf& c){
78 return FXComplexf(Math::cos(c.im)*Math::cosh(c.re),Math::sin(c.im)*Math::sinh(c.re));
79 }
80
81
82 // Complex hyperbolic tangent
ctanh(const FXComplexf & c)83 FXComplexf ctanh(const FXComplexf& c){
84 return csinh(c)/ccosh(c);
85 }
86
87
operator <<(FXStream & store,const FXComplexf & c)88 FXStream& operator<<(FXStream& store,const FXComplexf& c){
89 store << c.re << c.im;
90 return store;
91 }
92
93
operator >>(FXStream & store,FXComplexf & c)94 FXStream& operator>>(FXStream& store,FXComplexf& c){
95 store >> c.re >> c.im;
96 return store;
97 }
98
99 }
100