CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
binaryExpression.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://www.scicomp.uni-kl.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://www.scicomp.uni-kl.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 "../config.h"
38#include "../misc/macros.hpp"
39#include "../traits/expressionTraits.hpp"
40#include "expressionInterface.hpp"
41#include "logic/compileTimeTraversalLogic.hpp"
42#include "logic/nodeInterface.hpp"
43#include "logic/traversalLogic.hpp"
44
46namespace codi {
47
55 template<typename T_Real>
57 public:
58
59 using Real = CODI_DD(T_Real, double);
60
64 template<typename ArgA, typename ArgB>
65 static CODI_INLINE Real primal(ArgA const& argA, ArgB const& argB);
66
70 template<typename ArgA, typename ArgB>
71 static CODI_INLINE Real gradientA(ArgA const& argA, ArgB const& argB, Real const& result);
72
76 template<typename ArgA, typename ArgB>
77 static CODI_INLINE Real gradientB(ArgA const& argA, ArgB const& argB, Real const& result);
78 };
79
90 template<typename T_Real, typename T_ArgA, typename T_ArgB, template<typename> class T_Operation>
91 struct BinaryExpression : public ExpressionInterface<T_Real, BinaryExpression<T_Real, T_ArgA, T_ArgB, T_Operation> > {
92 public:
93 using Real = CODI_DD(T_Real, double);
96 using Operation = CODI_DD(CODI_T(T_Operation<Real>), CODI_T(BinaryOperation<Real>));
97
98 typename ArgA::StoreAs argA;
99 typename ArgB::StoreAs argB;
101
103 template<typename RealA, typename RealB>
106 : argA(argA.cast()),
107 argB(argB.cast()),
108 result(Operation::primal(this->argA.getValue(), this->argB.getValue())) {}
109
110 /*******************************************************************************/
113
118
120 CODI_INLINE Real const& getValue() const {
121 return result;
122 }
123
125 template<size_t argNumber>
127 if (0 == argNumber) {
128 return Operation::gradientA(argA.getValue(), argB.getValue(), result);
129 } else {
130 return Operation::gradientB(argA.getValue(), argB.getValue(), result);
131 }
132 }
133
135 /*******************************************************************************/
138
139 static bool constexpr EndPoint = false;
140
142 template<typename Logic, typename... Args>
143 CODI_INLINE void forEachLink(TraversalLogic<Logic>& logic, Args&&... args) const {
144 logic.cast().template link<0>(argA, *this, std::forward<Args>(args)...);
145 logic.cast().template link<1>(argB, *this, std::forward<Args>(args)...);
146 }
147
149 template<typename CompileTimeLogic, typename... Args>
150 CODI_INLINE static typename CompileTimeLogic::ResultType constexpr forEachLinkConstExpr(Args&&... args) {
151 return CompileTimeLogic::reduce(
152 CompileTimeLogic::template link<0, ArgA, BinaryExpression>(std::forward<Args>(args)...),
153 CompileTimeLogic::template link<1, ArgB, BinaryExpression>(std::forward<Args>(args)...));
154 }
155
157 };
158}
#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_T(...)
Abbreviation for CODI_TEMPLATE.
Definition: macros.hpp:111
CoDiPack - Code Differentiation Package.
Definition: codi.hpp:90
Represents an operator with two arguments in the expression tree.
Definition: binaryExpression.hpp:91
static CompileTimeLogic::ResultType constexpr forEachLinkConstExpr(Args &&... args)
Definition: binaryExpression.hpp:150
static bool constexpr EndPoint
If this expression is handled as a leaf in the tree.
Definition: binaryExpression.hpp:139
BinaryExpression(ExpressionInterface< RealA, ArgA > const &argA, ExpressionInterface< RealB, ArgB > const &argB)
Constructor.
Definition: binaryExpression.hpp:104
ArgA::StoreAs argA
First argument of the expression.
Definition: binaryExpression.hpp:98
ArgB::StoreAs argB
Second argument of the expression.
Definition: binaryExpression.hpp:99
T_ArgB ArgB
See BinaryExpression.
Definition: binaryExpression.hpp:95
T_Real Real
See BinaryExpression.
Definition: binaryExpression.hpp:93
Real result
Precomputed result.
Definition: binaryExpression.hpp:100
void forEachLink(TraversalLogic< Logic > &logic, Args &&... args) const
Definition: binaryExpression.hpp:143
typename ExpressionTraits::ValidateResult< typename ArgA::ActiveResult, typename ArgB::ActiveResult >::ActiveResult ActiveResult
Type into which the expression can be converted. Usually also the type from which it is constructed.
Definition: binaryExpression.hpp:117
Real const & getValue() const
Compute the primal value that is usually evaluated by the statement/expression.
Definition: binaryExpression.hpp:120
Real getJacobian() const
Definition: binaryExpression.hpp:126
T_Operation< Real > Operation
See BinaryExpression.
Definition: binaryExpression.hpp:96
T_ArgA ArgA
See BinaryExpression.
Definition: binaryExpression.hpp:94
Interface for implementing the logic for a BinaryExpression.
Definition: binaryExpression.hpp:56
static Real primal(ArgA const &argA, ArgB const &argB)
static Real gradientB(ArgA const &argA, ArgB const &argB, Real const &result)
T_Real Real
See BinaryOperation.
Definition: binaryExpression.hpp:59
static Real gradientA(ArgA const &argA, ArgB const &argB, Real const &result)
Base class for all CoDiPack expressions.
Definition: expressionInterface.hpp:59
Impl const & cast() const
Cast to the implementation.
Definition: expressionInterface.hpp:75
Definition: expressionTraits.hpp:69
Traversal of CoDiPack expressions.
Definition: traversalLogic.hpp:56
Impl & cast()
Cast to the implementation.
Definition: traversalLogic.hpp:62