forked from msupernaw/ATL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLog10.hpp
350 lines (306 loc) · 12.5 KB
/
Log10.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
/*
* File: Log10.hpp
* Author: matthewsupernaw
*
* Created on June 18, 2014, 12:35 PM
*/
/**
*
* @author Matthew R. Supernaw
*
* Public Domain Notice
* National Oceanic And Atmospheric Administration
*
* This software is a "United States Government Work" under the terms of the
* United States Copyright Act. It was written as part of the author's official
* duties as a United States Government employee and thus cannot be copyrighted.
* This software is freely available to the public for use. The National Oceanic
* And Atmospheric Administration and the U.S. Government have not placed any
* restriction on its use or reproduction. Although all reasonable efforts have
* been taken to ensure the accuracy and reliability of the software and data,
* the National Oceanic And Atmospheric Administration and the U.S. Government
* do not and cannot warrant the performance warrant the performance or results
* that may be obtained by using this software or data. The National Oceanic
* And Atmospheric Administration and the U.S. Government disclaim all
* warranties, express or implied, including warranties of performance,
* merchantability or fitness for any particular purpose.
*
* Please cite the author(s) in any work or product based on this material.
*
*/
#ifndef LOG10_HPP
#define LOG10_HPP
#include "Expression.hpp"
#include <vector>
#include "Divide.hpp"
#include "Log.hpp"
#ifndef AD_LOG10
#define AD_LOG10 2.30258509299404590109361379290930926799774169921875
#endif
namespace atl {
/**
* Expression template to handle log10 for variable or
* container expressions.
*
* \f$ {\it log_{10}}\left(f(x)\right) \f$
*
* or
*
* \f$ {\it log_{10}}\left(f_{i,j}(x)\right) \f$
*
*/
template<class REAL_T, class EXPR>
struct Log10 : public ExpressionBase<REAL_T, Log10<REAL_T, EXPR> > {
typedef REAL_T BASE_TYPE;
/**
* Constructor
*
* @param a
*/
Log10(const ExpressionBase<REAL_T, EXPR>& a)
: expr_m(a.Cast()) {
}
/**
* Computes the log10 of the evaluated expression.
*
* @return
*/
inline const REAL_T GetValue() const {
return std::log10(expr_m.GetValue());
}
/**
* Computes the log10 of the evaluated expression at index {i,j}.
*
* @param i
* @param j
* @return
*/
inline const REAL_T GetValue(size_t i, size_t j = 0) const {
return std::log10(expr_m.GetValue(i, j));
}
/**
* Returns true.
*
* @return
*/
inline bool IsNonlinear() const {
return true;
}
/**
* Push variable info into a set.
*
* @param ids
*/
inline void PushIds(typename atl::StackEntry<REAL_T>::vi_storage& ids)const {
expr_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 {
expr_m.PushIds(ids, i, j);
}
inline void PushNLIds(typename atl::StackEntry<REAL_T>::vi_storage& ids, bool nl = false)const {
expr_m.PushNLIds(ids, true);
}
inline const std::complex<REAL_T> ComplexEvaluate(uint32_t x, REAL_T h = 1e-20) const {
return std::log10(expr_m.ComplexEvaluate(x, h));
}
inline const REAL_T Taylor(uint32_t degree) const {
if (degree == 0) {
val_.reserve(5);
val_.resize(1);
val_[0] = std::log10(this->expr_m.Taylor(0));
return val_[0];
}
size_t l = val_.size();
val_.resize(degree + 1);
for (unsigned int i = l; i <= degree; ++i) {
val_[i] = expr_m.Taylor(i) / static_cast<REAL_T> (AD_LOG10);
for (unsigned int j = 1; j <= degree; ++j) {
val_[i] -= (static_cast<REAL_T> (1.0) -
static_cast<REAL_T> (j) / static_cast<REAL_T> (i)) *
expr_m.Taylor(j) * val_[i - j];
}
val_[i] /= expr_m.Taylor(0);
}
return val_[degree];
}
std::shared_ptr<DynamicExpressionBase<REAL_T> > ToDynamic() const {
return atl::log10(expr_m.ToDynamic());
}
/**
* Evaluates the first-order derivative with respect to x at
* index {i,j}.
*
* \f$ {{{{d}\over{d\,x}}\,f(x)}\over{\log 10\,f(x)}} \f$
*
* @param x
* @return
*/
inline const REAL_T EvaluateFirstDerivative(uint32_t x) const {
return (expr_m.EvaluateFirstDerivative(x) / (AD_LOG10 * expr_m.GetValue()));
}
/**
* Evaluates the second-order derivative with respect to x and y.
*
* \f${{{{d^2}\over{d\,x\,d\,y}}\,f(x,y)}\over{\log 10\,f(x,y
* )}}-{{{{d}\over{d\,x}}\,f(x,y)\,\left({{d}\over{d\,y}}\,
* f(x,y)\right)}\over{\log 10\,f(x,y)^2}}\f$
*
* @param x
* @param y
* @return
*/
inline REAL_T EvaluateSecondDerivative(uint32_t x, uint32_t y) const {
REAL_T fx = expr_m.GetValue();
return (expr_m.EvaluateSecondDerivative(x, y) / (AD_LOG10 * fx)) -
((expr_m.EvaluateFirstDerivative(x) * expr_m.EvaluateFirstDerivative(y)) / (AD_LOG10 * (fx * fx)));
}
/**
* Evaluates the third-order derivative with respect to x, y, and z.
*
* \f$ {{2\,\left({{d}\over{d\,x}}\,f\left(x , y , z\right)\right)\,\left(
* {{d}\over{d\,y}}\,f\left(x , y , z\right)\right)\,\left({{d}\over{d
* \,z}}\,f\left(x , y , z\right)\right)}\over{\log 10\,f^3\left(x , y
* , z\right)}}-{{{{d^2}\over{d\,x\,d\,y}}\,f\left(x , y , z\right)\,
* \left({{d}\over{d\,z}}\,f\left(x , y , z\right)\right)}\over{\log 10
* \,f^2\left(x , y , z\right)}}-{{{{d}\over{d\,x}}\,f\left(x , y , z
* \right)\,\left({{d^2}\over{d\,y\,d\,z}}\,f\left(x , y , z\right)
* \right)}\over{\log 10\,f^2\left(x , y , z\right)}}-{{{{d^2}\over{d\,
* x\,d\,z}}\,f\left(x , y , z\right)\,\left({{d}\over{d\,y}}\,f\left(x
* , y , z\right)\right)}\over{\log 10\,f^2\left(x , y , z\right)}}+{{
* {{d^3}\over{d\,x\,d\,y\,d\,z}}\,f\left(x , y , z\right)}\over{\log
* 10\,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 (2.0 * (expr_m.EvaluateFirstDerivative(x))*(expr_m.EvaluateFirstDerivative(y))
*(expr_m.EvaluateFirstDerivative(z))) / (AD_LOG10 * std::pow(expr_m.GetValue(), 3.0))
-((expr_m.EvaluateSecondDerivative(x, y))*(expr_m.EvaluateFirstDerivative(z)))
/ (AD_LOG10 * std::pow(expr_m.GetValue(), 2.0))-((expr_m.EvaluateFirstDerivative(x))
*(expr_m.EvaluateSecondDerivative(y, z))) / (AD_LOG10 * std::pow(expr_m.GetValue(), 2.0))
-((expr_m.EvaluateSecondDerivative(x, z))*(expr_m.EvaluateFirstDerivative(y)))
/ (AD_LOG10 * std::pow(expr_m.GetValue(), 2.0)) +
expr_m.EvaluateThirdDerivative(x, y, z) / (AD_LOG10 * expr_m.GetValue());
}
/**
*
* Evaluates the first-order derivative with respect to x.
*
* \f$ {{{{d}\over{d\,x}}\,f_{i,j}(x)}\over{\log 10\,f_{i,j}(x)}} \f$
*
* @param x
* @param i
* @param j
* @return
*/
inline const REAL_T EvaluateFirstDerivativeAt(uint32_t x, size_t i, size_t j = 0) const {
return (expr_m.EvaluateFirstDerivativeAt(x, i, j) / (AD_LOG10 * expr_m.GetValue(i, j)));
}
/**
* Evaluates the second-order derivative with respect to x and y at
* index {i,j}.
*
* \f${{{{d^2}\over{d\,x\,d\,y}}\,f_{i,j}(x,y)}\over{\log 10\,f_{i,j}(x,y
* )}}-{{{{d}\over{d\,x}}\,f_{i,j}(x,y)\,\left({{d}\over{d\,y}}\,f_{i,j
* }(x,y)\right)}\over{\log 10\,f_{i,j}(x,y)^2}}\f$
*
*
* @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 {
REAL_T fx = expr_m.GetValue(i, j);
return (expr_m.EvaluateSecondDerivativeAt(x, y, i, j) / (AD_LOG10 * fx)) -
((expr_m.EvaluateFirstDerivativeAt(x, i, j) * expr_m.EvaluateFirstDerivativeAt(y, i, j)) / (AD_LOG10 * (fx * fx)));
}
/**
* Evaluates the third-order derivative with respect to x, y, and z
* at index {i,j}.
*
* \f$ {{2\,\left({{d}\over{d\,x}}\,f_{i,j}(x,y,z)\right)\,\left({{d
* }\over{d\,y}}\,f_{i,j}(x,y,z)\right)\,\left({{d}\over{d\,z}}\,f_{i,j
* }(x,y,z)\right)}\over{\log 10\,f_{i,j}(x,y,z)^3}}-{{{{d^2}\over{d\,x
* \,d\,y}}\,f_{i,j}(x,y,z)\,\left({{d}\over{d\,z}}\,f_{i,j}(x,y,z)
* \right)}\over{\log 10\,f_{i,j}(x,y,z)^2}}-{{{{d}\over{d\,x}}\,f_{i,j
* }(x,y,z)\,\left({{d^2}\over{d\,y\,d\,z}}\,f_{i,j}(x,y,z)\right)
* }\over{\log 10\,f_{i,j}(x,y,z)^2}}-{{{{d^2}\over{d\,x\,d\,z}}\,f_{i,
* j}(x,y,z)\,\left({{d}\over{d\,y}}\,f_{i,j}(x,y,z)\right)}\over{\log
* 10\,f_{i,j}(x,y,z)^2}}+{{{{d^3}\over{d\,x\,d\,y\,d\,z}}\,f_{i,j}(x,y
* ,z)}\over{\log 10\,f_{i,j}(x,y,z)}} \f$
*
* @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 {
return (2.0 * (expr_m.EvaluateFirstDerivativeAt(x, i, j))*(expr_m.EvaluateFirstDerivativeAt(y, i, j))
*(expr_m.EvaluateFirstDerivativeAt(z, i, j))) / (AD_LOG10 * std::pow(expr_m.GetValue(i, j), 3.0))
-((expr_m.EvaluateSecondDerivativeAt(x, y, i, j))*(expr_m.EvaluateFirstDerivativeAt(z, i, j)))
/ (AD_LOG10 * std::pow(expr_m.GetValue(i, j), 2.0))-((expr_m.EvaluateFirstDerivativeAt(x, i, j))
*(expr_m.EvaluateDerivative(y, z, i, j))) / (AD_LOG10 * std::pow(expr_m.GetValue(i, j), 2.0))
-((expr_m.EvaluateSecondDerivativeAt(x, z, i, j))*(expr_m.EvaluateFirstDerivativeAt(y, i, j)))
/ (AD_LOG10 * std::pow(expr_m.GetValue(i, j), 2.0)) +
expr_m.EvaluateThirdDerivativeAt(x, y, z, i, j) / (AD_LOG10 * expr_m.GetValue(i, j));
}
/**
* Return the number of rows.
*
* @return
*/
size_t GetRows() const {
return expr_m.GetRows();
}
/**
* True if this expression is a scalar.
*
* @return
*/
bool IsScalar() const {
return expr_m.IsScalar();
}
/**
* Create a string representation of this expression template.
* @return
*/
const std::string ToExpressionTemplateString() const {
std::stringstream ss;
ss << "atl::Log10<T," << expr_m.ToExpressionTemplateString() << " >";
return ss.str();
}
const EXPR& expr_m;
mutable std::vector<REAL_T> val_;
};
/**
* Returns an expression template representing log10.
*
* @param exp
* @return
*/
template<class REAL_T, class EXPR>
inline const Log10<REAL_T, EXPR> log10(const ExpressionBase<REAL_T, EXPR>& exp) {
return Log10<REAL_T, EXPR>(exp.Cast());
}
// template<class REAL_T, class EXPR>
// inline const atl::Divide<REAL_T, atl::Log<REAL_T, EXPR >, atl::Real<REAL_T> > log10(const ExpressionBase<REAL_T, EXPR>& exp) {
//// const atl::Divide<REAL_T, atl::Log<REAL_T, EXPR >, atl::Real<REAL_T> > ret =
//// atl::log(exp)/static_cast<REAL_T>(AD_LOG10);
// return atl::Divide<REAL_T, atl::Log<REAL_T, EXPR >, atl::Real<REAL_T> >(atl::log(exp.Cast()),static_cast<REAL_T>(AD_LOG10));
// }
}//end namespace atl
#endif