CoDiPack  3.1.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
expressionTraits.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 <type_traits>
38
39#include "../config.h"
40#include "../expressions/logic/compileTimeTraversalLogic.hpp"
41#include "../misc/macros.hpp"
42#include "misc/enableIfHelpers.hpp"
43
45namespace codi {
46
47 template<typename T_Real, typename T_Impl>
49
50 template<typename T_Real, typename T_Gradient, typename T_Tape, typename T_Impl>
52
53 template<typename T_Real, template<typename> class T_ConversionOperator>
54 struct ConstantExpression;
55
56 template<typename T_Tape>
58
59 namespace RealTraits {
60 template<typename T_Real, typename>
62 }
63
64 template<typename T_Real>
65 struct EmptyOperation;
66
67 template<typename T_Real, template<typename> class T_Operation, typename... T_ArgExprs>
68 struct ComputeExpression;
69
71 namespace ExpressionTraits {
72
73 /*******************************************************************************/
76
83 template<typename... Logic>
85
87 template<>
89 public:
90
92 using ADLogic = void;
93 };
94
96 template<typename Logic>
97 struct ValidateADLogicImpl<Logic> {
98 public:
99
101 using ADLogic = Logic;
102 };
103
105 template<typename LogicA, typename LogicB>
106 struct ValidateADLogicImpl<LogicA, LogicB> {
107 private:
108 static bool constexpr isAVoid = std::is_same<void, LogicA>::value;
109 static bool constexpr isBVoid = std::is_same<void, LogicB>::value;
110 static bool constexpr isBothVoid = isAVoid & isBVoid;
111 static bool constexpr isBothSame = std::is_same<LogicA, LogicB>::value;
112
113 // Either one can be void (aka. constant value) but not both otherwise both need to be the same.
114 CODI_STATIC_ASSERT((!isBothVoid) & (!isAVoid | !isBVoid | isBothSame), "AD logic types need to be the same.");
115
116 public:
117
119 using ADLogic = typename std::conditional<isBVoid, LogicA, LogicB>::type;
120 };
121
123 template<typename LogicA, typename... LogicOther>
124 struct ValidateADLogicImpl<LogicA, LogicOther...> {
125 public:
126
128 using ADLogic =
129 typename ValidateADLogicImpl<LogicA, typename ValidateADLogicImpl<LogicOther...>::ADLogic>::ADLogic;
130 };
131
133 template<typename... Results>
135
141 template<typename T_Real, typename T_Tape, bool T_isStatic = false, typename = void>
143 using Real = CODI_DD(T_Real, CODI_ANY);
144 using Tape = CODI_DD(T_Tape, CODI_ANY);
145
148 };
149
151 template<typename Real, typename Tape, bool isStatic = false>
153
155 /*******************************************************************************/
158
160 template<typename Expr, typename = void>
161 struct IsExpression : std::false_type {};
162
163#ifndef DOXYGEN_DISABLE
164 template<typename Expr>
165 struct IsExpression<Expr, typename enable_if_base_of<ExpressionInterface<typename Expr::Real, Expr>, Expr>::type>
166 : std::true_type {};
167#endif
168
170 template<typename Expr>
172
174 template<typename Expr, typename T = void>
175 using EnableIfExpression = typename std::enable_if<IsExpression<Expr>::value, T>::type;
176
178 template<typename Expr, typename = void>
179 struct IsLhsExpression : std::false_type {};
180
181#ifndef DOXYGEN_DISABLE
182 template<typename Expr>
183 struct IsLhsExpression<
184 Expr, typename enable_if_base_of<
185 LhsExpressionInterface<typename Expr::Real, typename Expr::Gradient, typename Expr::Tape, Expr>,
186 Expr>::type> : std::true_type {};
187
188 template<typename Tape>
189 struct IsLhsExpression<StaticContextActiveType<Tape>> : std::true_type {};
190#endif
191
193 template<typename Expr>
195
197 template<typename Expr, typename T = void>
198 using EnableIfLhsExpression = typename std::enable_if<IsLhsExpression<Expr>::value, T>::type;
199
201 template<typename Expr>
202 struct IsConstantExpression : std::false_type {};
203
204#ifndef DOXYGEN_DISABLE
205 template<typename Real, template<typename> class ConversionOperator>
206 struct IsConstantExpression<ConstantExpression<Real, ConversionOperator>> : std::true_type {};
207#endif
208
209 template<typename Expr>
212
214 template<typename Expr, typename T = void>
215 using EnableIfConstantExpression = typename std::enable_if<IsConstantExpression<Expr>::value, T>::type;
216
218 template<typename Expr>
219 struct IsEmptyExpression : std::false_type {};
220
221#ifndef DOXYGEN_DISABLE
222 template<typename Real>
223 struct IsEmptyExpression<ComputeExpression<Real, EmptyOperation>> : std::true_type {};
224#endif
225
226 template<typename Expr>
229
231 template<typename Expr, typename T = void>
232 using EnableIfEmptyExpression = typename std::enable_if<IsEmptyExpression<Expr>::value, T>::type;
233
235 template<typename Expr>
236 struct IsStaticContextActiveType : std::false_type {};
237
238#ifndef DOXYGEN_DISABLE
239 template<typename Tape>
240 struct IsStaticContextActiveType<StaticContextActiveType<Tape>> : std::true_type {};
241#endif
242
244 template<typename Expr>
246
248 template<typename Expr, typename T = void>
249 using EnableIfStaticContextActiveType = typename std::enable_if<IsStaticContextActiveType<Expr>::value, T>::type;
250
252 /*******************************************************************************/
255
257 template<typename Expr>
258 struct NumberOfActiveTypeArguments : public CompileTimeTraversalLogic<size_t, NumberOfActiveTypeArguments<Expr>> {
259 public:
260
263
265 template<typename Node, typename = ExpressionTraits::EnableIfLhsExpression<Node>>
266 CODI_INLINE static size_t constexpr leaf() {
267 return 1;
268 }
270
272 static size_t constexpr value = Base::template eval<Expr>();
273 };
274
276 template<typename Expr>
278
280 template<typename Expr>
282 : public CompileTimeTraversalLogic<size_t, NumberOfConstantTypeArguments<Expr>> {
283 public:
284
287
289 template<typename Node, typename = EnableIfConstantExpression<Node>>
290 CODI_INLINE static size_t constexpr leaf() {
291 return ::codi::RealTraits::AggregatedTypeTraits<typename Node::Real, void>::Elements;
292 }
294
296 static size_t constexpr value = Base::template eval<Expr>();
297 };
298
300 template<typename Expr>
302
304 template<typename Expr>
305 struct NumberOfOperations : public CompileTimeTraversalLogic<size_t, NumberOfOperations<Expr>> {
306 public:
307
309 template<typename Node>
310 CODI_INLINE static size_t constexpr node() {
311 return 1 + NumberOfOperations::template toLinks<Node>();
312 }
313
315 static size_t constexpr value = NumberOfOperations::template eval<Expr>();
316 };
317
319 template<typename Expr>
321
323 }
324}
#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_STATIC_ASSERT(cond, message)
Static assert definition for CoDiPack. Not evaluated in IDE mode.
Definition macros.hpp:130
Traits for everything that can be an expression e.g. codi::RealReverse, a + b, etc....
Definition expressionTraits.hpp:71
ValidateADLogicImpl< Results... > ValidateADLogic
Validates if the AD logic of an arbitrary amount of expressions are the same or compatible....
Definition expressionTraits.hpp:134
bool constexpr numberOfConstantTypeArguments
Value entry of NumberOfConstantTypeArguments.
Definition expressionTraits.hpp:301
typename std::enable_if< IsStaticContextActiveType< Expr >::value, T >::type EnableIfStaticContextActiveType
Enable if wrapper for IsStaticContextActiveType.
Definition expressionTraits.hpp:249
bool constexpr isEmptyExpression
Value entry of IsEmptyExpression.
Definition expressionTraits.hpp:228
bool constexpr numberOfOperations
Value entry of NumberOfOperations.
Definition expressionTraits.hpp:320
bool constexpr isConstantExpression
Value entry of IsConstantExpression.
Definition expressionTraits.hpp:211
typename std::enable_if< IsEmptyExpression< Expr >::value, T >::type EnableIfEmptyExpression
Enable if wrapper for IsEmptyExpression.
Definition expressionTraits.hpp:232
typename std::enable_if< IsExpression< Expr >::value, T >::type EnableIfExpression
Enable if wrapper for IsExpression.
Definition expressionTraits.hpp:175
bool constexpr numberOfActiveTypeArguments
Value entry of NumberOfActiveTypeArguments.
Definition expressionTraits.hpp:277
bool constexpr isLhsExpression
Value entry of IsLhsExpression.
Definition expressionTraits.hpp:194
typename std::enable_if< IsLhsExpression< Expr >::value, T >::type EnableIfLhsExpression
Enable if wrapper for IsLhsExpression.
Definition expressionTraits.hpp:198
bool constexpr isExpression
Value entry of IsExpression.
Definition expressionTraits.hpp:171
typename std::enable_if< IsConstantExpression< Expr >::value, T >::type EnableIfConstantExpression
Enable if wrapper for IsConstantExpression.
Definition expressionTraits.hpp:215
typename ActiveResultImpl< Real, Tape, isStatic >::ActiveResult ActiveResult
Definition expressionTraits.hpp:152
bool constexpr isStaticContextActiveType
Value entry of IsStaticContextActiveType.
Definition expressionTraits.hpp:245
Traits for values that can be used as real values, e.g. double, float, codi::RealReverse etc....
Definition stdComplex.hpp:242
CoDiPack - Code Differentiation Package.
Definition codi.hpp:97
std::enable_if< std::is_base_of< Base, Impl >::value, R > enable_if_base_of
Enable if abbreviation for std::is_base_of.
Definition enableIfHelpers.hpp:47
Traversal of CoDiPack expressions during compile time.
Definition compileTimeTraversalLogic.hpp:59
inlinestatic ResultType constexpr eval(Args &&... args)
Definition compileTimeTraversalLogic.hpp:71
inlinestatic ResultType constexpr toLinks(Args &&... args)
Definition compileTimeTraversalLogic.hpp:191
Represents an operator or function with an arbitrary number of arguments in the expression tree.
Definition computeExpression.hpp:371
Represents constant values in the expression tree.
Definition constantExpression.hpp:78
Operation for empty expression.
Definition emptyExpression.hpp:52
Base class for all CoDiPack expressions.
Definition expressionInterface.hpp:60
Definition expressionTraits.hpp:142
int ActiveResult
The resulting active type of an expression.
Definition expressionTraits.hpp:147
T_Tape Tape
See ActiveResultImpl.
Definition expressionTraits.hpp:144
T_Real Real
See ActiveResultImpl.
Definition expressionTraits.hpp:143
If the expression inherits from ConstantExpression. Is either std::false_type or std::true_type.
Definition expressionTraits.hpp:202
If the expression inherits from EmptyExpression. Is either std::false_type or std::true_type.
Definition expressionTraits.hpp:219
If the expression inherits from ExpressionInterface. Is either std::false_type or std::true_type.
Definition expressionTraits.hpp:161
If the expression inherits from LhsExpressionInterface. Is either std::false_type or std::true_type.
Definition expressionTraits.hpp:179
If the expression inherits from StaticContextActiveType. Is either std::false_type or std::true_type.
Definition expressionTraits.hpp:236
Counts the number of nodes that inherit from LhsExpressionInterface in the expression.
Definition expressionTraits.hpp:258
static size_t constexpr value
See NumberOfActiveTypeArguments.
Definition expressionTraits.hpp:272
CompileTimeTraversalLogic< size_t, NumberOfActiveTypeArguments< Expr > > Base
Abbreviation for the base class type.
Definition expressionTraits.hpp:262
inlinestatic size_t constexpr leaf()
Called for all leaf nodes in the expression.
Definition expressionTraits.hpp:266
Counts the number of types that inherit from ConstantExpression in the expression.
Definition expressionTraits.hpp:282
static size_t constexpr value
See NumberOfConstantTypeArguments.
Definition expressionTraits.hpp:296
CompileTimeTraversalLogic< size_t, NumberOfConstantTypeArguments< Expr > > Base
Abbreviation for the base class type.
Definition expressionTraits.hpp:286
inlinestatic size_t constexpr leaf()
Called for all leaf nodes in the expression.
Definition expressionTraits.hpp:290
Counts the number of nodes in the expression.
Definition expressionTraits.hpp:305
inlinestatic size_t constexpr node()
Called for each node in the expression.
Definition expressionTraits.hpp:310
static size_t constexpr value
See NumberOfOperations.
Definition expressionTraits.hpp:315
typename std::conditional< isBVoid, LogicA, LogicB >::type ADLogic
The resulting AD logic type of an expression.
Definition expressionTraits.hpp:119
typename ValidateADLogicImpl< LogicA, typename ValidateADLogicImpl< LogicOther... >::ADLogic >::ADLogic ADLogic
The resulting AD logic type of an expression.
Definition expressionTraits.hpp:128
Logic ADLogic
The resulting AD logic type of an expression.
Definition expressionTraits.hpp:101
void ADLogic
The resulting AD logic type of an expression.
Definition expressionTraits.hpp:92
Validates if the AD logic of an arbitrary amount of expressions are the same or compatible....
Definition expressionTraits.hpp:84
Base class for all CoDiPack lvalue expression.
Definition lhsExpressionInterface.hpp:63
Methods that access inner values of aggregated types that contain CoDiPack active types.
Definition realTraits.hpp:233
Replacement type of LhsExpressionInterface types in ConstructStaticContext.
Definition staticContextActiveType.hpp:56