forked from msupernaw/ATL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinMax.hpp
365 lines (323 loc) · 13.9 KB
/
MinMax.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
* 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: MinMax.hpp
* Author: matthewsupernaw
*
* Created on December 28, 2016, 11:24 AM
*/
#ifndef MINMAX_HPP
#define MINMAX_HPP
#include "Expression.hpp"
#include "Add.hpp"
#include "Divide.hpp"
#include "Subtract.hpp"
#include "Fabs.hpp"
#include "Multiply.hpp"
#include "Matrix.hpp"
namespace atl {
template <class REAL_T, class LHS, class RHS>
struct Min : public ExpressionBase<REAL_T, Min<REAL_T, LHS, RHS> > {
typedef REAL_T BASE_TYPE;
/**
* Constructor for two variable types.
*
* @param lhs
* @param rhs
*/
Min(const ExpressionBase<REAL_T, LHS>& lhs, const ExpressionBase<REAL_T, RHS>& rhs)
: lhs_m(lhs.Cast()), rhs_m(rhs.Cast()) {
}
/**
* Constructor for real plus variable type.
*
*/
Min(const REAL_T& lhs, const ExpressionBase<REAL_T, RHS>& rhs)
: lhs_m(real_m), rhs_m(rhs.Cast()) {
real_m.value = lhs;
}
/**
* Constructor for variable plus real type.
* @param lhs
* @param rhs
*/
Min(const ExpressionBase<REAL_T, LHS>& lhs, const REAL_T& rhs)
: lhs_m(lhs.Cast()), rhs_m(real_m) {
real_m.value = rhs;
}
/**
* Compute the min of the lhs and rhs expressions.
*
* @return
*/
inline const REAL_T GetValue() const {
//(a + b - atl::fabs(a - b))*.5;
return (lhs_m.GetValue() + rhs_m.GetValue() - std::fabs(lhs_m.GetValue() - rhs_m.GetValue()))*.5;
}
/**
* Compute the min of the lhs and rhs expressions at index {i,j}.
*
* @return
*/
inline const REAL_T GetValue(size_t i, size_t j = 0) const {
return lhs_m.GetValue(i, j) + rhs_m.GetValue(i, j);
}
/**
* Push variable info into a set.
*
* @param ids
* @param i
* @param j
*/
inline void PushIds(typename atl::StackEntry<REAL_T>::vi_storage& ids)const {
lhs_m.PushIds(ids);
rhs_m.PushIds(ids);
}
/**
* Push variable info into a set at index {i,j}.
*
* @param ids
* @param i
* @param j
*/
inline void PushIds(typename atl::StackEntry<REAL_T>::vi_storage& ids, size_t i, size_t j = 0)const {
lhs_m.PushIds(ids, i, j);
rhs_m.PushIds(ids, i, j);
}
/**
* Evaluates the first-order derivative of this expression with respect
* to x.
*
* \f$ {{d}\over{d\,x}}\,g\left(x\right)+{{d}\over{d\,x}}\,f\left(x\right) \f$
*
* @param x
* @return
*/
inline REAL_T EvaluateFirstDerivative(uint32_t x) const {
return 0.5 * (-(1.0 * (lhs_m.GetValue() - rhs_m.GetValue())*
(lhs_m.EvaluateFirstDerivative(x) - lhs_m.EvaluateFirstDerivative(x))) /
std::pow(C + std::pow(lhs_m.GetValue() - rhs_m.GetValue(), 2.0), 0.5) +
lhs_m.EvaluateFirstDerivative(x) + lhs_m.EvaluateFirstDerivative(x));
}
/**
* \f$ {{d^2}\over{d\,x\,d\,y}}\,g\left(x , y\right)+{{d^2}\over{d\,x\,d\,
* y}}\,f\left(x , y\right) \f$
* @param x
* @param y
* @return
*/
inline REAL_T EvaluateSecondDerivative(uint32_t x, uint32_t y) const {
return 0.5 * (-(1.0 * (lhs_m.EvaluateFirstDerivative(x) -
rhs_m.EvaluateFirstDerivative(x))*(lhs_m.EvaluateFirstDerivative(y)
- rhs_m.EvaluateFirstDerivative(y))) / std::pow(C + std::pow(lhs_m.GetValue() -
rhs_m.GetValue(), 2.0), 0.5) - (1.0 * (lhs_m.GetValue() -
rhs_m.GetValue())*(lhs_m.EvaluateSecondDerivative(x, y) -
rhs_m.EvaluateSecondDerivative(x, y))) / std::pow(C + std::pow(lhs_m.GetValue() -
rhs_m.GetValue(), 2), 0.5) +
(1.0 * std::pow(lhs_m.GetValue() - rhs_m.GetValue(), 2.0) *
(lhs_m.EvaluateFirstDerivative(x) - rhs_m.EvaluateFirstDerivative(x))*
(lhs_m.EvaluateFirstDerivative(y) - rhs_m.EvaluateFirstDerivative(y))) /
std::pow(C + std::pow(lhs_m.GetValue() - rhs_m.GetValue(), 2.0), 1.5) +
rhs_m.EvaluateSecondDerivative(x, y) + lhs_m.EvaluateSecondDerivative(x, y));
}
/**
* \f$ {{d^3}\over{d\,x\,d\,y\,d\,z}}\,g\left(x , y , z\right)+{{d^3
* }\over{d\,x\,d\,y\,d\,z}}\,f\left(x , y , z\right) \f$
* @param x
* @param y
* @param z
* @return
*/
inline REAL_T EvaluateThirdDerivative(uint32_t x, uint32_t y, uint32_t z) const {
return 0.5 * (-((rhs_m.GetValue() - lhs_m.GetValue())*(rhs_m.EvaluateThirdDerivative(x, y, z) - lhs_m.EvaluateThirdDerivative(x, y, z)))
/ std::fabs(rhs_m.GetValue() - lhs_m.GetValue()) + rhs_m.EvaluateThirdDerivative(x, y, z) + lhs_m.EvaluateThirdDerivative(x, y, z));
}
/**
* Evaluates the first-order derivative of this expression with respect
* to x at index {i,j}.
*
* @param a
* @param i
* @param j
* @return
*/
inline REAL_T EvaluateFirstDerivativeAt(uint32_t x, size_t i, size_t j = 0) const {
return 0.5 * (-(1.0 * (lhs_m.GetValue(i, j) - rhs_m.GetValue(i, j))*
(lhs_m.EvaluateFirstDerivativeAt(x, i, j) - lhs_m.EvaluateFirstDerivativeAt(x, i, j))) /
std::pow(C + std::pow(lhs_m.GetValue(i, j) - rhs_m.GetValue(i, j), 2.0), 0.5) +
lhs_m.EvaluateFirstDerivativeAt(x, i, j) + lhs_m.EvaluateFirstDerivativeAt(x, i, j));
}
/**
*
* @param x
* @param y
* @param i
* @param j
* @return
*/
inline REAL_T EvaluateSecondDerivativeAt(uint32_t x, uint32_t y, size_t i, size_t j = 0) const {
return 0.5 * (-(1.0 * (lhs_m.EvaluateFirstDerivativeAt(x, i, j) -
rhs_m.EvaluateFirstDerivativeAt(x, i, j))*(lhs_m.EvaluateFirstDerivativeAt(y, i, j)
- rhs_m.EvaluateFirstDerivativeAt(y, i, j))) / std::pow(C + std::pow(lhs_m.GetValue() -
rhs_m.GetValue(), 2.0), 0.5) - (1.0 * (lhs_m.GetValue() -
rhs_m.GetValue())*(lhs_m.EvaluateSecondDerivativeAt(x, y, i, j) -
rhs_m.EvaluateSecondDerivativeAt(x, y, i, j))) / std::pow(C + std::pow(lhs_m.GetValue() -
rhs_m.GetValue(), 2), 0.5) +
(1.0 * std::pow(lhs_m.GetValue() - rhs_m.GetValue(), 2.0) *
(lhs_m.EvaluateFirstDerivativeAt(x, i, j) - rhs_m.EvaluateFirstDerivativeAt(x, i, j))*
(lhs_m.EvaluateFirstDerivativeAt(y, i, j) - rhs_m.EvaluateFirstDerivativeAt(y, i, j))) /
std::pow(C + std::pow(lhs_m.GetValue() - rhs_m.GetValue(), 2.0), 1.5) +
rhs_m.EvaluateSecondDerivativeAt(x, y, i, j) + lhs_m.EvaluateSecondDerivativeAt(x, y, i, j));
}
/**
*
* @param x
* @param y
* @param z
* @param i
* @param j
* @return
*/
inline REAL_T EvaluateThirdDerivativeAt(uint32_t x, uint32_t y, uint32_t z, size_t i, size_t j = 0) const {
REAL_T diff = lhs_m.GetValue() - rhs_m.GetValue();
REAL_T tmp0 = std::pow(diff, 2);
REAL_T tmp1 = std::pow(C + std::pow(diff, 2), .5);
REAL_T tmp2 = std::pow(C + std::pow(diff, 2.0), 1.5);
REAL_T tmp3 = (1.0 * std::pow(diff, 2.0));
return 0.5 * (-(1.0 * (lhs_m.EvaluateSecondDerivativeAt(x, y, i, j) - rhs_m.EvaluateSecondDerivativeAt(x, y, i, j))
*(lhs_m.EvaluateFirstDerivativeAt(z, i, j) - rhs_m.EvaluateFirstDerivativeAt(z, i, j))) /
tmp1 -
(1.0 * (lhs_m.EvaluateFirstDerivativeAt(x, i, j) - rhs_m.EvaluateFirstDerivativeAt(x, i, j))*
(lhs_m.EvaluateDerivative(y, z, i, j) - rhs_m.EvaluateDerivative(y, z, i, j))) /
tmp1 -
(1.0 * (lhs_m.EvaluateSecondDerivativeAt(x, z, i, j) - rhs_m.EvaluateSecondDerivativeAt(x, z, i, j))*
(lhs_m.EvaluateFirstDerivativeAt(y, i, j) - rhs_m.EvaluateFirstDerivativeAt(y, i, j))) /
tmp1 -
(1.0 * (lhs_m.GetValue(i, j) - rhs_m.GetValue(i, j))*(lhs_m.EvaluateThirdDerivativeAt(x, y, z, i, j) -
rhs_m.EvaluateThirdDerivativeAt(x, y, z, i, j))) /
tmp1 +
(3.0 * (lhs_m.GetValue(i, j) - rhs_m.GetValue(i, j))*(lhs_m.EvaluateFirstDerivativeAt(x, i, j) -
rhs_m.EvaluateFirstDerivativeAt(x, i, j))*(lhs_m.EvaluateFirstDerivativeAt(y, i, j) -
rhs_m.EvaluateFirstDerivativeAt(y, i, j))*(lhs_m.EvaluateFirstDerivativeAt(z, i, j) -
rhs_m.EvaluateFirstDerivativeAt(z, i, j))) /
tmp2 +
(1.0 * tmp0 * (lhs_m.EvaluateSecondDerivativeAt(x, y, i, j) -
rhs_m.EvaluateSecondDerivativeAt(x, y, i, j))*(lhs_m.EvaluateFirstDerivativeAt(z, i, j) - rhs_m.EvaluateFirstDerivativeAt(z, i, j))) /
tmp2 +
(1.0 * tmp0 * (lhs_m.EvaluateFirstDerivativeAt(x, i, j) -
rhs_m.EvaluateFirstDerivativeAt(x, i, j))*(lhs_m.EvaluateDerivative(y, z, i, j) - rhs_m.EvaluateDerivative(y, z, i, j))) /
tmp2 +
(1.0 * tmp0 * (lhs_m.EvaluateSecondDerivativeAt(x, z, i, j) -
rhs_m.EvaluateSecondDerivativeAt(x, z, i, j))*(lhs_m.EvaluateFirstDerivativeAt(y, i, j) - rhs_m.EvaluateFirstDerivativeAt(y, i, j))) /
tmp2 -
(3.0 * std::pow(lhs_m.GetValue(i, j) - rhs_m.GetValue(i, j), 3.0) * (lhs_m.EvaluateFirstDerivativeAt(x, i, j) -
rhs_m.EvaluateFirstDerivativeAt(x, i, j))*(lhs_m.EvaluateFirstDerivativeAt(y, i, j) -
rhs_m.EvaluateFirstDerivativeAt(y, i, j))*(lhs_m.EvaluateFirstDerivativeAt(z, i, j) - rhs_m.EvaluateFirstDerivativeAt(z, i, j))) /
std::pow(C + tmp0, 2.5) +
rhs_m.EvaluateThirdDerivativeAt(x, y, z, i, j) + lhs_m.EvaluateThirdDerivativeAt(x, y, z, i, j));
}
/**
* Return the number of columns.
*
* @return
*/
size_t GetColumns() const {
if (!lhs_m.IsScalar() && !rhs_m.IsScalar()) {
size_t lc = lhs_m.GetColumns();
size_t rc = rhs_m.GetColumns();
assert(lc == rc);
return lc < rc ? lc : rc;
} else if (!lhs_m.IsScalar()) {
return lhs_m.GetColumns();
} else {
return rhs_m.GetColumns();
}
}
/**
* Return the number of rows.
*
* @return
*/
size_t GetRows() const {
if (!lhs_m.IsScalar() && !rhs_m.IsScalar()) {
size_t lc = lhs_m.GetRows();
size_t rc = rhs_m.GetRows();
assert(lc == rc);
return lc < rc ? lc : rc;
} else if (!lhs_m.IsScalar()) {
return lhs_m.GetRows();
} else {
return rhs_m.GetRows();
}
}
/**
* True if the expression is a scalar.
* @return
*/
bool IsScalar() const {
if (lhs_m.IsScalar() && rhs_m.IsScalar()) {
return true;
}
return false;
}
/**
* Create a string representation of this expression template.
* @return
*/
const std::string ToExpressionTemplateString() const {
std::stringstream ss;
ss << "atl::Add<T," << lhs_m.ToExpressionTemplateString() << ", " << rhs_m.ToExpressionTemplateString() << " >";
return ss.str();
}
atl::Real<REAL_T> real_m; //used for operations involving real numbers
const LHS& lhs_m;
const RHS& rhs_m;
REAL_T value_m;
REAL_T C;
};
/**
* Returns the maximum between a and b in a continuous manner using:
*
* (a + b + \ref atl::ad_fabs(a - b)) *.5;
*
* This is an approximation with minimal error.
*
* @param a
* @param b
* @param C default = 1e-5
* @return
*/
template <typename T>
inline const atl::Variable<T> ad_max(const atl::Variable<T>& a, const atl::Variable<T>& b, atl::Variable<T> C = static_cast<T>(1e-5)) {
// atl::Variable<T> aminusb = a-b;
return (a + b + atl::ad_fabs(a-b,C))*static_cast<T>(.5);
}
#define AD_MAX(a,b) (a + b + atl::ad_fabs(a - b))*.5
/**
* Returns the minimum between a and b in a continuous manner using:
*
* (a + b - \ref atl::ad_fabs(a - b))*.5;
*
* This is an approximation with minimal error.
*
* @param a
* @param b
* @param C default = 1e-5
* @return
*/
template <typename T>
inline const atl::Variable<T> ad_min(const atl::Variable<T>& a, const atl::Variable<T>& b, atl::Variable<T> C = 1e-5) {
return (a + b - atl::ad_fabs(a - b,C))*.5;
}
#define AD_MIN(a,b) (a + b - atl::ad_fabs(a - b))*.5
// template <class REAL_T, class LHS, class RHS>
// inline const Min<REAL_T, LHS, RHS> min(const ExpressionBase<REAL_T, LHS>& a,
// const ExpressionBase<REAL_T, RHS>& b) {
// return Min<REAL_T, LHS, RHS > (a.Cast(), b.Cast());
// }
//
}
#endif /* MINMAX_HPP */