-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntervalIterator.h
More file actions
400 lines (340 loc) · 9.87 KB
/
IntervalIterator.h
File metadata and controls
400 lines (340 loc) · 9.87 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
#ifndef INTERVALITERATOR
#define INTERVALITERATOR
//#include <gsl/gsl_spline.h>
//#include "gsl_integration.h"
#include <iostream>
#include <complex>
#include <limits>
#include <iomanip>
#include <Eigen/Dense>
#include "StringStuff.h"
#include "SimpleListInitializer.h"
inline double uniformGrid (int index, double xmin, double xmax, int xpoints)
{
if (xpoints == 1)
{
assert(abs(xmin-xmax)<numeric_limits<double>::epsilon());
return xmin;
}
else
{
double step = (xmax-xmin)/(xpoints-1);
return xmin + index*step;
}
}
class IntervalIterator
{
friend class IntervalIntWrapper;
friend double area (IntervalIterator &It, double err_abs_input, double err_rel_input);
friend void normalize (IntervalIterator &It, double err_abs_input, double err_rel_input);
public:
IntervalIterator(){};
IntervalIterator (double xmin_input, double xmax_input, int xpoints_input, double (*genfunc_input)(int,double,double,int)=uniformGrid);
int index() const;
double value() const;
int points() const {return xpoints;};
double operator*() const {return value();};
int begin (int datasets=1);
int end();
void operator++() {++curr_index;};
void operator--() {--curr_index;};
IntervalIterator& operator = (const int &comp) {curr_index=comp; return *this;}
inline double operator [] (int index) const {return data(index,0);}
inline double operator () (int index) const {return data(index,0);}
inline double coeffRef (int index) const {return data.coeffRef(index,0);};
bool operator < (const int &comp) {return(curr_index< comp)?true:false;}
bool operator > (const int &comp) {return(curr_index> comp)?true:false;}
bool operator <= (const int &comp) {return(curr_index<=comp)?true:false;}
bool operator >= (const int &comp) {return(curr_index>=comp)?true:false;}
bool operator == (const int &comp) {return(curr_index==comp)?true:false;}
bool operator != (const int &comp) {return(curr_index!=comp)?true:false;}
void insert (double x)
{
if (data.cols() != 2) {data.conservativeResize(data.rows(),2);}
data(curr_index,0) = genfunc(curr_index,xmin,xmax,xpoints);
data(curr_index,1) = x;
}
void insert (const Eigen::VectorXd &v)
{
if (data.cols() != v.rows()+1) {data.conservativeResize(data.rows(),v.rows()+1);}
data(curr_index,0) = genfunc(curr_index,xmin,xmax,xpoints);
data.row(curr_index).tail(v.rows()) = v.transpose();
}
//void operator << (double d) {insert(d);}
//void operator << (const complex<double> &x) {insert({x.real(), x.imag()});}
SimpleListInitializer operator << (double x)
{
// data.conservativeResize(data.rows(),2);
// data(curr_index,0) = genfunc(curr_index,xmin,xmax,xpoints);
data(curr_index,1) = x;
return SimpleListInitializer(&data,curr_index,2);
}
SimpleListInitializer operator << (std::complex<double> x)
{
// data.conservativeResize(data.rows(),3);
// data(curr_index,0) = genfunc(curr_index,xmin,xmax,xpoints);
data(curr_index,1) = x.real();
data(curr_index,2) = x.imag();
return SimpleListInitializer(&data,curr_index,3);
}
#ifndef DONT_USE_INITLIST
void insert (initializer_list<double> line)
{
if (data.cols()!=line.size()) {data.conservativeResize(data.rows(),line.size()+1);}
data(curr_index,0) = genfunc(curr_index,xmin,xmax,xpoints);
int i=1;
for (auto d=line.begin(); d!=line.end(); ++d, ++i)
{
data(curr_index,i) = *d;
}
}
#endif
void insert (std::complex<double> x) {insert({x.real(), x.imag()});}
void save (std::string dumpfile);
void save (std::string dumpfile, int i);
void save (std::string dumpfile, int imin, int imax);
void save_EigenMatrix (std::string dumpfile, const Eigen::MatrixXd &M);
void save_abscissa (std::string dumpfile);
void reset (double xmin_input, double xmax_input, int xpoints_input);
void print_status();
string info();
Eigen::VectorXd get_abscissa() const;
Eigen::MatrixXd get_data() const;
void set_data (const Eigen::MatrixXd &data_input);
inline int rows() {return data.rows();}
double forward_step();
double backward_step();
private:
Eigen::MatrixXd data;
double xmin;
double xmax;
int xpoints;
int curr_index;
std::string dumpfile;
double (*genfunc)(int,double,double,int);
double function (double x, void*);
};
IntervalIterator::
IntervalIterator(double xmin_input, double xmax_input, int xpoints_input, double (*genfunc_input)(int,double,double,int))
{
genfunc = genfunc_input;
reset(xmin_input, xmax_input, xpoints_input);
}
void IntervalIterator::
reset (double xmin_input, double xmax_input, int xpoints_input)
{
xmin = xmin_input;
xmax = xmax_input;
xpoints = xpoints_input;
data.resize(xpoints,2);
for (int ix=0; ix<xpoints; ++ix) {data(ix,0) = genfunc(ix,xmin,xmax,xpoints);}
curr_index=0;
}
//inline int IntervalIterator::
//begin()
//{
// //data.conservativeResize(xpoints,1);
// data.rightCols(data.cols()-1).setZero();
// return 0;
//}
inline int IntervalIterator::
begin (int datasets)
{
data.conservativeResize(xpoints,datasets+1);
data.rightCols(data.cols()-1).setZero();
return 0;
}
inline int IntervalIterator::
end()
{
return xpoints;
}
inline double IntervalIterator::
value() const
{
return genfunc(curr_index,xmin,xmax,xpoints);
}
inline int IntervalIterator::
index() const
{
return curr_index;
}
void IntervalIterator::
save (std::string dumpfile)
{
std::ofstream file(dumpfile);
int Nrows = min(curr_index+1,static_cast<int>(data.rows()));
file << setprecision(14);
for (size_t r=0; r<Nrows; ++r)
{
for (size_t c=0; c<data.cols(); ++c)
{
file << data(r,c);
if (c != data.cols()-1)
{
file << "\t";
}
}
file << endl;
}
file.close();
}
void IntervalIterator::
save (std::string dumpfile, int i)
{
std::ofstream file(dumpfile);
int Nrows = min(curr_index+1,static_cast<int>(data.rows()));
Eigen::MatrixXd temp(Nrows,2);
temp.col(0) = data.col(0).head(Nrows);
temp.col(1) = data.col(i).head(Nrows);
save_EigenMatrix(dumpfile,temp);
}
void IntervalIterator::
save (std::string dumpfile, int imin, int imax)
{
int Nrows = min(curr_index+1,static_cast<int>(data.rows()));
Eigen::MatrixXd temp(Nrows,1+imax-imin+1);
temp.col(0) = data.col(0).head(Nrows);
temp.block(0,1, Nrows,imax-imin+1) = data.block(0,imin, Nrows,imax-imin+1);
save_EigenMatrix(dumpfile,temp);
}
void IntervalIterator::
save_EigenMatrix (std::string dumpfile, const Eigen::MatrixXd &M)
{
std::ofstream file(dumpfile);
file << setprecision(14);
for (int i=0; i<M.rows(); ++i)
{
for (int j=0; j<M.cols(); ++j)
{
file << M(i,j);
if (j != M.cols()-1) file << "\t";
}
if (i != M.rows()-1) file << endl;
}
file.close();
}
void IntervalIterator::
save_abscissa (std::string dumpfile)
{
std::ofstream file(dumpfile);
file << setprecision(14) << data.col(0) << endl;
file.close();
}
void IntervalIterator::
print_status()
{
draw_progressBar(20,static_cast<double>(curr_index+1.)/static_cast<double>(xpoints));
if (curr_index==end()-1) {cout << "\x1B[2K" << "\x1B[0E";}
}
string IntervalIterator::
info()
{
std::stringstream ss;
std::string xpoints_str;// = static_cast<ostringstream*>(&(ostringstream()<<xpoints))->str();
ss << "Interval from " << xmin << " to " << xmax << ": "
<< pad_zeros(curr_index+1, xpoints_str.size()) << "/" << xpoints
<< ", x=" << round(data(curr_index,0),1);
if (data.cols() > 1)
{
ss << ", f(x) = " << data.block(curr_index,1, 1,data.cols()-1);
// for (int i=1; i<data.cols(); ++i)
// {
// cout << data(curr_index,i) << "\t";
// }
}
return ss.str();
}
double IntervalIterator::
function (double x, void*)
{
// double * x_axis;
// double * y_value;
// x_axis = (double*) malloc (xpoints * sizeof * x_axis);
// y_value = (double*) malloc (xpoints * sizeof * y_value);
// for (int i=0; i<xpoints; ++i)
// {
// x_axis[i] = data(i,0);
// y_value[i] = data(i,1);
// }
//
// gsl_interp_accel * acc_int = gsl_interp_accel_alloc();
// gsl_spline * spline_int = gsl_spline_alloc (gsl_interp_akima, xpoints);
//
// gsl_spline_init (spline_int, x_axis, y_value, xpoints);
//
// return gsl_spline_eval(spline_int, x, acc_int);
return 1.;
}
inline double IntervalIterator::
forward_step()
{
if (curr_index<=data.rows()-2)
{
return data(curr_index+1,0)-data(curr_index,0);
}
else
{
return 0.;
}
}
inline double IntervalIterator::
backward_step()
{
if (curr_index>=1)
{
return data(curr_index,0)-data(curr_index-1,0);
}
else
{
return 0.;
}
}
inline Eigen::VectorXd IntervalIterator::
get_abscissa() const
{
return data.col(0);
}
inline Eigen::MatrixXd IntervalIterator::
get_data() const
{
return data;
}
inline void IntervalIterator::
set_data (const Eigen::MatrixXd &data_input)
{
assert(data.rows() == data_input.rows());
data.conservativeResize(data_input.rows(), data_input.cols()+1);
data.block(0,1, data_input.rows(),data_input.cols()) = data_input;
}
//-------------------------------------------------------------------
class IntervalIntWrapper
{
public:
static double function_glue (double x, void *){void * empty; return fObj->function(x,empty);}
static void setObj (IntervalIterator &obj) {fObj = &obj;}
private:
static IntervalIterator * fObj;
};
IntervalIterator * IntervalIntWrapper::fObj=NULL;
//-------------------------------------------------------------------
// integrate datapoints in given interval and normalize datapoints to this area
double area (IntervalIterator &It, double err_abs_input=1e-7, double err_rel_input=1e-7)
{
// IntervalIntWrapper::setObj(It);
//
// integration_params PARAM;
// //PARAM.xl=It.data(0,0); PARAM.xr=It.data(It.data.n_rows-1,0); PARAM.yl=It.data(0,1); PARAM.yr=It.data(It.data.n_rows-1,1);
// PARAM.xl=It.data(0,0); PARAM.xr=It.data(It.data.rows()-1,0); PARAM.yl=It.data(0,1); PARAM.yr=It.data(It.data.rows()-1,1);
// PARAM.err_abs=err_abs_input; PARAM.err_rel=err_rel_input;
// PARAM.GKrule=3;
//
// return integrate(&IntervalIntWrapper::function_glue, PARAM);
return 1.;
}
void normalize (IntervalIterator &It, double err_abs_input=1e-7, double err_rel_input=1e-7)
{
double A = area(It, err_abs_input,err_rel_input);
It.data.col(1) = It.data.col(1)/A;
}
#endif