CoDiPack  3.0.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-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 <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
65 namespace ExpressionTraits {
66
67 /*******************************************************************************/
70
77 template<typename... Logic>
79
81 template<typename Logic>
82 struct ValidateADLogicImpl<Logic> {
83 public:
84
86 using ADLogic = Logic;
87 };
88
90 template<typename LogicA, typename LogicB>
91 struct ValidateADLogicImpl<LogicA, LogicB> {
92 private:
93 static bool constexpr isAVoid = std::is_same<void, LogicA>::value;
94 static bool constexpr isBVoid = std::is_same<void, LogicB>::value;
95 static bool constexpr isBothVoid = isAVoid & isBVoid;
96 static bool constexpr isBothSame = std::is_same<LogicA, LogicB>::value;
97
98 // Either one can be void (aka. constant value) but not both otherwise both need to be the same.
99 CODI_STATIC_ASSERT((!isBothVoid) & (!isAVoid | !isBVoid | isBothSame), "AD logic types need to be the same.");
100
101 public:
102
104 using ADLogic = typename std::conditional<isBVoid, LogicA, LogicB>::type;
105 };
106
108 template<typename LogicA, typename... LogicOther>
109 struct ValidateADLogicImpl<LogicA, LogicOther...> {
110 public:
111
113 using ADLogic =
114 typename ValidateADLogicImpl<LogicA, typename ValidateADLogicImpl<LogicOther...>::ADLogic>::ADLogic;
115 };
116
118 template<typename... Results>
120
126 template<typename T_Real, typename T_Tape, bool T_isStatic = false, typename = void>
128 using Real = CODI_DD(T_Real, CODI_ANY);
129 using Tape = CODI_DD(T_Tape, CODI_ANY);
130
133 };
134
136 template<typename Real, typename Tape, bool isStatic = false>
138
140 /*******************************************************************************/
143
145 template<typename Expr, typename = void>
146 struct IsExpression : std::false_type {};
147
148#ifndef DOXYGEN_DISABLE
149 template<typename Expr>
150 struct IsExpression<Expr, typename enable_if_base_of<ExpressionInterface<typename Expr::Real, Expr>, Expr>::type>
151 : std::true_type {};
152#endif
153
155 template<typename Expr>
157
159 template<typename Expr, typename T = void>
160 using EnableIfExpression = typename std::enable_if<IsExpression<Expr>::value, T>::type;
161
163 template<typename Expr, typename = void>
164 struct IsLhsExpression : std::false_type {};
165
166#ifndef DOXYGEN_DISABLE
167 template<typename Expr>
168 struct IsLhsExpression<
169 Expr, typename enable_if_base_of<
170 LhsExpressionInterface<typename Expr::Real, typename Expr::Gradient, typename Expr::Tape, Expr>,
171 Expr>::type> : std::true_type {};
172
173 template<typename Tape>
174 struct IsLhsExpression<StaticContextActiveType<Tape>> : std::true_type {};
175#endif
176
178 template<typename Expr>
180
182 template<typename Expr, typename T = void>
183 using EnableIfLhsExpression = typename std::enable_if<IsLhsExpression<Expr>::value, T>::type;
184
186 template<typename Expr>
187 struct IsConstantExpression : std::false_type {};
188
189#ifndef DOXYGEN_DISABLE
190 template<typename Real, template<typename> class ConversionOperator>
191 struct IsConstantExpression<ConstantExpression<Real, ConversionOperator>> : std::true_type {};
192#endif
193
194 template<typename Expr>
197
199 template<typename Expr, typename T = void>
200 using EnableIfConstantExpression = typename std::enable_if<IsConstantExpression<Expr>::value, T>::type;
201
203 template<typename Expr>
204 struct IsStaticContextActiveType : std::false_type {};
205
206#ifndef DOXYGEN_DISABLE
207 template<typename Tape>
208 struct IsStaticContextActiveType<StaticContextActiveType<Tape>> : std::true_type {};
209#endif
210
212 template<typename Expr>
214
216 template<typename Expr, typename T = void>
217 using EnableIfStaticContextActiveType = typename std::enable_if<IsStaticContextActiveType<Expr>::value, T>::type;
218
220 /*******************************************************************************/
223
225 template<typename Expr>
226 struct NumberOfActiveTypeArguments : public CompileTimeTraversalLogic<size_t, NumberOfActiveTypeArguments<Expr>> {
227 public:
228
231
233 template<typename Node, typename = ExpressionTraits::EnableIfLhsExpression<Node>>
234 CODI_INLINE static size_t constexpr leaf() {
235 return 1;
236 }
238
240 static size_t constexpr value = Base::template eval<Expr>();
241 };
242
244 template<typename Expr>
246
248 template<typename Expr>
250 : public CompileTimeTraversalLogic<size_t, NumberOfConstantTypeArguments<Expr>> {
251 public:
252
255
257 template<typename Node, typename = EnableIfConstantExpression<Node>>
258 CODI_INLINE static size_t constexpr leaf() {
259 return ::codi::RealTraits::AggregatedTypeTraits<typename Node::Real, void>::Elements;
260 }
262
264 static size_t constexpr value = Base::template eval<Expr>();
265 };
266
268 template<typename Expr>
270
272 template<typename Expr>
273 struct NumberOfOperations : public CompileTimeTraversalLogic<size_t, NumberOfOperations<Expr>> {
274 public:
275
277 template<typename Node>
278 CODI_INLINE static size_t constexpr node() {
279 return 1 + NumberOfOperations::template toLinks<Node>();
280 }
281
283 static size_t constexpr value = NumberOfOperations::template eval<Expr>();
284 };
285
287 template<typename Expr>
289
291 }
292}
#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 everything that can be an expression e.g. codi::RealReverse, a + b, etc....
Definition expressionTraits.hpp:65
ValidateADLogicImpl< Results... > ValidateADLogic
Validates if the AD logic of an arbitrary amount of expressions are the same or compatible....
Definition expressionTraits.hpp:119
bool constexpr numberOfConstantTypeArguments
Value entry of NumberOfConstantTypeArguments.
Definition expressionTraits.hpp:269
typename std::enable_if< IsStaticContextActiveType< Expr >::value, T >::type EnableIfStaticContextActiveType
Enable if wrapper for IsStaticContextActiveType.
Definition expressionTraits.hpp:217
bool constexpr numberOfOperations
Value entry of NumberOfOperations.
Definition expressionTraits.hpp:288
bool constexpr isConstantExpression
Value entry of IsConstantExpression.
Definition expressionTraits.hpp:196
typename std::enable_if< IsExpression< Expr >::value, T >::type EnableIfExpression
Enable if wrapper for IsExpression.
Definition expressionTraits.hpp:160
bool constexpr numberOfActiveTypeArguments
Value entry of NumberOfActiveTypeArguments.
Definition expressionTraits.hpp:245
bool constexpr isLhsExpression
Value entry of IsLhsExpression.
Definition expressionTraits.hpp:179
typename std::enable_if< IsLhsExpression< Expr >::value, T >::type EnableIfLhsExpression
Enable if wrapper for IsLhsExpression.
Definition expressionTraits.hpp:183
bool constexpr isExpression
Value entry of IsExpression.
Definition expressionTraits.hpp:156
typename std::enable_if< IsConstantExpression< Expr >::value, T >::type EnableIfConstantExpression
Enable if wrapper for IsConstantExpression.
Definition expressionTraits.hpp:200
typename ActiveResultImpl< Real, Tape, isStatic >::ActiveResult ActiveResult
Definition expressionTraits.hpp:137
bool constexpr isStaticContextActiveType
Value entry of IsStaticContextActiveType.
Definition expressionTraits.hpp:213
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
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 constant values in the expression tree.
Definition constantExpression.hpp:78
Base class for all CoDiPack expressions.
Definition expressionInterface.hpp:60
Definition expressionTraits.hpp:127
int ActiveResult
The resulting active type of an expression.
Definition expressionTraits.hpp:132
T_Tape Tape
See ActiveResultImpl.
Definition expressionTraits.hpp:129
T_Real Real
See ActiveResultImpl.
Definition expressionTraits.hpp:128
If the expression inherits from ConstantExpression. Is either std::false_type or std::true_type.
Definition expressionTraits.hpp:187
If the expression inherits from ExpressionInterface. Is either std::false_type or std::true_type.
Definition expressionTraits.hpp:146
If the expression inherits from LhsExpressionInterface. Is either std::false_type or std::true_type.
Definition expressionTraits.hpp:164
If the expression inherits from StaticContextActiveType. Is either std::false_type or std::true_type.
Definition expressionTraits.hpp:204
Counts the number of nodes that inherit from LhsExpressionInterface in the expression.
Definition expressionTraits.hpp:226
static size_t constexpr value
See NumberOfActiveTypeArguments.
Definition expressionTraits.hpp:240
CompileTimeTraversalLogic< size_t, NumberOfActiveTypeArguments< Expr > > Base
Abbreviation for the base class type.
Definition expressionTraits.hpp:230
inlinestatic size_t constexpr leaf()
Called for all leaf nodes in the expression.
Definition expressionTraits.hpp:234
Counts the number of types that inherit from ConstantExpression in the expression.
Definition expressionTraits.hpp:250
static size_t constexpr value
See NumberOfConstantTypeArguments.
Definition expressionTraits.hpp:264
CompileTimeTraversalLogic< size_t, NumberOfConstantTypeArguments< Expr > > Base
Abbreviation for the base class type.
Definition expressionTraits.hpp:254
inlinestatic size_t constexpr leaf()
Called for all leaf nodes in the expression.
Definition expressionTraits.hpp:258
Counts the number of nodes in the expression.
Definition expressionTraits.hpp:273
inlinestatic size_t constexpr node()
Called for each node in the expression.
Definition expressionTraits.hpp:278
static size_t constexpr value
See NumberOfOperations.
Definition expressionTraits.hpp:283
typename std::conditional< isBVoid, LogicA, LogicB >::type ADLogic
The resulting AD logic type of an expression.
Definition expressionTraits.hpp:104
typename ValidateADLogicImpl< LogicA, typename ValidateADLogicImpl< LogicOther... >::ADLogic >::ADLogic ADLogic
The resulting AD logic type of an expression.
Definition expressionTraits.hpp:113
Logic ADLogic
The resulting AD logic type of an expression.
Definition expressionTraits.hpp:86
Validates if the AD logic of an arbitrary amount of expressions are the same or compatible....
Definition expressionTraits.hpp:78
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:226
Replacement type of LhsExpressionInterface types in ConstructStaticContext.
Definition staticContextActiveType.hpp:56