QuEST_complex.h
Go to the documentation of this file.
1 // Distributed under MIT licence. See https://github.com/QuEST-Kit/QuEST/blob/master/LICENCE.txt for details
2 
21 #ifndef QUEST_COMPLEX_H
22 #define QUEST_COMPLEX_H
23 
24 
25 /*
26  * creating precision-specific complex aliases
27  */
28 
29 // hide these from doxygen
30 // \cond HIDDEN_SYMBOLS
31 
32 // C++ uses complex<T>
33 #ifdef __cplusplus
34 
35 #include <cmath>
36 #include <complex>
37 
38 using namespace std;
39 
40 typedef complex<float> float_complex;
41 typedef complex<double> double_complex;
42 typedef complex<long double> long_double_complex;
43 
44 // enable C-style funcs in C++
45 #define creal(x) real(x)
46 #define cimag(x) imag(x)
47 #define carg(x) arg(x)
48 #define cabs(x) abs(x)
49 
50 #else
51 
52 // C uses complex type
53 #include <tgmath.h> // includes <math.h> and <complex.h>
54 
55 typedef float complex float_complex;
56 typedef double complex double_complex;
57 typedef long double complex long_double_complex;
58 
59 #define float_complex(r,i) ((float)(r) + ((float)(i))*I)
60 #define double_complex(r,i) ((double)(r) + ((double)(i))*I)
61 #define long_double_complex(r,i) ((long double)(r) + ((long double)(i))*I)
62 
63 #endif // #ifdef __cplusplus
64 
65 // \endcond
66 
67 
68 /*
69  * creating a single precision-agnostic type
70  */
71 
72 // this horrible hack is needed for doxygen doc
73 #define qcomp
74 #undef qcomp
75 
76 #if QuEST_PREC==1
77 #define qcomp float_complex
78 #elif QuEST_PREC==2
79 #define qcomp double_complex
80 #elif QuEST_PREC==4
81 #define qcomp long_double_complex
82 #endif
83 
84 
85 /*
86  * creating converters to/from QuEST's internal type
87  */
88 
89 #define toComplex(scalar) ((Complex) {.real = creal(scalar), .imag = cimag(scalar)})
90 #define fromComplex(comp) qcomp(comp.real, comp.imag)
91 
92 
93 /*
94  * creating doc
95  */
96 
144 #endif // #ifndef QUEST_COMPLEX_H