CoDiPack  3.0.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-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#include <type_traits>
39
40#include "../config.h"
41#include "../expressions/complex/complexPredef.hpp"
42#include "../misc/macros.hpp"
43#include "../misc/self.hpp"
44#include "expressionTraits.hpp"
45
47namespace codi {
48
49 namespace RealTraits {
50 template<typename T_Type, typename>
52 }
53
54 namespace ComputationTraits {
55
63 template<typename T_Jacobian, typename = void>
65 public:
66 CODI_STATIC_ASSERT(false && std::is_void<T_Jacobian>::value,
67 "Instantiation of unspecialized Jacobian transpose.");
68 using Jacobian = CODI_DD(T_Jacobian, CODI_ANY);
69
70 using Return = CODI_ANY;
71
73 static Return transpose(Jacobian const& jacobian);
74 };
75
78 template<typename Jacobian>
79 CODI_INLINE typename TransposeImpl<Jacobian>::Return transpose(Jacobian const& jacobian) {
81 }
82
91 template<typename T_Lhs, typename T_Rhs, typename = void>
92 struct UpdateImpl {
93 public:
94 using Lhs = CODI_DD(T_Lhs, double);
95 using Rhs = CODI_DD(T_Rhs, double);
96
97 using Return = Lhs&;
98
100 static Return update(Lhs& lhs, Rhs const& rhs) {
101 return lhs += rhs;
102 }
103 };
104
106 template<typename Lhs, typename Rhs>
107 CODI_INLINE typename UpdateImpl<Lhs, Rhs>::Return update(Lhs& lhs, Rhs const& rhs) {
108 return UpdateImpl<Lhs, Rhs>::update(lhs, rhs);
109 }
110 }
111
115#define CODI_CREATE_TRANSPOSE(Type, RType, trans) \
116 template<> \
117 struct ComputationTraits::TransposeImpl<Type> { \
118 public: \
119 using Jacobian = Type; \
120 using Return = RType; \
121 static Return transpose(Type const& jacobian) { \
122 return trans; \
123 } \
124 }
125
129#define CODI_CREATE_UPDATE(LhsType, RhsType, up) \
130 template<> \
131 struct ComputationTraits::UpdateImpl<LhsType, RhsType> { \
132 public: \
133 using Lhs = LhsType; \
134 using Rhs = RhsType; \
135 using Return = LhsType&; \
136 static Return update(Lhs& lhs, Rhs const& rhs) { \
137 return up; \
138 } \
139 }
140
141#ifndef DOXYGEN_DISABLE
142
144 template<typename T>
145 struct ComputationTraits::TransposeImpl<T, typename std::enable_if<std::is_floating_point<T>::value>::type> {
146 public:
147 using Jacobian = T;
148 using Return = T;
149
150 static Return transpose(Jacobian const& jacobian) {
151 return jacobian;
152 }
153 };
154
156 template<typename T>
158 T, std::enable_if_t<
159 ExpressionTraits::isExpression<T> &
160 std::is_floating_point<typename RealTraits::TraitsImplementation<T, void>::PassiveReal>::value>> {
161 public:
162 using Jacobian = T;
163 using Return = T;
164
165 static Return transpose(Jacobian const& jacobian) {
166 return jacobian;
167 }
168 };
169
171 template<typename Inner>
172 struct ComputationTraits::TransposeImpl<std::complex<Inner>> {
173 public:
174 using Jacobian = std::complex<Inner>;
175 using Return = std::complex<Inner>;
176
177 static Return transpose(Jacobian const& jacobian) {
178 return std::conj(jacobian);
179 }
180 };
181
183 template<typename Inner>
185 public:
186 using Jacobian = ActiveComplex<Inner>;
187 using Return = ActiveComplex<Inner>;
188
189 static Return transpose(Jacobian const& jacobian) {
190 return conj(jacobian);
191 }
192 };
193
194 CODI_CREATE_TRANSPOSE(int, int, jacobian);
195
199 template<typename Inner>
200 struct ComputationTraits::UpdateImpl<Inner, std::complex<Inner>> {
201 public:
202 using Lhs = Inner;
203 using Rhs = std::complex<Inner>;
204
205 using Return = Inner&;
206
207 static Return update(Lhs& lhs, Rhs const& rhs) {
208 return lhs += std::real(rhs);
209 }
210 };
211
214 template<typename Inner>
215 struct ComputationTraits::UpdateImpl<Inner, ActiveComplex<Inner>> {
216 public:
217 using Lhs = Inner;
218 using Rhs = ActiveComplex<Inner>;
219
220 using Return = Inner&;
221
222 static Return update(Lhs& lhs, Rhs const& rhs) {
223 return lhs += real(rhs);
224 }
225 };
226#endif
227}
#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_STATIC_ASSERT(cond, message)
Static assert definition for CoDiPack. Not evaluated in IDE mode.
Definition macros.hpp:129
Traits for values that can be used as real values, e.g. double, float, codi::RealReverse etc....
Definition stdComplex.hpp:204
CoDiPack - Code Differentiation Package.
Definition codi.hpp:94
inlineauto conj(ExpressionInterface< Real, Arg > const &arg)
Function overload for FUNCTION.
Definition allOperators.hpp:71
CODI_CREATE_TRANSPOSE(Vector4, Vector4Transpose, jacobian.transpose())
Transpose operation for the vector type.
inlineauto real(ExpressionInterface< std::complex< Real >, Arg > const &arg)
Function overload for FUNCTION.
Definition allOperators.hpp:75
Definition stdComplex.hpp:107
Perform or if entries are complex. No default implementation available.
Definition computationTraits.hpp:64
T_Jacobian Jacobian
See TransposeImpl.
Definition computationTraits.hpp:68
static Return transpose(Jacobian const &jacobian)
Perform or if entries are complex.
int Return
Deduced return type.
Definition computationTraits.hpp:70
Perform the operation lhs += rhs. Default logic uses operator +=.
Definition computationTraits.hpp:92
static Return update(Lhs &lhs, Rhs const &rhs)
Perform lhs += rhs.
Definition computationTraits.hpp:100
T_Rhs Rhs
See UpdateImpl.
Definition computationTraits.hpp:95
T_Lhs Lhs
See UpdateImpl.
Definition computationTraits.hpp:94
Lhs & Return
Deduced return type.
Definition computationTraits.hpp:97
Default implementation of the Jacobian interface.
Definition jacobian.hpp:60
Common traits for all types used as real values.
Definition realTraits.hpp:66