flexiblesusy is hosted by Hepforge, IPPP Durham
FlexibleSUSY
complex.hpp
Go to the documentation of this file.
1// ====================================================================
2// This file is part of FlexibleSUSY.
3//
4// FlexibleSUSY is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published
6// by the Free Software Foundation, either version 3 of the License,
7// or (at your option) any later version.
8//
9// FlexibleSUSY is distributed in the hope that it will be useful, but
10// WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with FlexibleSUSY. If not, see
16// <http://www.gnu.org/licenses/>.
17// ====================================================================
18
19#ifndef FS_COMPLEX_H
20#define FS_COMPLEX_H
21
22#include <cmath>
23#include <complex>
24
25namespace flexiblesusy {
26
33template <typename T>
34struct Complex {
35 constexpr Complex(T re_ = T{}, T im_ = T{}) : re(re_), im(im_) {}
36 operator std::complex<T>() const noexcept { return std::complex<T>(re, im); }
37 T re{};
38 T im{};
39};
40
41template <typename T>
42constexpr T arg(const Complex<T>& z) noexcept
43{
44 return std::atan2(z.im, z.re);
45}
46
47template <typename T>
48constexpr Complex<T> conj(const Complex<T>& z) noexcept
49{
50 return { z.re, -z.im };
51}
52
53template <typename T>
54Complex<T> log(const Complex<T>& z) noexcept
55{
56 T a = arg(z);
57
58 if (z.im == T(0) && a < T(0)) {
59 a = -a;
60 }
61
62 return { std::log(norm(z)), a };
63}
64
65template <typename T>
66constexpr T norm(const Complex<T>& z) noexcept
67{
68 return std::hypot(z.re, z.im);
69}
70
71template <typename T>
72constexpr T norm_sqr(const Complex<T>& z) noexcept
73{
74 return z.re*z.re + z.im*z.im;
75}
76
77template <typename T>
78constexpr Complex<T> operator+(const Complex<T>& a, const Complex<T>& b) noexcept
79{
80 return { a.re + b.re, a.im + b.im };
81}
82
83template <typename T>
84constexpr Complex<T> operator+(const Complex<T>& z, T x) noexcept
85{
86 return { z.re + x, z.im };
87}
88
89template <typename T>
90constexpr Complex<T> operator+(T x, const Complex<T>& z) noexcept
91{
92 return { x + z.re, z.im };
93}
94
95template <typename T>
96constexpr Complex<T> operator-(const Complex<T>& a, const Complex<T>& b) noexcept
97{
98 return { a.re - b.re, a.im - b.im };
99}
100
101template <typename T>
102constexpr Complex<T> operator-(T x, const Complex<T>& z) noexcept
103{
104 return { x - z.re, -z.im };
105}
106
107template <typename T>
108constexpr Complex<T> operator-(const Complex<T>& z, T x) noexcept
109{
110 return { z.re - x, z.im };
111}
112
113template <typename T>
114constexpr Complex<T> operator-(const Complex<T>& z) noexcept
115{
116 return { -z.re, -z.im };
117}
118
119template <typename T>
120constexpr Complex<T> operator*(const Complex<T>& a, const Complex<T>& b) noexcept
121{
122 return { a.re*b.re - a.im*b.im, a.re*b.im + a.im*b.re };
123}
124
125template <typename T>
126constexpr Complex<T> operator*(T x, const Complex<T>& z) noexcept
127{
128 return { x*z.re, x*z.im };
129}
130
131template <typename T>
132constexpr Complex<T> operator*(const Complex<T>& z, T x) noexcept
133{
134 return x*z;
135}
136
137template <typename T>
138constexpr Complex<T> operator/(T x, const Complex<T>& z) noexcept
139{
140 return x*conj(z)/norm_sqr(z);
141}
142
143template <typename T>
144constexpr Complex<T> operator/(const Complex<T>& z, T x) noexcept
145{
146 return { z.re/x, z.im/x };
147}
148
149} // namespace flexiblesusy
150
151#endif
constexpr T norm(const Complex< T > &z) noexcept
Definition: complex.hpp:66
constexpr Complex< T > operator*(const Complex< T > &a, const Complex< T > &b) noexcept
Definition: complex.hpp:120
constexpr Complex< T > operator/(T x, const Complex< T > &z) noexcept
Definition: complex.hpp:138
constexpr T arg(const Complex< T > &z) noexcept
Definition: complex.hpp:42
constexpr Complex< T > operator+(const Complex< T > &a, const Complex< T > &b) noexcept
Definition: complex.hpp:78
constexpr T norm_sqr(const Complex< T > &z) noexcept
Definition: complex.hpp:72
constexpr Complex< T > operator-(const Complex< T > &a, const Complex< T > &b) noexcept
Definition: complex.hpp:96
Complex< T > log(const Complex< T > &z) noexcept
Definition: complex.hpp:54
constexpr Complex< T > conj(const Complex< T > &z) noexcept
Definition: complex.hpp:48
Class representing complex a number.
Definition: complex.hpp:34
constexpr Complex(T re_=T{}, T im_=T{})
Definition: complex.hpp:35