CoDiPack  3.0.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
mathStatementGenLogic.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 <map>
39#include <type_traits>
40#include <utility>
41#include <vector>
42
43#include "../../../config.h"
45#include "../../../traits/expressionTraits.hpp"
46#include "../traversalLogic.hpp"
47#include "forEachLeafLogic.hpp"
48
50namespace codi {
51
56 template<typename Identifier>
57 struct MathStatementGenLogic : public ForEachLeafLogic<MathStatementGenLogic<Identifier>> {
58 public:
59 Identifier passiveThreshold;
60
63
65 template<typename Node>
66 CODI_INLINE void eval(NodeInterface<Node> const& node, std::string& mathRep) {
67 std::vector<std::string> topNodeRep;
68 this->toNode(node.cast(), topNodeRep);
69
70 // Check for unnecessary brackets at the start of the statement.
71 // If found, remove first and last bracket.
72 if (topNodeRep[0].find("(") == 0) {
73 topNodeRep[0] = topNodeRep[0].substr(1, topNodeRep[0].size() - 2);
74 }
75
76 mathRep = topNodeRep[0];
77 }
78
79 /*******************************************************************************/
82
84 template<typename Node>
85 CODI_INLINE void node(Node const& node, std::vector<std::string>& nodeRep) {
86 std::vector<std::string> linkRep;
87 std::string stmtOperator = node.getMathRep();
88
89 this->toLinks(node, linkRep);
90
91 if (linkRep.size() == 2) {
92 // Determine if the operator comes before the two args.
93 // This is indicated by the addition of "()" at the end of the stmtOperator.
94 if (stmtOperator.find("()") != std::string::npos) {
95 // Remove ')'.
96 stmtOperator.pop_back();
97 nodeRep.push_back(stmtOperator + linkRep[0] + ", " + linkRep[1] + ")");
98 } else {
99 nodeRep.push_back("(" + linkRep[0] + " " + stmtOperator + " " + linkRep[1] + ")");
100 }
101 } else {
102 nodeRep.push_back(stmtOperator + "(" + linkRep[0] + ")");
103 }
104 }
105
107 template<typename Node>
108 void handleActive(Node const& node, std::vector<std::string>& linkRep) {
109 // Check for passive values
110 if (node.getIdentifier() <= passiveThreshold) {
111 linkRep.push_back("p(" + std::to_string(node.getValue()) + ")");
112 } else {
113 linkRep.push_back("x" + std::to_string(node.getIdentifier()));
114 }
115 }
116
118 template<typename Node>
119 void handleConstant(Node const& node, std::vector<std::string>& linkRep) {
120 linkRep.push_back("c(" + convert_value(node.getValue()) + ")");
121 }
122
123 private:
124
125 template<typename T>
126 std::string convert_value(T const& v) {
127 return std::to_string(v);
128 }
129
130 template<typename T>
131 std::string convert_value(std::complex<T> const& v) {
132 return "(" + std::to_string(std::real(v)) + " + " + std::to_string(std::imag(v)) + ")";
133 }
134
135 template<typename T>
136 std::string convert_value(T* const& v) {
137 return "p" + std::to_string(reinterpret_cast<size_t>(v));
138 }
139 };
140}
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:469
CoDiPack - Code Differentiation Package.
Definition codi.hpp:94
Implement logic for leaf nodes only.
Definition forEachLeafLogic.hpp:60
Identifier passiveThreshold
The identifiers that are allocated for passive values.
Definition mathStatementGenLogic.hpp:59
inlinevoid node(Node const &node, std::vector< std::string > &nodeRep)
Links two nodes with a math operation.
Definition mathStatementGenLogic.hpp:85
inlinevoid eval(NodeInterface< Node > const &node, std::string &mathRep)
Produces a math representation string for a given statement.
Definition mathStatementGenLogic.hpp:66
void handleConstant(Node const &node, std::vector< std::string > &linkRep)
Called for leaf nodes which implement ConstantExpression.
Definition mathStatementGenLogic.hpp:119
MathStatementGenLogic(Identifier passiveThreshold=0)
Constructor.
Definition mathStatementGenLogic.hpp:62
void handleActive(Node const &node, std::vector< std::string > &linkRep)
Called for leaf nodes which implement LhsExpressionInterface.
Definition mathStatementGenLogic.hpp:108
Node side interface for the traversal of expressions.
Definition nodeInterface.hpp:56
inlineImpl const & cast() const
Cast to the implementation.
Definition nodeInterface.hpp:62
inlinevoid toLinks(Node const &node, Args &&... args)
Definition traversalLogic.hpp:149
inlinevoid toNode(Node const &node, Args &&... args)
Definition traversalLogic.hpp:143