CoDiPack  3.1.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
stdComplex.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2026 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
5 * Homepage: http://scicomp.rptu.de
6 * Contact: Prof. Nicolas R. Gauger (codi@scicomp.uni-kl.de)
7 *
8 * Lead developers: Max Sagebaum, Johannes Blühdorn (SciComp, University of Kaiserslautern-Landau)
9 *
10 * This file is part of CoDiPack (http://scicomp.rptu.de/software/codi).
11 *
12 * CoDiPack is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * CoDiPack is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty
19 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * See the GNU General Public License for more details.
22 * You should have received a copy of the GNU
23 * General Public License along with CoDiPack.
24 * If not, see <http://www.gnu.org/licenses/>.
25 *
26 * For other licensing options please contact us.
27 *
28 * Authors:
29 * - SciComp, University of Kaiserslautern-Landau:
30 * - Max Sagebaum
31 * - Johannes Blühdorn
32 * - Former members:
33 * - Tim Albring
34 */
35#pragma once
36
37#include <complex>
38
39#include "../../config.h"
40#include "../../misc/macros.hpp"
41#include "../../misc/self.hpp"
42#include "../../traits/computationTraits.hpp"
43#include "../activeType.hpp"
44#include "../aggregate/aggregatedActiveType.hpp"
45#include "../computeExpression.hpp"
46#include "../expressionInterface.hpp"
47#include "../expressionMemberOperations.hpp"
48#include "../real/binaryOperators.hpp"
49#include "../real/unaryOperators.hpp"
50#include "allOperators.hpp"
51#include "complexPredef.hpp"
52
54namespace codi {
61 template<typename T_Real, typename T_Impl>
63 T_Real, T_Impl,
64 typename std::enable_if<std::is_same<T_Real, std::complex<typename T_Real::value_type>>::value>::type> {
65 public:
66 using Real = CODI_DD(T_Real, std::complex<double>);
68
69 using InnerType = typename Real::value_type;
70
73
78
82
86
87 private:
88 CODI_INLINE Impl const& cast() const {
89 return static_cast<Impl const&>(*this);
90 }
91 };
92
101 template<typename T_InnerActive, typename T_Impl>
102 struct ActiveComplex : public AggregatedActiveType<std::complex<typename T_InnerActive::Real>, T_InnerActive,
103 ResolveSelf<T_Impl, ActiveComplex<T_InnerActive, T_Impl>>>,
104 public AssignmentOperators<typename T_InnerActive::Real, false,
105 ResolveSelf<T_Impl, ActiveComplex<T_InnerActive, T_Impl>>>,
106 public AssignmentOperators<std::complex<typename T_InnerActive::Real>, false,
107 ResolveSelf<T_Impl, ActiveComplex<T_InnerActive, T_Impl>>> {
108 public:
109
110 using InnerActive = CODI_DD(T_InnerActive, CODI_DEFAULT_ACTIVE_TYPE);
112
113 using InnerReal = typename InnerActive::Real;
114 using Real = std::complex<InnerReal>;
116
125
127
128 using Base::Base;
129 using Base::operator=;
130
131 using AssignReal::operator+=;
132 using AssignComplex::operator+=;
133 using AssignReal::operator-=;
134 using AssignComplex::operator-=;
135 using AssignReal::operator*=;
136 using AssignComplex::operator*=;
137 using AssignReal::operator/=;
138 using AssignComplex::operator/=;
139
142 Base::store(arg, codi::EventHints::Statement::Copy);
143 }
144
146 template<typename ArgR>
151
154 Base::values[0] = argR;
156 }
157
159 template<typename ArgR, typename ArgI>
166
168 template<typename ArgI>
170 : Base() {
171 Base::values[0] = argR;
172 Base::values[1] = argI.cast();
173 }
174
176 template<typename ArgR>
178 : Base() {
179 Base::values[0] = argR.cast();
180 Base::values[1] = argI;
181 }
182
185 Base::values[0] = argR;
186 Base::values[1] = argI;
187 }
188
190
193 Base::store(arg, codi::EventHints::Statement::Copy);
194
195 return *this;
196 }
197
199 template<typename ArgR>
201 Base::values[0] = argR.cast();
203
204 return *this;
205 }
206
209 Base::values[0] = argR;
211
212 return *this;
213 }
214
215 using Base::real;
216
218 template<typename Arg>
222
224 void real(InnerReal const& arg) {
225 Base::values[0] = arg;
226 }
227
228 using Base::imag;
229
231 template<typename Arg>
235
237 void imag(InnerReal const& arg) {
238 Base::values[1] = arg;
239 }
240 };
241
242 namespace RealTraits {
243
245 template<typename T_InnerReal>
246 struct AggregatedTypeTraits<std::complex<T_InnerReal>>
247 : public ArrayAggregatedTypeTraitsBase<std::complex<T_InnerReal>, T_InnerReal,
248 std::complex<RealTraits::Real<T_InnerReal>>, 2> {
249 public:
251 static CODI_INLINE std::string getMathRep() {
252 return "complex()";
253 }
254 };
255
257 template<typename T_InnerReal>
260 ActiveComplex<T_InnerReal>, T_InnerReal,
261 typename std::conditional<std::is_floating_point<RealTraits::Real<T_InnerReal>>::value,
262 std::complex<RealTraits::Real<T_InnerReal>>,
263 ActiveComplex<RealTraits::Real<T_InnerReal>>>::type,
264 2> {
265 public:
267 static CODI_INLINE std::string getMathRep() {
268 return "complex()";
269 }
270 };
271 }
272
274 template<typename T_InnerReal, typename T_Tape>
275 struct ExpressionTraits::ActiveResultImpl<std::complex<T_InnerReal>, T_Tape, false> {
276 using InnerReal = CODI_DD(T_InnerReal, CODI_ANY);
277 using Tape = CODI_DD(T_Tape, CODI_ANY);
278
281
283#if CODI_SpecializeStdComplex
284 using ActiveResult = std::complex<InnerActiveResult>;
285#else
287#endif
288 };
289}
290
291#if CODI_SpecializeStdComplex
292namespace std {
293
294 /*******************************************************************************/
297
303 template<typename T_Tape>
304 class complex<codi::ActiveType<T_Tape>>
305 : public codi::ActiveComplex<codi::ActiveType<T_Tape>, complex<codi::ActiveType<T_Tape>>> {
306 public:
307
310
311 using Real = complex<typename Tape::Real>;
313 using InnerReal = typename InnerActive::Real;
314
316 using Base = codi::ActiveComplex<codi::ActiveType<Tape>, complex<codi::ActiveType<T_Tape>>>;
317
318 using Base::Base;
319 CODI_INLINE complex(complex const& value) : Base(value) {}
320
321 CODI_INLINE ~complex() = default;
322
323 using Base::operator=;
324
326 CODI_INLINE complex& operator=(complex const& value) {
327 Base::store(value, codi::EventHints::Statement::Copy);
328
329 return *this;
330 }
331 };
332
334 template<typename Tape>
335 CODI_INLINE std::ostream& operator<<(std::ostream& out, std::complex<codi::ActiveType<Tape>> const& v) {
336 out << v.getValue();
337
338 return out;
339 }
340
342 /*******************************************************************************/
354
355 #define FUNCTION operator+
356 #define OPERATION_LOGIC codi::OperationAdd
357 #include "binaryComplexToComplexStdSpecialization.tpp"
358
359 #define FUNCTION operator-
360 #define OPERATION_LOGIC codi::OperationSubstract
361 #include "binaryComplexToComplexStdSpecialization.tpp"
362
363 #define FUNCTION operator*
364 #define OPERATION_LOGIC codi::OperationMultiply
365 #include "binaryComplexToComplexStdSpecialization.tpp"
366
367 #define FUNCTION operator/
368 #define OPERATION_LOGIC codi::OperationDivide
369 #include "binaryComplexToComplexStdSpecialization.tpp"
370
371 // polar needs to be created in the codi namespace.
372
373 #define FUNCTION pow
374 #define OPERATION_LOGIC codi::OperationPow
375 #include "binaryComplexToComplexStdSpecialization.tpp"
376
378 /*******************************************************************************/
390
392 template<typename Tape>
393 CODI_INLINE complex<codi::ActiveType<Tape>> const& operator+(complex<codi::ActiveType<Tape>> const& arg) {
394 return arg;
395 }
396
397 #define FUNCTION operator-
398 #define OPERATION_LOGIC codi::OperationUnaryMinus
399 #include "unaryComplexToComplexStdSpecialization.tpp"
400
401 #define FUNCTION abs
402 #define OPERATION_LOGIC codi::OperationComplexAbs
403 #include "unaryComplexToRealStdSpecialization.tpp"
404
405 #define FUNCTION acos
406 #define OPERATION_LOGIC codi::OperationAcos
407 #include "unaryComplexToComplexStdSpecialization.tpp"
408
409 #define FUNCTION acosh
410 #define OPERATION_LOGIC codi::OperationAcosh
411 #include "unaryComplexToComplexStdSpecialization.tpp"
412
413 #define FUNCTION arg
414 #define OPERATION_LOGIC codi::OperationComplexArg
415 #include "unaryComplexToRealStdSpecialization.tpp"
416
417 #define FUNCTION asin
418 #define OPERATION_LOGIC codi::OperationAsin
419 #include "unaryComplexToComplexStdSpecialization.tpp"
420
421 #define FUNCTION asinh
422 #define OPERATION_LOGIC codi::OperationAsinh
423 #include "unaryComplexToComplexStdSpecialization.tpp"
424
425 #define FUNCTION atan
426 #define OPERATION_LOGIC codi::OperationAtan
427 #include "unaryComplexToComplexStdSpecialization.tpp"
428
429 #define FUNCTION atanh
430 #define OPERATION_LOGIC codi::OperationAtanh
431 #include "unaryComplexToComplexStdSpecialization.tpp"
432
433 #define FUNCTION conj
434 #define OPERATION_LOGIC codi::OperationComplexConj
435 #include "unaryComplexToComplexStdSpecialization.tpp"
436
437 #define FUNCTION cos
438 #define OPERATION_LOGIC codi::OperationCos
439 #include "unaryComplexToComplexStdSpecialization.tpp"
440
441 #define FUNCTION cosh
442 #define OPERATION_LOGIC codi::OperationCosh
443 #include "unaryComplexToComplexStdSpecialization.tpp"
444
445 #define FUNCTION exp
446 #define OPERATION_LOGIC codi::OperationExp
447 #include "unaryComplexToComplexStdSpecialization.tpp"
448
449 #define FUNCTION imag
450 #define OPERATION_LOGIC codi::OperationComplexImag
451 #include "unaryComplexToRealStdSpecialization.tpp"
452
453 #define FUNCTION log
454 #define OPERATION_LOGIC codi::OperationLog
455 #include "unaryComplexToComplexStdSpecialization.tpp"
456
457 #define FUNCTION log10
458 #define OPERATION_LOGIC codi::OperationLog10
459 #include "unaryComplexToComplexStdSpecialization.tpp"
460
461 #define FUNCTION norm
462 #define OPERATION_LOGIC codi::OperationComplexNorm
463 #include "unaryComplexToRealStdSpecialization.tpp"
464
465 #define FUNCTION proj
466 #define OPERATION_LOGIC codi::OperationComplexProj
467 #include "unaryComplexToComplexStdSpecialization.tpp"
468
469 #define FUNCTION real
470 #define OPERATION_LOGIC codi::OperationComplexReal
471 #include "unaryComplexToRealStdSpecialization.tpp"
472
473 #define FUNCTION sin
474 #define OPERATION_LOGIC codi::OperationSin
475 #include "unaryComplexToComplexStdSpecialization.tpp"
476
477 #define FUNCTION sinh
478 #define OPERATION_LOGIC codi::OperationSinh
479 #include "unaryComplexToComplexStdSpecialization.tpp"
480
481 #define FUNCTION sqrt
482 #define OPERATION_LOGIC codi::OperationSqrt
483 #include "unaryComplexToComplexStdSpecialization.tpp"
484
485 #define FUNCTION tan
486 #define OPERATION_LOGIC codi::OperationTan
487 #include "unaryComplexToComplexStdSpecialization.tpp"
488
489 #define FUNCTION tanh
490 #define OPERATION_LOGIC codi::OperationTanh
491 #include "unaryComplexToComplexStdSpecialization.tpp"
492
494}
495#endif
T_Tape Tape
See complex.
Definition stdComplex.hpp:309
typename InnerActive::Real InnerReal
See ActiveComplex::InnerReal.
Definition stdComplex.hpp:313
complex< typename Tape::Real > Real
See ActiveComplex::Real.
Definition stdComplex.hpp:311
codi::ActiveType< Tape > InnerActive
See ActiveComplex::InnerActive.
Definition stdComplex.hpp:312
inline ~complex()=default
Destructor.
inlinecomplex & operator=(complex const &value)
< Use Base assign operators.
Definition stdComplex.hpp:326
codi::ActiveComplex< codi::ActiveType< Tape >, complex< codi::ActiveType< T_Tape > > > Base
Base class abbreviation.
Definition stdComplex.hpp:316
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:469
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:97
#define CODI_ANY
Used in default declarations of expression templates.
Definition macros.hpp:101
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:117
typename ActiveResultImpl< Real, Tape, isStatic >::ActiveResult ActiveResult
Definition expressionTraits.hpp:152
Traits for values that can be used as real values, e.g. double, float, codi::RealReverse etc....
Definition stdComplex.hpp:242
typename TraitsImplementation< Type >::PassiveReal PassiveReal
The original computation type that was used in the application.
Definition realTraits.hpp:123
CoDiPack - Code Differentiation Package.
Definition codi.hpp:97
typename std::conditional< std::is_same< Self, Impl >::value, SelfType, Impl >::type ResolveSelf
Definition self.hpp:55
inlineauto arg(ExpressionInterface< std::complex< Real >, Arg > const &arg)
Function overload for FUNCTION.
Definition allOperators.hpp:75
Definition stdComplex.hpp:107
inlineActiveComplex & operator=(ExpressionInterface< InnerReal, ArgR > const &argR)
Assign operation for the inner type of the complex number.
Definition stdComplex.hpp:200
void real(ExpressionInterface< InnerReal, Arg > const &arg)
Update the real part of the complex number.
Definition stdComplex.hpp:219
RealTraits::PassiveReal< InnerReal > PassiveInnerReal
Passive inner value.
Definition stdComplex.hpp:115
AssignmentOperators< InnerReal, false, ResolveSelf< Impl, ActiveComplex > > AssignReal
Definition stdComplex.hpp:118
std::complex< InnerReal > Real
Complex value with the inner real.
Definition stdComplex.hpp:114
void imag(ExpressionInterface< InnerReal, Arg > const &arg)
Update the imaginary part of the complex number.
Definition stdComplex.hpp:232
inline ~ActiveComplex()=default
Destructor.
typename InnerActive::Real InnerReal
Inner real value.
Definition stdComplex.hpp:113
InnerActive value_type
std::complex interface.
Definition stdComplex.hpp:126
inlineActiveComplex & operator=(PassiveInnerReal const &argR)
Assign operation for the inner passive type.
Definition stdComplex.hpp:208
ResolveSelf< T_Impl, ActiveComplex > Impl
See ActiveComplex.
Definition stdComplex.hpp:111
AggregatedActiveType< Real, InnerActive, Impl > Base
Abbreviation for the base class.
Definition stdComplex.hpp:117
void imag(InnerReal const &arg)
Update the imaginary part of the complex number.
Definition stdComplex.hpp:237
T_InnerActive InnerActive
See ActiveComplex.
Definition stdComplex.hpp:110
void real(InnerReal const &arg)
Update the real part of the complex number.
Definition stdComplex.hpp:224
AssignmentOperators< std::complex< InnerReal >, false, ResolveSelf< Impl, ActiveComplex > > AssignComplex
Definition stdComplex.hpp:121
inlineActiveComplex & operator=(ActiveComplex const &arg)
Assign operation for copy operations.
Definition stdComplex.hpp:192
Represents a concrete lvalue in the CoDiPack expression tree.
Definition activeType.hpp:52
typename Tape::Real Real
InnerActiveType values[Elements]
Array representation.
Definition aggregatedActiveType.hpp:75
Represents a concrete aggregated lvalue int the CoDiPack expression tree.
Definition aggregatedActiveType.hpp:164
AggregatedActiveTypeBase< Real, InnerActive, Impl, false > Base
Definition aggregatedActiveType.hpp:170
inlinevoid store(ExpressionInterface< Real, Rhs > const &rhs, EventHints::Statement const &eventType)
Definition aggregatedActiveType.hpp:224
Provides assignment operators for LhsExpressionInterface implementations.
Definition assignmentOperators.hpp:120
Represents an operator or function with an arbitrary number of arguments in the expression tree.
Definition computeExpression.hpp:371
Base class for all CoDiPack expressions.
Definition expressionInterface.hpp:60
inlineImpl const & cast() const
Cast to the implementation.
Definition expressionInterface.hpp:76
ComputeExpression< InnerType, OperationComplexImag, Impl > ExpressionComplexImag
Expression returned by imag.
Definition stdComplex.hpp:80
ComputeExpression< InnerType, OperationComplexReal, Impl > ExpressionComplexReal
Expression returned by real.
Definition stdComplex.hpp:72
Definition expressionMemberOperations.hpp:65
inlineImpl const & cast() const
Cast to implementation.
Definition expressionMemberOperations.hpp:72
T_Impl Impl
See ExpressionMemberOperations.
Definition expressionMemberOperations.hpp:67
T_InnerReal InnerReal
See ActiveResultImpl.
Definition stdComplex.hpp:276
std::complex< InnerActiveResult > ActiveResult
The resulting active type of an expression.
Definition stdComplex.hpp:284
ExpressionTraits::ActiveResult< InnerReal, Tape, false > InnerActiveResult
Active result of the inner type.
Definition stdComplex.hpp:280
Definition expressionTraits.hpp:142
Full tape interface that supports all features of CoDiPack.
Definition fullTapeInterface.hpp:89
static inlinestd::string getMathRep()
Get the math symbol of the operation. E.g. + for operators and pow() for functions.
Definition stdComplex.hpp:267
static inlinestd::string getMathRep()
Get the math symbol of the operation. E.g. + for operators and pow() for functions.
Definition stdComplex.hpp:251
Methods that access inner values of aggregated types that contain CoDiPack active types.
Definition realTraits.hpp:233
Base implementation of AggregatedTypeTraits for aggregated types that can be defined as an array.
Definition realTraits.hpp:308