21#include <boost/variant.hpp>
31 return std::stoi(str);
34inline long to_numeric<long>(
const std::string &str) {
36 return std::stol(str);
42inline int to_numeric<int>(
const std::string &str) {
43 return std::stoi(str);
45 return std::stoi(str);
51inline float to_numeric<float>(
const std::string &str) {
52 return std::stof(str);
54 return std::stof(str);
60inline double to_numeric<double>(
const std::string &str) {
62 return std::stod(str);
70bool AlwaysTrue(T t){
return true;}
73bool AlwaysFalse(T t){
return false;}
76T vec_sum(
const std::vector<T> &v){
78 for(
const T &a : v) sum += a;
101void Matrix(T **matrix,
long rows,
long cols){
102 matrix =
new T*[rows];
103 matrix[0] =
new T[rows*cols];
104 for (
long i = 1; i < rows; ++i)
105 matrix[i] = matrix[0] + i * cols;
109void free_Matrix(T **matrix,
long rows,
long){
110 if (rows)
delete[] matrix[0];
114PosType **PosTypeMatrix(
size_t rows,
size_t cols);
115void free_PosTypeMatrix(PosType **matrix,
size_t rows,
size_t cols);
116PosType **PosTypeMatrix(
long rows1,
long rows2,
long cols1,
long cols2);
117void free_PosTypeMatrix(PosType **matrix,
long rows1,
long rows2,
long cols1,
long cols2);
123 D3Matrix(
size_t xsize,
size_t ysize,
size_t zsize)
124 :xn(xsize),yn(ysize),zn(zsize){
125 array =
new T[xn*yn*zn];
131 T& operator()(
size_t i,
size_t j,
size_t k){
132 return array[i + j*xn + k*xn*yn];
135 T& operator()(
size_t m){
139 size_t xindex(
size_t m){
142 size_t yindex(
size_t m){
143 return (m % (xn*yn) ) / xn;
146 size_t zindex(
size_t m){
164 :xn(xsize),yn(ysize){
165 array =
new T[xn*yn];
171 T& operator()(
size_t i,
size_t j){
172 return array[i + j*xn];
175 T& operator()(
size_t m){
179 size_t xindex(
size_t m){
182 size_t yindex(
size_t m){
208 T& operator()(
int i,
int j){
211 size_t k = (i <= j ) ? j + (m-i)*i/2 : i + (m-j)*j/2;
216 T& operator[](
size_t k){
220 int size(){
return n;}
224 return (i <= j ) ? j + (m-i)*i/2 : i + (m-j)*j/2;
270template<
typename BaseT>
274 template<
typename ValueT = BaseT>
278 typedef typename std::vector<BaseT*>::iterator base_iterator;
282 typedef ValueT value_type;
283 typedef ValueT* pointer;
284 typedef const ValueT* const_pointer;
285 typedef ValueT& reference;
286 typedef const ValueT& const_reference;
287 typedef typename base_iterator::difference_type difference_type;
288 typedef typename base_iterator::iterator_category iterator_category;
291 iterator(base_iterator i) : it(i) {}
292 iterator(
const iterator& other) : it(other.it) {}
294 iterator& operator=(
const iterator& rhs) { it = rhs.it;
return *
this; }
296 reference operator*() {
return (reference)(**it); }
300 pointer operator->() {
return (pointer)(*it); }
301 const_pointer operator->()
const {
return (const_pointer)(*it); }
303 iterator& operator++() { ++it;
return *
this; }
304 iterator operator++(
int) { iterator tmp(*
this); ++it;
return tmp; }
305 iterator& operator--() { --it;
return *
this; }
306 iterator operator--(
int) { iterator tmp(*
this); --it;
return tmp; }
308 bool operator==(
const iterator& rhs)
const {
return (it == rhs.it); }
309 bool operator!=(
const iterator& rhs)
const {
return (it != rhs.it); }
311 iterator& operator+=(difference_type n) { it += n;
return *
this; }
312 iterator& operator-=(difference_type n) { it -= n;
return *
this; }
314 reference operator[](difference_type n) {
return (reference)*it[n]; }
318 friend iterator operator+(
const iterator& i, difference_type n) {
return iterator(i.it + n); }
319 friend iterator operator+(difference_type n,
const iterator& i) {
return iterator(i.it + n); }
320 friend iterator operator-(
const iterator& i, difference_type n) {
return iterator(i.it - n); }
321 friend iterator operator-(difference_type n,
const iterator& i) {
return iterator(i.it - n); }
323 friend difference_type operator-(
const iterator& b,
const iterator& a) {
return (b.it - a.it); }
325 friend bool operator<(
const iterator&a,
const iterator& b) {
return (a.it < b.it); }
326 friend bool operator>(
const iterator&a,
const iterator& b) {
return (a.it > b.it); }
327 friend bool operator<=(
const iterator&a,
const iterator& b) {
return (a.it <= b.it); }
328 friend bool operator>=(
const iterator&a,
const iterator& b) {
return (a.it >= b.it); }
348 swap(a.items, b.items);
349 swap(a.tmap, b.tmap);
361 template<
typename Sub
classT>
368 SubclassT* copy =
new SubclassT(obj);
371 items.push_back(copy);
374 tmap[
typeid(SubclassT)].push_back(copy);
382 BaseT* back = items.back();
385 tmap[back->type()].pop_back();
395 template<
typename Sub
classT>
399 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
400 if(found == tmap.end())
404 found->second.pop_back();
407 for(
typename base_container::reverse_iterator it = items.rbegin(); it != items.rend(); ++it)
409 if((*it)->type() ==
typeid(SubclassT))
422 for(std::size_t i = 0, n = items.size(); i < n; ++i)
429 template<
typename Sub
classT>
433 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
434 if(found == tmap.end())
437 base_container& titems = found.second;
440 for(std::size_t i = 0, n = titems.size(); i < n; ++i)
447 items.erase(std::remove_if(items.begin(), items.end(), is_type<SubclassT>), items.end());
463 template<
typename Sub
classT>
466 return is_type<SubclassT>(items[i]);
488 const BaseT&
get(std::size_t i)
const
494 template<
typename Sub
classT>
495 SubclassT&
get(std::size_t i)
497 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
498 if(found == tmap.end())
499 throw std::out_of_range(std::string() +
"type " +
typeid(SubclassT).name() +
" not in vector");
500 return (SubclassT&)*found->second[i];
504 template<
typename Sub
classT>
505 const SubclassT&
get(std::size_t i)
const
507 typename tmap_t::const_iterator found = tmap.find(
typeid(SubclassT));
508 if(found == tmap.end())
509 throw std::out_of_range(std::string() +
"type " +
typeid(SubclassT).name() +
" not in vector");
510 return (
const SubclassT&)*found->second[i];
514 BaseT&
at(std::size_t i)
520 const BaseT&
at(std::size_t i)
const
529 template<
typename Sub
classT>
530 SubclassT&
at(std::size_t i)
532 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
533 if(found == tmap.end())
534 throw std::out_of_range(std::string() +
"type " +
typeid(SubclassT).name() +
" not in vector");
535 return (SubclassT&)*found->second.at(i);
539 template<
typename Sub
classT>
540 const SubclassT&
at(std::size_t i)
const
542 typename tmap_t::const_iterator found = tmap.find(
typeid(SubclassT));
543 if(found == tmap.end())
544 throw std::out_of_range(std::string() +
"type " +
typeid(SubclassT).name() +
" not in vector");
545 return (
const SubclassT&)*found->second.at(i);
555 template<
typename Sub
classT>
558 typename tmap_t::const_iterator found = tmap.find(
typeid(SubclassT));
559 if(found == tmap.end())
561 return found->second.size();
567 return items.empty();
571 template<
typename Sub
classT>
574 typename tmap_t::const_iterator found = tmap.find(
typeid(SubclassT));
575 if(found == tmap.end())
578 return found->second.empty();
584 return iterator<>(items.begin());
590 return iterator<>(items.end());
594 template<
typename Sub
classT>
597 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
598 if(found == tmap.end())
599 return iterator<SubclassT>(items.end());
600 return iterator<SubclassT>(found->second.begin());
604 template<
typename Sub
classT>
607 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
608 if(found == tmap.end())
609 return iterator<SubclassT>(items.end());
610 return iterator<SubclassT>(found->second.end());
617 typedef std::vector<BaseT*> base_container;
619 inline static void check_subclass(
const BaseT&) {}
621 template<
typename Sub
classT>
622 inline static bool is_type(
const BaseT* obj) {
return (
typeid(*obj) ==
typeid(SubclassT)); }
624 base_container items;
626 typedef std::map<detail::type_index, base_container> tmap_t;
631template<
typename BaseT>
635 template<
typename ValueT = BaseT*>
639 typedef typename std::vector<BaseT*>::iterator base_iterator;
643 typedef typename std::vector<ValueT>::iterator::value_type value_type;
644 typedef typename std::vector<ValueT>::iterator::pointer pointer;
645 typedef typename std::vector<ValueT>::iterator::reference reference;
646 typedef typename base_iterator::difference_type difference_type;
647 typedef typename base_iterator::iterator_category iterator_category;
650 iterator(base_iterator i) : it(i) {}
651 iterator(
const iterator& other) : it(other.it) {}
653 iterator& operator=(
const iterator& rhs) { it = rhs.it;
return *
this; }
655 reference operator*() {
return (reference)*it; }
656 const reference operator*()
const {
return (
const reference)*it; }
658 iterator& operator++() { ++it;
return *
this; }
659 iterator operator++(
int) { iterator tmp(*
this); ++it;
return tmp; }
660 iterator& operator--() { --it;
return *
this; }
661 iterator operator--(
int) { iterator tmp(*
this); --it;
return tmp; }
663 bool operator==(
const iterator& rhs)
const {
return (it == rhs.it); }
664 bool operator!=(
const iterator& rhs)
const {
return (it != rhs.it); }
666 iterator& operator+=(difference_type n) { it += n;
return *
this; }
667 iterator& operator-=(difference_type n) { it -= n;
return *
this; }
669 pointer operator[](difference_type n) {
return (pointer)it[n]; }
670 const pointer operator[](difference_type n)
const {
return (
const pointer)it[n]; }
672 friend iterator operator+(
const iterator& i, difference_type n) {
return iterator(i.it + n); }
673 friend iterator operator+(difference_type n,
const iterator& i) {
return iterator(i.it + n); }
674 friend iterator operator-(
const iterator& i, difference_type n) {
return iterator(i.it - n); }
675 friend iterator operator-(difference_type n,
const iterator& i) {
return iterator(i.it - n); }
677 friend difference_type operator-(
const iterator& b,
const iterator& a) {
return (b.it - a.it); }
679 friend bool operator<(
const iterator&a,
const iterator& b) {
return (a.it < b.it); }
680 friend bool operator>(
const iterator&a,
const iterator& b) {
return (a.it > b.it); }
681 friend bool operator<=(
const iterator&a,
const iterator& b) {
return (a.it <= b.it); }
682 friend bool operator>=(
const iterator&a,
const iterator& b) {
return (a.it >= b.it); }
693 : items(other.items), tmap(other.tmap)
708 swap(a.items, b.items);
709 swap(a.tmap, b.tmap);
739 items.push_back(obj);
742 tmap[
typeid(*obj)].push_back(obj);
749 BaseT* back = items.back();
752 tmap[
typeid(*back)].pop_back();
761 template<
typename Sub
classT>
765 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
766 if(found == tmap.end())
770 found->second.pop_back();
773 for(
typename std::vector<BaseT*>::reverse_iterator it = items.rbegin(); it != items.rend(); ++it)
775 if(
typeid(**it) ==
typeid(SubclassT))
788 while(!empty()) pop_back();
792 template<
typename Sub
classT>
796 tmap.erase(
typeid(SubclassT));
799 items.erase(std::remove_if(items.begin(), items.end(), is_type<SubclassT>), items.end());
815 template<
typename Sub
classT>
818 return is_type<SubclassT>(items[i]);
834 const BaseT*
get(std::size_t i)
const
840 template<
typename Sub
classT>
841 SubclassT*
get(std::size_t i)
843 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
844 if(found == tmap.end())
845 throw std::out_of_range(std::string() +
"type " +
typeid(SubclassT).name() +
" not in vector");
846 return (SubclassT*)found->second[i];
850 template<
typename Sub
classT>
851 const SubclassT*
get(std::size_t i)
const
853 typename tmap_t::const_iterator found = tmap.find(
typeid(SubclassT));
854 if(found == tmap.end())
855 throw std::out_of_range(std::string() +
"type " +
typeid(SubclassT).name() +
" not in vector");
856 return (
const SubclassT*)found->second[i];
860 BaseT*
at(std::size_t i)
866 const BaseT*
at(std::size_t i)
const
875 template<
typename Sub
classT>
876 SubclassT*
at(std::size_t i)
878 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
879 if(found == tmap.end())
880 throw std::out_of_range(std::string() +
"type " +
typeid(SubclassT).name() +
" not in vector");
881 return (SubclassT*)found->second.at(i);
885 template<
typename Sub
classT>
886 const SubclassT*
at(std::size_t i)
const
888 typename tmap_t::const_iterator found = tmap.find(
typeid(SubclassT));
889 if(found == tmap.end())
890 throw std::out_of_range(std::string() +
"type " +
typeid(SubclassT).name() +
" not in vector");
891 return (
const SubclassT*)found->second.at(i);
901 template<
typename Sub
classT>
904 typename tmap_t::const_iterator found = tmap.find(
typeid(SubclassT));
905 if(found == tmap.end())
907 return found->second.size();
913 return items.empty();
917 template<
typename Sub
classT>
920 typename tmap_t::const_iterator found = tmap.find(
typeid(SubclassT));
921 if(found == tmap.end())
924 return found->second.empty();
930 return iterator<>(items.begin());
936 return iterator<>(items.end());
940 template<
typename Sub
classT>
943 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
944 if(found == tmap.end())
945 return iterator<SubclassT>(items.end());
946 return iterator<SubclassT>(found->second.begin());
950 template<
typename Sub
classT>
953 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
954 if(found == tmap.end())
955 return iterator<SubclassT>(items.end());
956 return iterator<SubclassT>(found->second.end());
960 inline void check_subclass(
const BaseT*) {}
962 template<
typename Sub
classT>
963 inline static bool is_type(
const BaseT* obj) {
return (
typeid(*obj) ==
typeid(SubclassT)); }
965 typedef std::vector<BaseT*> base_container;
967 base_container items;
969 typedef std::map<detail::type_index, base_container> tmap_t;
974std::size_t lower_bound(std::vector<BaseT*>& items, PosType target){
975 std::size_t ju,jm,jl;
977 if(items.size() == 0)
return 0;
983 if(items[jm]->compareZ(target))
992template<
typename Container>
1015 n = (int)(range/smallsize+1);
1018 int xy2d (
int x,
int y);
1019 int xy2d (PosType x, PosType y);
1020 void d2xy(
int d,
int *x,
int *y);
1021 void d2xy(
int d, PosType *x, PosType *y);
1025 PosType xo[2],range;
1026 void rot(
int s,
int *x,
int *y,
int rx,
int ry);
1030T between(
const T& x,
const T& l,
const T& u)
1032 return std::max(l, std::min(u, x));
1040 RandomNumbers(
unsigned int seed);
1041 ~RandomNumbers(
void);
1043 PosType operator()(
void);
1045 PosType gauss(){
return norm_dist(rand_gen);}
1048 std::normal_distribution<> norm_dist;
1049 std::mt19937 rand_gen;
1075 }
while( s > 1.0 || s == 0.0);
1077 s = sqrt(-2*log(s)/s);
1086 int poisson(
double lam){
1088 return std::max<long>(std::lround(
gauss()*sqrt(lam) + lam ) ,0);
1090 double L = exp(-lam),p=1;
1153template <
typename T,
typename R>
1160 if(vec.size() < 2)
return;
1161 for (
size_t i = vec.size()-1; i>0; --i) {
1162 ran_t = (size_t)(ran()*(i+1));
1164 std::swap(vec[ran_t],vec[i]);
1170template <
typename T>
1172 ,std::vector<size_t> &index
1176 index.resize(v.size());
1177 for (
size_t i = 0; i != index.size(); ++i) index[i] = i;
1180 std::sort(index.begin(), index.end(),[&v](
size_t i1,
size_t i2) {return v[i1] < v[i2];});
1183template <
typename T>
1185 ,std::vector<size_t> &index
1191 for (
size_t i = 0; i != index.size(); ++i) index[i] = i;
1194 std::sort(index.begin(), index.end(),
1195 [v](
size_t i1,
size_t i2) {return v[i1] < v[i2];});
1199template <
typename T>
1201 ,std::vector<size_t> &index
1205 index.resize(v.size());
1206 for (
size_t i = 0; i != index.size(); ++i) index[i] = i;
1209 std::sort(index.begin(), index.end(),
1210 [&v](
size_t i1,
size_t i2) {return v[i1] > v[i2];});
1218template <
typename R>
1222 if(N == 0)
throw std::invalid_argument(
"N=0");
1224 for(
size_t i=0 ; i<N ; ++i) index[i] = i;
1234 size_t tmp = index[index_internal];
1235 if(index_internal == index.size()){
1244 if(index_internal == index.size()){
1249 return index[index_internal];
1259 std::vector<size_t> index;
1260 size_t index_internal;
1265template <
typename T>
1266void apply_permutation(
1268 const std::vector<std::size_t>& p)
1270 std::vector<T> copy(p.size());
1272 for(std::size_t i = 0; i < p.size(); ++i){
1276 for(std::size_t i = 0; i < p.size(); ++i){
1277 vec[i] = copy[p[i]];
1281template <
typename T>
1282void apply_permutation(
1283 std::vector<T>& vec,
1284 const std::vector<std::size_t>& p)
1286 apply_permutation(vec.data(),p);
1295 std::valarray<double>
const &aa
1296 ,std::valarray<double>
const &bb
1301 ,std::vector<double> &ll
1302 ,std::vector<double> &Pl
1303 ,
double zeropaddingfactor
1307 std::valarray<double> &aa
1312 ,std::vector<double> &ll
1313 ,std::vector<double> &Pl
1317 std::valarray<double> &aa
1322 ,
const std::vector<double> &ll
1323 ,std::vector<double> &Pl
1324 ,std::vector<double> &llave
1328 std::valarray<float>
const &aa
1329 ,std::valarray<float>
const &bb
1334 ,std::vector<double> &ll
1335 ,std::vector<double> &Pl
1336 ,
double zeropaddingfactor
1340 std::valarray<float> &aa
1345 ,std::vector<double> &ll
1346 ,std::vector<double> &Pl
1350 std::valarray<float> &aa
1355 ,
const std::vector<double> &ll
1356 ,std::vector<double> &Pl
1357 ,std::vector<double> &llave
1366inline bool file_exists (
const std::string& name) {
1368 return (stat (name.c_str(), &buffer) == 0);
1373 std::string filename
1376 ,
bool verbose =
false
1381 std::ifstream file_in(filename.c_str());
1383 std::string space =
" ";
1387 std::stringstream buffer;
1390 std::cout <<
"Can't open file " << filename << std::endl;
1392 throw std::runtime_error(
" Cannot open file.");
1395 std::cout <<
"Reading from " << filename << std::endl;
1397 while(i < skiplines){
1398 getline(file_in,myline);
1401 while(file_in.peek() ==
'#'){
1402 file_in.ignore(10000,
'\n');
1405 std::cout <<
"skipped "<< i <<
" comment lines in " << filename << std::endl;
1409 while(getline(file_in,myline)){
1411 if(myline[0] ==
'#'){
1412 std::cout <<
"skipped line " << i << std::endl;
1416 pos= myline.find_first_not_of(space);
1417 myline.erase(0,pos);
1421 if(verbose) std::cout << myt1 <<
" ";
1429 std::cout <<
"Read " << x.size() <<
" lines from " << filename << std::endl;
1434template <
class T1,
class T2>
1436 std::string filename
1439 ,std::string delineator =
" "
1441 ,
bool verbose =
false
1448 std::ifstream file_in(filename.c_str());
1450 std::string space =
" ";
1455 std::stringstream buffer;
1458 std::cout <<
"Can't open file " << filename << std::endl;
1460 throw std::runtime_error(
" Cannot open file.");
1463 std::cout <<
"Reading caustic information from " << filename << std::endl;
1465 while(i < skiplines){
1466 getline(file_in,myline);
1469 while(file_in.peek() ==
'#'){
1470 file_in.ignore(10000,
'\n');
1473 std::cout <<
"skipped "<< i <<
" comment lines in " << filename << std::endl;
1477 while(getline(file_in,myline)){
1479 if(myline[0] ==
'#'){
1480 std::cout <<
"skipped line " << i << std::endl;
1484 pos= myline.find_first_not_of(space);
1485 myline.erase(0,pos);
1488 pos = myline.find(delineator);
1489 strg.assign(myline,0,pos);
1492 if(verbose) std::cout << myt1 <<
" ";
1495 myline.erase(0,pos+1);
1496 pos= myline.find_first_not_of(space);
1497 myline.erase(0,pos);
1502 pos = myline.find(space);
1503 strg.assign(myline,0,pos);
1506 if(verbose) std::cout << myt2 << std::endl;
1514 std::cout <<
"Read " << x.size() <<
" lines from " << filename << std::endl;
1518template <
class T1,
class T2,
class T3>
1520 std::string filename
1524 ,std::string delineator =
" "
1525 ,
bool verbose =
false
1535 std::ifstream file_in(filename.c_str());
1537 std::string space =
" ";
1543 std::stringstream buffer;
1546 std::cout <<
"Can't open file " << filename << std::endl;
1548 throw std::runtime_error(
" Cannot open file.");
1551 std::cout <<
"Reading caustic information from " << filename << std::endl;
1553 while(file_in.peek() ==
'#'){
1554 file_in.ignore(10000,
'\n');
1557 std::cout <<
"skipped "<< i <<
" comment lines in " << filename << std::endl;
1561 while(getline(file_in,myline)){
1563 if(myline[0] ==
'#'){
1564 std::cout <<
"skipped line " << i << std::endl;
1568 pos= myline.find_first_not_of(space);
1569 myline.erase(0,pos);
1572 pos = myline.find(delineator);
1573 strg.assign(myline,0,pos);
1576 if(verbose) std::cout << myt1 <<
" ";
1579 myline.erase(0,pos+1);
1580 pos= myline.find_first_not_of(space);
1581 myline.erase(0,pos);
1589 pos = myline.find(space);
1590 strg.assign(myline,0,pos);
1593 if(verbose) std::cout << myt2 << std::endl;
1596 myline.erase(0,pos+1);
1597 pos= myline.find_first_not_of(space);
1598 myline.erase(0,pos);
1604 pos = myline.find(space);
1605 strg.assign(myline,0,pos);
1608 if(verbose) std::cout << myt3 << std::endl;
1616 std::cout <<
"Read " << x.size() <<
" lines from " << filename << std::endl;
1619int NumberOfEntries(
const std::string &
string,
char deliniator);
1623 ,
char comment_char =
'#'
1624 ,
char deliniator =
' '
1632 ,
const std::string filespec
1633 ,std::vector<std::string> & filenames
1651template <
typename T>
1653 ,std::string filename
1656 ,
char comment_char =
'#'
1658 ,
size_t MaxNrows = std::numeric_limits<size_t>::max()
1659 ,
bool verbose =
true){
1661 std::ifstream file(filename);
1663 if (!file.is_open()){
1664 std::cerr <<
"file " << filename <<
" cann't be opened." << std::endl;
1665 throw std::runtime_error(
"no file");
1674 size_t first_data_line;
1678 while(i < skiplines){
1679 std::getline(file,line);
1686 first_data_line = file.tellg();
1687 std::getline(file,line);
1689 }
while(line[0] == comment_char);
1691 columns = NumberOfEntries(line,
' ');
1693 file.seekg(first_data_line);
1695 std::copy(std::istream_iterator<T>(file),
1696 std::istream_iterator<T>(),
1697 std::back_inserter(data));
1699 rows = data.size()/columns;
1701 std::cout <<
"Read " << rows <<
" rows of " << columns <<
" columns from file " << filename << std::endl;
1722template <
typename T>
1724 ,std::vector<std::vector<T> > &data
1725 ,std::vector<std::string> &column_names
1726 ,
size_t MaxNumber = 100000000
1727 ,
char comment_char =
'#'
1728 ,
char deliniator =
','
1729 ,std::string replace =
"\\N"
1730 ,std::function<
bool(std::vector<T> &)> accept = [](std::vector<T> &v){
return true;}
1733 bool verbose =
false;
1735 std::ifstream file(filename);
1737 if (!file.is_open()){
1738 std::cerr <<
"file " << filename <<
" cann't be opened." << std::endl;
1739 throw std::runtime_error(
"no file");
1742 int count_preamble=0;
1746 std::getline(file,line);
1749 }
while(line[0] == comment_char);
1752 std::stringstream lineStream(line);
1754 column_names.empty();
1755 while(std::getline(lineStream,cell, deliniator))
1757 column_names.push_back(cell);
1760 if (!lineStream && cell.empty())
1762 column_names.push_back(
"");
1767 for(
auto st : column_names){
1768 std::cout << i++ <<
" " << st << std::endl;
1772 int columns = NumberOfEntries(line,deliniator);
1775 size_t number_of_data_lines = 0;
1776 while(std::getline(file, line) && number_of_data_lines < MaxNumber ) ++number_of_data_lines;
1779 std::vector<std::vector<T> > tmp_data(columns,std::vector<T>(0));
1780 swap(data,tmp_data);
1781 std::vector<T> tmp_row(columns);
1787 file.open(filename);
1788 for(
int i=0; i < count_preamble; ++i){
1789 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
1796 while(std::getline(file,line) && rows < MaxNumber){
1798 std::stringstream lineStream(line);
1802 while(std::getline(lineStream,cell,deliniator))
1804 if(cell==replace) cell=
'0';
1807 cell.erase(remove_if(cell.begin(),cell.end(), isspace), cell.end());
1813 if(accept(tmp_row)){
1815 for(
int i=0 ; i < columns ; ++i) data[i].push_back(tmp_row[i]);
1825template <
typename T>
1827 ,std::vector<std::vector<T> > &ranges
1828 ,std::vector<std::string> &column_names
1829 ,
size_t MaxNumber = 100000000
1830 ,
char comment_char =
'#'
1831 ,
char deliniator =
','
1832 ,std::string replace =
"\\N"
1833 ,std::function<
bool(std::vector<T> &)> accept = [](std::vector<T> &v){
return true;}
1836 bool verbose =
false;
1838 std::ifstream file(filename);
1840 if (!file.is_open()){
1841 std::cerr <<
"file " << filename <<
" cann't be opened." << std::endl;
1842 throw std::runtime_error(
"no file");
1845 int count_preamble=0;
1849 std::getline(file,line);
1852 }
while(line[0] == comment_char);
1855 std::stringstream lineStream(line);
1857 column_names.empty();
1858 while(std::getline(lineStream,cell,
','))
1860 column_names.push_back(cell);
1863 if (!lineStream && cell.empty())
1865 column_names.push_back(
"");
1870 for(
auto st : column_names){
1871 std::cout << i++ <<
" " << st << std::endl;
1875 int columns = NumberOfEntries(line,deliniator);
1877 std::vector<std::vector<T> > tmp_ranges(columns,std::vector<T>(2));
1878 swap(ranges,tmp_ranges);
1879 std::vector<T> tmp_row(columns);
1885 file.open(filename);
1886 for(
int i=0; i < count_preamble; ++i){
1887 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
1892 while(std::getline(file,line) && rows < MaxNumber){
1894 std::stringstream lineStream(line);
1898 while(std::getline(lineStream,cell,deliniator))
1900 if(cell==replace) cell=
'0';
1903 cell.erase(remove_if(cell.begin(),cell.end(), isspace), cell.end());
1907 if(accept(tmp_row)){
1909 for(
int j=0 ; j < columns ; ++j){
1910 ranges[j][0] = tmp_row[j];
1911 ranges[j][1] = tmp_row[j];
1915 for(
int j=0 ; j < columns ; ++j){
1916 if(ranges[j][0] > tmp_row[j]) ranges[j][0] = tmp_row[j];
1917 if(ranges[j][1] < tmp_row[j]) ranges[j][1] = tmp_row[j];
1941template <
typename T>
1943 ,std::vector<std::vector<T> > &data
1944 ,std::vector<std::string> &column_names
1945 ,
size_t Nmax = 1000000
1946 ,
char comment_char =
'#'
1947 ,
char deliniator =
','
1949 ,std::string reject =
""
1953 std::ifstream file(filename);
1955 if (!file.is_open()){
1956 std::cerr <<
"file " << filename <<
" cann't be opened." << std::endl;
1957 throw std::runtime_error(
"no file");
1962 std::getline(file,line);
1964 }
while(line[0] == comment_char);
1967 std::stringstream lineStream(line);
1969 column_names.empty();
1973 while(std::getline(lineStream,cell,deliniator))
1975 column_names.push_back(cell);
1979 if (!lineStream && cell.empty())
1981 column_names.push_back(
"");
1985 while(std::getline(lineStream,cell,deliniator))
1987 column_names.push_back(cell);
1990 data.emplace_back(column_names.size());
1992 for(
auto a : column_names){
1994 cell.erase(remove_if(cell.begin(),cell.end(), isspace), cell.end());
2000 int columns = NumberOfEntries(line,deliniator);
2002 for(
auto &v : data ) v.empty();
2004 while(std::getline(file,line) && ii < Nmax){
2006 data.emplace_back(columns);
2009 std::stringstream lineStream(line);
2013 while(std::getline(lineStream,cell, deliniator))
2015 assert(i < columns);
2016 cell.erase(remove_if(cell.begin(),cell.end(), isspace), cell.end());
2052 ,
const std::vector<std::string> header
2053 ,std::vector<T *> &data
2056 std::ofstream s(filename +
".csv");
2058 long ncol = header.size();
2059 assert(ncol == data.size() );
2060 for(
int i = 0 ; i < header.size()-1 ; ++i){
2061 s << header[i] <<
",";
2063 s << header.back() << std::endl;
2065 size_t nrow = data[0]->size();
2066 for(
auto v : data) assert(nrow == v->size() );
2068 for(
size_t j = 0 ; j < nrow ; ++j){
2069 for(
int i = 0 ; i < ncol-1 ; ++i){
2070 s << data[i]->operator[](j) <<
",";
2072 s << data.back()->operator[](j) <<
"\n";
2088 XYcsvLookUp(std::string datafile
2092 ,
size_t MaxNumber = 1000000
2093 ,
bool verbose =
false);
2095 std::string datafile
2098 ,std::vector<double> Xbins
2099 ,
size_t MaxNumber = 1000000
2100 ,
bool verbose =
false);
2103 std::vector<double> find(
double x,
double y);
2106 double operator[](std::string label)
const;
2109 double operator[](
int i)
const{
2110 return (*current)[i];
2113 double getX()
const {
2114 return (*current)[Xindex];
2117 double getY()
const {
2118 return (*current)[Yindex];
2121 std::vector<double> record()
const{
2125 std::vector<double> operator*()
const {
2130 if(current != data.end() ) ++current;
2133 if(current != data.begin() ) --current;
2136 std::vector<std::string> labels()
const{
2137 return column_names;
2140 double Xmin()
const {
return xmin;}
2141 double Xmax()
const {
return xmax;}
2143 double Ymin(
double x)
const;
2145 double Ymax(
double x)
const;
2147 void printcurrent(){
2149 for(
auto label : column_names){
2150 std::cout << label <<
" : " << (*current)[i++] << std::endl;
2159 std::vector<std::vector<double> >::iterator current;
2160 std::vector<std::vector<std::vector<double> >::iterator> borders;
2161 std::vector<std::vector<double> > data;
2162 std::vector<std::string> column_names;
2163 std::vector<double> Xborders;
2165 std::string filename;
2170void splitstring(std::string &line,std::vector<std::string> &vec,
const std::string &delimiter);
2180template<
typename T>
2184 ,
size_t MaxNumber = 1000000
2185 ,
char comment_char =
'#'
2186 ,
char deliniator =
','
2187 ,std::string replace =
"\\N"
2188 ,std::function<
bool(std::vector<T> &)> accept = [](std::vector<T> &v){
return true;}
2189 ):filename(datafile){
2190 input(datafile,MaxNumber,comment_char,deliniator,replace,accept);
2196 ,
size_t MaxNumber = 1000000
2197 ,
char comment_char =
'#'
2198 ,
char deliniator =
','
2199 ,std::string replace =
"\\N"
2200 ,std::function<
bool(std::vector<T> &)> accept = [](std::vector<T> &v){
return true;}
2202 filename = datafile;
2205 for(
int i=0 ; i<column_names.size() ; ++i) datamap[column_names[i]] = i;
2209 void pop(std::string colname){
2211 int i=datamap[colname];
2213 swap(column_names[i],column_names.back());
2214 column_names.pop_back();
2215 swap(data[i],data.back());
2219 for(
int i=0 ; i<column_names.size() ; ++i) datamap[column_names[i]] = i;
2224 if(datamap.find(label) == datamap.end()){
2225 std::cerr <<
"No label - " << label <<
" - in " << filename <<std::endl;
2226 throw std::invalid_argument(
"no label");
2228 return data[datamap[label]];
2229 for(
auto c : column_names ) std::cout << c <<
" ";
2230 std::cout << std::endl;
2231 throw std::invalid_argument(label +
" was not one of the columns of the galaxy data file :" + filename);
2235 void add_column(
const std::string &name,
const std::vector<T> &vec){
2236 if(vec.size() != data[0].size()){
2237 std::cerr <<
"Added column to DataFrame needs to have the same size." << std::endl;
2238 throw std::invalid_argument(
"wrong size");
2240 column_names.push_back(name);
2241 data.push_back(vec);
2242 data[name] = data.size()-1;
2252 return column_names;
2256 void sortby(std::string name){
2257 std::vector<size_t> index(data[0].size());
2258 size_t N = index.size();
2259 for(
size_t i=0 ; i<N ; ++i) index[i] = i;
2262 std::vector<T> tmp_v(N);
2264 for(
size_t j=0 ; j<data.size() ; ++j){
2265 for(
size_t i=0 ; i<N ; ++i){
2266 tmp_v[i] = data[j][index[i]];
2268 swap(data[j],tmp_v);
2274 std::vector<size_t> index(data[0].size());
2275 size_t N = index.size();
2276 for(
size_t i=0 ; i<N ; ++i) index[i] = i;
2279 std::vector<T> tmp_v(N);
2281 for(
size_t j=0 ; j<data.size() ; ++j){
2282 for(
size_t i=0 ; i<N ; ++i){
2283 tmp_v[i] = data[j][index[i]];
2285 swap(data[j],tmp_v);
2289 size_t number_of_rows(){
return data[0].size();}
2290 size_t number_of_columns(){
return data.size();}
2292 std::vector<std::vector<T> > data;
2294 std::map<std::string,int> datamap;
2295 std::vector<std::string> column_names;
2296 std::string filename;
2300template <
typename T>
2301double KleinSum(std::vector<T> &input){
2302 double s = 0.0,cs = 0.0,ccs = 0.0,c,cc;
2303 size_t n = input.size();
2304 for(
size_t i = 0 ; i<n ; ++i){
2305 double t = s + input[i];
2306 if( abs(s) >= abs(input[i]) ){
2307 c = (s - t) + input[i];
2309 c = (input[i] - t) + s;
2313 if( abs(cs) >= abs(c) ){
2321 return s + cs + ccs;
2325template <
typename T,
typename F>
2326double PairWiseSum(T *begin,T *end,F value){
2328 size_t n = end - begin;
2330 for(T* p=begin ; p != end ; ++p){
2334 sum = PairWiseSum<T,F>(begin,begin + n/2,value) + PairWiseSum<T,F>(begin + n/2,end,value);
2340template <
typename T>
2341double PairWiseSum(T *begin,T *end){
2343 size_t n = end - begin;
2345 for(T* p=begin ; p != end ; ++p){
2349 sum = PairWiseSum(begin,begin + n/2) + PairWiseSum(begin + n/2,end);
2355template <
typename T>
2356double PairWiseSum(std::vector<T> &v){
2357 return PairWiseSum(v.data(),v.data() + v.size());
2361template <
typename T,
typename F>
2362double PairWiseSum(std::vector<T> &v,F value){
2363 return PairWiseSum(v.data(),v.data() + v.size(),value);
2367template <
typename T>
2372 SUMMER():batch(1000),n(0),ntotal(0),current(0.0){};
2374 :batch(batchsize),n(0),current(0.0),ntotal(0){};
2382 v.push_back(current);
2390 if(v.size() ==0 )
return 0;
2391 v.push_back(current);
2392 return Utilities::PairWiseSum(v);
2397 std::vector<T> dump;
2421template <
typename T =
double>
2422double hypergeometric( T a, T b, T c, T x )
2426 throw std::invalid_argument(
"out of bounds");
2428 const double TOLERANCE = 1.0e-10;
2429 double term = a * b * x / c;
2430 double value = 1.0 + term;
2433 while ( abs( term ) > TOLERANCE )
2436 term *= a * b * x / c / n;
2446 typedef boost::variant<int,long,size_t,float,double,std::string,bool> MULTITYPE;
2447 typedef std::map<std::string,MULTITYPE> LINE;
2450 std::vector<LINE> lines;
2451 std::string filename;
2452 std::string blank_val;
2453 std::vector<std::string> header;
2456 long last_line_printed;
2458 std::set<std::string> labels;
2460 std::map<std::string,std::string> label_comments;
2466 const long default_precision = std::cout.precision();
2467 std::ofstream logfile;
2468 logfile.open(filename,std::ios_base::app);
2470 size_t n=labels.size();
2471 if(n == nlabels && last_line_printed > 0){
2472 std::cout << std::setprecision(precision);
2473 for(
size_t j=last_line_printed ; j<lines.size() ; ++j ){
2475 for(
auto &label : labels){
2478 logfile << std::setprecision(17) << lines[j].at(label);
2479 std::cout << std::setprecision(precision);
2480 }
else if(label ==
"RA" || label ==
"DEC" ){
2481 logfile << std::setprecision(8) << lines[j].at(label) ;
2482 std::cout << std::setprecision(precision);
2484 logfile << std::setprecision(precision) << lines[j].at(label) ;
2486 if(i<n-1) logfile <<
",";
2487 }
catch(std::exception& e){
2488 logfile << std::setprecision(precision) << blank_val ;
2489 if(i<n-1) logfile <<
",";
2493 logfile << std::endl;
2495 std::cout << std::setprecision(default_precision);
2497 last_line_printed = lines.size();
2506 LOGDATA(std::string file
2507 ,std::string blanck_value =
"0"
2509 ):filename(file),blank_val(blanck_value)
2510 ,nbatch(Nbatch),precision(std::cout.precision())
2511 ,last_line_printed(0),nlabels(0),nprints(0){
2515 ~LOGDATA(){print_to_file();}
2517 std::string output_file(){
return filename;}
2519 void set_precision(
int p){
2524 size_t ncol(){
return labels.size();}
2526 std::set<std::string> names = labels;
2528 void print_to_file(){
2530 const int default_precision = std::cout.precision();
2533 if(lines.size() == 0 )
return;
2535 std::ofstream logfile(filename);
2537 time_t now = time(0);
2538 struct tm date = *localtime(&now);
2539 logfile <<
"# " << date.tm_hour <<
":" << date.tm_min <<
" " << date.tm_mday <<
"/" << date.tm_mon <<
"/" << date.tm_year + 1900 << std::endl;
2540 for(std::string &s : header){
2541 logfile <<
"# " << s << std::endl;
2547 for(
auto &label : labels){
2548 auto it = label_comments.find(label);
2549 if(it == label_comments.end() ){
2550 logfile <<
"# " << i <<
" " << label <<
" : no comment" << std::endl;
2552 logfile <<
"# " << i <<
" " << it->first <<
" : " << it->second << std::endl;
2558 nlabels = labels.size();
2559 for(
auto &label : labels){
2561 if(i<nlabels-1) logfile <<
",";
2564 logfile << std::endl;
2566 std::cout << std::setprecision(precision);
2567 for(LINE &line : lines){
2569 for(
auto &label : labels){
2572 logfile << std::setprecision(17) << line.at(label) ;
2573 std::cout << std::setprecision(precision);
2574 }
else if(label ==
"RA" || label ==
"DEC" ){
2575 logfile << std::setprecision(8) << line.at(label) ;
2576 std::cout << std::setprecision(precision);
2578 logfile << std::setprecision(precision) << line.at(label);
2580 if(i<nlabels-1) logfile <<
",";
2581 }
catch(std::exception& e){
2582 logfile << std::setprecision(precision) << blank_val;
2583 if(i<nlabels-1) logfile <<
",";
2587 logfile << std::endl;
2589 std::cout << std::setprecision(default_precision);
2591 last_line_printed = lines.size();
2596 void add(
const LINE &line){
2597 for (
auto itr = line.begin(); itr != line.end(); ++itr) labels.insert(itr->first);
2598 lines.push_back(line);
2599 if(lines.size() % nbatch == 0) append_file();
2603 void replace(
const LINE &line){
2604 for (
auto itr = line.begin(); itr != line.end(); ++itr) labels.insert(itr->first);
2605 lines.back() = line;
2609 void header_comment(
const std::string &s){
2610 header.push_back(s);
2614 void label_description(
const std::string &name
2615 ,
const std::string &description
2617 label_comments[name] = description;
2658template <
typename T>
2659std::valarray<T>
AdaptiveSmooth(
const std::valarray<T> &map_in,
size_t Nx,
size_t Ny,T value){
2661 std::valarray<T> map_out(map_in.size());
2664 for(
long i=0;i<Nx;++i){
2665 for(
long j=0;j<Ny;++j){
2667 val = map_in[i+j*Nx];
2668 while(val < value && r < std::min(Nx, Ny) ){
2672 long imin,imax,jmin,jmax;
2674 imin = (i-r < 0) ? 0 : i-r;
2675 imax = (i+r > Nx-1) ? Nx-1 : i+r;
2677 jmin = (j-r < 0) ? 0 : j-r;
2678 jmax = (j+r > Ny-1) ? Ny-1 : j+r;
2681 for(
long ii=imin;ii<=imax;++ii){
2682 for(
long jj=jmin;jj<=jmax;++jj){
2683 if( (ii-i)*(ii-i) + (jj-j)*(jj-j) < r){
2684 val += map_in[ii+jj*Nx];
2692 map_out[i+j*Nx] = val/area;
2 dimensional matrix
Definition utilities_slsim.h:160
3 dimensional matrix, fixed size
Definition utilities_slsim.h:121
class for impoting data from a csv file and allowing label string lookup like a data frame.
Definition utilities_slsim.h:2181
std::vector< T > & operator[](const std::string &label)
returns column by name
Definition utilities_slsim.h:2223
void input(std::string datafile, size_t MaxNumber=1000000, char comment_char='#', char deliniator=',', std::string replace="\\N", std::function< bool(std::vector< T > &)> accept=[](std::vector< T > &v){return true;})
Definition utilities_slsim.h:2195
std::vector< T > & operator[](int i)
returns column by number
Definition utilities_slsim.h:2246
std::vector< std::string > labels() const
returns labels of the columns from the data file
Definition utilities_slsim.h:2251
void pop(std::string colname)
remove a column
Definition utilities_slsim.h:2209
void add_column(const std::string &name, const std::vector< T > &vec)
add a column to the data frame. This does a copy.
Definition utilities_slsim.h:2235
DataFrame(std::string datafile, size_t MaxNumber=1000000, char comment_char='#', char deliniator=',', std::string replace="\\N", std::function< bool(std::vector< T > &)> accept=[](std::vector< T > &v){return true;})
Definition utilities_slsim.h:2183
Class for calculating the Hilbert curve distance in two dimensions.
Definition utilities_slsim.h:1003
HilbertCurve(PosType x_min, PosType y_min, PosType my_range, PosType smallsize)
Definition utilities_slsim.h:1005
int xy2d(int x, int y)
convert (x,y) to d
Definition utilities.cpp:291
void d2xy(int d, int *x, int *y)
convert d to (x,y)
Definition utilities.cpp:303
BaseT * at(std::size_t i)
indexed access with bounds checking
Definition utilities_slsim.h:860
friend void swap(MixedVector< BaseT * > &a, MixedVector< BaseT * > &b)
swap two MixedVectors
Definition utilities_slsim.h:704
void push_back(BaseT *obj)
add an object of type SubclassT to the vector, this does not copy the object but will delete is when ...
Definition utilities_slsim.h:733
const SubclassT * get(std::size_t i) const
indexed access for type SubclassT (const)
Definition utilities_slsim.h:851
SubclassT * at(std::size_t i)
Templated indexing operator for elements of a specific derived class. The index runs 0 ....
Definition utilities_slsim.h:876
std::size_t size() const
number of elements of a specific type
Definition utilities_slsim.h:902
bool type(std::size_t i)
Checks if element i is of the derived type SubclassT.
Definition utilities_slsim.h:816
void clear()
clear all elements of a given type
Definition utilities_slsim.h:793
iterator< SubclassT > end()
get iterator to last of items of type SubclassT
Definition utilities_slsim.h:951
void pop_back()
pop element from back of vector
Definition utilities_slsim.h:746
void pop_back()
erase the last element of a specific type
Definition utilities_slsim.h:762
void clear()
clear all elements
Definition utilities_slsim.h:786
iterator begin()
get iterator to first of all items
Definition utilities_slsim.h:928
iterator end()
get iterator to last of all items
Definition utilities_slsim.h:934
const SubclassT * at(std::size_t i) const
Templated indexing operator for elements of a specific derived class (const).
Definition utilities_slsim.h:886
BaseT * get(std::size_t i)
indexed access
Definition utilities_slsim.h:828
std::size_t size() const
number of all elements
Definition utilities_slsim.h:895
MixedVector()
default constructor
Definition utilities_slsim.h:687
BaseT ** data()
direct access to underlying array
Definition utilities_slsim.h:803
const BaseT * at(std::size_t i) const
indexed access with bounds checking (const)
Definition utilities_slsim.h:866
BaseT & back()
back of list
Definition utilities_slsim.h:721
BaseT * operator[](std::size_t i) const
Indexing operator for all elements in form of a reference to the base class.
Definition utilities_slsim.h:822
const BaseT & back() const
back of list
Definition utilities_slsim.h:727
const BaseT * get(std::size_t i) const
indexed access (const)
Definition utilities_slsim.h:834
SubclassT * get(std::size_t i)
indexed access for type SubclassT
Definition utilities_slsim.h:841
const BaseT ** data() const
direct access to underlying array (const)
Definition utilities_slsim.h:809
~MixedVector()
destroy the vector and all contained items
Definition utilities_slsim.h:698
bool empty() const
check if vector of items of type SubclassT is empty
Definition utilities_slsim.h:918
MixedVector< BaseT * > & operator=(MixedVector< BaseT * > rhs)
assign one MixedVector to another
Definition utilities_slsim.h:713
iterator< SubclassT > begin()
get iterator to first of items of type SubclassT
Definition utilities_slsim.h:941
bool empty() const
check if vector of items is empty
Definition utilities_slsim.h:911
MixedVector(const MixedVector< BaseT * > &other)
copy constructor
Definition utilities_slsim.h:692
A container that can hold mixed objects all derived from a base class and retains the ability to acce...
Definition utilities_slsim.h:272
void pop_back()
erase the last element of a specific type
Definition utilities_slsim.h:396
bool empty() const
check if vector of items of type SubclassT is empty
Definition utilities_slsim.h:572
MixedVector()
default constructor
Definition utilities_slsim.h:333
std::size_t size() const
number of all elements
Definition utilities_slsim.h:549
MixedVector< BaseT > & operator=(MixedVector< BaseT > rhs)
assign one MixedVector to another
Definition utilities_slsim.h:353
const BaseT & operator[](std::size_t i) const
Indexing operator for all elements (const).
Definition utilities_slsim.h:476
friend void swap(MixedVector< BaseT > &a, MixedVector< BaseT > &b)
swap two MixedVectors
Definition utilities_slsim.h:344
BaseT * data()
direct access to underlying array
Definition utilities_slsim.h:451
void pop_back()
pop element from back of vector
Definition utilities_slsim.h:379
SubclassT & get(std::size_t i)
indexed access for type SubclassT
Definition utilities_slsim.h:495
~MixedVector()
destroy the vector and all contained items
Definition utilities_slsim.h:338
BaseT & get(std::size_t i)
indexed access
Definition utilities_slsim.h:482
void clear()
clear all elements
Definition utilities_slsim.h:420
bool type(std::size_t i)
Checks if element i is of the derived type SubclassT.
Definition utilities_slsim.h:464
iterator begin()
get iterator to first of all items
Definition utilities_slsim.h:582
const BaseT & get(std::size_t i) const
indexed access (const)
Definition utilities_slsim.h:488
void copy_back(const SubclassT &obj)
add a copy of an object of type SubclassT to the vector, in contrast push_back() deos not copy and wi...
Definition utilities_slsim.h:362
const BaseT * data() const
direct access to underlying array (const)
Definition utilities_slsim.h:457
bool empty() const
check if vector of items is empty
Definition utilities_slsim.h:565
BaseT & at(std::size_t i)
indexed access with bounds checking
Definition utilities_slsim.h:514
const SubclassT & at(std::size_t i) const
Templated indexing operator for elements of a specific derived class (const).
Definition utilities_slsim.h:540
SubclassT & at(std::size_t i)
Templated indexing operator for elements of a specific derived class. The index runs 0 ....
Definition utilities_slsim.h:530
std::size_t size() const
number of elements of a specific type
Definition utilities_slsim.h:556
const BaseT & at(std::size_t i) const
indexed access with bounds checking (const)
Definition utilities_slsim.h:520
const SubclassT & get(std::size_t i) const
indexed access for type SubclassT (const)
Definition utilities_slsim.h:505
iterator< SubclassT > begin()
get iterator to first of items of type SubclassT
Definition utilities_slsim.h:595
void clear()
clear all elements of a given type
Definition utilities_slsim.h:430
BaseT & operator[](std::size_t i)
Indexing operator for all elements.
Definition utilities_slsim.h:470
iterator< SubclassT > end()
get iterator to last of items of type SubclassT
Definition utilities_slsim.h:605
iterator end()
get iterator to last of all items
Definition utilities_slsim.h:588
This is a class for generating random numbers. It simplifies and fool proofs initialization and allow...
Definition utilities_slsim.h:1059
PosType operator()(void)
return a uniform random number between 0 and 1
Definition utilities.cpp:429
long getseed()
total number of calls
Definition utilities_slsim.h:1124
PosType gauss()
generates a Gaussian distributed number with unit variance by polar Box-Muller transform
Definition utilities_slsim.h:1068
class for adding large amounts of numbers with less error than the simple sum
Definition utilities_slsim.h:2368
void operator+=(T x)
add another number
Definition utilities_slsim.h:2377
size_t total_number()
returns the number of numbers that have been added
Definition utilities_slsim.h:2404
T operator*()
returns the current total
Definition utilities_slsim.h:2389
void reset()
reset to start over, frees memory
Definition utilities_slsim.h:2396
Gives a randomized sequence of numbers from 0 to N-1.
Definition utilities_slsim.h:1219
size_t operator*()
return the index number
Definition utilities_slsim.h:1231
size_t operator++(int)
goto the next number
Definition utilities_slsim.h:1233
size_t operator++()
goto the next number
Definition utilities_slsim.h:1243
void reshuffle(R &ran)
get a new random order
Definition utilities_slsim.h:1253
Symetric matrix.
Definition utilities_slsim.h:199
size_t oned_index(int i, int j)
convertion from 2d to 1d index
Definition utilities_slsim.h:223
bool make_directories(const std::string &root_dir)
make a directory if it doesn't exist
Definition utilities.cpp:564
void ReadASCII(std::vector< T > &data, std::string filename, int &columns, int &rows, char comment_char='#', int skiplines=0, size_t MaxNrows=std::numeric_limits< size_t >::max(), bool verbose=true)
This function will read in all the numbers from a multi-column ,space seporated ASCII data file.
Definition utilities_slsim.h:1652
void read3columnfile(std::string filename, std::vector< T1 > &x, std::vector< T2 > &y, std::vector< T3 > &z, std::string delineator=" ", bool verbose=false)
Read in data from an ASCII file with three columns.
Definition utilities_slsim.h:1519
void read2columnfile(std::string filename, std::vector< T1 > &x, std::vector< T2 > &y, std::string delineator=" ", int skiplines=0, bool verbose=false)
Read in data from an ASCII file with two columns.
Definition utilities_slsim.h:1435
int ReadCSVnumerical1(std::string filename, std::vector< std::vector< T > > &data, std::vector< std::string > &column_names, size_t MaxNumber=100000000, char comment_char='#', char deliniator=',', std::string replace="\\N", std::function< bool(std::vector< T > &)> accept=[](std::vector< T > &v){return true;})
Read numerical data from a csv file with a header.
Definition utilities_slsim.h:1723
int ReadCSVnumerical2(std::string filename, std::vector< std::vector< T > > &data, std::vector< std::string > &column_names, size_t Nmax=1000000, char comment_char='#', char deliniator=',', bool header=true, std::string reject="")
Read numerical data from a csv file with a header.
Definition utilities_slsim.h:1942
size_t ReadCSVrange(std::string filename, std::vector< std::vector< T > > &ranges, std::vector< std::string > &column_names, size_t MaxNumber=100000000, char comment_char='#', char deliniator=',', std::string replace="\\N", std::function< bool(std::vector< T > &)> accept=[](std::vector< T > &v){return true;})
Definition utilities_slsim.h:1826
void writeCSV(const std::string filename, const std::vector< std::string > header, std::vector< T * > &data)
write a CSV data file for some data vectors
Definition utilities_slsim.h:2051
bool check_directory(std::string dir)
returns true if director exists and false if it doesn't
Definition utilities.cpp:556
int CountColumns(std::string filename, char comment_char='#', char deliniator=' ')
Count the number of columns in a ASCII data file.
Definition utilities.cpp:614
void read1columnfile(std::string filename, std::vector< T > &x, int skiplines=0, bool verbose=false)
Definition utilities_slsim.h:1372
void ReadFileNames(std::string dir, const std::string filespec, std::vector< std::string > &filenames, bool verbose)
Reads the file names in a directory that contain a specific sub string.
Definition utilities.cpp:574
Definition utilities.h:39
T to_numeric(const std::string &str)
convert a string to a numerical value of various types
Definition utilities_slsim.h:30
std::valarray< T > AdaptiveSmooth(const std::valarray< T > &map_in, size_t Nx, size_t Ny, T value)
Smooth a 2 dimensional map stored in a valarray with a density dependent kernel.
Definition utilities_slsim.h:2659
void powerspectrum2d(std::valarray< double > const &aa, std::valarray< double > const &bb, long nx, long ny, double boxlx, double boxly, std::vector< double > &ll, std::vector< double > &Pl, double zeropaddingfactor)
Calculates power spectrum from a 2d map or the cross-power spectrum between two 2d maps.
Definition utilities.cpp:839
void sort_indexes(const std::vector< T > &v, std::vector< size_t > &index)
Find the indexes that sort a vector in asending order.
Definition utilities_slsim.h:1171
void shuffle(std::vector< T > &vec, R &ran)
Shuffles a vector into a random order.
Definition utilities_slsim.h:1154
void delete_container(Container &c)
delete the objects that are pointed to in a container of pointers
Definition utilities_slsim.h:993
void splitstring(std::string &line, std::vector< std::string > &vec, const std::string &delimiter)
split string into vector of seporate strings that were seporated by
Definition utilities.cpp:828
void powerspectrum2dprebin(std::valarray< double > &aa, long nx, long ny, double boxlx, double boxly, const std::vector< double > &ll, std::vector< double > &Pl, std::vector< double > &llave)
Definition utilities.cpp:454
void sort_indexes_decending(const std::vector< T > &v, std::vector< size_t > &index)
Find the indexes that sort a vector in descending order.
Definition utilities_slsim.h:1200
int GetNThreads()
returns the compiler variable N_THREADS that is maximum number of threads to be used.
Definition utilities.cpp:553