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--present, The Simons Foundation
2// This file is part of TRIQS/nda and is licensed under the Apache License, Version 2.0.
3// SPDX-License-Identifier: Apache-2.0
4// See LICENSE in the root of this distribution for details.
5
10
11#ifndef STDUTILS_COMPLEX_H
12#define STDUTILS_COMPLEX_H
13
14#include <complex>
15#include <concepts>
16#include <type_traits>
17
18using namespace std::literals::complex_literals;
19
20namespace std { // has to be in the right namespace for ADL
21
26
27// define operators (+,-,*,/) for std::complex and various other arithmetic types
28#define IMPL_OP(OP) \
29 \
30 template <typename T, typename U> \
31 requires(std::is_arithmetic_v<T> and std::is_arithmetic_v<U> and std::common_with<T, U>) \
32 auto operator OP(std::complex<T> const &x, U y) { \
33 using C = std::complex<std::common_type_t<T, U>>; \
34 return C(x.real(), x.imag()) OP C(y); \
35 } \
36 \
37 \
38 template <typename T, typename U> \
39 requires(std::is_arithmetic_v<T> and std::is_arithmetic_v<U> and std::common_with<T, U>) \
40 auto operator OP(T x, std::complex<U> const &y) { \
41 using C = std::complex<std::common_type_t<T, U>>; \
42 return C(x) OP C(y.real(), y.imag()); \
43 } \
44 \
45 \
46 template <typename T, typename U> \
47 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>) \
48 auto operator OP(std::complex<T> const &x, std::complex<U> const &y) { \
49 using C = std::complex<std::common_type_t<T, U>>; \
50 return C(x.real(), x.imag()) OP C(y.real(), y.imag()); \
51 }
52
53 IMPL_OP(+)
54 IMPL_OP(-)
55 IMPL_OP(*)
56 IMPL_OP(/)
57
58#undef IMPL_OP
59
61
62} // namespace std
63
64#endif // STDUTILS_COMPLEX_H