GLAMERDOC++
Gravitational Lensing Code Library
Loading...
Searching...
No Matches
utilities_slsim.h
1#ifndef _UTILITIES_H
2#define _UTILITIES_H
3
4#include "standard.h"
5#include <typeinfo>
6#include <vector>
7#include <map>
8#include <string>
9#include <stdexcept>
10#include <algorithm>
11#include <utility>
12#include <iterator>
13#include <cstdlib>
14#include <random>
15//#if __cplusplus >= 201103L
16#include <typeindex>
17#include <dirent.h>
18#include <unistd.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21#include <boost/variant.hpp>
22#include <set>
23#include <iomanip>
24#include <thread>
25
26namespace Utilities
27{
29template<class T>
30T to_numeric(const std::string &str) {
31 return std::stoi(str);
32};
33template<>
34inline long to_numeric<long>(const std::string &str) {
35 try{
36 return std::stol(str);
37 }catch(...){
38 return -500;
39 }
40};
41template<>
42inline int to_numeric<int>(const std::string &str) {
43 return std::stoi(str);
44 try{
45 return std::stoi(str);
46 }catch(...){
47 return -500;
48 }
49};
50template<>
51inline float to_numeric<float>(const std::string &str) {
52 return std::stof(str);
53 try{
54 return std::stof(str);
55 }catch(...){
56 return -500;
57 }
58};
59template<>
60inline double to_numeric<double>(const std::string &str) {
61 try{
62 return std::stod(str);
63 }catch(...){
64 return -500;
65 }
66};
67//********************************************************
68
69template <typename T>
70bool AlwaysTrue(T t){return true;}
71
72template <typename T>
73bool AlwaysFalse(T t){return false;}
74
75template <typename T>
76T vec_sum(const std::vector<T> &v){
77 double sum=0.0;
78 for(const T &a : v) sum += a;
79 return sum;
80}
81
82// this is not for the user
83namespace detail
84{
85//#if __cplusplus < 201103L
86//class type_index
87//{
88//public:
89// type_index(const std::type_info& type) : t(type) {}
90// inline bool operator<(const type_index& rhs) const { return t.before(rhs.t); }
91//
92//private:
93// const std::type_info& t;
94//};
95//#else
96using std::type_index;
97//#endif
98}
99
100template <class T>
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;
106}
107
108template <class T>
109void free_Matrix(T **matrix, long rows, long){
110 if (rows) delete[] matrix[0];
111 delete []matrix;
112}
113
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);
118
120template<typename T>
122public:
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];
126 }
127 ~D3Matrix(){
128 delete[] array;
129 }
130
131 T& operator()(size_t i,size_t j,size_t k){
132 return array[i + j*xn + k*xn*yn];
133 }
134
135 T& operator()(size_t m){
136 return array[m];
137 }
138
139 size_t xindex(size_t m){
140 return m % (xn);
141 }
142 size_t yindex(size_t m){
143 return (m % (xn*yn) ) / xn;
144 }
145
146 size_t zindex(size_t m){
147 return m / (xn*yn);
148 }
149
150private:
151 size_t xn;
152 size_t yn;
153 size_t zn;
154 T *array;
155};
156
157
159template<typename T>
161
162public:
163 D2Matrix(size_t xsize,size_t ysize)
164 :xn(xsize),yn(ysize){
165 array = new T[xn*yn];
166 }
167 ~D2Matrix(){
168 delete[] array;
169 }
170
171 T& operator()(size_t i,size_t j){
172 return array[i + j*xn];
173 }
174
175 T& operator()(size_t m){
176 return array[m];
177 }
178
179 size_t xindex(size_t m){
180 return m % xn;
181 }
182 size_t yindex(size_t m){
183 return m / xn;
184 }
185
186
187private:
188 size_t xn;
189 size_t yn;
190 T *array;
191};
192
198template <typename T>
200 std::vector<T> v;
201 int n;
202 int m;
203public:
204 SymmetricMatrix(size_t n):n(n){
205 v.resize(n*(n+1)/2);
206 m = 2*n-1;
207 }
208 T& operator()(int i,int j){
209 //long k = j + (2*n-1-i)*i/2 ;
210 //size_t k = (i <= j ) ? j + (2*n-1-i)*i/2 : i + (2*n-1-j)*j/2;
211 size_t k = (i <= j ) ? j + (m-i)*i/2 : i + (m-j)*j/2;
212 //assert(k>=0);
213 //assert(k < v.size());
214 return v[ k ];
215 }
216 T& operator[](size_t k){
217 return v[ k ];
218 }
219
220 int size(){return n;}
221
223 size_t oned_index(int i,int j){
224 return (i <= j ) ? j + (m-i)*i/2 : i + (m-j)*j/2;
225 }
226};
227
228
270template<typename BaseT>
272{
273public: /* iterators */
274 template<typename ValueT = BaseT> // TODO: needs to check subclass type
275 class iterator
276 {
277 private:
278 typedef typename std::vector<BaseT*>::iterator base_iterator;
279 base_iterator it;
280
281 public:
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;
289
290 iterator() {}
291 iterator(base_iterator i) : it(i) {}
292 iterator(const iterator& other) : it(other.it) {}
293
294 iterator& operator=(const iterator& rhs) { it = rhs.it; return *this; }
295
296 reference operator*() { return (reference)(**it); }
297
298 //const reference operator*() const { return (const reference)(**it); }
299
300 pointer operator->() { return (pointer)(*it); }
301 const_pointer operator->() const { return (const_pointer)(*it); }
302
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; }
307
308 bool operator==(const iterator& rhs) const { return (it == rhs.it); }
309 bool operator!=(const iterator& rhs) const { return (it != rhs.it); }
310
311 iterator& operator+=(difference_type n) { it += n; return *this; }
312 iterator& operator-=(difference_type n) { it -= n; return *this; }
313
314 reference operator[](difference_type n) { return (reference)*it[n]; }
315
316 //const reference operator[](difference_type n) const { return (const reference)*it[n]; }
317
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); }
322
323 friend difference_type operator-(const iterator& b, const iterator& a) { return (b.it - a.it); }
324
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); }
329 };
330
331public:
334 {
335 }
336
339 {
340 clear();
341 }
342
345 {
346 using std::swap;
347
348 swap(a.items, b.items);
349 swap(a.tmap, b.tmap);
350 }
351
354 {
355 // copy-and-swap idiom
356 swap(*this, rhs);
357 return *this;
358 }
359
361 template<typename SubclassT>
362 void copy_back(const SubclassT& obj)
363 {
364 // make sure this is a subclass of BaseT
365 check_subclass(obj);
366
367 // copy the object
368 SubclassT* copy = new SubclassT(obj);
369
370 // add the copy of the object to the list of items
371 items.push_back(copy);
372
373 // add the copy to type map
374 tmap[typeid(SubclassT)].push_back(copy);
375 }
376
377
379 void pop_back()
380 {
381 // get very last element
382 BaseT* back = items.back();
383
384 // remove from the vector in the type map
385 tmap[back->type()].pop_back();
386
387 // remove from items
388 items.pop_back();
389
390 // delete from memory
391 delete back;
392 }
393
395 template<typename SubclassT>
396 void pop_back()
397 {
398 // search for vector belonging to SubclassT
399 typename tmap_t::iterator found = tmap.find(typeid(SubclassT));
400 if(found == tmap.end())
401 return;
402
403 // pop from vector for type
404 found->second.pop_back();
405
406 // go through list of all items from back, until item of correct type is found
407 for(typename base_container::reverse_iterator it = items.rbegin(); it != items.rend(); ++it)
408 {
409 if((*it)->type() == typeid(SubclassT))
410 {
411 // remove the item
412 delete (*it);
413 items.erase(it);
414 return;
415 }
416 }
417 }
418
420 void clear()
421 {
422 for(std::size_t i = 0, n = items.size(); i < n; ++i)
423 delete items[i];
424 items.clear();
425 tmap.clear();
426 }
427
429 template<typename SubclassT>
430 void clear()
431 {
432 // search for vector belonging to SubclassT
433 typename tmap_t::iterator found = tmap.find(typeid(SubclassT));
434 if(found == tmap.end())
435 return;
436
437 base_container& titems = found.second;
438
439 // delete items from vector for subclass
440 for(std::size_t i = 0, n = titems.size(); i < n; ++i)
441 delete titems[i];
442
443 // remove items for subclass
444 tmap.erase(found);
445
446 // erase items matching type
447 items.erase(std::remove_if(items.begin(), items.end(), is_type<SubclassT>), items.end());
448 }
449
451 BaseT* data()
452 {
453 return items.data();
454 }
455
457 const BaseT* data() const
458 {
459 return items.data();
460 }
461
463 template<typename SubclassT>
464 bool type(std::size_t i)
465 {
466 return is_type<SubclassT>(items[i]);
467 }
468
470 BaseT& operator[](std::size_t i)
471 {
472 return *items[i];
473 }
474
476 const BaseT& operator[](std::size_t i) const
477 {
478 return *items[i];
479 }
480
482 BaseT& get(std::size_t i)
483 {
484 return *items[i];
485 }
486
488 const BaseT& get(std::size_t i) const
489 {
490 return *items[i];
491 }
492
494 template<typename SubclassT>
495 SubclassT& get(std::size_t i)
496 {
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];
501 }
502
504 template<typename SubclassT>
505 const SubclassT& get(std::size_t i) const
506 {
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];
511 }
512
514 BaseT& at(std::size_t i)
515 {
516 return *items.at(i);
517 }
518
520 const BaseT& at(std::size_t i) const
521 {
522 return *items.at(i);
523 }
524
529 template<typename SubclassT>
530 SubclassT& at(std::size_t i)
531 {
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);
536 }
537
539 template<typename SubclassT>
540 const SubclassT& at(std::size_t i) const
541 {
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);
546 }
547
549 std::size_t size() const
550 {
551 return items.size();
552 }
553
555 template<typename SubclassT>
556 std::size_t size() const
557 {
558 typename tmap_t::const_iterator found = tmap.find(typeid(SubclassT));
559 if(found == tmap.end())
560 return 0;
561 return found->second.size();
562 }
563
565 bool empty() const
566 {
567 return items.empty();
568 }
569
571 template<typename SubclassT>
572 bool empty() const
573 {
574 typename tmap_t::const_iterator found = tmap.find(typeid(SubclassT));
575 if(found == tmap.end())
576 return true;
577
578 return found->second.empty();
579 }
580
582 iterator<> begin()
583 {
584 return iterator<>(items.begin());
585 }
586
588 iterator<> end()
589 {
590 return iterator<>(items.end());
591 }
592
594 template<typename SubclassT>
595 iterator<SubclassT> begin()
596 {
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());
601 }
602
604 template<typename SubclassT>
605 iterator<SubclassT> end()
606 {
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());
611 }
612
613private:
615 MixedVector(const MixedVector<BaseT>& other);
616
617 typedef std::vector<BaseT*> base_container;
618
619 inline static void check_subclass(const BaseT&) {}
620
621 template<typename SubclassT>
622 inline static bool is_type(const BaseT* obj) { return (typeid(*obj) == typeid(SubclassT)); }
623
624 base_container items;
625
626 typedef std::map<detail::type_index, base_container> tmap_t;
627 tmap_t tmap;
628};
629
631template<typename BaseT>
632class MixedVector<BaseT*>
633{
634public: /* iterators */
635 template<typename ValueT = BaseT*> // TODO: needs to check subclass pointer type
636 class iterator
637 {
638 private:
639 typedef typename std::vector<BaseT*>::iterator base_iterator;
640 base_iterator it;
641
642 public:
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;
648
649 iterator() {}
650 iterator(base_iterator i) : it(i) {}
651 iterator(const iterator& other) : it(other.it) {}
652
653 iterator& operator=(const iterator& rhs) { it = rhs.it; return *this; }
654
655 reference operator*() { return (reference)*it; }
656 const reference operator*() const { return (const reference)*it; }
657
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; }
662
663 bool operator==(const iterator& rhs) const { return (it == rhs.it); }
664 bool operator!=(const iterator& rhs) const { return (it != rhs.it); }
665
666 iterator& operator+=(difference_type n) { it += n; return *this; }
667 iterator& operator-=(difference_type n) { it -= n; return *this; }
668
669 pointer operator[](difference_type n) { return (pointer)it[n]; }
670 const pointer operator[](difference_type n) const { return (const pointer)it[n]; }
671
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); }
676
677 friend difference_type operator-(const iterator& b, const iterator& a) { return (b.it - a.it); }
678
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); }
683 };
684
685public:
688 {
689 }
690
693 : items(other.items), tmap(other.tmap)
694 {
695 }
696
699 {
700 clear();
701 }
702
705 {
706 using std::swap;
707
708 swap(a.items, b.items);
709 swap(a.tmap, b.tmap);
710 }
711
714 {
715 // copy-and-swap idiom
716 swap(*this, rhs);
717 return *this;
718 }
719
721 BaseT& back()
722 {
723 return items.back();
724 }
725
727 const BaseT& back() const
728 {
729 return items.back();
730 }
731
733 void push_back(BaseT* obj)
734 {
735 // make sure this is a subclass of BaseT
736 check_subclass(obj);
737
738 // add the copy of the object pointer to the list of items
739 items.push_back(obj);
740
741 // add the copy to type map
742 tmap[typeid(*obj)].push_back(obj);
743 }
744
746 void pop_back()
747 {
748 // get very last element
749 BaseT* back = items.back();
750
751 // remove from the vector in the type map
752 tmap[typeid(*back)].pop_back();
753
754 // remove from items
755 items.pop_back();
756
757 delete back; // delete object
758 }
759
761 template<typename SubclassT>
762 void pop_back()
763 {
764 // search for vector belonging to SubclassT
765 typename tmap_t::iterator found = tmap.find(typeid(SubclassT));
766 if(found == tmap.end())
767 return;
768
769 // pop from vector for type
770 found->second.pop_back();
771
772 // go through list of all items from back, until item of correct type is found
773 for(typename std::vector<BaseT*>::reverse_iterator it = items.rbegin(); it != items.rend(); ++it)
774 {
775 if(typeid(**it) == typeid(SubclassT))
776 {
777 // remove the item
778 delete(*it);
779 items.erase(it);
780 return;
781 }
782 }
783 }
784
786 void clear()
787 {
788 while(!empty()) pop_back();
789 }
790
792 template<typename SubclassT>
793 void clear()
794 {
795 // remove vector for subclass
796 tmap.erase(typeid(SubclassT));
797
798 // erase items matching type
799 items.erase(std::remove_if(items.begin(), items.end(), is_type<SubclassT>), items.end());
800 }
801
803 BaseT** data()
804 {
805 return items.data();
806 }
807
809 const BaseT** data() const
810 {
811 return items.data();
812 }
813
815 template<typename SubclassT>
816 bool type(std::size_t i)
817 {
818 return is_type<SubclassT>(items[i]);
819 }
820
822 BaseT* operator[](std::size_t i) const
823 {
824 return items[i];
825 }
826
828 BaseT* get(std::size_t i)
829 {
830 return items[i];
831 }
832
834 const BaseT* get(std::size_t i) const
835 {
836 return items[i];
837 }
838
840 template<typename SubclassT>
841 SubclassT* get(std::size_t i)
842 {
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];
847 }
848
850 template<typename SubclassT>
851 const SubclassT* get(std::size_t i) const
852 {
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];
857 }
858
860 BaseT* at(std::size_t i)
861 {
862 return items.at(i);
863 }
864
866 const BaseT* at(std::size_t i) const
867 {
868 return items.at(i);
869 }
870
875 template<typename SubclassT>
876 SubclassT* at(std::size_t i)
877 {
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);
882 }
883
885 template<typename SubclassT>
886 const SubclassT* at(std::size_t i) const
887 {
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);
892 }
893
895 std::size_t size() const
896 {
897 return items.size();
898 }
899
901 template<typename SubclassT>
902 std::size_t size() const
903 {
904 typename tmap_t::const_iterator found = tmap.find(typeid(SubclassT));
905 if(found == tmap.end())
906 return 0;
907 return found->second.size();
908 }
909
911 bool empty() const
912 {
913 return items.empty();
914 }
915
917 template<typename SubclassT>
918 bool empty() const
919 {
920 typename tmap_t::const_iterator found = tmap.find(typeid(SubclassT));
921 if(found == tmap.end())
922 return true;
923
924 return found->second.empty();
925 }
926
928 iterator<> begin()
929 {
930 return iterator<>(items.begin());
931 }
932
934 iterator<> end()
935 {
936 return iterator<>(items.end());
937 }
938
940 template<typename SubclassT>
941 iterator<SubclassT> begin()
942 {
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());
947 }
948
950 template<typename SubclassT>
951 iterator<SubclassT> end()
952 {
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());
957 }
958
959private:
960 inline void check_subclass(const BaseT*) {}
961
962 template<typename SubclassT>
963 inline static bool is_type(const BaseT* obj) { return (typeid(*obj) == typeid(SubclassT)); }
964
965 typedef std::vector<BaseT*> base_container;
966
967 base_container items;
968
969 typedef std::map<detail::type_index, base_container> tmap_t;
970 tmap_t tmap;
971};
972
973template<class BaseT>
974std::size_t lower_bound(std::vector<BaseT*>& items, PosType target){
975 std::size_t ju,jm,jl;
976
977 if(items.size() == 0) return 0;
978
979 jl=0;
980 ju=items.size()-1;
981 while (ju-jl > 1) {
982 jm=(ju+jl) >> 1;
983 if(items[jm]->compareZ(target))
984 jl=jm;
985 else
986 ju=jm;
987 }
988 return jl;
989}
990
992template<typename Container>
993void delete_container(Container& c) { while(!c.empty()) delete c.back(), c.pop_back(); }
994
1004public:
1006 PosType x_min
1007 ,PosType y_min
1008 ,PosType my_range
1009 ,PosType smallsize
1010 ):
1011 range(my_range)
1012 {
1013 xo[0] = x_min;
1014 xo[1] = y_min;
1015 n = (int)(range/smallsize+1);
1016 }
1017
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);
1022
1023private:
1024 int n;
1025 PosType xo[2],range;
1026 void rot(int s,int *x, int *y, int rx, int ry);
1027};
1028
1029template<typename T>
1030T between(const T& x, const T& l, const T& u)
1031{
1032 return std::max(l, std::min(u, x));
1033}
1034
1035#ifdef ENABLE_CLANG
1037class RandomNumbers{
1038public:
1039
1040 RandomNumbers(unsigned int seed);
1041 ~RandomNumbers(void);
1042
1043 PosType operator()(void);
1045 PosType gauss(){return norm_dist(rand_gen);}
1046private:
1047
1048 std::normal_distribution<> norm_dist;
1049 std::mt19937 rand_gen;
1050};
1051#endif
1052
1060public:
1061
1062 RandomNumbers_NR(long seed);
1063 // sets seed from clock
1065
1066 PosType operator()(void);
1068 PosType gauss(){
1069 ++calls;
1070 if(count){
1071 do{
1072 u = 2*ran2() - 1;
1073 v = 2*ran2() - 1;
1074 s = u*u +v*v;
1075 }while( s > 1.0 || s == 0.0);
1076
1077 s = sqrt(-2*log(s)/s);
1078 count = false;
1079 return s*u;
1080 }else{
1081 count = true;
1082 return s*v;
1083 }
1084 };
1085
1086 int poisson(double lam){
1087 if(lam > 200){
1088 return std::max<long>(std::lround( gauss()*sqrt(lam) + lam ) ,0);
1089 }
1090 double L = exp(-lam),p=1;
1091 int k = 0;
1092 do{
1093 ++k;
1094 p *= operator()();
1095 }while(p>L);
1096
1097 return k-1;
1098 }
1099
1100// int poisson(double lam){
1101//
1102// int step = 500;
1103// double Lam = lam,p=1;
1104// int k = 0;
1105// do{
1106// ++k;
1107// p *= operator()();
1108// while(p<1 && Lam>0){
1109// if(Lam > step){
1110// p *= exp(step);
1111// Lam -= step;
1112// }else{
1113// p *= exp(Lam);
1114// Lam=0;
1115// }
1116// }
1117// }while(p>1);
1118//
1119// return k-1;
1120// }
1121
1122 size_t calls = 0;
1123
1124 long getseed(){return firstseed;}
1125private:
1126 long idum;
1127 PosType ran2(void);
1128
1129 int IM1;
1130 int IM2;
1131 PosType AM;
1132 //int IMM1 = (IM1-1);
1133 int IA1;
1134 int IA2;
1135 int IQ1;
1136 int IQ2;
1137 int IR1;
1138 int IR2;
1139 int NDIV;
1140 PosType EPS;
1141 PosType RNMX;
1142
1143 long idum2;
1144 long iy;
1145 long iv[32];
1146 bool count;
1147 PosType u,v,s;
1148 long firstseed;
1149
1150};
1151
1153template <typename T, typename R>
1155 std::vector<T> &vec
1156 ,R &ran
1157){
1158
1159 size_t ran_t;
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));
1163 //swap(tmp,vec[ran_t]);
1164 std::swap(vec[ran_t],vec[i]);
1165 //swap(vec[i],tmp);
1166 }
1167}
1168
1170template <typename T>
1171void sort_indexes(const std::vector<T> &v
1172 ,std::vector<size_t> &index
1173) {
1174
1175 // initialise original index locations
1176 index.resize(v.size());
1177 for (size_t i = 0; i != index.size(); ++i) index[i] = i;
1178
1179 // sort indexes based on comparing values in v
1180 std::sort(index.begin(), index.end(),[&v](size_t i1, size_t i2) {return v[i1] < v[i2];});
1181}
1182
1183template <typename T>
1184void sort_indexes(const T *v
1185 ,std::vector<size_t> &index
1186 ,size_t N) {
1187
1188 // initialise original index locations
1189 index.resize(N);
1190
1191 for (size_t i = 0; i != index.size(); ++i) index[i] = i;
1192
1193 // sort indexes based on comparing values in v
1194 std::sort(index.begin(), index.end(),
1195 [v](size_t i1, size_t i2) {return v[i1] < v[i2];});
1196}
1197
1199template <typename T>
1200void sort_indexes_decending(const std::vector<T> &v
1201 ,std::vector<size_t> &index
1202) {
1203
1204 // initialise original index locations
1205 index.resize(v.size());
1206 for (size_t i = 0; i != index.size(); ++i) index[i] = i;
1207
1208 // sort indexes based on comparing values in v
1209 std::sort(index.begin(), index.end(),
1210 [&v](size_t i1, size_t i2) {return v[i1] > v[i2];});
1211}
1212
1218template <typename R>
1220public:
1221 ShuffledIndex(size_t N,R &ran){
1222 if(N == 0) throw std::invalid_argument("N=0");
1223
1224 for(size_t i=0 ; i<N ; ++i) index[i] = i;
1225 index.resize(N);
1226 Utilities::shuffle(index,ran);
1227 index_internal = 0;
1228 }
1229
1231 size_t operator*(){return index[index_internal];}
1233 size_t operator++(int){
1234 size_t tmp = index[index_internal];
1235 if(index_internal == index.size()){
1236 index_internal = 0;
1237 }else{
1238 ++index_internal;
1239 }
1240 return tmp;
1241 }
1243 size_t operator++(){
1244 if(index_internal == index.size()){
1245 index_internal = 0;
1246 }else{
1247 ++index_internal;
1248 }
1249 return index[index_internal];
1250 }
1251
1253 void reshuffle(R &ran){
1254 Utilities::shuffle(index,ran);
1255 index_internal = 0;
1256 }
1257
1258private:
1259 std::vector<size_t> index;
1260 size_t index_internal;
1261};
1262
1263
1264// reorders vec according to index p
1265template <typename T>
1266void apply_permutation(
1267 T *vec,
1268 const std::vector<std::size_t>& p)
1269{
1270 std::vector<T> copy(p.size());
1271
1272 for(std::size_t i = 0; i < p.size(); ++i){
1273 copy[i] = vec[i];
1274 }
1275
1276 for(std::size_t i = 0; i < p.size(); ++i){
1277 vec[i] = copy[p[i]];
1278 }
1279}
1280
1281template <typename T>
1282void apply_permutation(
1283 std::vector<T>& vec,
1284 const std::vector<std::size_t>& p)
1285{
1286 apply_permutation(vec.data(),p);
1287}
1288
1289
1294void powerspectrum2d(
1295 std::valarray<double> const &aa
1296 ,std::valarray<double> const &bb
1297 ,long nx
1298 ,long ny
1299 ,double boxlx
1300 ,double boxly
1301 ,std::vector<double> &ll
1302 ,std::vector<double> &Pl
1303 ,double zeropaddingfactor
1304 );
1305
1306void powerspectrum2d(
1307 std::valarray<double> &aa
1308 ,long nx
1309 ,long ny
1310 ,double boxlx
1311 ,double boxly
1312 ,std::vector<double> &ll
1313 ,std::vector<double> &Pl
1314);
1315
1317 std::valarray<double> &aa
1318 ,long nx
1319 ,long ny
1320 ,double boxlx
1321 ,double boxly
1322 ,const std::vector<double> &ll
1323 ,std::vector<double> &Pl
1324 ,std::vector<double> &llave
1325);
1326
1327void powerspectrum2d(
1328 std::valarray<float> const &aa
1329 ,std::valarray<float> const &bb
1330 ,long nx
1331 ,long ny
1332 ,double boxlx
1333 ,double boxly
1334 ,std::vector<double> &ll
1335 ,std::vector<double> &Pl
1336 ,double zeropaddingfactor
1337 );
1338
1339void powerspectrum2d(
1340 std::valarray<float> &aa
1341 ,long nx
1342 ,long ny
1343 ,double boxlx
1344 ,double boxly
1345 ,std::vector<double> &ll
1346 ,std::vector<double> &Pl
1347);
1348
1350 std::valarray<float> &aa
1351 ,int nx
1352 ,int ny
1353 ,double boxlx
1354 ,double boxly
1355 ,const std::vector<double> &ll
1356 ,std::vector<double> &Pl
1357 ,std::vector<double> &llave
1358);
1359
1361int GetNThreads();
1362
1364namespace IO{
1365
1366inline bool file_exists (const std::string& name) {
1367 struct stat buffer;
1368 return (stat (name.c_str(), &buffer) == 0);
1369}
1370
1371 template <class T>
1373 std::string filename
1374 ,std::vector<T> &x
1375 ,int skiplines = 0
1376 ,bool verbose = false
1377 ){
1378
1379 x.clear();
1380
1381 std::ifstream file_in(filename.c_str());
1382 std::string myline;
1383 std::string space = " ";
1384 T myt1;
1385
1386 std::string strg;
1387 std::stringstream buffer;
1388
1389 if(!file_in){
1390 std::cout << "Can't open file " << filename << std::endl;
1391 ERROR_MESSAGE();
1392 throw std::runtime_error(" Cannot open file.");
1393 }
1394
1395 std::cout << "Reading from " << filename << std::endl;
1396 size_t i=0;
1397 while(i < skiplines){
1398 getline(file_in,myline);
1399 ++i;
1400 }
1401 while(file_in.peek() == '#'){
1402 file_in.ignore(10000,'\n');
1403 ++i;
1404 }
1405 std::cout << "skipped "<< i << " comment lines in " << filename << std::endl;
1406
1407 size_t pos;
1408 // read in data
1409 while(getline(file_in,myline)){
1410
1411 if(myline[0] == '#'){
1412 std::cout << "skipped line " << i << std::endl;
1413 continue;
1414 }
1415
1416 pos= myline.find_first_not_of(space);
1417 myline.erase(0,pos);
1418
1419 buffer << myline;
1420 buffer >> myt1;
1421 if(verbose) std::cout << myt1 << " ";
1422 x.push_back(myt1);
1423
1424 strg.clear();
1425 buffer.clear();
1426 myline.clear();
1427
1428 }
1429 std::cout << "Read " << x.size() << " lines from " << filename << std::endl;
1430 }
1431
1434template <class T1,class T2>
1436 std::string filename
1437 ,std::vector<T1> &x
1438 ,std::vector<T2> &y
1439 ,std::string delineator = " "
1440 ,int skiplines = 0
1441 ,bool verbose = false
1442
1443 ){
1444
1445 x.clear();
1446 y.clear();
1447
1448 std::ifstream file_in(filename.c_str());
1449 std::string myline;
1450 std::string space = " ";
1451 T1 myt1;
1452 T2 myt2;
1453
1454 std::string strg;
1455 std::stringstream buffer;
1456
1457 if(!file_in){
1458 std::cout << "Can't open file " << filename << std::endl;
1459 ERROR_MESSAGE();
1460 throw std::runtime_error(" Cannot open file.");
1461 }
1462
1463 std::cout << "Reading caustic information from " << filename << std::endl;
1464 size_t i=0;
1465 while(i < skiplines){
1466 getline(file_in,myline);
1467 ++i;
1468 }
1469 while(file_in.peek() == '#'){
1470 file_in.ignore(10000,'\n');
1471 ++i;
1472 }
1473 std::cout << "skipped "<< i << " comment lines in " << filename << std::endl;
1474
1475 size_t pos;
1476 // read in data
1477 while(getline(file_in,myline)){
1478
1479 if(myline[0] == '#'){
1480 std::cout << "skipped line " << i << std::endl;
1481 continue;
1482 }
1483
1484 pos= myline.find_first_not_of(space);
1485 myline.erase(0,pos);
1486
1487
1488 pos = myline.find(delineator);
1489 strg.assign(myline,0,pos);
1490 buffer << strg;
1491 buffer >> myt1;
1492 if(verbose) std::cout << myt1 << " ";
1493 x.push_back(myt1);
1494
1495 myline.erase(0,pos+1);
1496 pos= myline.find_first_not_of(space);
1497 myline.erase(0,pos);
1498
1499 strg.clear();
1500 buffer.clear();
1501
1502 pos = myline.find(space);
1503 strg.assign(myline,0,pos);
1504 buffer << strg;
1505 buffer >> myt2;
1506 if(verbose) std::cout << myt2 << std::endl;
1507 y.push_back(myt2);
1508
1509 strg.clear();
1510 buffer.clear();
1511 myline.clear();
1512
1513 }
1514 std::cout << "Read " << x.size() << " lines from " << filename << std::endl;
1515}
1518template <class T1,class T2,class T3>
1520 std::string filename
1521 ,std::vector<T1> &x
1522 ,std::vector<T2> &y
1523 ,std::vector<T3> &z
1524 ,std::string delineator = " "
1525 ,bool verbose = false
1526
1527 ){
1528
1529
1530 assert(0); // Untested!!!!
1531 x.clear();
1532 y.clear();
1533 z.clear();
1534
1535 std::ifstream file_in(filename.c_str());
1536 std::string myline;
1537 std::string space = " ";
1538 T1 myt1;
1539 T2 myt2;
1540 T3 myt3;
1541
1542 std::string strg;
1543 std::stringstream buffer;
1544
1545 if(!file_in){
1546 std::cout << "Can't open file " << filename << std::endl;
1547 ERROR_MESSAGE();
1548 throw std::runtime_error(" Cannot open file.");
1549 }
1550
1551 std::cout << "Reading caustic information from " << filename << std::endl;
1552 size_t i=0;
1553 while(file_in.peek() == '#'){
1554 file_in.ignore(10000,'\n');
1555 ++i;
1556 }
1557 std::cout << "skipped "<< i << " comment lines in " << filename << std::endl;
1558
1559 size_t pos;
1560 // read in data
1561 while(getline(file_in,myline)){
1562
1563 if(myline[0] == '#'){
1564 std::cout << "skipped line " << i << std::endl;
1565 continue;
1566 }
1567
1568 pos= myline.find_first_not_of(space);
1569 myline.erase(0,pos);
1570
1571
1572 pos = myline.find(delineator);
1573 strg.assign(myline,0,pos);
1574 buffer << strg;
1575 buffer >> myt1;
1576 if(verbose) std::cout << myt1 << " ";
1577 x.push_back(myt1);
1578
1579 myline.erase(0,pos+1);
1580 pos= myline.find_first_not_of(space);
1581 myline.erase(0,pos);
1582
1583 strg.clear();
1584 buffer.clear();
1585
1586 // ******************
1587
1588
1589 pos = myline.find(space);
1590 strg.assign(myline,0,pos);
1591 buffer << strg;
1592 buffer >> myt2;
1593 if(verbose) std::cout << myt2 << std::endl;
1594 y.push_back(myt2);
1595
1596 myline.erase(0,pos+1);
1597 pos= myline.find_first_not_of(space);
1598 myline.erase(0,pos);
1599
1600 strg.clear();
1601 buffer.clear();
1602
1603 // ******************
1604 pos = myline.find(space);
1605 strg.assign(myline,0,pos);
1606 buffer << strg;
1607 buffer >> myt3;
1608 if(verbose) std::cout << myt3 << std::endl;
1609 y.push_back(myt3);
1610
1611 strg.clear();
1612 buffer.clear();
1613 myline.clear();
1614
1615 }
1616 std::cout << "Read " << x.size() << " lines from " << filename << std::endl;
1617}
1618
1619int NumberOfEntries(const std::string &string,char deliniator);
1620
1622int CountColumns(std::string filename
1623 ,char comment_char = '#'
1624 ,char deliniator = ' '
1625);
1626
1630void ReadFileNames(
1631 std::string dir
1632 ,const std::string filespec
1633 ,std::vector<std::string> & filenames
1634 ,bool verbose);
1635
1637bool check_directory(std::string dir);
1639bool make_directories(const std::string &root_dir);
1640
1641
1651template <typename T>
1652void ReadASCII(std::vector<T> &data
1653 ,std::string filename
1654 ,int &columns
1655 ,int &rows
1656 ,char comment_char = '#'
1657 ,int skiplines = 0
1658 ,size_t MaxNrows = std::numeric_limits<size_t>::max()
1659 ,bool verbose = true){
1660
1661 std::ifstream file(filename);
1662 // find number of particles
1663 if (!file.is_open()){
1664 std::cerr << "file " << filename << " cann't be opened." << std::endl;
1665 throw std::runtime_error("no file");
1666 }
1667
1668
1669 data.empty();
1670
1671 std::string line;
1672 columns = 0;
1673 rows = 0;
1674 size_t first_data_line;
1675
1676 // skip over first lines
1677 int i=0;
1678 while(i < skiplines){
1679 std::getline(file,line);
1680 if(!file) break; // probably EOF
1681 ++i;
1682 }
1683
1684 // read comment lines and first data line
1685 do{
1686 first_data_line = file.tellg();
1687 std::getline(file,line);
1688 if(!file) break; // probably EOF
1689 }while(line[0] == comment_char);
1690
1691 columns = NumberOfEntries(line,' ');
1692
1693 file.seekg(first_data_line); // move back to first data line
1694
1695 std::copy(std::istream_iterator<T>(file),
1696 std::istream_iterator<T>(),
1697 std::back_inserter(data));
1698
1699 rows = data.size()/columns;
1700 if(verbose){
1701 std::cout << "Read " << rows << " rows of " << columns << " columns from file " << filename << std::endl;
1702 }
1703}
1704
1722template <typename T>
1723int ReadCSVnumerical1(std::string filename
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;}
1731){
1732
1733 bool verbose = false;
1734
1735 std::ifstream file(filename);
1736 // find number of particles
1737 if (!file.is_open()){
1738 std::cerr << "file " << filename << " cann't be opened." << std::endl;
1739 throw std::runtime_error("no file");
1740 }
1741
1742 int count_preamble=0;
1743 std::string line;
1744 // read comment lines and first data line
1745 do{
1746 std::getline(file,line);
1747 ++count_preamble;
1748 if(!file) break; // probably EOF
1749 }while(line[0] == comment_char);
1750
1751 // read the names
1752 std::stringstream lineStream(line);
1753 std::string cell;
1754 column_names.empty();
1755 while(std::getline(lineStream,cell, deliniator))
1756 {
1757 column_names.push_back(cell);
1758 }
1759 // This checks for a trailing comma with no data after it.
1760 if (!lineStream && cell.empty())
1761 {
1762 column_names.push_back("");
1763 }
1764
1765 if(verbose){ // print colum names
1766 int i = 0;
1767 for(auto st : column_names){
1768 std::cout << i++ << " " << st << std::endl;
1769 }
1770 }
1771
1772 int columns = NumberOfEntries(line,deliniator);
1773
1774 // count number of data line
1775 size_t number_of_data_lines = 0;
1776 while(std::getline(file, line) && number_of_data_lines < MaxNumber ) ++number_of_data_lines;
1777
1778 //std::vector<std::vector<T> > tmp_data(columns,std::vector<T>(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);
1782
1784 //file.seekg(std::ios::beg);
1785 file.clear();
1786 file.close();
1787 file.open(filename);
1788 for(int i=0; i < count_preamble; ++i){
1789 file.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
1790 }
1791 //std::getline(file,line);
1792
1793 size_t rows = 0;
1794 //data.resize(columns);
1795 //for(auto &v : data ) v.empty();
1796 while(std::getline(file,line) && rows < MaxNumber){
1797 // read the names
1798 std::stringstream lineStream(line);
1799 std::string cell;
1800
1801 int i=0;
1802 while(std::getline(lineStream,cell,deliniator))
1803 {
1804 if(cell==replace) cell='0';
1805
1807 cell.erase(remove_if(cell.begin(),cell.end(), isspace), cell.end());
1808 //data[i].push_back(to_numeric<T>(cell));
1809 //data[i][rows] = to_numeric<T>(cell);
1810 tmp_row[i] = to_numeric<T>(cell);
1811 i = (i+1)%columns;
1812 }
1813 if(accept(tmp_row)){
1814 ++rows;
1815 for(int i=0 ; i < columns ; ++i) data[i].push_back(tmp_row[i]);
1816 }
1817 }
1818 return 1;
1819}
1820
1825template <typename T>
1826size_t ReadCSVrange(std::string filename
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;}
1834){
1835
1836 bool verbose = false;
1837
1838 std::ifstream file(filename);
1839 // find number of particles
1840 if (!file.is_open()){
1841 std::cerr << "file " << filename << " cann't be opened." << std::endl;
1842 throw std::runtime_error("no file");
1843 }
1844
1845 int count_preamble=0;
1846 std::string line;
1847 // read comment lines and first data line
1848 do{
1849 std::getline(file,line);
1850 ++count_preamble;
1851 if(!file) break; // probably EOF
1852 }while(line[0] == comment_char);
1853
1854 // read the names
1855 std::stringstream lineStream(line);
1856 std::string cell;
1857 column_names.empty();
1858 while(std::getline(lineStream,cell, ','))
1859 {
1860 column_names.push_back(cell);
1861 }
1862 // This checks for a trailing comma with no data after it.
1863 if (!lineStream && cell.empty())
1864 {
1865 column_names.push_back("");
1866 }
1867
1868 if(verbose){ // print colum names
1869 int i = 0;
1870 for(auto st : column_names){
1871 std::cout << i++ << " " << st << std::endl;
1872 }
1873 }
1874
1875 int columns = NumberOfEntries(line,deliniator);
1876
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);
1880
1882 //file.seekg(std::ios::beg);
1883 file.clear();
1884 file.close();
1885 file.open(filename);
1886 for(int i=0; i < count_preamble; ++i){
1887 file.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
1888 }
1889 //std::getline(file,line);
1890
1891 size_t rows = 0;
1892 while(std::getline(file,line) && rows < MaxNumber){
1893 // read the names
1894 std::stringstream lineStream(line);
1895 std::string cell;
1896
1897 int i=0;
1898 while(std::getline(lineStream,cell,deliniator))
1899 {
1900 if(cell==replace) cell='0';
1901
1903 cell.erase(remove_if(cell.begin(),cell.end(), isspace), cell.end());
1904 tmp_row[i] = to_numeric<T>(cell);
1905 i = (i+1)%columns;
1906 }
1907 if(accept(tmp_row)){
1908 if(rows==0){
1909 for(int j=0 ; j < columns ; ++j){
1910 ranges[j][0] = tmp_row[j];
1911 ranges[j][1] = tmp_row[j];
1912
1913 }
1914 }else{
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];
1918 }
1919 }
1920 ++rows;
1921 }
1922 }
1923 return rows;
1924}
1925
1941template <typename T>
1942int ReadCSVnumerical2(std::string filename
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 = ','
1948 ,bool header = true
1949 ,std::string reject = ""
1950){
1951
1952
1953 std::ifstream file(filename);
1954
1955 if (!file.is_open()){
1956 std::cerr << "file " << filename << " cann't be opened." << std::endl;
1957 throw std::runtime_error("no file");
1958 }
1959 std::string line;
1960 // read comment lines and first data line
1961 do{
1962 std::getline(file,line);
1963 if(!file) break; // probably EOF
1964 }while(line[0] == comment_char);
1965
1966 // read the names
1967 std::stringstream lineStream(line);
1968 std::string cell;
1969 column_names.empty();
1970 long ii = 0;
1971
1972 if(header){
1973 while(std::getline(lineStream,cell,deliniator))
1974 {
1975 column_names.push_back(cell);
1976 }
1977
1978 // This checks for a trailing comma with no data after it.
1979 if (!lineStream && cell.empty())
1980 {
1981 column_names.push_back("");
1982 }
1983 }else{
1984
1985 while(std::getline(lineStream,cell,deliniator))
1986 {
1987 column_names.push_back(cell);
1988 }
1989
1990 data.emplace_back(column_names.size());
1991 int i=0;
1992 for(auto a : column_names){
1994 cell.erase(remove_if(cell.begin(),cell.end(), isspace), cell.end());
1995 data.back()[i++] = to_numeric<T>(a);
1996 }
1997 ++ii;
1998 }
1999
2000 int columns = NumberOfEntries(line,deliniator);
2001
2002 for(auto &v : data ) v.empty();
2003
2004 while(std::getline(file,line) && ii < Nmax){
2005
2006 data.emplace_back(columns);
2007
2008 // read the numbers
2009 std::stringstream lineStream(line);
2010 std::string cell;
2011
2012 int i=0;
2013 while(std::getline(lineStream,cell, deliniator))
2014 {
2015 assert(i < columns);
2016 cell.erase(remove_if(cell.begin(),cell.end(), isspace), cell.end());
2017 if(cell == reject){
2018 data.pop_back();
2019 --ii;
2020 break;
2021 }else{
2022 data.back()[i] = to_numeric<T>(cell);
2023 ++i;
2024 }
2025 }
2026 ++ii;
2027 }
2028
2029 return 1;
2030}
2031
2050template<typename T>
2051void writeCSV(const std::string filename
2052 ,const std::vector<std::string> header
2053 ,std::vector<T *> &data
2054){
2055
2056 std::ofstream s(filename + ".csv");
2057
2058 long ncol = header.size();
2059 assert(ncol == data.size() );
2060 for(int i = 0 ; i < header.size()-1 ; ++i){
2061 s << header[i] << ",";
2062 }
2063 s << header.back() << std::endl;
2064
2065 size_t nrow = data[0]->size();
2066 for(auto v : data) assert(nrow == v->size() );
2067
2068 for(size_t j = 0 ; j < nrow ; ++j){
2069 for(int i = 0 ; i < ncol-1 ; ++i){
2070 s << data[i]->operator[](j) << ",";
2071 }
2072 s << data.back()->operator[](j) << "\n";
2073 }
2074}
2075
2076/*** \brief A class for reading and then looking up objects from a CSV catalog.
2077
2078 The constructor will read in the whole catalog and sort it into Nxbins X-bins. Each
2079 X-bin is then sorted by Y.
2080
2081 The find(x,y) function will set the current value to a galaxy with a x within the
2082 x-bin of x and a y close to y. The other information in the row of the csv file for
2083 this entry can then be read.
2084
2085 */
2086class XYcsvLookUp{
2087public:
2088 XYcsvLookUp(std::string datafile
2089 ,std::string Xlabel
2090 ,std::string Ylabel
2091 ,int Nxbins
2092 ,size_t MaxNumber = 1000000
2093 ,bool verbose = false);
2094 XYcsvLookUp(
2095 std::string datafile
2096 ,std::string Xlabel
2097 ,std::string Ylabel
2098 ,std::vector<double> Xbins
2099 ,size_t MaxNumber = 1000000
2100 ,bool verbose = false);
2101
2103 std::vector<double> find(double x,double y);
2104
2106 double operator[](std::string label) const;
2107
2109 double operator[](int i) const{
2110 return (*current)[i];
2111 }
2113 double getX() const {
2114 return (*current)[Xindex];
2115 }
2117 double getY() const {
2118 return (*current)[Yindex];
2119 }
2121 std::vector<double> record() const{
2122 return *current;
2123 }
2125 std::vector<double> operator*() const {
2126 return *current;
2127 }
2128
2129 void operator++(){
2130 if(current != data.end() ) ++current;
2131 }
2132 void operator--(){
2133 if(current != data.begin() ) --current;
2134 }
2136 std::vector<std::string> labels() const{
2137 return column_names;
2138 }
2139
2140 double Xmin() const {return xmin;}
2141 double Xmax() const {return xmax;}
2143 double Ymin(double x) const;
2145 double Ymax(double x) const;
2146
2147 void printcurrent(){
2148 int i = 0;
2149 for(auto label : column_names){
2150 std::cout << label << " : " << (*current)[i++] << std::endl;
2151 }
2152 }
2153
2154private:
2155 int Xindex;
2156 int Yindex;
2157 double xmax;
2158 double xmin;
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;
2164 size_t NinXbins;
2165 std::string filename;
2166};
2167} // Utilities::IO
2168
2170void splitstring(std::string &line,std::vector<std::string> &vec,const std::string &delimiter);
2171
2180template< typename T>
2182public:
2183 DataFrame(std::string datafile
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);
2191 };
2192
2193 DataFrame(){};
2194
2195 void input(std::string datafile
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;}
2201 ){
2202 filename = datafile;
2203 Utilities::IO::ReadCSVnumerical1(datafile,data,column_names,MaxNumber,'#',',',replace,accept);
2204
2205 for(int i=0 ; i<column_names.size() ; ++i) datamap[column_names[i]] = i;
2206 };
2207
2209 void pop(std::string colname){
2210
2211 int i=datamap[colname];
2212
2213 swap(column_names[i],column_names.back());
2214 column_names.pop_back();
2215 swap(data[i],data.back());
2216 data.pop_back();
2217
2218 datamap.empty();
2219 for(int i=0 ; i<column_names.size() ; ++i) datamap[column_names[i]] = i;
2220 }
2221
2223 std::vector<T>& operator[](const std::string &label){
2224 if(datamap.find(label) == datamap.end()){
2225 std::cerr << "No label - " << label << " - in " << filename <<std::endl;
2226 throw std::invalid_argument("no label");
2227 }
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);
2232 };
2233
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");
2239 }
2240 column_names.push_back(name);
2241 data.push_back(vec);
2242 data[name] = data.size()-1;
2243 };
2244
2246 std::vector<T>& operator[](int i){
2247 return data[i];
2248 };
2249
2251 std::vector<std::string> labels() const{
2252 return column_names;
2253 };
2254
2255 // sort by one of the columns
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;
2260
2261 sort_indexes(data[datamap[name]],index);
2262 std::vector<T> tmp_v(N);
2263
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]];
2267 }
2268 swap(data[j],tmp_v);
2269 }
2270 }
2271
2272 // shuffle order of rows
2273 void shuffle(Utilities::RandomNumbers_NR &ran){
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;
2277 Utilities::shuffle(index, ran);
2278
2279 std::vector<T> tmp_v(N);
2280
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]];
2284 }
2285 swap(data[j],tmp_v);
2286 }
2287 }
2288
2289 size_t number_of_rows(){return data[0].size();}
2290 size_t number_of_columns(){return data.size();}
2291
2292 std::vector<std::vector<T> > data;
2293private:
2294 std::map<std::string,int> datamap;
2295 std::vector<std::string> column_names;
2296 std::string filename;
2297};
2298
2299// this is a summation algorithm that maintains percision for large sequences of numbers
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];
2308 }else{
2309 c = (input[i] - t) + s;
2310 }
2311 s = t;
2312 t = cs + c;
2313 if( abs(cs) >= abs(c) ){
2314 cc = (cs - t) + c;
2315 }else{
2316 cc = (c - t) + cs;
2317 }
2318 cs = t;
2319 ccs = ccs + cc;
2320 }
2321 return s + cs + ccs;
2322}
2323
2324
2325template <typename T,typename F>
2326double PairWiseSum(T *begin,T *end,F value){
2327 double sum = 0;
2328 size_t n = end - begin;
2329 if(n <= 10){
2330 for(T* p=begin ; p != end ; ++p){
2331 sum += value(p);
2332 }
2333 }else{
2334 sum = PairWiseSum<T,F>(begin,begin + n/2,value) + PairWiseSum<T,F>(begin + n/2,end,value);
2335 }
2336 return sum;
2337}
2338
2340template <typename T>
2341double PairWiseSum(T *begin,T *end){
2342 double sum = 0;
2343 size_t n = end - begin;
2344 if(n <= 10){
2345 for(T* p=begin ; p != end ; ++p){
2346 sum += *p;
2347 }
2348 }else{
2349 sum = PairWiseSum(begin,begin + n/2) + PairWiseSum(begin + n/2,end);
2350 }
2351 return sum;
2352}
2353
2355template <typename T>
2356double PairWiseSum(std::vector<T> &v){
2357 return PairWiseSum(v.data(),v.data() + v.size());
2358}
2359
2361template <typename T,typename F>
2362double PairWiseSum(std::vector<T> &v,F value){
2363 return PairWiseSum(v.data(),v.data() + v.size(),value);
2364}
2365
2367template <typename T>
2369
2370public:
2371
2372 SUMMER():batch(1000),n(0),ntotal(0),current(0.0){};
2373 SUMMER(size_t batchsize)
2374 :batch(batchsize),n(0),current(0.0),ntotal(0){};
2375
2377 void operator+=(T x){
2378 ++n;
2379 current += x;
2380 ++ntotal;
2381 if(n % batch == 0){
2382 v.push_back(current);
2383 n=0;
2384 current = 0;
2385 }
2386 }
2387
2390 if(v.size() ==0 ) return 0;
2391 v.push_back(current);
2392 return Utilities::PairWiseSum(v);
2393 }
2394
2396 void reset(){
2397 std::vector<T> dump;
2398 std::swap(v,dump);
2399 n = ntotal = 0;
2400 current = 0;
2401 }
2402
2405 return ntotal;
2406 }
2407
2408private:
2409 size_t batch;
2410 size_t n;
2411 size_t ntotal;
2412 T current;
2413 std::vector<T> v;
2414};
2415
2416/*
2417This is 1F2(a,b,c:x)
2418
2419 Note convergence restrictions: abs(x) < 1 and c not a negative integer or zero
2420*/
2421template <typename T = double>
2422double hypergeometric( T a, T b, T c, T x )
2423{
2424
2425 if(abs(x) > 1 ){
2426 throw std::invalid_argument("out of bounds");
2427 }
2428 const double TOLERANCE = 1.0e-10;
2429 double term = a * b * x / c;
2430 double value = 1.0 + term;
2431 int n = 1;
2432
2433 while ( abs( term ) > TOLERANCE )
2434 {
2435 a++, b++, c++, n++;
2436 term *= a * b * x / c / n;
2437 value += term;
2438 }
2439
2440 return value;
2441}
2442
2443class LOGDATA{
2444
2445public:
2446 typedef boost::variant<int,long,size_t,float,double,std::string,bool> MULTITYPE;
2447 typedef std::map<std::string,MULTITYPE> LINE;
2448
2449private:
2450 std::vector<LINE> lines;
2451 std::string filename;
2452 std::string blank_val;
2453 std::vector<std::string> header;
2454 long nbatch;
2455 int precision;
2456 long last_line_printed;
2457 long nlabels;
2458 std::set<std::string> labels;
2459
2460 std::map<std::string,std::string> label_comments;
2461
2462 long nprints;
2463
2464 void append_file(){
2465
2466 const long default_precision = std::cout.precision();
2467 std::ofstream logfile;
2468 logfile.open(filename,std::ios_base::app);
2469
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 ){
2474 int i=0;
2475 for(auto &label : labels){
2476 try{
2477 if(label == "ID"){
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);
2483 }else{
2484 logfile << std::setprecision(precision) << lines[j].at(label) ;
2485 }
2486 if(i<n-1) logfile << ",";
2487 }catch(std::exception& e){
2488 logfile << std::setprecision(precision) << blank_val ;
2489 if(i<n-1) logfile << ",";
2490 }
2491 ++i;
2492 }
2493 logfile << std::endl;
2494 }
2495 std::cout << std::setprecision(default_precision);
2496
2497 last_line_printed = lines.size();
2498 }else{
2499 // number of labels has changed print from the beginning
2500 print_to_file();
2501 }
2502 }
2503
2504public:
2505
2506 LOGDATA(std::string file
2507 ,std::string blanck_value = "0"
2508 ,long Nbatch=1000
2509 ):filename(file),blank_val(blanck_value)
2510 ,nbatch(Nbatch),precision(std::cout.precision())
2511 ,last_line_printed(0),nlabels(0),nprints(0){
2512 print_to_file();
2513 };
2514
2515 ~LOGDATA(){print_to_file();}
2516
2517 std::string output_file(){return filename;}
2518
2519 void set_precision(int p){
2520 precision = p;
2521 }
2522
2523 // current number of columns
2524 size_t ncol(){return labels.size();}
2525 // names of columns
2526 std::set<std::string> names = labels;
2527
2528 void print_to_file(){
2529
2530 const int default_precision = std::cout.precision();
2531 //std::cout << std::setprecision(12);
2532
2533 if(lines.size() == 0 ) return;
2534
2535 std::ofstream logfile(filename);
2536
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;
2542 }
2543
2544 // print comments on labels
2545 {
2546 int i=1;
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;
2551 }else{
2552 logfile << "# " << i << " " << it->first << " : " << it->second << std::endl;
2553 }
2554 ++i;
2555 }
2556 }
2557 size_t i=0;
2558 nlabels = labels.size();
2559 for(auto &label : labels){
2560 logfile << label;
2561 if(i<nlabels-1) logfile << ",";
2562 ++i;
2563 }
2564 logfile << std::endl;
2565
2566 std::cout << std::setprecision(precision);
2567 for(LINE &line : lines){
2568 i=0;
2569 for(auto &label : labels){
2570 try{
2571 if(label == "ID"){
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);
2577 }else{
2578 logfile << std::setprecision(precision) << line.at(label);
2579 }
2580 if(i<nlabels-1) logfile << ",";
2581 }catch(std::exception& e){
2582 logfile << std::setprecision(precision) << blank_val;
2583 if(i<nlabels-1) logfile << ",";
2584 }
2585 ++i;
2586 }
2587 logfile << std::endl;
2588 }
2589 std::cout << std::setprecision(default_precision);
2590
2591 last_line_printed = lines.size();
2592 ++nprints;
2593 }
2594
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();
2600 }
2601
2603 void replace(const LINE &line){
2604 for (auto itr = line.begin(); itr != line.end(); ++itr) labels.insert(itr->first);
2605 lines.back() = line;
2606 }
2607
2609 void header_comment(const std::string &s){
2610 header.push_back(s);
2611 }
2612
2614 void label_description(const std::string &name
2615 ,const std::string &description
2616 ){
2617 label_comments[name] = description;
2618 }
2619};
2620
2621// /// alternative to std::thread that counts threads
2622// template<class Function, class ... Args >
2623// class MyThread
2624// {
2625// private:
2626// std::thread thread_;
2627//
2628// static long count;
2629// static long unjoined;
2630//
2631// public:
2632// MyThread(Function&& f,Args&&... args)
2633// {
2634// if(count > 20) throw std::runtime_error("too many threads");
2635// thread_ = std::thread(std::forward<Function>(f),std::forward<Args>(args)...);
2636// ++count;
2637// ++unjoined;
2638// }
2639//
2640// ~MyThread(){
2641// --count;
2642// }
2643//
2644// void join(){
2645// thread_.join();
2646// --unjoined;
2647// }
2648//
2649//
2650// };
2651
2652
2658template <typename T>
2659std::valarray<T> AdaptiveSmooth(const std::valarray<T> &map_in,size_t Nx,size_t Ny,T value){
2660
2661 std::valarray<T> map_out(map_in.size());
2662 long r,area;
2663 T val;
2664 for(long i=0;i<Nx;++i){
2665 for(long j=0;j<Ny;++j){
2666 r = 0;
2667 val = map_in[i+j*Nx];
2668 while(val < value && r < std::min(Nx, Ny) ){
2669
2670 area = 0;
2671 val = 0;
2672 long imin,imax,jmin,jmax;
2673
2674 imin = (i-r < 0) ? 0 : i-r;
2675 imax = (i+r > Nx-1) ? Nx-1 : i+r;
2676
2677 jmin = (j-r < 0) ? 0 : j-r;
2678 jmax = (j+r > Ny-1) ? Ny-1 : j+r;
2679
2680
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];
2685 ++area;
2686 }
2687 }
2688 }
2689 ++r;
2690 }
2691
2692 map_out[i+j*Nx] = val/area;
2693 }
2694 }
2695
2696 return map_out;
2697 }
2698} // Utilities
2699
2700//#endif
2701
2702
2703#endif
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