-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_helper.h
More file actions
452 lines (366 loc) · 14.2 KB
/
Copy pathmath_helper.h
File metadata and controls
452 lines (366 loc) · 14.2 KB
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#ifndef __CXX_UTIL_MATH_HELPER_H__
#define __CXX_UTIL_MATH_HELPER_H__
#include <armadillo>
#include "arma_helper.h"
#include <functional>
#include <iostream>
namespace cxut {
inline bool null_qr(arma::mat& ns, arma::mat const& A) {
if (A.is_empty()) {
ns.clear();
return true;
}
arma::mat q, r;
bool status = arma::qr(q, r, A.t());
if (status) {
arma::vec s = arma::sum(arma::abs(r), 1);
double tol = arma::datum::eps * std::max(A.n_rows, A.n_cols);
ns = q.cols(arma::find(s<tol));
}
return status;
}
inline arma::mat null_qr(arma::mat const& A) {
arma::mat ns;
bool status = null_qr(ns, A);
if (!status) {
std::cout << "null_qr(): qr decomposition failed." << std::endl;
exit(EXIT_FAILURE);
}
return ns;
}
inline arma::mat orth_lowdin(arma::mat const& A) {
arma::vec eigval;
arma::mat eigvec;
arma::eig_sym(eigval, eigvec, A*A.t());
return arma::solve(eigvec*arma::diagmat(arma::sqrt(eigval))*eigvec.t(), A);
}
// find the smallest/largest number
template <typename T1, typename T2>
auto min(T1 const& i, T2 const& j) {
return (i < j) ? i : j;
}
template <typename T1, typename T2, typename ...Ts>
auto min(T1 const& i, T2 const& j, Ts const& ...args) {
auto tmp = min(j, args...);
return (i < tmp) ? i : tmp;
}
template <typename T1, typename T2>
auto max(T1 const& i, T2 const& j) {
return (i > j) ? i : j;
}
template <typename T1, typename T2, typename ...Ts>
auto max(T1 const& i, T2 const& j, Ts const& ...args) {
auto tmp = max(j, args...);
return (i > tmp) ? i : tmp;
}
// first-order numerical differentiation by finite difference
inline std::function<double(double)> grad(std::function<double(double)> const& f, double const& delta = 1e-3) {
return [=] (double x) -> double {
double m1 = f(x+delta) - f(x-delta);
double m2 = f(x+2.0*delta) - f(x-2.0*delta);
double m3 = f(x+3.0*delta) - f(x-3.0*delta);
return (m3 - 9.0*m2 + 45.0*m1) / (60.0*delta);
};
}
// (d/dxi) f(x0,x1,...)
template <typename V>
std::function<double(V)> gradi(std::function<double(V)> const& f, size_t const& i, double const& delta = 1e-3) {
return [=] (V const& v) -> double {
std::function<double(double)> g = [=, v=v] (double const& x) mutable {
v[i] = x;
return f(v);
};
return grad(g, delta)(v[i]);
};
}
// grad f(x0,x1,...)
template <typename V>
std::function<V(V)> grad(std::function<double(V)> const& f, double const& delta = 1e-3) {
return [=] (V const& x) -> V {
V df = x;
for (size_t i = 0; i != x.size(); ++i) {
df[i] = gradi(f, i, delta)(x);
}
return df;
};
}
// generate 1D grid points according to some grid density
inline arma::vec grid1d(double const& xmin, double const& xmax, std::function<double(double)> density, double const& x0) {
if (x0 < xmin || x0 > xmax)
return arma::vec{};
arma::vec gr = {x0};
double gr_last = gr.back();
while ( gr_last < xmax ) {
gr.resize(gr.n_elem+1);
gr.back() = gr_last + 1.0 / density(gr_last);
gr_last = gr.back();
}
arma::vec gl = {x0};
double gl_last = gl.back();
while ( gl_last > xmin ) {
gl.resize(gl.n_elem+1);
gl.back() = gl_last - 1.0 / density(gl_last);
gl_last = gl.back();
}
return join_cols(flipud(gl.tail(gl.n_elem-1)), gr);
}
inline arma::vec grid1d(double const& xmin, double const& xmax, std::function<double(double)> density) {
return grid1d(xmin, xmax, density, xmin);
}
// Broyden's quasi-Newton method
// the good and bad Broyden's methods are identical in 1D
inline int broydenroot(std::function<double(double)> f, double& x, double const& beta = 0.7, double const& tol = 1e-12, unsigned int const& max_iter = 50) {
double fx = f(x);
if (std::abs(fx) < tol)
return 0;
// compute the initial Jacobian by finite difference
double delta = 1e-6 * std::max(1.0, std::sqrt(std::abs(x)));
double J = (f(x+delta) - fx) / delta;
double dx = 0.0;
double fx_new = 0.0;
for (unsigned int counter = 1; counter != max_iter; ++counter) {
if (std::abs(J) < 1e-14) {
std::cout << "broydenroot: the Jacobian appears to be singular." << std::endl;
return 2;
}
dx = -fx / J * beta;
x += dx;
fx_new = f(x);
if (std::abs(fx_new) < tol)
return 0;
J = (fx_new - fx) / dx;
fx = fx_new;
}
std::cout << "broydenroot: fails to find the root." << std::endl;
return 1;
}
inline int broydenroot(std::function<arma::vec(arma::vec)> f, arma::vec& x, double const& beta = 0.7, double const& tol = 1e-12, unsigned int const& max_iter = 50, std::string const& method = "good") {
arma::vec fx = f(x);
if (arma::norm(fx) < tol)
return 0;
int md = -1;
if (!method.compare("good"))
md = 0;
if (!method.compare("inv"))
md = 1;
if (!method.compare("bad"))
md = 2;
if ( md < 0 ) {
std::cerr << "broydenroot: invalid method \"" << method << "\", use default instead. " << std::endl;
md = 0;
}
arma::uword len_x = x.n_elem;
arma::uword len_f = fx.n_elem;
// compute the initial Jacobian by finite difference
arma::mat J(len_f, len_x);
double delta = 1e-6 * max(1.0, std::sqrt(arma::norm(x)));
arma::vec dxi(len_x);
arma::vec df(len_f);
for (arma::uword i = 0; i != len_x; ++i) {
dxi.zeros();
dxi(i) = delta;
df = f(x+dxi) - fx;
J.col(i) = df / delta;
}
arma::mat invJ;
if ( md > 0 ) {
if (len_x == len_f) {
bool info = arma::inv(invJ, J);
if (!info) {
std::cout << "broydenroot: the Jacobian appears to be singular." << std::endl;
return 2;
}
} else {
std::cerr << "broydenroot: inverse update requires the number of equations equals to the number of variables." << std::endl;
exit(EXIT_FAILURE);
}
}
arma::vec dx(len_x);
arma::vec fx_new(len_f);
for (unsigned int counter = 1; counter != max_iter; ++counter) {
if (md > 0) {
dx = -invJ * fx * beta;
} else {
dx = -arma::solve(J, fx) * beta;
}
x += dx;
fx_new = f(x);
if (arma::norm(fx_new) < tol)
return 0;
df = fx_new - fx;
fx = fx_new;
// Broyden's update
switch (md) {
case 2:
invJ += ( dx - invJ * df ) / arma::dot(df, df) * df.as_row();
break;
case 1:
invJ += ( dx - invJ * df ) / arma::dot(dx, invJ * df) * dx.as_row() * invJ;
break;
default:
J += ( df - J * dx ) / arma::dot(dx, dx) * dx.as_row();
}
}
std::cout << "broydenroot: fails to find the root." << std::endl;
return 1;
}
inline int diis(std::function<double(double)> iter, double& x, double tol = 1e-8, size_t const& max_iter = 50, size_t const& max_subspace = 20) {
double xdiis = iter(x);
double r = xdiis - x;
x = xdiis;
arma::rowvec xs = {x};
arma::rowvec rs = {r};
arma::mat B = rs.t() * rs;
auto diismat = [&B] () -> arma::mat {
return arma::join_cols(
join_rows(B, arma::ones(B.n_rows, 1)),
join_rows(arma::ones(1, B.n_cols), arma::mat{0.0})
);
};
auto diisvec = [&B] () -> arma::vec {
return join_cols(arma::zeros(B.n_cols), arma::vec{1.0});
};
size_t counter = 0;
while (counter < max_iter) {
if ( B.n_cols > max_subspace || arma::rcond(diismat()) < 1e-14 ) {
rs.shed_col(0);
xs.shed_col(0);
B.shed_col(0);
B.shed_row(0);
continue;
}
++counter;
xdiis = arma::dot( xs, arma::solve(diismat(), diisvec()).eval().head_rows(B.n_cols) );
x = iter(xdiis);
r = x - xdiis;
if (std::abs(r) < tol) {
return 0;
}
xs.insert_cols(xs.n_cols, arma::vec{x});
rs.insert_cols(rs.n_cols, arma::vec{r});
B.resize(B.n_rows+1, B.n_cols+1);
B.row(B.n_rows-1) = rs.col(rs.n_cols-1).t() * rs;
B.col(B.n_cols-1) = B.row(B.n_rows-1).t();
}
std::cerr << "DIIS fails to converge." << std::endl;
return 1;
}
inline int diis(std::function< std::tuple<double, double>(double) > iter_err, double& x, double tol = 1e-8, size_t const& max_iter = 50, size_t const& max_subspace = 20) {
double r;
std::tie(x, r) = iter_err(x);
arma::rowvec xs = {x};
arma::rowvec rs = {r};
arma::mat B = rs.t() * rs;
auto diismat = [&B] () -> arma::mat {
return arma::join_cols(
join_rows(B, arma::ones(B.n_rows, 1)),
join_rows(arma::ones(1, B.n_cols), arma::mat{0.0})
);
};
auto diisvec = [&B] () -> arma::vec {
return join_cols(arma::zeros(B.n_cols), arma::vec{1.0});
};
size_t counter = 0;
while (counter < max_iter) {
if ( B.n_cols > max_subspace || arma::rcond(diismat()) < 1e-14 ) {
rs.shed_col(0);
xs.shed_col(0);
B.shed_col(0);
B.shed_row(0);
continue;
}
++counter;
std::tie(x, r) = iter_err( arma::dot( xs, arma::solve(diismat(), diisvec()).eval().head_rows(B.n_cols) ) );
if (std::abs(r) < tol) {
return 0;
}
xs.insert_cols(xs.n_cols, arma::vec{x});
rs.insert_cols(rs.n_cols, arma::vec{r});
B.resize(B.n_rows+1, B.n_cols+1);
B.row(B.n_rows-1) = rs.col(rs.n_cols-1).t() * rs;
B.col(B.n_cols-1) = B.row(B.n_rows-1).t();
}
std::cerr << "DIIS fails to converge." << std::endl;
return 1;
}
inline int diis(std::function<arma::vec(arma::vec)> iter, arma::vec& x, double tol = 1e-8, size_t const& max_iter = 50, size_t const& max_subspace = 20) {
arma::vec xdiis = iter(x);
arma::vec r = xdiis - x;
x = xdiis;
arma::mat xs = x;
arma::mat rs = r;
arma::mat B = rs.t() * rs;
auto diismat = [&B] () -> arma::mat {
return arma::join_cols(
join_rows(B, arma::ones(B.n_rows, 1)),
join_rows(arma::ones(1, B.n_cols), arma::mat{0.0})
);
};
auto diisvec = [&B] () -> arma::vec {
return join_cols(arma::zeros(B.n_cols), arma::vec{1.0});
};
size_t counter = 0;
while (counter < max_iter) {
if ( B.n_cols > max_subspace || arma::rcond(diismat()) < 1e-14 ) {
rs.shed_col(0);
xs.shed_col(0);
B.shed_col(0);
B.shed_row(0);
continue;
}
++counter;
xdiis = xs * arma::solve(diismat(), diisvec()).eval().head_rows(B.n_cols);
x = iter(xdiis);
r = x - xdiis;
if (arma::norm(r) < tol) {
return 0;
}
xs.insert_cols(xs.n_cols, x);
rs.insert_cols(rs.n_cols, r);
B.resize(B.n_rows+1, B.n_cols+1);
B.row(B.n_rows-1) = rs.col(rs.n_cols-1).t() * rs;
B.col(B.n_cols-1) = B.row(B.n_rows-1).t();
}
std::cerr << "DIIS fails to converge." << std::endl;
return 1;
}
inline int diis(std::function< std::tuple<arma::vec, arma::vec>(arma::vec) > iter_err, arma::vec& x, double tol = 1e-8, size_t const& max_iter = 50, size_t const& max_subspace = 20) {
arma::vec r;
std::tie(x, r) = iter_err(x);
arma::mat xs = x;
arma::mat rs = r;
arma::mat B = rs.t() * rs;
auto diismat = [&B] () -> arma::mat {
return arma::join_cols(
join_rows(B, arma::ones(B.n_rows, 1)),
join_rows(arma::ones(1, B.n_cols), arma::mat{0.0})
);
};
auto diisvec = [&B] () -> arma::vec {
return join_cols(arma::zeros(B.n_cols), arma::vec{1.0});
};
size_t counter = 0;
while (counter < max_iter) {
if ( B.n_cols > max_subspace || arma::rcond(diismat()) < 1e-14 ) {
rs.shed_col(0);
xs.shed_col(0);
B.shed_col(0);
B.shed_row(0);
continue;
}
++counter;
std::tie(x, r) = iter_err(xs * arma::solve(diismat(), diisvec()).eval().head_rows(B.n_cols));
if (arma::norm(r) < tol) {
return 0;
}
xs.insert_cols(xs.n_cols, x);
rs.insert_cols(rs.n_cols, r);
B.resize(B.n_rows+1, B.n_cols+1);
B.row(B.n_rows-1) = rs.col(rs.n_cols-1).t() * rs;
B.col(B.n_cols-1) = B.row(B.n_rows-1).t();
}
std::cerr << "DIIS fails to converge." << std::endl;
return 1;
}
}
#endif