CoDiPack  3.0.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-2025 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
141 template<typename ArgR>
146
149 Base::values[0] = argR;
151 }
152
154 template<typename ArgR, typename ArgI>
161
163 template<typename ArgI>
165 : Base() {
166 Base::values[0] = argR;
167 Base::values[1] = argI.cast();
168 }
169
171 template<typename ArgR>
173 : Base() {
174 Base::values[0] = argR.cast();
175 Base::values[1] = argI;
176 }
177
180 Base::values[0] = argR;
181 Base::values[1] = argI;
182 }
183
185
187 template<typename ArgR>
189 Base::values[0] = argR.cast();
191
192 return *this;
193 }
194
197 Base::values[0] = argR;
199
200 return *this;
201 }
202 };
203
204 namespace RealTraits {
205
207 template<typename T_InnerReal>
208 struct AggregatedTypeTraits<std::complex<T_InnerReal>>
209 : public ArrayAggregatedTypeTraitsBase<std::complex<T_InnerReal>, T_InnerReal,
210 std::complex<RealTraits::Real<T_InnerReal>>, 2> {
211 public:
213 static CODI_INLINE std::string getMathRep() {
214 return "complex()";
215 }
216 };
217
219 template<typename T_InnerReal>
222 ActiveComplex<T_InnerReal>, T_InnerReal,
223 typename std::conditional<std::is_floating_point<RealTraits::Real<T_InnerReal>>::value,
224 std::complex<RealTraits::Real<T_InnerReal>>,
225 ActiveComplex<RealTraits::Real<T_InnerReal>>>::type,
226 2> {
227 public:
229 static CODI_INLINE std::string getMathRep() {
230 return "complex()";
231 }
232 };
233 }
234
236 template<typename T_InnerReal, typename T_Tape>
237 struct ExpressionTraits::ActiveResultImpl<std::complex<T_InnerReal>, T_Tape, false> {
238 using InnerReal = CODI_DD(T_InnerReal, CODI_ANY);
239 using Tape = CODI_DD(T_Tape, CODI_ANY);
240
243
245#if CODI_SpecializeStdComplex
246 using ActiveResult = std::complex<InnerActiveResult>;
247#else
249#endif
250 };
251}
252
253#if CODI_SpecializeStdComplex
254namespace std {
255
256 /*******************************************************************************/
259
265 template<typename T_Tape>
266 class complex<codi::ActiveType<T_Tape>>
267 : public codi::ActiveComplex<codi::ActiveType<T_Tape>, complex<codi::ActiveType<T_Tape>>> {
268 public:
269
272
273 using Real = complex<typename Tape::Real>;
275 using InnerReal = typename InnerActive::Real;
276
278 using Base = codi::ActiveComplex<codi::ActiveType<Tape>, complex<codi::ActiveType<T_Tape>>>;
279
280 using Base::Base;
281 CODI_INLINE complex(complex const& value) : Base(value) {}
282
283 CODI_INLINE ~complex() = default;
284
285 using Base::operator=;
286
288 CODI_INLINE complex& operator=(complex const& value) {
289 Base::store(value, codi::EventHints::Statement::Copy);
290
291 return *this;
292 }
293 };
294
296 template<typename Tape>
297 CODI_INLINE std::ostream& operator<<(std::ostream& out, std::complex<codi::ActiveType<Tape>> const& v) {
298 out << v.getValue();
299
300 return out;
301 }
302
304 /*******************************************************************************/
316
317 #define FUNCTION operator+
318 #define OPERATION_LOGIC codi::OperationAdd
319 #include "binaryComplexToComplexStdSpecialization.tpp"
320
321 #define FUNCTION operator-
322 #define OPERATION_LOGIC codi::OperationSubstract
323 #include "binaryComplexToComplexStdSpecialization.tpp"
324
325 #define FUNCTION operator*
326 #define OPERATION_LOGIC codi::OperationMultiply
327 #include "binaryComplexToComplexStdSpecialization.tpp"
328
329 #define FUNCTION operator/
330 #define OPERATION_LOGIC codi::OperationDivide
331 #include "binaryComplexToComplexStdSpecialization.tpp"
332
333 // polar needs to be created in the codi namespace.
334
335 #define FUNCTION pow
336 #define OPERATION_LOGIC codi::OperationPow
337 #include "binaryComplexToComplexStdSpecialization.tpp"
338
340 /*******************************************************************************/
352
354 template<typename Tape>
355 CODI_INLINE complex<codi::ActiveType<Tape>> const& operator+(complex<codi::ActiveType<Tape>> const& arg) {
356 return arg;
357 }
358
359 #define FUNCTION operator-
360 #define OPERATION_LOGIC codi::OperationUnaryMinus
361 #include "unaryComplexToComplexStdSpecialization.tpp"
362
363 #define FUNCTION abs
364 #define OPERATION_LOGIC codi::OperationComplexAbs
365 #include "unaryComplexToRealStdSpecialization.tpp"
366
367 #define FUNCTION acos
368 #define OPERATION_LOGIC codi::OperationAcos
369 #include "unaryComplexToComplexStdSpecialization.tpp"
370
371 #define FUNCTION acosh
372 #define OPERATION_LOGIC codi::OperationAcosh
373 #include "unaryComplexToComplexStdSpecialization.tpp"
374
375 #define FUNCTION arg
376 #define OPERATION_LOGIC codi::OperationComplexArg
377 #include "unaryComplexToRealStdSpecialization.tpp"
378
379 #define FUNCTION asin
380 #define OPERATION_LOGIC codi::OperationAsin
381 #include "unaryComplexToComplexStdSpecialization.tpp"
382
383 #define FUNCTION asinh
384 #define OPERATION_LOGIC codi::OperationAsinh
385 #include "unaryComplexToComplexStdSpecialization.tpp"
386
387 #define FUNCTION atan
388 #define OPERATION_LOGIC codi::OperationAtan
389 #include "unaryComplexToComplexStdSpecialization.tpp"
390
391 #define FUNCTION atanh
392 #define OPERATION_LOGIC codi::OperationAtanh
393 #include "unaryComplexToComplexStdSpecialization.tpp"
394
395 #define FUNCTION conj
396 #define OPERATION_LOGIC codi::OperationComplexConj
397 #include "unaryComplexToComplexStdSpecialization.tpp"
398
399 #define FUNCTION cos
400 #define OPERATION_LOGIC codi::OperationCos
401 #include "unaryComplexToComplexStdSpecialization.tpp"
402
403 #define FUNCTION cosh
404 #define OPERATION_LOGIC codi::OperationCosh
405 #include "unaryComplexToComplexStdSpecialization.tpp"
406
407 #define FUNCTION exp
408 #define OPERATION_LOGIC codi::OperationExp
409 #include "unaryComplexToComplexStdSpecialization.tpp"
410
411 #define FUNCTION imag
412 #define OPERATION_LOGIC codi::OperationComplexImag
413 #include "unaryComplexToRealStdSpecialization.tpp"
414
415 #define FUNCTION log
416 #define OPERATION_LOGIC codi::OperationLog
417 #include "unaryComplexToComplexStdSpecialization.tpp"
418
419 #define FUNCTION log10
420 #define OPERATION_LOGIC codi::OperationLog10
421 #include "unaryComplexToComplexStdSpecialization.tpp"
422
423 #define FUNCTION norm
424 #define OPERATION_LOGIC codi::OperationComplexNorm
425 #include "unaryComplexToRealStdSpecialization.tpp"
426
427 #define FUNCTION proj
428 #define OPERATION_LOGIC codi::OperationComplexProj
429 #include "unaryComplexToComplexStdSpecialization.tpp"
430
431 #define FUNCTION real
432 #define OPERATION_LOGIC codi::OperationComplexReal
433 #include "unaryComplexToRealStdSpecialization.tpp"
434
435 #define FUNCTION sin
436 #define OPERATION_LOGIC codi::OperationSin
437 #include "unaryComplexToComplexStdSpecialization.tpp"
438
439 #define FUNCTION sinh
440 #define OPERATION_LOGIC codi::OperationSinh
441 #include "unaryComplexToComplexStdSpecialization.tpp"
442
443 #define FUNCTION sqrt
444 #define OPERATION_LOGIC codi::OperationSqrt
445 #include "unaryComplexToComplexStdSpecialization.tpp"
446
447 #define FUNCTION tan
448 #define OPERATION_LOGIC codi::OperationTan
449 #include "unaryComplexToComplexStdSpecialization.tpp"
450
451 #define FUNCTION tanh
452 #define OPERATION_LOGIC codi::OperationTanh
453 #include "unaryComplexToComplexStdSpecialization.tpp"
454
456}
457#endif
T_Tape Tape
See complex.
Definition stdComplex.hpp:271
typename InnerActive::Real InnerReal
See ActiveComplex::InnerReal.
Definition stdComplex.hpp:275
complex< typename Tape::Real > Real
See ActiveComplex::Real.
Definition stdComplex.hpp:273
codi::ActiveType< Tape > InnerActive
See ActiveComplex::InnerActive.
Definition stdComplex.hpp:274
inline ~complex()=default
Destructor.
inlinecomplex & operator=(complex const &value)
< Use Base assign operators.
Definition stdComplex.hpp:288
codi::ActiveComplex< codi::ActiveType< Tape >, complex< codi::ActiveType< T_Tape > > > Base
Base class abbreviation.
Definition stdComplex.hpp:278
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:469
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:96
#define CODI_ANY
Used in default declarations of expression templates.
Definition macros.hpp:100
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:116
typename ActiveResultImpl< Real, Tape, isStatic >::ActiveResult ActiveResult
Definition expressionTraits.hpp:137
Traits for values that can be used as real values, e.g. double, float, codi::RealReverse etc....
Definition stdComplex.hpp:204
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:94
typename std::conditional< std::is_same< Self, Impl >::value, SelfType, Impl >::type ResolveSelf
Definition self.hpp:55
Definition stdComplex.hpp:107
inlineActiveComplex & operator=(ExpressionInterface< InnerReal, ArgR > const &argR)
Assign operation for the inner type of the complex number.
Definition stdComplex.hpp:188
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
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:196
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
T_InnerActive InnerActive
See ActiveComplex.
Definition stdComplex.hpp:110
AssignmentOperators< std::complex< InnerReal >, false, ResolveSelf< Impl, ActiveComplex > > AssignComplex
Definition stdComplex.hpp:121
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)
Has to be called by an AD variable every time it is assigned.
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:238
std::complex< InnerActiveResult > ActiveResult
The resulting active type of an expression.
Definition stdComplex.hpp:246
ExpressionTraits::ActiveResult< InnerReal, Tape, false > InnerActiveResult
Active result of the inner type.
Definition stdComplex.hpp:242
Definition expressionTraits.hpp:127
Full tape interface that supports all features of CoDiPack.
Definition fullTapeInterface.hpp:84
static inlinestd::string getMathRep()
Get the math symbol of the operation. E.g. + for operators and pow() for functions.
Definition stdComplex.hpp:229
static inlinestd::string getMathRep()
Get the math symbol of the operation. E.g. + for operators and pow() for functions.
Definition stdComplex.hpp:213
Methods that access inner values of aggregated types that contain CoDiPack active types.
Definition realTraits.hpp:226
Base implementation of AggregatedTypeTraits for aggregated types that can be defined as an array.
Definition realTraits.hpp:301