CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
computationTraits.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2024 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#include <type_traits>
39
40#include "../config.h"
41#include "../misc/macros.hpp"
42#include "expressionTraits.hpp"
43
45namespace codi {
46
47 namespace ComputationTraits {
48
60 template<typename T_Outer, typename T_Inner, typename = void>
62 public:
63 CODI_STATIC_ASSERT(false && std::is_void<T_Outer>::value, "Instantiation of unspecialized adjoint conversion.");
64 using Outer = CODI_DD(T_Outer, CODI_ANY);
65 using Inner = CODI_DD(T_Inner, CODI_ANY);
66
67 using Return = CODI_ANY;
68
70 static Return adjointConversion(Inner const& jacobian);
71 };
72
78 template<typename Outer, typename Inner>
79 CODI_INLINE typename AdjointConversionImpl<Outer, Inner>::Return adjointConversion(Inner const& jacobian) {
81 }
82
90 template<typename T_Jacobian, typename = void>
92 public:
93 CODI_STATIC_ASSERT(false && std::is_void<T_Jacobian>::value,
94 "Instantiation of unspecialized Jacobian transpose.");
95 using Jacobian = CODI_DD(T_Jacobian, CODI_ANY);
96
97 using Return = CODI_ANY;
98
100 static Return transpose(Jacobian const& jacobian);
101 };
102
105 template<typename Jacobian>
106 CODI_INLINE typename TransposeImpl<Jacobian>::Return transpose(Jacobian const& jacobian) {
107 return TransposeImpl<Jacobian>::transpose(jacobian);
108 }
109
118 template<typename T_Lhs, typename T_Rhs, typename = void>
119 struct UpdateImpl {
120 public:
121 using Lhs = CODI_DD(T_Lhs, double);
122 using Rhs = CODI_DD(T_Rhs, double);
123
124 using Return = Lhs&;
125
127 static Return update(Lhs& lhs, Rhs const& rhs) {
128 return lhs += rhs;
129 }
130 };
131
133 template<typename Lhs, typename Rhs>
134 CODI_INLINE typename UpdateImpl<Lhs, Rhs>::Return update(Lhs& lhs, Rhs const& rhs) {
135 return UpdateImpl<Lhs, Rhs>::update(lhs, rhs);
136 }
137 }
138
142#define CODI_CREATE_ADJOINT_CONVERSION(OuterType, InnerType, RType, conversion) \
143 template<> \
144 struct ComputationTraits::AdjointConversionImpl<OuterType, InnerType> { \
145 public: \
146 using Outer = OuteType; \
147 using Inner = InnerType; \
148 using Return = RType; \
149 static Return adjointConversion(Inner const& jacobian) { \
150 return conversion; \
151 } \
152 }
153
157#define CODI_CREATE_TRANSPOSE(Type, RType, trans) \
158 template<> \
159 struct ComputationTraits::Transpose<Type> { \
160 public: \
161 using Jacobian = Type; \
162 using Return = RType; \
163 static Return transpose(Type const& jacobian) { \
164 return trans; \
165 } \
166 }
167
171#define CODI_CREATE_UPDATE(LhsType, RhsType, up) \
172 template<> \
173 struct ComputationTraits::UpdateImpl<LhsType, RhsType> { \
174 public: \
175 using Lhs = LhsType; \
176 using Rhs = RhsType; \
177 using Return = LhsType&; \
178 static Return update(Lhs& lhs, Rhs const& rhs) { \
179 return up; \
180 } \
181 }
182
183#ifndef DOXYGEN_DISABLE
184
186 template<typename Type>
187 struct ComputationTraits::AdjointConversionImpl<Type, Type> {
188 public:
189 using Outer = Type;
190 using Inner = Type;
191
192 using Return = Type;
193
194 static Return adjointConversion(Inner const& jacobian) {
195 return jacobian;
196 }
197 };
198
200 template<typename T_Outer, typename T_Inner>
201 struct ComputationTraits::AdjointConversionImpl<
202 T_Outer, T_Inner,
203 typename std::enable_if<!std::is_same<T_Outer, T_Inner>::value &
204 ExpressionTraits::IsExpression<T_Inner>::value>::type> {
205 public:
206
207 using Outer = T_Outer;
208 using Inner = T_Inner;
209
210 using InnerActive = typename Inner::ActiveResult;
211 using InnerActiveConversion = ComputationTraits::AdjointConversionImpl<Outer, InnerActive>;
212
213 using Return = typename InnerActiveConversion::Return;
214
215 static Return adjointConversion(Inner const& jacobian) {
216 return InnerActiveConversion::adjointConversion(jacobian);
217 }
218 };
219
221 template<typename Type>
222 struct ComputationTraits::AdjointConversionImpl<Type, std::complex<Type>> {
223 public:
224 using Outer = Type;
225 using Inner = std::complex<Type>;
226
227 using Return = Type;
228
229 static Return adjointConversion(Inner const& jacobian) {
230 return std::real(jacobian);
231 }
232 };
233
235 template<typename T>
236 struct ComputationTraits::TransposeImpl<T, typename std::enable_if<std::is_floating_point<T>::value>::type> {
237 public:
238 using Jacobian = T;
239 using Return = T;
240
241 static Return transpose(Jacobian const& jacobian) {
242 return jacobian;
243 }
244 };
245
247 template<typename Inner>
248 struct ComputationTraits::TransposeImpl<std::complex<Inner>> {
249 public:
250 using Jacobian = std::complex<Inner>;
251 using Return = std::complex<Inner>;
252
253 static Return transpose(Jacobian const& jacobian) {
254 return std::conj(jacobian);
255 }
256 };
257
261 template<typename Inner>
262 struct ComputationTraits::UpdateImpl<Inner, std::complex<Inner>> {
263 public:
264 using Lhs = Inner;
265 using Rhs = std::complex<Inner>;
266
267 using Return = Inner&;
268
269 static Return update(Lhs& lhs, Rhs const& rhs) {
270 return lhs += std::real(rhs);
271 }
272 };
273#endif
274}
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:457
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
#define CODI_ANY
Used in default declarations of expression templates.
Definition macros.hpp:98
#define CODI_STATIC_ASSERT(cond, message)
Static assert in CoDiPack.
Definition macros.hpp:123
CoDiPack - Code Differentiation Package.
Definition codi.hpp:90
Perform the conversion of Outer(Inner) in the adjoint context. Default implementation for Outer == In...
Definition computationTraits.hpp:61
int Return
Deduced return type.
Definition computationTraits.hpp:67
T_Inner Inner
See ReduceImpl.
Definition computationTraits.hpp:65
T_Outer Outer
See ReduceImpl.
Definition computationTraits.hpp:64
static Return adjointConversion(Inner const &jacobian)
Perform the adjoint of Outer(Inner).
Perform or if entries are complex. No default implementation available.
Definition computationTraits.hpp:91
T_Jacobian Jacobian
See TransposeImpl.
Definition computationTraits.hpp:95
int Return
Deduced return type.
Definition computationTraits.hpp:97
static Return transpose(Jacobian const &jacobian)
Perform or if entries are complex.
Perform the operation lhs += rhs. Default logic uses operator +=.
Definition computationTraits.hpp:119
static Return update(Lhs &lhs, Rhs const &rhs)
Perform lhs += rhs.
Definition computationTraits.hpp:127
T_Rhs Rhs
See UpdateImpl.
Definition computationTraits.hpp:122
T_Lhs Lhs
See UpdateImpl.
Definition computationTraits.hpp:121
Lhs & Return
Deduced return type.
Definition computationTraits.hpp:124
Default implementation of the Jacobian interface.
Definition jacobian.hpp:60