CoDiPack  2.2.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-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 <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>
48 struct ExpressionInterface;
49
50 template<typename T_Real, typename T_Gradient, typename T_Tape, typename T_Impl>
51 struct LhsExpressionInterface;
52
53 template<typename T_Real, template<typename> class T_ConversionOperator>
54 struct ConstantExpression;
55
56 template<typename T_Tape>
57 struct StaticContextActiveType;
58
60 namespace ExpressionTraits {
61
62 /*******************************************************************************/
65
68 template<typename ResultA, typename ResultB, typename = void>
70 private:
71 static bool constexpr isAVoid = std::is_same<void, ResultA>::value;
72 static bool constexpr isBVoid = std::is_same<void, ResultB>::value;
73 static bool constexpr isBothVoid = isAVoid & isBVoid;
74 static bool constexpr isBothSame = std::is_same<ResultA, ResultB>::value;
75
76 // Either one can be void (aka. constant value) but not both otherwise both need to be the same.
77 CODI_STATIC_ASSERT((!isBothVoid) & (!isAVoid | !isBVoid | isBothSame), "Result types need to be the same.");
78
79 public:
80
82 using ActiveResult = typename std::conditional<isBVoid, ResultA, ResultB>::type;
83 };
84
86 template<typename ResultA, typename ResultB>
88
90 /*******************************************************************************/
93
95 template<typename Expr, typename = void>
96 struct IsExpression : std::false_type {};
97
98#ifndef DOXYGEN_DISABLE
99 template<typename Expr>
100 struct IsExpression<Expr, typename enable_if_base_of<ExpressionInterface<typename Expr::Real, Expr>, Expr>::type>
101 : std::true_type {};
102#endif
103
104#if CODI_IS_CPP14
106 template<typename Expr>
107 bool constexpr isExpression = IsExpression<Expr>::value;
108#endif
109
111 template<typename Expr, typename T = void>
112 using EnableIfExpression = typename std::enable_if<IsExpression<Expr>::value, T>::type;
113
115 template<typename Expr, typename = void>
116 struct IsLhsExpression : std::false_type {};
117
118#ifndef DOXYGEN_DISABLE
119 template<typename Expr>
120 struct IsLhsExpression<
121 Expr, typename enable_if_base_of<
122 LhsExpressionInterface<typename Expr::Real, typename Expr::Gradient, typename Expr::Tape, Expr>,
123 Expr>::type> : std::true_type {};
124
125 template<typename Tape>
126 struct IsLhsExpression<StaticContextActiveType<Tape>> : std::true_type {};
127#endif
128
129#if CODI_IS_CPP14
131 template<typename Expr>
132 bool constexpr isLhsExpression = IsLhsExpression<Expr>::value;
133#endif
134
136 template<typename Expr, typename T = void>
137 using EnableIfLhsExpression = typename std::enable_if<IsLhsExpression<Expr>::value, T>::type;
138
140 template<typename Expr>
141 struct IsConstantExpression : std::false_type {};
142
143#ifndef DOXYGEN_DISABLE
144 template<typename Real, template<typename> class ConversionOperator>
145 struct IsConstantExpression<ConstantExpression<Real, ConversionOperator>> : std::true_type {};
146#endif
147
148#if CODI_IS_CPP14
149 template<typename Expr>
151 bool constexpr isConstantExpression = IsConstantExpression<Expr>::value;
152#endif
153
155 template<typename Expr, typename T = void>
156 using EnableIfConstantExpression = typename std::enable_if<IsConstantExpression<Expr>::value, T>::type;
157
159 template<typename Expr>
160 struct IsStaticContextActiveType : std::false_type {};
161
162#ifndef DOXYGEN_DISABLE
163 template<typename Tape>
164 struct IsStaticContextActiveType<StaticContextActiveType<Tape>> : std::true_type {};
165#endif
166
167#if CODI_IS_CPP14
169 template<typename Expr>
170 bool constexpr isStaticContextActiveType = IsStaticContextActiveType<Expr>::value;
171#endif
172
174 template<typename Expr, typename T = void>
175 using EnableIfStaticContextActiveType = typename std::enable_if<IsStaticContextActiveType<Expr>::value, T>::type;
176
178 /*******************************************************************************/
181
183 template<typename Expr>
184 struct NumberOfActiveTypeArguments : public CompileTimeTraversalLogic<size_t, NumberOfActiveTypeArguments<Expr>> {
185 public:
186
189
191 template<typename Node, typename = ExpressionTraits::EnableIfLhsExpression<Node>>
192 CODI_INLINE static size_t constexpr leaf() {
193 return 1;
194 }
196
198 static size_t constexpr value = Base::template eval<Expr>();
199 };
200
201#if CODI_IS_CPP14
203 template<typename Expr>
204 bool constexpr numberOfActiveTypeArguments = NumberOfActiveTypeArguments<Expr>::value;
205#endif
206
208 template<typename Expr>
210 : public CompileTimeTraversalLogic<size_t, NumberOfConstantTypeArguments<Expr>> {
211 public:
212
215
217 template<typename Node, typename = EnableIfConstantExpression<Node>>
218 CODI_INLINE static size_t constexpr leaf() {
219 return 1;
220 }
222
224 static size_t constexpr value = Base::template eval<Expr>();
225 };
226
227#if CODI_IS_CPP14
229 template<typename Expr>
230 bool constexpr numberOfConstantTypeArguments = NumberOfConstantTypeArguments<Expr>::value;
231#endif
232
234 template<typename Expr>
235 struct NumberOfOperations : public CompileTimeTraversalLogic<size_t, NumberOfOperations<Expr>> {
236 public:
237
239 template<typename Node>
240 CODI_INLINE static size_t constexpr node() {
242 }
243
245 static size_t constexpr value = NumberOfOperations::template eval<Expr>();
246 };
247
248#if CODI_IS_CPP14
250 template<typename Expr>
251 bool constexpr numberOfOperations = NumberOfOperations<Expr>::value;
252#endif
253
255 }
256}
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:457
#define CODI_STATIC_ASSERT(cond, message)
Static assert in CoDiPack.
Definition macros.hpp:123
typename std::enable_if< IsStaticContextActiveType< Expr >::value, T >::type EnableIfStaticContextActiveType
Enable if wrapper for IsStaticContextActiveType.
Definition expressionTraits.hpp:175
typename std::enable_if< IsExpression< Expr >::value, T >::type EnableIfExpression
Enable if wrapper for IsExpression.
Definition expressionTraits.hpp:112
typename std::enable_if< IsLhsExpression< Expr >::value, T >::type EnableIfLhsExpression
Enable if wrapper for IsLhsExpression.
Definition expressionTraits.hpp:137
typename std::enable_if< IsConstantExpression< Expr >::value, T >::type EnableIfConstantExpression
Enable if wrapper for IsConstantExpression.
Definition expressionTraits.hpp:156
CoDiPack - Code Differentiation Package.
Definition codi.hpp:90
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
Represents a concrete lvalue in the CoDiPack expression tree.
Definition activeType.hpp:52
Traversal of CoDiPack expressions during compile time.
Definition compileTimeTraversalLogic.hpp:57
static ResultType constexpr link(Args &&... args)
Called for all links in the expression.
Definition compileTimeTraversalLogic.hpp:126
Represents constant values in the expression tree.
Definition constantExpression.hpp:78
Base class for all CoDiPack expressions.
Definition expressionInterface.hpp:59
If the expression inherits from ConstantExpression. Is either std::false_type or std::true_type.
Definition expressionTraits.hpp:141
If the expression inherits from ExpressionInterface. Is either std::false_type or std::true_type.
Definition expressionTraits.hpp:96
If the expression inherits from LhsExpressionInterface. Is either std::false_type or std::true_type.
Definition expressionTraits.hpp:116
If the expression inherits from StaticContextActiveType. Is either std::false_type or std::true_type.
Definition expressionTraits.hpp:160
Counts the number of nodes that inherit from LhsExpressionInterface in the expression.
Definition expressionTraits.hpp:184
static size_t constexpr value
See NumberOfActiveTypeArguments.
Definition expressionTraits.hpp:198
static size_t constexpr leaf()
Called for all leaf nodes in the expression.
Definition expressionTraits.hpp:192
Counts the number of types that inherit from ConstantExpression in the expression.
Definition expressionTraits.hpp:210
static size_t constexpr leaf()
Called for all leaf nodes in the expression.
Definition expressionTraits.hpp:218
static size_t constexpr value
See NumberOfConstantTypeArguments.
Definition expressionTraits.hpp:224
Counts the number of nodes in the expression.
Definition expressionTraits.hpp:235
static size_t constexpr node()
Called for each node in the expression.
Definition expressionTraits.hpp:240
static size_t constexpr value
See NumberOfOperations.
Definition expressionTraits.hpp:245
Definition expressionTraits.hpp:69
typename std::conditional< isBVoid, ResultA, ResultB >::type ActiveResult
The resulting active type of an expression.
Definition expressionTraits.hpp:82
Base class for all CoDiPack lvalue expression.
Definition lhsExpressionInterface.hpp:63
Replacement type of LhsExpressionInterface types in ConstructStaticContext.
Definition staticContextActiveType.hpp:56