21#include <boost/variant.hpp>
31 return std::stoi(str);
34inline long to_numeric<long>(
const std::string &str) {
36 return std::stol(str);
43 return std::stoi(str);
45 return std::stoi(str);
52 return std::stof(str);
54 return std::stof(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){
163 D2Matrix(
size_t xsize,
size_t ysize)
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){
199class SymmetricMatrix{
204 SymmetricMatrix(
int n):n(n){
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))
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;
1059class RandomNumbers_NR{
1062 RandomNumbers_NR(
long seed);
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>
1221 ShuffledIndex(
size_t N,R &ran){
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);
1294 std::valarray<double>
const &aa
1295 ,std::valarray<double>
const &bb
1300 ,std::vector<double> &ll
1301 ,std::vector<double> &Pl
1302 ,
double zeropaddingfactor
1306 std::valarray<double> &aa
1311 ,std::vector<double> &ll
1312 ,std::vector<double> &Pl
1316 std::valarray<double> &aa
1321 ,
const std::vector<double> &ll
1322 ,std::vector<double> &Pl
1323 ,std::vector<double> &llave
1327 std::valarray<float>
const &aa
1328 ,std::valarray<float>
const &bb
1333 ,std::vector<double> &ll
1334 ,std::vector<double> &Pl
1335 ,
double zeropaddingfactor
1339 std::valarray<float> &aa
1344 ,std::vector<double> &ll
1345 ,std::vector<double> &Pl
1349 std::valarray<float> &aa
1354 ,
const std::vector<double> &ll
1355 ,std::vector<double> &Pl
1356 ,std::vector<double> &llave
1365inline bool file_exists (
const std::string& name) {
1367 return (stat (name.c_str(), &buffer) == 0);
1372 std::string filename
1375 ,
bool verbose =
false
1380 std::ifstream file_in(filename.c_str());
1382 std::string space =
" ";
1386 std::stringstream buffer;
1389 std::cout <<
"Can't open file " << filename << std::endl;
1391 throw std::runtime_error(
" Cannot open file.");
1394 std::cout <<
"Reading from " << filename << std::endl;
1396 while(i < skiplines){
1397 getline(file_in,myline);
1400 while(file_in.peek() ==
'#'){
1401 file_in.ignore(10000,
'\n');
1404 std::cout <<
"skipped "<< i <<
" comment lines in " << filename << std::endl;
1408 while(getline(file_in,myline)){
1410 if(myline[0] ==
'#'){
1411 std::cout <<
"skipped line " << i << std::endl;
1415 pos= myline.find_first_not_of(space);
1416 myline.erase(0,pos);
1420 if(verbose) std::cout << myt1 <<
" ";
1428 std::cout <<
"Read " << x.size() <<
" lines from " << filename << std::endl;
1433template <
class T1,
class T2>
1435 std::string filename
1438 ,std::string delineator =
" "
1440 ,
bool verbose =
false
1447 std::ifstream file_in(filename.c_str());
1449 std::string space =
" ";
1454 std::stringstream buffer;
1457 std::cout <<
"Can't open file " << filename << std::endl;
1459 throw std::runtime_error(
" Cannot open file.");
1462 std::cout <<
"Reading caustic information from " << filename << std::endl;
1464 while(i < skiplines){
1465 getline(file_in,myline);
1468 while(file_in.peek() ==
'#'){
1469 file_in.ignore(10000,
'\n');
1472 std::cout <<
"skipped "<< i <<
" comment lines in " << filename << std::endl;
1476 while(getline(file_in,myline)){
1478 if(myline[0] ==
'#'){
1479 std::cout <<
"skipped line " << i << std::endl;
1483 pos= myline.find_first_not_of(space);
1484 myline.erase(0,pos);
1487 pos = myline.find(delineator);
1488 strg.assign(myline,0,pos);
1491 if(verbose) std::cout << myt1 <<
" ";
1494 myline.erase(0,pos+1);
1495 pos= myline.find_first_not_of(space);
1496 myline.erase(0,pos);
1501 pos = myline.find(space);
1502 strg.assign(myline,0,pos);
1505 if(verbose) std::cout << myt2 << std::endl;
1513 std::cout <<
"Read " << x.size() <<
" lines from " << filename << std::endl;
1517template <
class T1,
class T2,
class T3>
1519 std::string filename
1523 ,std::string delineator =
" "
1524 ,
bool verbose =
false
1534 std::ifstream file_in(filename.c_str());
1536 std::string space =
" ";
1542 std::stringstream buffer;
1545 std::cout <<
"Can't open file " << filename << std::endl;
1547 throw std::runtime_error(
" Cannot open file.");
1550 std::cout <<
"Reading caustic information from " << filename << std::endl;
1552 while(file_in.peek() ==
'#'){
1553 file_in.ignore(10000,
'\n');
1556 std::cout <<
"skipped "<< i <<
" comment lines in " << filename << std::endl;
1560 while(getline(file_in,myline)){
1562 if(myline[0] ==
'#'){
1563 std::cout <<
"skipped line " << i << std::endl;
1567 pos= myline.find_first_not_of(space);
1568 myline.erase(0,pos);
1571 pos = myline.find(delineator);
1572 strg.assign(myline,0,pos);
1575 if(verbose) std::cout << myt1 <<
" ";
1578 myline.erase(0,pos+1);
1579 pos= myline.find_first_not_of(space);
1580 myline.erase(0,pos);
1588 pos = myline.find(space);
1589 strg.assign(myline,0,pos);
1592 if(verbose) std::cout << myt2 << std::endl;
1595 myline.erase(0,pos+1);
1596 pos= myline.find_first_not_of(space);
1597 myline.erase(0,pos);
1603 pos = myline.find(space);
1604 strg.assign(myline,0,pos);
1607 if(verbose) std::cout << myt3 << std::endl;
1615 std::cout <<
"Read " << x.size() <<
" lines from " << filename << std::endl;
1618int NumberOfEntries(
const std::string &
string,
char deliniator);
1622 ,
char comment_char =
'#'
1623 ,
char deliniator =
' '
1631 ,
const std::string filespec
1632 ,std::vector<std::string> & filenames
1650template <
typename T>
1652 ,std::string filename
1655 ,
char comment_char =
'#'
1657 ,
size_t MaxNrows = std::numeric_limits<size_t>::max()
1658 ,
bool verbose =
true){
1660 std::ifstream file(filename);
1662 if (!file.is_open()){
1663 std::cerr <<
"file " << filename <<
" cann't be opened." << std::endl;
1664 throw std::runtime_error(
"no file");
1673 size_t first_data_line;
1677 while(i < skiplines){
1678 std::getline(file,line);
1685 first_data_line = file.tellg();
1686 std::getline(file,line);
1688 }
while(line[0] == comment_char);
1690 columns = NumberOfEntries(line,
' ');
1692 file.seekg(first_data_line);
1694 std::copy(std::istream_iterator<T>(file),
1695 std::istream_iterator<T>(),
1696 std::back_inserter(data));
1698 rows = data.size()/columns;
1700 std::cout <<
"Read " << rows <<
" rows of " << columns <<
" columns from file " << filename << std::endl;
1721template <
typename T>
1723 ,std::vector<std::vector<T> > &data
1724 ,std::vector<std::string> &column_names
1725 ,
size_t MaxNumber = 100000000
1726 ,
char comment_char =
'#'
1727 ,
char deliniator =
','
1728 ,std::string replace =
"\\N"
1729 ,std::function<
bool(std::vector<T> &)> accept = [](std::vector<T> &v){
return true;}
1732 bool verbose =
false;
1734 std::ifstream file(filename);
1736 if (!file.is_open()){
1737 std::cerr <<
"file " << filename <<
" cann't be opened." << std::endl;
1738 throw std::runtime_error(
"no file");
1741 int count_preamble=0;
1745 std::getline(file,line);
1748 }
while(line[0] == comment_char);
1751 std::stringstream lineStream(line);
1753 column_names.empty();
1754 while(std::getline(lineStream,cell, deliniator))
1756 column_names.push_back(cell);
1759 if (!lineStream && cell.empty())
1761 column_names.push_back(
"");
1766 for(
auto st : column_names){
1767 std::cout << i++ <<
" " << st << std::endl;
1771 int ncolumns = NumberOfEntries(line,deliniator);
1774 std::getline(file,line);
1776 size_t n=0,m = 0,i=0;
1778 m = line.find(
",",n);
1779 cell = line.substr(n,m-n);
1781 cell.erase(remove_if(cell.begin(),cell.end(), isspace), cell.end());
1784 }
while(m != std::string::npos);
1788 size_t number_of_data_lines = 1;
1789 while(std::getline(file, line) && number_of_data_lines < MaxNumber ) ++number_of_data_lines;
1792 std::vector<std::vector<T> > tmp_data(ncolumns,std::vector<T>(0));
1793 swap(data,tmp_data);
1794 std::vector<T> tmp_row(ncolumns);
1800 file.open(filename);
1801 for(
int i=0; i < count_preamble; ++i){
1802 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
1806 while(std::getline(file,line) && rows < MaxNumber){
1808 std::stringstream lineStream(line);
1812 while(std::getline(lineStream,cell,deliniator))
1814 if(cell==replace) cell=
'0';
1817 cell.erase(remove_if(cell.begin(),cell.end(), isspace), cell.end());
1823 if(accept(tmp_row)){
1825 for(
int i=0 ; i < ncolumns ; ++i) data[i].push_back(tmp_row[i]);
1835template <
typename T>
1837 ,std::vector<std::vector<T> > &ranges
1838 ,std::vector<std::string> &column_names
1839 ,
size_t MaxNumber = 100000000
1840 ,
char comment_char =
'#'
1841 ,
char deliniator =
','
1842 ,std::string replace =
"\\N"
1843 ,std::function<
bool(std::vector<T> &)> accept = [](std::vector<T> &v){
return true;}
1846 bool verbose =
false;
1848 std::ifstream file(filename);
1850 if (!file.is_open()){
1851 std::cerr <<
"file " << filename <<
" cann't be opened." << std::endl;
1852 throw std::runtime_error(
"no file");
1855 int count_preamble=0;
1859 std::getline(file,line);
1862 }
while(line[0] == comment_char);
1865 std::stringstream lineStream(line);
1867 column_names.empty();
1868 while(std::getline(lineStream,cell,
','))
1870 column_names.push_back(cell);
1873 if (!lineStream && cell.empty())
1875 column_names.push_back(
"");
1880 for(
auto st : column_names){
1881 std::cout << i++ <<
" " << st << std::endl;
1885 int columns = NumberOfEntries(line,deliniator);
1887 std::vector<std::vector<T> > tmp_ranges(columns,std::vector<T>(2));
1888 swap(ranges,tmp_ranges);
1889 std::vector<T> tmp_row(columns);
1895 file.open(filename);
1896 for(
int i=0; i < count_preamble; ++i){
1897 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
1902 while(std::getline(file,line) && rows < MaxNumber){
1904 std::stringstream lineStream(line);
1908 while(std::getline(lineStream,cell,deliniator))
1910 if(cell==replace) cell=
'0';
1913 cell.erase(remove_if(cell.begin(),cell.end(), isspace), cell.end());
1917 if(accept(tmp_row)){
1919 for(
int j=0 ; j < columns ; ++j){
1920 ranges[j][0] = tmp_row[j];
1921 ranges[j][1] = tmp_row[j];
1925 for(
int j=0 ; j < columns ; ++j){
1926 if(ranges[j][0] > tmp_row[j]) ranges[j][0] = tmp_row[j];
1927 if(ranges[j][1] < tmp_row[j]) ranges[j][1] = tmp_row[j];
1951template <
typename T>
1953 ,std::vector<std::vector<T> > &data
1954 ,std::vector<std::string> &column_names
1955 ,
size_t Nmax = 1000000
1956 ,
char comment_char =
'#'
1957 ,
char deliniator =
','
1959 ,std::string reject =
""
1963 std::ifstream file(filename);
1965 if (!file.is_open()){
1966 std::cerr <<
"file " << filename <<
" cann't be opened." << std::endl;
1967 throw std::runtime_error(
"no file");
1972 std::getline(file,line);
1974 }
while(line[0] == comment_char);
1977 std::stringstream lineStream(line);
1979 column_names.empty();
1983 while(std::getline(lineStream,cell,deliniator))
1985 column_names.push_back(cell);
1989 if (!lineStream && cell.empty())
1991 column_names.push_back(
"");
1995 while(std::getline(lineStream,cell,deliniator))
1997 column_names.push_back(cell);
2000 data.emplace_back(column_names.size());
2002 for(
auto a : column_names){
2004 cell.erase(remove_if(cell.begin(),cell.end(), isspace), cell.end());
2010 int columns = NumberOfEntries(line,deliniator);
2012 for(
auto &v : data ) v.empty();
2014 while(std::getline(file,line) && ii < Nmax){
2016 data.emplace_back(columns);
2019 std::stringstream lineStream(line);
2023 while(std::getline(lineStream,cell, deliniator))
2025 assert(i < columns);
2026 cell.erase(remove_if(cell.begin(),cell.end(), isspace), cell.end());
2062 ,
const std::vector<std::string> header
2063 ,std::vector<T *> &data
2066 std::ofstream s(filename +
".csv");
2068 long ncol = header.size();
2069 assert(ncol == data.size() );
2070 for(
int i = 0 ; i < header.size()-1 ; ++i){
2071 s << header[i] <<
",";
2073 s << header.back() << std::endl;
2075 size_t nrow = data[0]->size();
2076 for(
auto v : data) assert(nrow == v->size() );
2078 for(
size_t j = 0 ; j < nrow ; ++j){
2079 for(
int i = 0 ; i < ncol-1 ; ++i){
2080 s << data[i]->operator[](j) <<
",";
2082 s << data.back()->operator[](j) <<
"\n";
2098 XYcsvLookUp(std::string datafile
2102 ,
size_t MaxNumber = 1000000
2103 ,
bool verbose =
false);
2105 std::string datafile
2108 ,std::vector<double> Xbins
2109 ,
size_t MaxNumber = 1000000
2110 ,
bool verbose =
false);
2113 std::vector<double> find(
double x,
double y);
2116 double operator[](std::string label)
const;
2119 double operator[](
int i)
const{
2120 return (*current)[i];
2123 double getX()
const {
2124 return (*current)[Xindex];
2127 double getY()
const {
2128 return (*current)[Yindex];
2131 std::vector<double> record()
const{
2135 std::vector<double> operator*()
const {
2140 if(current != data.end() ) ++current;
2143 if(current != data.begin() ) --current;
2146 std::vector<std::string> labels()
const{
2147 return column_names;
2150 double Xmin()
const {
return xmin;}
2151 double Xmax()
const {
return xmax;}
2153 double Ymin(
double x)
const;
2155 double Ymax(
double x)
const;
2157 void printcurrent(){
2159 for(
auto label : column_names){
2160 std::cout << label <<
" : " << (*current)[i++] << std::endl;
2169 std::vector<std::vector<double> >::iterator current;
2170 std::vector<std::vector<std::vector<double> >::iterator> borders;
2171 std::vector<std::vector<double> > data;
2172 std::vector<std::string> column_names;
2173 std::vector<double> Xborders;
2175 std::string filename;
2180void splitstring(std::string &line,std::vector<std::string> &vec,
const std::string &delimiter);
2190template<
typename T>
2194 ,
size_t MaxNumber = 1000000
2195 ,
char comment_char =
'#'
2196 ,
char deliniator =
','
2197 ,std::string replace =
"\\N"
2198 ,std::function<
bool(std::vector<T> &)> accept = [](std::vector<T> &v){
return true;}
2199 ):filename(datafile){
2200 input(datafile,MaxNumber,comment_char,deliniator,replace,accept);
2206 ,
size_t MaxNumber = 1000000
2207 ,
char comment_char =
'#'
2208 ,
char deliniator =
','
2209 ,std::string replace =
"\\N"
2210 ,std::function<
bool(std::vector<T> &)> accept = [](std::vector<T> &v){
return true;}
2212 filename = datafile;
2214 for(
int i=0 ; i<column_names.size() ; ++i) datamap[column_names[i]] = i;
2218 void pop(std::string colname){
2220 int i=datamap[colname];
2222 swap(column_names[i],column_names.back());
2223 column_names.pop_back();
2224 swap(data[i],data.back());
2228 for(
int i=0 ; i<column_names.size() ; ++i) datamap[column_names[i]] = i;
2233 if(datamap.find(label) == datamap.end()){
2234 std::cerr <<
"No label - " << label <<
" - in " << filename <<std::endl;
2235 throw std::invalid_argument(
"no label");
2237 return data[datamap[label]];
2244 void add_column(
const std::string &name,
const std::vector<T> &vec){
2245 if(vec.size() != data[0].size()){
2246 std::cerr <<
"Added column to DataFrame needs to have the same size." << std::endl;
2247 throw std::invalid_argument(
"wrong size");
2249 column_names.push_back(name);
2250 data.push_back(vec);
2251 data[name] = data.size()-1;
2261 return column_names;
2265 void sortby(std::string name){
2266 std::vector<size_t> index(data[0].size());
2267 size_t N = index.size();
2268 for(
size_t i=0 ; i<N ; ++i) index[i] = i;
2271 std::vector<T> tmp_v(N);
2273 for(
size_t j=0 ; j<data.size() ; ++j){
2274 for(
size_t i=0 ; i<N ; ++i){
2275 tmp_v[i] = data[j][index[i]];
2277 swap(data[j],tmp_v);
2282 void shuffle(Utilities::RandomNumbers_NR &ran){
2283 std::vector<size_t> index(data[0].size());
2284 size_t N = index.size();
2285 for(
size_t i=0 ; i<N ; ++i) index[i] = i;
2288 std::vector<T> tmp_v(N);
2290 for(
size_t j=0 ; j<data.size() ; ++j){
2291 for(
size_t i=0 ; i<N ; ++i){
2292 tmp_v[i] = data[j][index[i]];
2294 swap(data[j],tmp_v);
2298 size_t number_of_rows(){
return data[0].size();}
2299 size_t number_of_columns(){
return data.size();}
2301 std::vector<std::vector<T> > data;
2303 std::map<std::string,int> datamap;
2304 std::vector<std::string> column_names;
2305 std::string filename;
2309template <
typename T>
2310double KleinSum(std::vector<T> &input){
2311 double s = 0.0,cs = 0.0,ccs = 0.0,c,cc;
2312 size_t n = input.size();
2313 for(
size_t i = 0 ; i<n ; ++i){
2314 double t = s + input[i];
2315 if( abs(s) >= abs(input[i]) ){
2316 c = (s - t) + input[i];
2318 c = (input[i] - t) + s;
2322 if( abs(cs) >= abs(c) ){
2330 return s + cs + ccs;
2334template <
typename T,
typename F>
2335double PairWiseSum(T *begin,T *end,F value){
2337 size_t n = end - begin;
2339 for(T* p=begin ; p != end ; ++p){
2343 sum = PairWiseSum<T,F>(begin,begin + n/2,value) + PairWiseSum<T,F>(begin + n/2,end,value);
2349template <
typename T>
2350double PairWiseSum(T *begin,T *end){
2352 size_t n = end - begin;
2354 for(T* p=begin ; p != end ; ++p){
2358 sum = PairWiseSum(begin,begin + n/2) + PairWiseSum(begin + n/2,end);
2364template <
typename T>
2365double PairWiseSum(std::vector<T> &v){
2366 return PairWiseSum(v.data(),v.data() + v.size());
2370template <
typename T,
typename F>
2371double PairWiseSum(std::vector<T> &v,F value){
2372 return PairWiseSum(v.data(),v.data() + v.size(),value);
2376template <
typename T>
2381 SUMMER():batch(1000),n(0),ntotal(0),current(0.0){};
2382 SUMMER(
size_t batchsize)
2383 :batch(batchsize),n(0),current(0.0),ntotal(0){};
2391 v.push_back(current);
2399 if(v.size() ==0 )
return 0;
2400 v.push_back(current);
2401 return Utilities::PairWiseSum(v);
2406 std::vector<T> dump;
2430template <
typename T =
double>
2431double hypergeometric( T a, T b, T c, T x )
2435 throw std::invalid_argument(
"out of bounds");
2437 const double TOLERANCE = 1.0e-10;
2438 double term = a * b * x / c;
2439 double value = 1.0 + term;
2442 while ( abs( term ) > TOLERANCE )
2445 term *= a * b * x / c / n;
2455 typedef boost::variant<int,size_t,long,float,double,std::string,bool> MULTITYPE;
2456 typedef std::map<std::string,MULTITYPE> LINE;
2457 typedef std::map<std::string,std::vector<double> > VLINE;
2458 typedef std::map<std::string,std::vector<bool> > BLINE;
2466 std::string filename;
2467 std::string blanck_val;
2470 LOGPARAMS(std::string file,std::string blanck_value =
"0"):filename(file),blanck_val(blanck_value){
2475 print_file(filename);
2478 void operator()(std::string label,MULTITYPE v){lines[label] = v;}
2479 void operator()(std::string label,
const std::vector<double> &v){vlines[label] = v;}
2480 void operator()(std::string label,
const std::vector<bool> &v){blines[label] = v;}
2481 void operator()(std::string label){lines[label] =
"-----";}
2484 MULTITYPE operator[](std::string label){
return lines[label];}
2485 void setlogfile(std::string name){filename = name;}
2488 for(
auto a : lines){
2489 std::cout << std::left << std::setw(25) << a.first <<
" " << a.second << std::endl;
2491 for(
auto a : vlines){
2492 std::cout << std::left << std::setw(25) << a.first <<
" : ";
2493 for(
double x : a.second ) std::cout << x <<
" ";
2494 std::cout << std::endl;
2496 for(
auto a : blines){
2497 std::cout << std::left << a.first <<
" : ";
2498 for(
double x : a.second ) std::cout << x <<
" ";
2499 std::cout << std::endl;
2501 time_t now = time(0);
2502 std::cout <<
"log has existed for " << difftime(now,to)/60 <<
" min" << std::endl;
2505 void print_to_file(){
2506 print_file(filename);
2509 void print_file(std::string filename){
2512 std::ofstream logfile(filename);
2513 time_t now = time(0);
2514 struct tm date = *localtime(&now);
2515 logfile << date.tm_hour <<
":" << date.tm_min <<
" " << date.tm_mday <<
"/" << date.tm_mon <<
"/" << date.tm_year + 1900 << std::endl;
2516 logfile <<
"log has existed for " << difftime(now,to)/60 <<
" min" << std::endl;
2517 for(
auto a : lines){
2518 logfile << std::left << std::setw(23) << a.first <<
" " << a.second << std::endl;
2520 for(
auto a : vlines){
2521 logfile << std::left << std::setw(23) << a.first <<
" : ";
2522 for(
double x : a.second ) logfile << x <<
" ";
2523 logfile << std::endl;
2525 for(
auto a : blines){
2526 logfile << std::left << a.first <<
" : ";
2527 for(
double x : a.second ) logfile << x <<
" ";
2528 logfile << std::endl;
2538 typedef boost::variant<int,long,size_t,float,double,std::string,bool> MULTITYPE;
2539 typedef std::map<std::string,MULTITYPE> LINE;
2542 std::vector<LINE> lines;
2543 std::string filename;
2544 std::string blank_val;
2545 std::vector<std::string> header;
2548 long last_line_printed;
2550 std::set<std::string> labels;
2552 std::map<std::string,std::string> label_comments;
2558 const long default_precision = std::cout.precision();
2559 std::ofstream logfile;
2560 logfile.open(filename,std::ios_base::app);
2562 size_t n=labels.size();
2563 if(n == nlabels && last_line_printed > 0){
2564 std::cout << std::setprecision(precision);
2565 for(
size_t j=last_line_printed ; j<lines.size() ; ++j ){
2567 for(
auto &label : labels){
2570 logfile << std::setprecision(17) << lines[j].at(label);
2571 std::cout << std::setprecision(precision);
2572 }
else if(label ==
"RA" || label ==
"DEC" ){
2573 logfile << std::setprecision(8) << lines[j].at(label) ;
2574 std::cout << std::setprecision(precision);
2576 logfile << std::setprecision(precision) << lines[j].at(label) ;
2578 if(i<n-1) logfile <<
",";
2579 }
catch(std::exception& e){
2580 logfile << std::setprecision(precision) << blank_val ;
2581 if(i<n-1) logfile <<
",";
2585 logfile << std::endl;
2587 std::cout << std::setprecision(default_precision);
2589 last_line_printed = lines.size();
2598 LOGDATA(std::string file
2599 ,std::string blanck_value =
"0"
2601 ):filename(file),blank_val(blanck_value)
2602 ,nbatch(Nbatch),precision(std::cout.precision())
2603 ,last_line_printed(0),nlabels(0),nprints(0){
2607 ~LOGDATA(){print_to_file();}
2609 std::string output_file(){
return filename;}
2611 void set_precision(
int p){
2616 size_t ncol(){
return labels.size();}
2618 std::set<std::string> names = labels;
2620 void print_to_file(){
2622 const int default_precision = std::cout.precision();
2625 if(lines.size() == 0 )
return;
2627 std::ofstream logfile(filename);
2629 time_t now = time(0);
2630 struct tm date = *localtime(&now);
2631 logfile <<
"# " << date.tm_hour <<
":" << date.tm_min <<
" " << date.tm_mday <<
"/" << date.tm_mon <<
"/" << date.tm_year + 1900 << std::endl;
2632 for(std::string &s : header){
2633 logfile <<
"# " << s << std::endl;
2639 for(
auto &label : labels){
2640 auto it = label_comments.find(label);
2641 if(it == label_comments.end() ){
2642 logfile <<
"# " << i <<
" " << label <<
" : no comment" << std::endl;
2644 logfile <<
"# " << i <<
" " << it->first <<
" : " << it->second << std::endl;
2650 nlabels = labels.size();
2651 for(
auto &label : labels){
2653 if(i<nlabels-1) logfile <<
",";
2656 logfile << std::endl;
2658 std::cout << std::setprecision(precision);
2659 for(LINE &line : lines){
2661 for(
auto &label : labels){
2664 logfile << std::setprecision(17) << line.at(label) ;
2665 std::cout << std::setprecision(precision);
2666 }
else if(label ==
"RA" || label ==
"DEC" ){
2667 logfile << std::setprecision(8) << line.at(label) ;
2668 std::cout << std::setprecision(precision);
2670 logfile << std::setprecision(precision) << line.at(label);
2672 if(i<nlabels-1) logfile <<
",";
2673 }
catch(std::exception& e){
2674 logfile << std::setprecision(precision) << blank_val;
2675 if(i<nlabels-1) logfile <<
",";
2679 logfile << std::endl;
2681 std::cout << std::setprecision(default_precision);
2683 last_line_printed = lines.size();
2688 void add(
const LINE &line){
2689 for (
auto itr = line.begin(); itr != line.end(); ++itr) labels.insert(itr->first);
2690 lines.push_back(line);
2691 if(lines.size() % nbatch == 0) append_file();
2695 void replace(
const LINE &line){
2696 for (
auto itr = line.begin(); itr != line.end(); ++itr) labels.insert(itr->first);
2697 lines.back() = line;
2701 void header_comment(
const std::string &s){
2702 header.push_back(s);
2706 void label_description(
const std::string &name
2707 ,
const std::string &description
2709 label_comments[name] = description;
2750template <
typename T>
2751std::valarray<T>
AdaptiveSmooth(
const std::valarray<T> &map_in,
size_t Nx,
size_t Ny,T value){
2753 std::valarray<T> map_out(map_in.size());
2756 for(
long i=0;i<Nx;++i){
2757 for(
long j=0;j<Ny;++j){
2759 val = map_in[i+j*Nx];
2760 while(val < value && r < std::min(Nx, Ny) ){
2764 long imin,imax,jmin,jmax;
2766 imin = (i-r < 0) ? 0 : i-r;
2767 imax = (i+r > Nx-1) ? Nx-1 : i+r;
2769 jmin = (j-r < 0) ? 0 : j-r;
2770 jmax = (j+r > Ny-1) ? Ny-1 : j+r;
2773 for(
long ii=imin;ii<=imax;++ii){
2774 for(
long jj=jmin;jj<=jmax;++jj){
2775 if( (ii-i)*(ii-i) + (jj-j)*(jj-j) < r){
2776 val += map_in[ii+jj*Nx];
2784 map_out[i+j*Nx] = val/area;
std::vector< T > & operator[](const std::string &label)
returns column by name
Definition utilities_slsim.h:2232
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:2205
std::vector< T > & operator[](int i)
returns column by number
Definition utilities_slsim.h:2255
std::vector< std::string > labels() const
returns labels of the columns from the data file
Definition utilities_slsim.h:2260
void pop(std::string colname)
remove a column
Definition utilities_slsim.h:2218
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:2244
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:2193
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
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
void operator+=(T x)
add another number
Definition utilities_slsim.h:2386
size_t total_number()
returns the number of numbers that have been added
Definition utilities_slsim.h:2413
T operator*()
returns the current total
Definition utilities_slsim.h:2398
void reset()
reset to start over, frees memory
Definition utilities_slsim.h:2405
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
size_t oned_index(int i, int j)
convertion from 2d to 1d index
Definition utilities_slsim.h:223
namespace for input/output utilities
Definition utilities_slsim.h:1363
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:1651
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:1518
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:1434
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:1722
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:1952
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:1836
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:2061
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:1371
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:2751
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 range(std::vector< T > vec, T &max, T &min)
find the maximum and minimum of a vector
Definition utilities.h:247
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