| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2015 - 2025 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_complex_overloads_h |
| 16 |
#define dealii_complex_overloads_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#include <complex> |
| 21 |
#include <type_traits> |
| 22 |
|
| 23 |
|
| 24 |
DEAL_II_NAMESPACE_OPEN |
| 25 |
|
| 26 |
#ifndef DOXYGEN |
| 27 |
|
| 28 |
// Forward declarations |
| 29 |
template <typename T, typename U> |
| 30 |
struct ProductType; |
| 31 |
|
| 32 |
#endif |
| 33 |
|
| 34 |
#ifndef DEAL_II_HAVE_COMPLEX_OPERATOR_OVERLOADS |
| 35 |
|
| 36 |
/** |
| 37 |
* Provide an <tt>operator*</tt> that operates on mixed complex floating point |
| 38 |
* types. Annoyingly, the standard library does not provide such an |
| 39 |
* operator. |
| 40 |
* |
| 41 |
* @note Because the C++ standard does not provide for mixed-precision complex |
| 42 |
* operators, code such as the following does not compile: |
| 43 |
* @code |
| 44 |
* double factor = 3.141; |
| 45 |
* std::complex<float> x(1,2); // =1+2i |
| 46 |
* auto z = factor*x; // error |
| 47 |
* @endcode |
| 48 |
* Because this does not compile, writing mixed-precision complex linear |
| 49 |
* algebra libraries is not easily possible without much additional work that |
| 50 |
* requires explicit casts. For example, one would have to write the code above |
| 51 |
* as follows: |
| 52 |
* @code |
| 53 |
* double factor = 1.0; |
| 54 |
* std::complex<float> x(1,2); // =1+2i |
| 55 |
* using P = ProductType<double,std::complex<float>>::type; |
| 56 |
* auto z = static_cast<P>(factor) * static_cast<P>(x); |
| 57 |
* @endcode |
| 58 |
* This makes much code unreadable. As a consequence, we define the |
| 59 |
* necessary overloaded multiplication and division operators for mixed |
| 60 |
* complex arithmetic (in namespace `dealii`) to make the code above |
| 61 |
* compile without the extra casts. |
| 62 |
* |
| 63 |
* @relatesalso ProductType |
| 64 |
*/ |
| 65 |
template <typename T, typename U> |
| 66 |
inline std::enable_if_t< |
| 67 |
std::is_floating_point_v<T> && std::is_floating_point_v<U> && |
| 68 |
!std::is_same_v<T, U>, |
| 69 |
typename ProductType<std::complex<T>, std::complex<U>>::type> |
| 70 |
operator*(const std::complex<T> &left, const std::complex<U> &right) |
| 71 |
{ |
| 72 |
using result_type = |
| 73 |
typename ProductType<std::complex<T>, std::complex<U>>::type; |
| 74 |
return static_cast<result_type>(left) * static_cast<result_type>(right); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Provide an <tt>operator/</tt> that operates on mixed complex floating point |
| 79 |
* types. Annoyingly, the standard library does not provide such an |
| 80 |
* operator. |
| 81 |
* |
| 82 |
* @note Because the C++ standard does not provide for mixed-precision complex |
| 83 |
* operators, code such as the following does not compile: |
| 84 |
* @code |
| 85 |
* double factor = 3.141; |
| 86 |
* std::complex<float> x(1,2); // =1+2i |
| 87 |
* auto z = factor*x; // error |
| 88 |
* @endcode |
| 89 |
* Because this does not compile, writing mixed-precision complex linear |
| 90 |
* algebra libraries is not easily possible without much additional work that |
| 91 |
* requires explicit casts. For example, one would have to write the code above |
| 92 |
* as follows: |
| 93 |
* @code |
| 94 |
* double factor = 1.0; |
| 95 |
* std::complex<float> x(1,2); // =1+2i |
| 96 |
* using P = ProductType<double,std::complex<float>>::type; |
| 97 |
* auto z = static_cast<P>(factor) * static_cast<P>(x); |
| 98 |
* @endcode |
| 99 |
* This makes much code unreadable. As a consequence, we define the |
| 100 |
* necessary overloaded multiplication and division operators for mixed |
| 101 |
* complex arithmetic (in namespace `dealii`) to make the code above |
| 102 |
* compile without the extra casts. |
| 103 |
* |
| 104 |
* @relatesalso ProductType |
| 105 |
*/ |
| 106 |
template <typename T, typename U> |
| 107 |
inline std::enable_if_t< |
| 108 |
std::is_floating_point_v<T> && std::is_floating_point_v<U> && |
| 109 |
!std::is_same_v<T, U>, |
| 110 |
typename ProductType<std::complex<T>, std::complex<U>>::type> |
| 111 |
operator/(const std::complex<T> &left, const std::complex<U> &right) |
| 112 |
{ |
| 113 |
using result_type = |
| 114 |
typename ProductType<std::complex<T>, std::complex<U>>::type; |
| 115 |
return static_cast<result_type>(left) / static_cast<result_type>(right); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Provide an <tt>operator*</tt> for a scalar multiplication of a complex |
| 120 |
* floating point type with a different real floating point type. Annoyingly, |
| 121 |
* the standard library does not provide such an operator. |
| 122 |
* |
| 123 |
* @note Because the C++ standard does not provide for mixed-precision complex |
| 124 |
* operators, code such as the following does not compile: |
| 125 |
* @code |
| 126 |
* double factor = 3.141; |
| 127 |
* std::complex<float> x(1,2); // =1+2i |
| 128 |
* auto z = factor*x; // error |
| 129 |
* @endcode |
| 130 |
* Because this does not compile, writing mixed-precision complex linear |
| 131 |
* algebra libraries is not easily possible without much additional work that |
| 132 |
* requires explicit casts. For example, one would have to write the code above |
| 133 |
* as follows: |
| 134 |
* @code |
| 135 |
* double factor = 1.0; |
| 136 |
* std::complex<float> x(1,2); // =1+2i |
| 137 |
* using P = ProductType<double,std::complex<float>>::type; |
| 138 |
* auto z = static_cast<P>(factor) * static_cast<P>(x); |
| 139 |
* @endcode |
| 140 |
* This makes much code unreadable. As a consequence, we define the |
| 141 |
* necessary overloaded multiplication and division operators for mixed |
| 142 |
* complex arithmetic (in namespace `dealii`) to make the code above |
| 143 |
* compile without the extra casts. |
| 144 |
* |
| 145 |
* @relatesalso ProductType |
| 146 |
*/ |
| 147 |
template <typename T, typename U> |
| 148 |
inline std::enable_if_t<std::is_floating_point_v<T> && |
| 149 |
std::is_floating_point_v<U> && !std::is_same_v<T, U>, |
| 150 |
typename ProductType<std::complex<T>, U>::type> |
| 151 |
operator*(const std::complex<T> &left, const U &right) |
| 152 |
{ |
| 153 |
using result_type = typename ProductType<std::complex<T>, U>::type; |
| 154 |
return static_cast<result_type>(left) * static_cast<result_type>(right); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Provide an <tt>operator/</tt> for a scalar division of a complex |
| 159 |
* floating point type with a different real floating point type. Annoyingly, |
| 160 |
* the standard library does not provide such an operator. |
| 161 |
* |
| 162 |
* @note Because the C++ standard does not provide for mixed-precision complex |
| 163 |
* operators, code such as the following does not compile: |
| 164 |
* @code |
| 165 |
* double factor = 3.141; |
| 166 |
* std::complex<float> x(1,2); // =1+2i |
| 167 |
* auto z = factor*x; // error |
| 168 |
* @endcode |
| 169 |
* Because this does not compile, writing mixed-precision complex linear |
| 170 |
* algebra libraries is not easily possible without much additional work that |
| 171 |
* requires explicit casts. For example, one would have to write the code above |
| 172 |
* as follows: |
| 173 |
* @code |
| 174 |
* double factor = 1.0; |
| 175 |
* std::complex<float> x(1,2); // =1+2i |
| 176 |
* using P = ProductType<double,std::complex<float>>::type; |
| 177 |
* auto z = static_cast<P>(factor) * static_cast<P>(x); |
| 178 |
* @endcode |
| 179 |
* This makes much code unreadable. As a consequence, we define the |
| 180 |
* necessary overloaded multiplication and division operators for mixed |
| 181 |
* complex arithmetic (in namespace `dealii`) to make the code above |
| 182 |
* compile without the extra casts. |
| 183 |
* |
| 184 |
* @relatesalso ProductType |
| 185 |
*/ |
| 186 |
template <typename T, typename U> |
| 187 |
inline std::enable_if_t<std::is_floating_point_v<T> && |
| 188 |
std::is_floating_point_v<U> && !std::is_same_v<T, U>, |
| 189 |
typename ProductType<std::complex<T>, U>::type> |
| 190 |
operator/(const std::complex<T> &left, const U &right) |
| 191 |
{ |
| 192 |
using result_type = typename ProductType<std::complex<T>, U>::type; |
| 193 |
return static_cast<result_type>(left) / static_cast<result_type>(right); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Provide an <tt>operator*</tt> for a scalar multiplication of a real |
| 198 |
* floating point type with a different complex floating point type. |
| 199 |
* Annoyingly, the standard library does not provide such an operator. |
| 200 |
* |
| 201 |
* @note Because the C++ standard does not provide for mixed-precision complex |
| 202 |
* operators, code such as the following does not compile: |
| 203 |
* @code |
| 204 |
* double factor = 3.141; |
| 205 |
* std::complex<float> x(1,2); // =1+2i |
| 206 |
* auto z = factor*x; // error |
| 207 |
* @endcode |
| 208 |
* Because this does not compile, writing mixed-precision complex linear |
| 209 |
* algebra libraries is not easily possible without much additional work that |
| 210 |
* requires explicit casts. For example, one would have to write the code above |
| 211 |
* as follows: |
| 212 |
* @code |
| 213 |
* double factor = 1.0; |
| 214 |
* std::complex<float> x(1,2); // =1+2i |
| 215 |
* using P = ProductType<double,std::complex<float>>::type; |
| 216 |
* auto z = static_cast<P>(factor) * static_cast<P>(x); |
| 217 |
* @endcode |
| 218 |
* This makes much code unreadable. As a consequence, we define the |
| 219 |
* necessary overloaded multiplication and division operators for mixed |
| 220 |
* complex arithmetic (in namespace `dealii`) to make the code above |
| 221 |
* compile without the extra casts. |
| 222 |
* |
| 223 |
* @relatesalso ProductType |
| 224 |
*/ |
| 225 |
template <typename T, typename U> |
| 226 |
inline std::enable_if_t<std::is_floating_point_v<T> && |
| 227 |
std::is_floating_point_v<U> && !std::is_same_v<T, U>, |
| 228 |
typename ProductType<T, std::complex<U>>::type> |
| 229 |
operator*(const T &left, const std::complex<U> &right) |
| 230 |
{ |
| 231 |
using result_type = typename ProductType<T, std::complex<U>>::type; |
| 232 |
return static_cast<result_type>(left) * static_cast<result_type>(right); |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Provide an <tt>operator/</tt> for a scalar division of a real |
| 237 |
* floating point type with a different complex floating point type. |
| 238 |
* Annoyingly, the standard library does not provide such an operator. |
| 239 |
* |
| 240 |
* @note Because the C++ standard does not provide for mixed-precision complex |
| 241 |
* operators, code such as the following does not compile: |
| 242 |
* @code |
| 243 |
* double factor = 3.141; |
| 244 |
* std::complex<float> x(1,2); // =1+2i |
| 245 |
* auto z = factor*x; // error |
| 246 |
* @endcode |
| 247 |
* Because this does not compile, writing mixed-precision complex linear |
| 248 |
* algebra libraries is not easily possible without much additional work that |
| 249 |
* requires explicit casts. For example, one would have to write the code above |
| 250 |
* as follows: |
| 251 |
* @code |
| 252 |
* double factor = 1.0; |
| 253 |
* std::complex<float> x(1,2); // =1+2i |
| 254 |
* using P = ProductType<double,std::complex<float>>::type; |
| 255 |
* auto z = static_cast<P>(factor) * static_cast<P>(x); |
| 256 |
* @endcode |
| 257 |
* This makes much code unreadable. As a consequence, we define the |
| 258 |
* necessary overloaded multiplication and division operators for mixed |
| 259 |
* complex arithmetic (in namespace `dealii`) to make the code above |
| 260 |
* compile without the extra casts. |
| 261 |
* |
| 262 |
* @relatesalso ProductType |
| 263 |
*/ |
| 264 |
template <typename T, typename U> |
| 265 |
inline std::enable_if_t<std::is_floating_point_v<T> && |
| 266 |
std::is_floating_point_v<U> && !std::is_same_v<T, U>, |
| 267 |
typename ProductType<T, std::complex<U>>::type> |
| 268 |
operator/(const T &left, const std::complex<U> &right) |
| 269 |
{ |
| 270 |
using result_type = typename ProductType<T, std::complex<U>>::type; |
| 271 |
return static_cast<result_type>(left) / static_cast<result_type>(right); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
#endif /* DEAL_II_HAVE_COMPLEX_OPERATOR_OVERLOADS */ |
| 276 |
|
| 277 |
DEAL_II_NAMESPACE_CLOSE |
| 278 |
|
| 279 |
#endif |