OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
GSLComplex.h
Go to the documentation of this file.
1//
2// GSL Complex compatibility to replace gsl_complex
3//
4// Copyright (c) 2023, Paul Scherrer Institute, Villigen PSI, Switzerland
5// All rights reserved
6//
7// This file is part of OPAL.
8//
9// OPAL is free software: you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation, either version 3 of the License, or
12// (at your option) any later version.
13//
14// You should have received a copy of the GNU General License
15// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
16//
17
18#ifndef OPAL_GSL_COMPLEX_HH
19#define OPAL_GSL_COMPLEX_HH
20
21#include <cmath>
22#include <complex>
23
28 double dat[2]; // [real, imag]
29
30 gsl_complex() : dat{0.0, 0.0} {}
31 gsl_complex(double r, double i) : dat{r, i} {}
32 gsl_complex(const std::complex<double>& c) : dat{c.real(), c.imag()} {}
33
34 operator std::complex<double>() const { return std::complex<double>(dat[0], dat[1]); }
35
36 double real() const { return dat[0]; }
37 double imag() const { return dat[1]; }
38};
39
41#define GSL_REAL(z) ((z).dat[0])
43#define GSL_IMAG(z) ((z).dat[1])
44
49inline gsl_complex gsl_complex_rect(double x, double y) { return gsl_complex(x, y); }
50
55inline gsl_complex gsl_complex_polar(double r, double theta) {
56 return gsl_complex(r * std::cos(theta), r * std::sin(theta));
57}
58
62inline double gsl_complex_arg(gsl_complex z) { return std::arg(std::complex<double>(z)); }
63
67inline double gsl_complex_abs(gsl_complex z) { return std::abs(std::complex<double>(z)); }
68
72inline double gsl_complex_abs2(gsl_complex z) { return z.dat[0] * z.dat[0] + z.dat[1] * z.dat[1]; }
73
80 return gsl_complex(a.dat[0] + b.dat[0], a.dat[1] + b.dat[1]);
81}
82
89 return gsl_complex(a.dat[0] - b.dat[0], a.dat[1] - b.dat[1]);
90}
91
98 std::complex<double> ca(a), cb(b);
99 return gsl_complex(ca * cb);
100}
101
107 std::complex<double> ca(a), cb(b);
108 return gsl_complex(ca / cb);
109}
110
117 return gsl_complex(a.dat[0] + x, a.dat[1]);
118}
119
126 return gsl_complex(a.dat[0] - x, a.dat[1]);
127}
128
135 return gsl_complex(a.dat[0] * x, a.dat[1] * x);
136}
137
144 return gsl_complex(a.dat[0] / x, a.dat[1] / x);
145}
146
152
158 std::complex<double> ca(a);
159 return gsl_complex(1.0 / ca);
160}
161
167
173 std::complex<double> ca(a);
174 return gsl_complex(std::sqrt(ca));
175}
176
182 std::complex<double> ca(a);
183 return gsl_complex(std::exp(ca));
184}
185
191 std::complex<double> ca(a);
192 return gsl_complex(std::log(ca));
193}
194
200 std::complex<double> ca(a);
201 return gsl_complex(std::sin(ca));
202}
203
209 std::complex<double> ca(a);
210 return gsl_complex(std::cos(ca));
211}
212
219 std::complex<double> ca(a), cb(b);
220 return gsl_complex(std::pow(ca, cb));
221}
222
228 std::complex<double> ca(a);
229 return gsl_complex(std::tanh(ca));
230}
231
232#endif // OPAL_GSL_COMPLEX_HH
gsl_complex gsl_complex_add_real(gsl_complex a, double x)
Add real scalar .
Definition GSLComplex.h:116
gsl_complex gsl_complex_rect(double x, double y)
Construct .
Definition GSLComplex.h:49
gsl_complex gsl_complex_cos(gsl_complex a)
Complex cosine .
Definition GSLComplex.h:208
gsl_complex gsl_complex_exp(gsl_complex a)
Complex exponential .
Definition GSLComplex.h:181
gsl_complex gsl_complex_mul_real(gsl_complex a, double x)
Multiply by real scalar .
Definition GSLComplex.h:134
gsl_complex gsl_complex_div(gsl_complex a, gsl_complex b)
Quotient .
Definition GSLComplex.h:106
gsl_complex gsl_complex_sin(gsl_complex a)
Complex sine .
Definition GSLComplex.h:199
gsl_complex gsl_complex_div_real(gsl_complex a, double x)
Divide by real scalar .
Definition GSLComplex.h:143
gsl_complex gsl_complex_negative(gsl_complex a)
Negation .
Definition GSLComplex.h:166
gsl_complex gsl_complex_mul(gsl_complex a, gsl_complex b)
Product .
Definition GSLComplex.h:97
double gsl_complex_abs(gsl_complex z)
Magnitude .
Definition GSLComplex.h:67
gsl_complex gsl_complex_polar(double r, double theta)
Construct .
Definition GSLComplex.h:55
gsl_complex gsl_complex_sub_real(gsl_complex a, double x)
Subtract real scalar .
Definition GSLComplex.h:125
gsl_complex gsl_complex_conjugate(gsl_complex a)
Complex conjugate .
Definition GSLComplex.h:151
gsl_complex gsl_complex_add(gsl_complex a, gsl_complex b)
Sum .
Definition GSLComplex.h:79
gsl_complex gsl_complex_pow(gsl_complex a, gsl_complex b)
Complex power .
Definition GSLComplex.h:218
double gsl_complex_arg(gsl_complex z)
Argument (phase) .
Definition GSLComplex.h:62
gsl_complex gsl_complex_tanh(gsl_complex a)
Complex hyperbolic tangent .
Definition GSLComplex.h:227
gsl_complex gsl_complex_inverse(gsl_complex a)
Multiplicative inverse .
Definition GSLComplex.h:157
gsl_complex gsl_complex_sqrt(gsl_complex a)
Complex square root .
Definition GSLComplex.h:172
double gsl_complex_abs2(gsl_complex z)
Squared magnitude .
Definition GSLComplex.h:72
gsl_complex gsl_complex_log(gsl_complex a)
Complex natural logarithm .
Definition GSLComplex.h:190
gsl_complex gsl_complex_sub(gsl_complex a, gsl_complex b)
Difference .
Definition GSLComplex.h:88
Complex number stored as .
Definition GSLComplex.h:27
gsl_complex(const std::complex< double > &c)
Definition GSLComplex.h:32
double imag() const
Definition GSLComplex.h:37
double dat[2]
Definition GSLComplex.h:28
double real() const
Definition GSLComplex.h:36
gsl_complex(double r, double i)
Definition GSLComplex.h:31