TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
complex.hpp
Go to the documentation of this file.
1// Copyright (c) 2019-2024 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Thomas Hahn, Sergei Iskakov, Olivier Parcollet, Nils Wentzell
16
22#ifndef STDUTILS_COMPLEX_H
23#define STDUTILS_COMPLEX_H
24
25#include <complex>
26#include <concepts>
27#include <type_traits>
28
29using namespace std::literals::complex_literals;
30
31namespace std { // has to be in the right namespace for ADL
32
38// define operators (+,-,*,/) for std::complex and various other arithmetic types
39#define IMPL_OP(OP) \
40 \
41 template <typename T, typename U> \
42 requires(std::is_arithmetic_v<T> and std::is_arithmetic_v<U> and std::common_with<T, U>) \
43 auto operator OP(std::complex<T> const &x, U y) { \
44 using C = std::complex<std::common_type_t<T, U>>; \
45 return C(x.real(), x.imag()) OP C(y); \
46 } \
47 \
48 \
49 template <typename T, typename U> \
50 requires(std::is_arithmetic_v<T> and std::is_arithmetic_v<U> and std::common_with<T, U>) \
51 auto operator OP(T x, std::complex<U> const &y) { \
52 using C = std::complex<std::common_type_t<T, U>>; \
53 return C(x) OP C(y.real(), y.imag()); \
54 } \
55 \
56 \
57 template <typename T, typename U> \
58 requires(std::is_arithmetic_v<T> and std::is_arithmetic_v<U> and std::common_with<T, U> and !std::is_same_v<T, U>) \
59 auto operator OP(std::complex<T> const &x, std::complex<U> const &y) { \
60 using C = std::complex<std::common_type_t<T, U>>; \
61 return C(x.real(), x.imag()) OP C(y.real(), y.imag()); \
62 }
63
64 IMPL_OP(+)
65 IMPL_OP(-)
66 IMPL_OP(*)
67 IMPL_OP(/)
68
69#undef IMPL_OP
70
73} // namespace std
74
75#endif // STDUTILS_COMPLEX_H