-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpractice.cpp
286 lines (209 loc) · 6.58 KB
/
practice.cpp
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
#include <iostream>
#include <stan/math.hpp>
#include <Eigen/Core>
#include <boost/math/tools/promotion.hpp>
using namespace Eigen;
using namespace std;
#include "compute_jacobian.h"
// template <typename T1, typename T2, typename T3>
// inline
// typename boost::math::tools::promote_args<T1, T2, T3>::type
// normal_log(const T1& y, const T2& mu, const T3& sigma) {
// using std::pow; using std::log;
// return -0.5 * pow((y - mu) / sigma, 2.0)
// -log(sigma)
// -0.5 * log(2 * stan::math::pi());
// }
// int increment(int x) {
// return x+1;
// }
// class Func {
// public:
// void operator() (const string& str) const {
// cout << str << endl;
// }
// };
// class StringAppend {
// public:
// explicit StringAppend(const string& str) : ss(str) {}
// void operator() (const string& str) const {
// cout << str << " " << ss << endl;
// }
// private:
// const string ss;
// };
// class ShorterThan {
// public:
// explicit ShorterThan(int maxLength) : length(maxLength) {}
// bool operator() (const string& str) const {
// return str.length() < length;
// }
// private:
// const int length;
// };
// struct normal_ll {
// const Matrix<double, Dynamic, 1> y_;
// normal_ll(const Matrix<double, Dynamic, 1>& y) : y_(y) {}
// template <typename T>
// T operator() (const Matrix<T, Dynamic, 1>& theta) const {
// T mu = theta[0];
// T sigma = theta[1];
// T lp = 0;
// for (int n = 0; n < y_.size(); ++n) {
// lp += normal_log(y_[n], mu, sigma);
// }
// return lp;
// }
// };
// bool is_greater_than_5(int value) {
// return (value > 5);
// }
template <typename T>
Eigen::Matrix<T, Eigen::Dynamic, 1> f( const Eigen::Matrix<T, Eigen::Dynamic, 1>& x)
{
Eigen::SparseMatrix<T> G(20,20);
std::vector<Eigen::Triplet<T> > ijv;
ijv.emplace_back(0,0,x(0));
ijv.emplace_back(1,1,x(1));
ijv.emplace_back(2,2,x(2));
ijv.emplace_back(3,3,x(3));
for(int d = 4;d<20;d++) ijv.emplace_back(d,d,1);
G.setFromTriplets(ijv.begin(),ijv.end());
G.makeCompressed();
return (Eigen::RowVectorXd::Constant(1,20,1) * G).transpose();
}
int x = 5;
void cool() {
cout << ::x << endl;
}
int main(int argc, char *argv[])
{
// Matrix<double, Dynamic, 1> M(2);
// M << 1.,
// 2.;
// std::cout << M.size() << std::endl;
// std::cout << "log normal(1 | 2, 3)="
// << stan::math::normal_log(1, 2, 3)
// << std::endl;
// using std::pow;
// double y = 1.3;
// stan::math::var mu = 0.5, sigma = 1.2;
// stan::math::var lp = 0;
// lp -= 0.5 * log(2 * stan::math::pi());
// lp -= log(sigma);
// lp -= 0.5 * pow((y - mu) / sigma, 2);
// std::cout << "f(mu, sigma) = " << lp.val() << std::endl;
// // // way 1
// // lp.grad();
// // std::cout << " d.f / d.u = " << mu.adj()
// // << " d.f / d.sigma = " << sigma.adj() << std::endl;
// // way 2
// std::vector<stan::math::var> theta;
// theta.push_back(mu);
// theta.push_back(sigma);
// std::vector<double> g;
// lp.grad(theta, g);
// std::cout << " d.f / d.mu = " << g[0]
// << " d.f / d.sigma = " << g[1] << std::endl;
// double y = 1.3;
// stan::math::var mu = 0.5, sigma = 1.2;
// stan::math::var lp = normal_log(y, mu, sigma);
// int x = 5;
// int y = increment(x);
// std::cout << y << std::endl;
// Func myFunc;
// myFunc("Hello World!");
// A(0, 0) = x;
// for (int i = 0; i < 100; i++) {
// y = sin(x+y);
// }
// std::cout << "value = " << y.val() << std::endl;
// y.grad();
// std::cout << " d.f / d.x = " << x.adj()
// << " d.f / d.y = " << y.adj() << std::endl;
// stan::math::var x = 0.5, y = 0.5;
// Matrix<stan::math::var, 3, 1> A;
// A(0, 0) = x;
// A(0, 1) = 5.0;
// Matrix<double, Dynamic, Dynamic> A(4, 4);
// A.resize(5, 5);
// Matrix<float,3*4,3> M_4(3*4, 3);
// Matrix<double, Dynamic, 1> B(3);
// stan::math::var x = 0.5, y = 0.5;
// Matrix<stan::math::var, 3, 1> A;
// A(0, 0) = x;
// A(1, 0) = 5.0;
// A(2, 0) = x*x+y;
// std::cout << A << std::endl;
// A(2).grad();
// std::cout << x.adj() << std::endl;
// vector<Triplet<stan::math::var>> triplets;
// auto lambda = [] () {cout << "reach here" << endl;};
// lambda();
// auto sum = [] (int x, int y) {return x+y;};
// cout << sum(2, 3) << endl;
// cout << sum(3, 4) << endl;
// vector<int> numbers{1, 5, 10, 20};
// auto num = count_if(numbers.begin(), numbers.end(), [] (int x) {return x>5;});
// cout << num << endl;
// stan::math::var x = 0.5, y = 0.5;
// Matrix<stan::math::var, 3, 1> A;
// Matrix<stan::math::var, 1, 3> B;
// A << 1, 2, 3;
// B << 4, 5, 6;
// auto C = B*A;
// cout << C << endl;
// Matrix<stan::math::var, 3, 1> A;
// Matrix<double, 1, 3> B;
// A << 1, 2, 3;
// B << 4, 5, 6;
// auto C = B*A;
// cout << C << endl;
// stan::math::var a = 5;
// double b = 3;
// cout << a*b << endl;
// Matrix<stan::math::var, 3, 2> A;
// A << 1, 2, 3,
// 4, 5, 6;
// cout << A.row(1) << endl;
// SparseMatrix<stan::math::var> K;
// K.resize(100, 100);
// Matrix<double, Dynamic, 1> u(100);
// u.setZero();
// auto f = K*u;
// cout << f.bottomRows(10) << endl;
Eigen::VectorXd x = Eigen::VectorXd::Constant(20,1,1);
Eigen::VectorXd fx;
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> J;
stan::math::start_nested();
Matrix<stan::math::var, Dynamic, 1> x_var(x.size());
for (int k = 0; k < x.size(); ++k) {
x_var(k) = x(k);
}
Matrix<stan::math::var, Dynamic, 1> fx_var = f(x_var);
fx.resize(fx_var.size());
for (int i = 0; i < fx_var.size(); ++i) {
fx(i) = fx_var(i).val();
}
J.resize(fx_var.size(), x.size());
for (int i = 0; i < fx_var.size(); ++i) {
if (i > 0)
stan::math::set_zero_all_adjoints_nested();
stan::math::grad(fx_var(i).vi_);
for (int k = 0; k < x.size(); ++k)
J(i, k) = x_var(k).adj();
}
stan::math::recover_memory_nested();
cout << J << endl;
// cout << ::x << endl;
// ::x = ::x + 1;
// cout << ::x << endl;
cool();
::x = ::x + 1;
cool();
return 0;
}
// stan::math::jacobian(f<stan::math::var>, x, fx, J);
// cout << fx << endl;
// cout << "--------------" << endl;
// cout << f_x_var << endl;