18 template <
typename Tx,
typename Ty>
class Table2D 21 typedef std::map<Tx, Ty> Tmap;
36 Tx round(Tx x) {
return (x >= 0) ? int(x / dx + 0.5) * dx : int(x / dx - 0.5) * dx; }
63 Table2D(Tx resolution = 0.2, type key = XYDATA)
66 setResolution(resolution);
70 std::map<std::string, std::vector<double>>
to_map()
72 std::map<std::string, std::vector<double>> m;
73 m[
"x"].reserve(map.size());
74 m[
"y"].reserve(map.size());
76 m[
"x"].push_back(i.first);
77 m[
"y"].push_back(
get(i.first));
82 void clear() { map.clear(); }
84 void setResolution(Tx resolution)
86 assert(resolution > 0);
91 void setResolution(std::vector<Tx>& resolution)
93 assert(resolution[0] > 0);
102 Ty&
operator()(std::vector<Tx>& x) {
return map[round(x[0])]; }
108 auto it = map.find(round(x[0]));
115 template <
class T =
double>
void save(
const std::string& filename,
T scale = 1,
T translate = 0)
117 if (tabletype == HISTOGRAM) {
119 map.begin()->second *= 2;
121 (--map.end())->second *= 2;
125 std::ofstream f(filename.c_str());
129 f << m.first <<
" " << (m.second + translate) * scale <<
"\n";
133 if (tabletype == HISTOGRAM) {
135 map.begin()->second /= 2;
137 (--map.end())->second /= 2;
142 template <
class T =
double>
void normSave(
const std::string& filename)
144 if (tabletype == HISTOGRAM) {
146 map.begin()->second *= 2;
148 (--map.end())->second *= 2;
152 std::ofstream f(filename.c_str());
154 Ty cnt = count() * dx;
157 f << m.first <<
" " << m.second / cnt <<
"\n";
161 if (tabletype == HISTOGRAM) {
163 map.begin()->second /= 2;
165 (--map.end())->second /= 2;
170 template <
class T =
double>
void sumSave(std::string filename,
T scale = 1)
172 if (tabletype == HISTOGRAM) {
174 map.begin()->second *= 2;
176 (--map.end())->second *= 2;
180 std::ofstream f(filename.c_str());
184 for (
auto& m : map) {
186 f << m.first <<
" " << sum_t * scale <<
"\n";
191 if (tabletype == HISTOGRAM) {
193 map.begin()->second /= 2;
195 (--map.end())->second /= 2;
199 const Tmap& getMap()
const {
return map; }
201 Tmap& getMap() {
return map; }
203 Tx getResolution() {
return dx; }
208 assert(!map.empty());
211 avg += m.first * m.second;
212 return avg / count();
218 assert(!map.empty());
222 std2 += m.second * (m.first -
avg) * (m.first - avg);
223 return sqrt(std2 / count());
227 typename Tmap::const_iterator
min()
229 assert(!map.empty());
230 Ty
min = std::numeric_limits<Ty>::max();
231 typename Tmap::const_iterator it;
232 for (
auto m = map.begin(); m != map.end(); ++m)
233 if (m->second < min) {
241 typename Tmap::const_iterator
max()
243 assert(!map.empty());
244 Ty
max = std::numeric_limits<Ty>::min();
245 typename Tmap::const_iterator it;
246 for (
auto m = map.begin(); m != map.end(); ++m)
247 if (m->second > max) {
257 assert(!map.empty());
259 for (
auto& m : map) {
267 Ty
avg(
const std::vector<Tx>& limits)
271 assert(!map.empty());
272 for (
auto& m : map) {
273 if (m.first >= limits[0] && m.first <= limits[1]) {
288 std::vector<double> sendBuf;
289 assert(!map.empty());
290 for (
auto& m : map) {
291 sendBuf.push_back(m.first);
292 sendBuf.push_back(m.second);
294 sendBuf.resize(size, -1);
305 std::map<double, Average<double>> all;
306 for (
int i = 0; i < int(v.size()) - 1; i += 2)
307 if (v.at(i + 1) != -1)
308 all[v.at(i)] += v.at(i + 1);
319 bool load(
const std::string& filename)
321 std::ifstream f(filename.c_str());
330 if (tabletype == HISTOGRAM) {
332 map.begin()->second /= 2;
334 (--map.end())->second /= 2;
346 assert(!this->map.empty() &&
"Map is empty!");
347 Eigen::MatrixXd
table(2, map.size());
350 for (
auto& m : this->map) {
351 table(0, I) = m.first;
352 table(1, I) = m.second;
364 assert(a.tabletype == b.tabletype &&
"Table a and b needs to be of same type");
365 Table2D<Tx, Ty> c(std::min(a.getResolution(), b.getResolution()), a.tabletype);
366 auto a_map = a.getMap();
367 auto b_map = b.getMap();
371 a_map.begin()->second *= 2;
372 if (a_map.size() > 1)
373 (--a_map.end())->second *= 2;
375 b_map.begin()->second *= 2;
376 if (b_map.size() > 1)
377 (--b_map.end())->second *= 2;
380 for (
auto& m1 : a_map) {
381 for (
auto& m2 : b_map) {
382 c(m1.first) = m1.second - m2.second;
389 a_map.begin()->second /= 2;
390 if (a_map.size() > 1)
391 (--a_map.end())->second /= 2;
393 b_map.begin()->second /= 2;
394 if (b_map.size() > 1)
395 (--b_map.end())->second /= 2;
405 assert(a.tabletype == b.tabletype &&
"Table a and b needs to be of same type");
406 Table2D<Tx, Ty> c(std::min(a.getResolution(), b.getResolution()), a.tabletype);
407 auto a_map = a.getMap();
408 auto b_map = b.getMap();
412 a_map.begin()->second *= 2;
413 if (a_map.size() > 1)
414 (--a_map.end())->second *= 2;
416 b_map.begin()->second *= 2;
417 if (b_map.size() > 1)
418 (--b_map.end())->second *= 2;
421 for (
auto& m : a_map) {
422 c(m.first) += m.second;
424 for (
auto& m : b_map) {
425 c(m.first) += m.second;
430 a_map.begin()->second /= 2;
431 if (a_map.size() > 1)
432 (--a_map.end())->second /= 2;
434 b_map.begin()->second /= 2;
435 if (b_map.size() > 1)
436 (--b_map.end())->second /= 2;
Tmap::const_iterator min()
Definition: table_2d.h:227
Ty & operator()(Tx x)
Access operator - returns reference to y(x)
Definition: table_2d.h:99
Table2D< Tx, Ty > operator-(Table2D< Tx, Ty > &a, Table2D< Tx, Ty > &b)
Subtract two tables.
Definition: table_2d.h:362
double T
floating point size
Definition: units.h:73
General class for handling 2D tables - xy data, for example.
Definition: table_2d.h:18
Tx mean()
Definition: table_2d.h:206
Table2D(Tx resolution=0.2, type key=XYDATA)
Constructor.
Definition: table_2d.h:63
Ty avg(const std::vector< Tx > &limits)
Definition: table_2d.h:267
Ty sumy() const
Sum of all y values (same as count())
Definition: table_2d.h:50
Ty find(std::vector< Tx > &x)
Find key and return corresponding value otherwise zero.
Definition: table_2d.h:105
std::map< std::string, std::vector< double > > to_map()
Convert to map.
Definition: table_2d.h:70
Tx std()
Definition: table_2d.h:216
Cell list class templates.
Definition: actions.cpp:11
void buf2hist(std::vector< double > &v)
Convert vector of floats to table2D.
Definition: table_2d.h:301
void sumSave(std::string filename, T scale=1)
Sums up all previous elements and saves table to disk.
Definition: table_2d.h:170
std::vector< double > hist2buf(int &size)
Convert table2D to vector of floats.
Definition: table_2d.h:286
Table2D< Tx, Ty > operator+(Table2D< Tx, Ty > &a, Table2D< Tx, Ty > &b)
Addition two tables.
Definition: table_2d.h:403
bool load(const std::string &filename)
Load table from disk.
Definition: table_2d.h:319
Tmap::const_iterator max()
Definition: table_2d.h:241
Ty & operator()(std::vector< Tx > &x)
Access operator - returns reference to y(x)
Definition: table_2d.h:102
void save(const std::string &filename, T scale=1, T translate=0)
Save table to disk.
Definition: table_2d.h:115
Tx minx()
Definition: table_2d.h:255
void normSave(const std::string &filename)
Save normalized table to disk.
Definition: table_2d.h:142
Eigen::MatrixXd tableToMatrix()
Convert table to matrix.
Definition: table_2d.h:344