forked from msupernaw/ATL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Expression.hpp
276 lines (214 loc) · 7.96 KB
/
Expression.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: Expression.hpp
* Author: matthewsupernaw
*
* Created on October 31, 2016, 12:55 PM
*/
#ifndef EXPRESSION_HPP
#define EXPRESSION_HPP
#include <cassert>
#include "Tape.hpp"
#include <sstream>
#include <ostream>
#include <complex>
namespace atl {
enum ExpressionType {
ET_BASE = 0,
REAL_SCALAR,
VARIABLE_SCALAR,
VARIABLE_SCALAR_EXPRESSION,
REAL_MATRIX,
VARIABLE_MATRIX,
REAL_MATRIX_EXPRESSION,
VARIABLE_MATRIX_EXPRESSION,
REAL_VECTOR,
VARIABLE_VECTOR,
REAL_VECTOR_EXPRESSION,
VARIABLE_VECTOR_EXPRESSION
};
enum BinaryMethod {
EXP_EXP = 0,
EXP_REAL,
REAL_EXP
};
template<typename T>
struct ExpressionTrait {
static ExpressionType et_type;
};
template<typename T>
ExpressionType ExpressionTrait<T>::et_type = ET_BASE;
template<class REAL_T, class A>
struct ExpressionBase {
std::shared_ptr<typename atl::StackEntry<REAL_T>::VariableInfoPtr > info_e;
ExpressionBase() {
}
/**
* Cast this expression template to it's child
* representation.
*
* @return
*/
inline const A & Cast() const {
return static_cast<const A&> (*this);
}
inline const REAL_T GetValue() const {
return Cast().GetValue();
}
inline const REAL_T GetValue(size_t i, size_t j = 0) const {
return Cast().GetValue(i, j);
}
inline bool IsNonlinear() const {
return Cast().IsNonlinear();
}
inline void PushIds(typename atl::StackEntry<REAL_T>::vi_storage& ids)const {
Cast().PushIds(ids);
}
inline void PushNLIds(typename atl::StackEntry<REAL_T>::vi_storage& ids, bool nl = false)const {
Cast().PushIds(ids, nl);
}
inline void PushIds(typename atl::StackEntry<REAL_T>::vi_storage& ids, size_t i, size_t j = 0)const {
Cast().PushIds(ids, i, j);
}
inline const std::complex<REAL_T> ComplexEvaluate(uint32_t x, REAL_T h = 1e-20) const {
return Cast().ComplexEvaluate(x, h);
}
std::shared_ptr<DynamicExpressionBase<REAL_T> > ToDynamic() const {
return Cast().ToDynamic();
}
inline REAL_T EvaluateFirstDerivative(uint32_t x) const {
return Cast().EvaluateFirstDerivative(x);
}
inline REAL_T EvaluateSecondDerivative(uint32_t x, uint32_t y) const {
return Cast().EvaluateSecondDerivative(x, y);
}
inline REAL_T EvaluateThirdDerivative(uint32_t x, uint32_t y, uint32_t z) const {
return Cast().EvaluateThirdDerivative(x, y, z);
}
inline const std::complex<REAL_T> ComplexEvaluate(uint32_t x, size_t i, size_t j = 0, REAL_T h = 1e-20) const {
return Cast().ComplexEvaluate(x, i, j, h);
}
inline const REAL_T Taylor(uint32_t degree) const {
return Cast().Taylor(degree);
}
inline REAL_T EvaluateFirstDerivativeAt(uint32_t x, size_t i, size_t j = 0) const {
return Cast().EvaluateFirstDerivativeAt(x, i, j);
}
inline REAL_T EvaluateSecondDerivativeAt(uint32_t x, uint32_t y, size_t i, size_t j = 0) const {
return Cast().EvaluateSecondDerivativeAt(x, y, i, j);
}
inline REAL_T EvaluateThirdDerivativeAt(uint32_t x, uint32_t y, uint32_t z, size_t i, size_t j = 0) const {
return Cast().EvaluateThirdDerivativeAt(x, y, z, i, j);
}
const ExpressionBase& operator=(const ExpressionBase & exp) const {
return *this;
}
size_t GetColumns() const {
return Cast().GetColumns();
}
size_t GetRows() const {
return Cast().GetRows();
}
bool IsScalar()const {
return Cast().IsScalar();
}
void PrepareGraph() const {
Cast().PrepareGraph();
}
void PushGraph(atl::Tape<REAL_T>& tape) const {
Cast().PushGraph(tape);
}
std::shared_ptr<typename atl::StackEntry<REAL_T>::VariableInfoPtr > GetLocalInfo() const {
return Cast().GetLocalInfo();
}
/**
* Create a string representation of this expression template.
* @return
*/
std::string ToExpressionTemplateString() const {
return Cast().ToExpressionTemplateString();
}
};
}
template <class REAL_T, class T>
std::ostream & operator<<(std::ostream &vout, const atl::ExpressionBase<REAL_T, T> &e) {
vout << e.GetValue();
return vout;
}
template<class REAL_T, class T, class TT>
inline const int operator==(const atl::ExpressionBase<REAL_T, T>& lhs, const atl::ExpressionBase<REAL_T, TT>& rhs) {
return lhs.GetValue() == rhs.GetValue();
}
template<class REAL_T, class T, class TT>
inline const int operator!=(const atl::ExpressionBase<REAL_T, T>& lhs, const atl::ExpressionBase<REAL_T, TT>& rhs) {
return lhs.GetValue() != rhs.GetValue();
}
template<class REAL_T, class T, class TT>
inline const int operator<(const atl::ExpressionBase<REAL_T, T>& lhs, const atl::ExpressionBase<REAL_T, TT>& rhs) {
return lhs.GetValue() < rhs.GetValue();
}
template<class REAL_T, class T, class TT>
inline const int operator>(const atl::ExpressionBase<REAL_T, T>& lhs, const atl::ExpressionBase<REAL_T, TT>& rhs) {
return lhs.GetValue() > rhs.GetValue();
}
template<class REAL_T, class T, class TT>
inline const int operator<=(const atl::ExpressionBase<REAL_T, T>& lhs, const atl::ExpressionBase<REAL_T, TT>& rhs) {
return lhs.GetValue() <= rhs.GetValue();
}
template<class REAL_T, class T, class TT>
inline const int operator>=(const atl::ExpressionBase<REAL_T, T>& lhs, const atl::ExpressionBase<REAL_T, TT>& rhs) {
return lhs.GetValue() >= rhs.GetValue();
}
template<class REAL_T, class T>
inline const int operator==(const REAL_T &lhs, const atl::ExpressionBase<REAL_T, T>& rhs) {
return lhs == rhs.GetValue();
}
template<class REAL_T, class T>
inline const int operator!=(const REAL_T &lhs, const atl::ExpressionBase<REAL_T, T>& rhs) {
return lhs != rhs.GetValue();
}
template<class REAL_T, class T>
inline const int operator<(const REAL_T &lhs, const atl::ExpressionBase<REAL_T, T>& rhs) {
return lhs < rhs.GetValue();
}
template<class REAL_T, class T>
inline const int operator>(const REAL_T &lhs, const atl::ExpressionBase<REAL_T, T>& rhs) {
return lhs > rhs.GetValue();
}
template<class REAL_T, class T>
inline const int operator<=(const REAL_T &lhs, const atl::ExpressionBase<REAL_T, T>& rhs) {
return lhs <= rhs.GetValue();
}
template<class REAL_T, class T>
inline const int operator>=(const REAL_T &lhs, const atl::ExpressionBase<REAL_T, T>& rhs) {
return lhs >= rhs.GetValue();
}
template<class REAL_T, class T>
inline const int operator==(const atl::ExpressionBase<REAL_T, T>& lhs, const REAL_T &rhs) {
return lhs.GetValue() == rhs;
}
template<class REAL_T, class T>
inline const int operator!=(const atl::ExpressionBase<REAL_T, T>& lhs, const REAL_T &rhs) {
return lhs.GetValue() != rhs;
}
template<class REAL_T, class T>
inline const int operator<(const atl::ExpressionBase<REAL_T, T>& lhs, const REAL_T &rhs) {
return lhs.GetValue() <= rhs;
}
template<class REAL_T, class T>
inline const int operator>(const atl::ExpressionBase<REAL_T, T>& lhs, const REAL_T &rhs) {
return lhs.GetValue() > rhs;
}
template<class REAL_T, class T>
inline const int operator<=(const atl::ExpressionBase<REAL_T, T>& lhs, const REAL_T &rhs) {
return lhs.GetValue() <= rhs;
}
template<class REAL_T, class T>
inline const int operator>=(const atl::ExpressionBase<REAL_T, T>& lhs, const REAL_T &rhs) {
return lhs.GetValue() >= rhs;
}
#endif /* EXPRESSION_HPP */