20 #include <sys/types.h>
21 #include <boost/variant.hpp>
37 return std::stoi(str);
40 inline long to_numeric<long>(
const std::string &str) {
42 return std::stol(str);
48 inline int to_numeric<int>(
const std::string &str) {
49 return std::stoi(str);
51 return std::stoi(str);
57 inline float to_numeric<float>(
const std::string &str) {
58 return std::stof(str);
60 return std::stof(str);
66 inline double to_numeric<double>(
const std::string &str) {
68 return std::stod(str);
76 bool AlwaysTrue(T t){
return true;}
79 bool AlwaysFalse(T t){
return false;}
82 T vec_sum(
const std::vector<T> &v){
84 for(
const T &a : v) sum += a;
102 using std::type_index;
107 void Matrix(T **matrix,
long rows,
long cols){
108 matrix =
new T*[rows];
109 matrix[0] =
new T[rows*cols];
110 for (
long i = 1; i < rows; ++i)
111 matrix[i] = matrix[0] + i * cols;
115 void free_Matrix(T **matrix,
long rows,
long){
116 if (rows)
delete[] matrix[0];
120 PosType **PosTypeMatrix(
size_t rows,
size_t cols);
121 void free_PosTypeMatrix(PosType **matrix,
size_t rows,
size_t cols);
122 PosType **PosTypeMatrix(
long rows1,
long rows2,
long cols1,
long cols2);
123 void free_PosTypeMatrix(PosType **matrix,
long rows1,
long rows2,
long cols1,
long cols2);
129 D3Matrix(
size_t xsize,
size_t ysize,
size_t zsize)
130 :xn(xsize),yn(ysize),zn(zsize){
131 array =
new T[xn*yn*zn];
137 T& operator()(
size_t i,
size_t j,
size_t k){
138 return array[i + j*xn + k*xn*yn];
141 T& operator()(
size_t m){
145 size_t xindex(
size_t m){
148 size_t yindex(
size_t m){
149 return (m % (xn*yn) ) / xn;
152 size_t zindex(
size_t m){
170 :xn(xsize),yn(ysize){
171 array =
new T[xn*yn];
177 T& operator()(
size_t i,
size_t j){
178 return array[i + j*xn];
181 T& operator()(
size_t m){
185 size_t xindex(
size_t m){
188 size_t yindex(
size_t m){
204 template <
typename T>
214 T& operator()(
int i,
int j){
217 size_t k = (i <= j ) ? j + (m-i)*i/2 : i + (m-j)*j/2;
222 T& operator[](
size_t k){
225 const T &operator[](
size_t k)
const {
229 int size(){
return n;}
233 return (i <= j ) ? j + (m-i)*i/2 : i + (m-j)*j/2;
279 template<
typename BaseT>
283 template<
typename ValueT = BaseT>
287 typedef typename std::vector<BaseT*>::iterator base_iterator;
291 typedef ValueT value_type;
292 typedef ValueT* pointer;
293 typedef const ValueT* const_pointer;
294 typedef ValueT& reference;
295 typedef const ValueT& const_reference;
296 typedef typename base_iterator::difference_type difference_type;
297 typedef typename base_iterator::iterator_category iterator_category;
300 iterator(base_iterator i) : it(i) {}
301 iterator(
const iterator& other) : it(other.it) {}
303 iterator&
operator=(
const iterator& rhs) { it = rhs.it;
return *
this; }
305 reference operator*() {
return (reference)(**it); }
309 pointer operator->() {
return (pointer)(*it); }
310 const_pointer operator->()
const {
return (const_pointer)(*it); }
312 iterator& operator++() { ++it;
return *
this; }
313 iterator operator++(
int) { iterator tmp(*
this); ++it;
return tmp; }
314 iterator& operator--() { --it;
return *
this; }
315 iterator operator--(
int) { iterator tmp(*
this); --it;
return tmp; }
317 bool operator==(
const iterator& rhs)
const {
return (it == rhs.it); }
318 bool operator!=(
const iterator& rhs)
const {
return (it != rhs.it); }
320 iterator& operator+=(difference_type n) { it += n;
return *
this; }
321 iterator& operator-=(difference_type n) { it -= n;
return *
this; }
323 reference
operator[](difference_type n) {
return (reference)*it[n]; }
327 friend iterator operator+(
const iterator& i, difference_type n) {
return iterator(i.it + n); }
328 friend iterator operator+(difference_type n,
const iterator& i) {
return iterator(i.it + n); }
329 friend iterator operator-(
const iterator& i, difference_type n) {
return iterator(i.it - n); }
330 friend iterator operator-(difference_type n,
const iterator& i) {
return iterator(i.it - n); }
332 friend difference_type operator-(
const iterator& b,
const iterator& a) {
return (b.it - a.it); }
334 friend bool operator<(
const iterator&a,
const iterator& b) {
return (a.it < b.it); }
335 friend bool operator>(
const iterator&a,
const iterator& b) {
return (a.it > b.it); }
336 friend bool operator<=(
const iterator&a,
const iterator& b) {
return (a.it <= b.it); }
337 friend bool operator>=(
const iterator&a,
const iterator& b) {
return (a.it >= b.it); }
357 swap(a.items, b.items);
358 swap(a.tmap, b.tmap);
370 template<
typename Sub
classT>
377 SubclassT* copy =
new SubclassT(obj);
380 items.push_back(copy);
383 tmap[
typeid(SubclassT)].push_back(copy);
391 BaseT* back = items.back();
394 tmap[back->type()].pop_back();
404 template<
typename Sub
classT>
408 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
409 if(found == tmap.end())
413 found->second.pop_back();
416 for(
typename base_container::reverse_iterator it = items.rbegin(); it != items.rend(); ++it)
418 if((*it)->type() ==
typeid(SubclassT))
431 for(std::size_t i = 0, n = items.size(); i < n; ++i)
438 template<
typename Sub
classT>
442 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
443 if(found == tmap.end())
446 base_container& titems = found.second;
449 for(std::size_t i = 0, n = titems.size(); i < n; ++i)
456 items.erase(std::remove_if(items.begin(), items.end(), is_type<SubclassT>), items.end());
472 template<
typename Sub
classT>
475 return is_type<SubclassT>(items[i]);
497 const BaseT&
get(std::size_t i)
const
503 template<
typename Sub
classT>
504 SubclassT&
get(std::size_t i)
506 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
507 if(found == tmap.end())
508 throw std::out_of_range(std::string() +
"type " +
typeid(SubclassT).name() +
" not in vector");
509 return (SubclassT&)*found->second[i];
513 template<
typename Sub
classT>
514 const SubclassT&
get(std::size_t i)
const
516 typename tmap_t::const_iterator found = tmap.find(
typeid(SubclassT));
517 if(found == tmap.end())
518 throw std::out_of_range(std::string() +
"type " +
typeid(SubclassT).name() +
" not in vector");
519 return (
const SubclassT&)*found->second[i];
523 BaseT&
at(std::size_t i)
529 const BaseT&
at(std::size_t i)
const
538 template<
typename Sub
classT>
539 SubclassT&
at(std::size_t i)
541 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
542 if(found == tmap.end())
543 throw std::out_of_range(std::string() +
"type " +
typeid(SubclassT).name() +
" not in vector");
544 return (SubclassT&)*found->second.at(i);
548 template<
typename Sub
classT>
549 const SubclassT&
at(std::size_t i)
const
551 typename tmap_t::const_iterator found = tmap.find(
typeid(SubclassT));
552 if(found == tmap.end())
553 throw std::out_of_range(std::string() +
"type " +
typeid(SubclassT).name() +
" not in vector");
554 return (
const SubclassT&)*found->second.at(i);
564 template<
typename Sub
classT>
567 typename tmap_t::const_iterator found = tmap.find(
typeid(SubclassT));
568 if(found == tmap.end())
570 return found->second.size();
576 return items.empty();
580 template<
typename Sub
classT>
583 typename tmap_t::const_iterator found = tmap.find(
typeid(SubclassT));
584 if(found == tmap.end())
587 return found->second.empty();
593 return iterator<>(items.begin());
599 return iterator<>(items.end());
603 template<
typename Sub
classT>
606 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
607 if(found == tmap.end())
608 return iterator<SubclassT>(items.end());
609 return iterator<SubclassT>(found->second.begin());
613 template<
typename Sub
classT>
616 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
617 if(found == tmap.end())
618 return iterator<SubclassT>(items.end());
619 return iterator<SubclassT>(found->second.end());
626 typedef std::vector<BaseT*> base_container;
628 inline static void check_subclass(
const BaseT&) {}
630 template<
typename Sub
classT>
631 inline static bool is_type(
const BaseT* obj) {
return (
typeid(*obj) ==
typeid(SubclassT)); }
633 base_container items;
635 typedef std::map<detail::type_index, base_container> tmap_t;
640 template<
typename BaseT>
644 template<
typename ValueT = BaseT*>
648 typedef typename std::vector<BaseT*>::iterator base_iterator;
652 typedef typename std::vector<ValueT>::iterator::value_type value_type;
653 typedef typename std::vector<ValueT>::iterator::pointer pointer;
654 typedef typename std::vector<ValueT>::iterator::reference reference;
655 typedef typename base_iterator::difference_type difference_type;
656 typedef typename base_iterator::iterator_category iterator_category;
659 iterator(base_iterator i) : it(i) {}
660 iterator(
const iterator& other) : it(other.it) {}
662 iterator& operator=(
const iterator& rhs) { it = rhs.it;
return *
this; }
664 reference operator*() {
return (reference)*it; }
665 const reference operator*()
const {
return (
const reference)*it; }
667 iterator& operator++() { ++it;
return *
this; }
668 iterator operator++(
int) { iterator tmp(*
this); ++it;
return tmp; }
669 iterator& operator--() { --it;
return *
this; }
670 iterator operator--(
int) { iterator tmp(*
this); --it;
return tmp; }
672 bool operator==(
const iterator& rhs)
const {
return (it == rhs.it); }
673 bool operator!=(
const iterator& rhs)
const {
return (it != rhs.it); }
675 iterator& operator+=(difference_type n) { it += n;
return *
this; }
676 iterator& operator-=(difference_type n) { it -= n;
return *
this; }
678 pointer operator[](difference_type n) {
return (pointer)it[n]; }
679 const pointer operator[](difference_type n)
const {
return (
const pointer)it[n]; }
681 friend iterator operator+(
const iterator& i, difference_type n) {
return iterator(i.it + n); }
682 friend iterator operator+(difference_type n,
const iterator& i) {
return iterator(i.it + n); }
683 friend iterator operator-(
const iterator& i, difference_type n) {
return iterator(i.it - n); }
684 friend iterator operator-(difference_type n,
const iterator& i) {
return iterator(i.it - n); }
686 friend difference_type operator-(
const iterator& b,
const iterator& a) {
return (b.it - a.it); }
688 friend bool operator<(
const iterator&a,
const iterator& b) {
return (a.it < b.it); }
689 friend bool operator>(
const iterator&a,
const iterator& b) {
return (a.it > b.it); }
690 friend bool operator<=(
const iterator&a,
const iterator& b) {
return (a.it <= b.it); }
691 friend bool operator>=(
const iterator&a,
const iterator& b) {
return (a.it >= b.it); }
702 : items(other.items), tmap(other.tmap)
717 swap(a.items, b.items);
718 swap(a.tmap, b.tmap);
748 items.push_back(obj);
751 tmap[
typeid(*obj)].push_back(obj);
758 BaseT* back = items.back();
761 tmap[
typeid(*back)].pop_back();
770 template<
typename Sub
classT>
774 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
775 if(found == tmap.end())
779 found->second.pop_back();
782 for(
typename std::vector<BaseT*>::reverse_iterator it = items.rbegin(); it != items.rend(); ++it)
784 if(
typeid(**it) ==
typeid(SubclassT))
797 while(!empty()) pop_back();
801 template<
typename Sub
classT>
805 tmap.erase(
typeid(SubclassT));
808 items.erase(std::remove_if(items.begin(), items.end(), is_type<SubclassT>), items.end());
824 template<
typename Sub
classT>
827 return is_type<SubclassT>(items[i]);
843 const BaseT*
get(std::size_t i)
const
849 template<
typename Sub
classT>
850 SubclassT*
get(std::size_t i)
852 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
853 if(found == tmap.end())
854 throw std::out_of_range(std::string() +
"type " +
typeid(SubclassT).name() +
" not in vector");
855 return (SubclassT*)found->second[i];
859 template<
typename Sub
classT>
860 const SubclassT*
get(std::size_t i)
const
862 typename tmap_t::const_iterator found = tmap.find(
typeid(SubclassT));
863 if(found == tmap.end())
864 throw std::out_of_range(std::string() +
"type " +
typeid(SubclassT).name() +
" not in vector");
865 return (
const SubclassT*)found->second[i];
869 BaseT*
at(std::size_t i)
875 const BaseT*
at(std::size_t i)
const
884 template<
typename Sub
classT>
885 SubclassT*
at(std::size_t i)
887 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
888 if(found == tmap.end())
889 throw std::out_of_range(std::string() +
"type " +
typeid(SubclassT).name() +
" not in vector");
890 return (SubclassT*)found->second.at(i);
894 template<
typename Sub
classT>
895 const SubclassT*
at(std::size_t i)
const
897 typename tmap_t::const_iterator found = tmap.find(
typeid(SubclassT));
898 if(found == tmap.end())
899 throw std::out_of_range(std::string() +
"type " +
typeid(SubclassT).name() +
" not in vector");
900 return (
const SubclassT*)found->second.at(i);
910 template<
typename Sub
classT>
913 typename tmap_t::const_iterator found = tmap.find(
typeid(SubclassT));
914 if(found == tmap.end())
916 return found->second.size();
922 return items.empty();
926 template<
typename Sub
classT>
929 typename tmap_t::const_iterator found = tmap.find(
typeid(SubclassT));
930 if(found == tmap.end())
933 return found->second.empty();
939 return iterator<>(items.begin());
945 return iterator<>(items.end());
949 template<
typename Sub
classT>
952 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
953 if(found == tmap.end())
954 return iterator<SubclassT>(items.end());
955 return iterator<SubclassT>(found->second.begin());
959 template<
typename Sub
classT>
962 typename tmap_t::iterator found = tmap.find(
typeid(SubclassT));
963 if(found == tmap.end())
964 return iterator<SubclassT>(items.end());
965 return iterator<SubclassT>(found->second.end());
969 inline void check_subclass(
const BaseT*) {}
971 template<
typename Sub
classT>
972 inline static bool is_type(
const BaseT* obj) {
return (
typeid(*obj) ==
typeid(SubclassT)); }
974 typedef std::vector<BaseT*> base_container;
976 base_container items;
978 typedef std::map<detail::type_index, base_container> tmap_t;
982 template<
class BaseT>
983 std::size_t lower_bound(std::vector<BaseT*>& items, PosType target){
984 std::size_t ju,jm,jl;
986 if(items.size() == 0)
return 0;
992 if(items[jm]->compareZ(target))
1001 template<
typename Container>
1024 n = (int)(range/smallsize+1);
1027 int xy2d (
int x,
int y);
1028 int xy2d (PosType x, PosType y);
1029 void d2xy(
int d,
int *x,
int *y);
1030 void d2xy(
int d, PosType *x, PosType *y);
1034 PosType xo[2],range;
1035 void rot(
int s,
int *x,
int *y,
int rx,
int ry);
1038 template<
typename T>
1039 T between(
const T& x,
const T& l,
const T& u)
1041 return std::max(l, std::min(u, x));
1045 class RandomNumbers{
1049 RandomNumbers(
unsigned int seed);
1050 ~RandomNumbers(
void);
1052 PosType operator()(
void);
1054 PosType gauss(){
return norm_dist(rand_gen);}
1057 std::normal_distribution<> norm_dist;
1058 std::mt19937 rand_gen;
1084 }
while( s > 1.0 || s == 0.0);
1086 s = sqrt(-2*log(s)/s);
1095 int poisson(
double lam){
1097 return std::max<long>(std::lround(
gauss()*sqrt(lam) + lam ) ,0);
1099 double L = exp(-lam),p=1;
1161 class BetaDistribution {
1163 using result_type = double;
1165 BetaDistribution(
double alpha,
double beta)
1168 gamma_alpha_(alpha, 1.0),
1169 gamma_beta_(beta, 1.0)
1171 if (!(alpha > 0.0) || !(beta > 0.0)) {
1172 throw std::invalid_argument(
1173 "Beta distribution parameters must be positive");
1177 double operator()(){
1181 const double x = gamma_alpha_(engine);
1182 const double y = gamma_beta_(engine);
1183 const double sum = x + y;
1185 if (sum > 0.0 && std::isfinite(sum)) {
1191 double alpha() const noexcept {
return alpha_; }
1192 double beta() const noexcept {
return beta_; }
1194 static constexpr
double min() noexcept {
return 0.0; }
1195 static constexpr
double max() noexcept {
return 1.0; }
1199 std::mt19937_64 engine{std::random_device{}()};
1202 std::gamma_distribution<double> gamma_alpha_;
1203 std::gamma_distribution<double> gamma_beta_;
1209 template <
typename T,
typename R>
1216 if(vec.size() < 2)
return;
1217 for (
size_t i = vec.size()-1; i>0; --i) {
1218 ran_t = (size_t)(ran()*(i+1));
1220 std::swap(vec[ran_t],vec[i]);
1226 template <
typename T>
1228 ,std::vector<size_t> &index
1232 index.resize(v.size());
1233 for (
size_t i = 0; i != index.size(); ++i) index[i] = i;
1236 std::sort(index.begin(), index.end(),[&v](
size_t i1,
size_t i2) {return v[i1] < v[i2];});
1239 template <
typename T>
1241 ,std::vector<size_t> &index
1247 for (
size_t i = 0; i != index.size(); ++i) index[i] = i;
1250 std::sort(index.begin(), index.end(),
1251 [v](
size_t i1,
size_t i2) {return v[i1] < v[i2];});
1255 template <
typename T>
1257 ,std::vector<size_t> &index
1261 index.resize(v.size());
1262 for (
size_t i = 0; i != index.size(); ++i) index[i] = i;
1265 std::sort(index.begin(), index.end(),
1266 [&v](
size_t i1,
size_t i2) {return v[i1] > v[i2];});
1274 template <
typename R>
1278 if(N == 0)
throw std::invalid_argument(
"N=0");
1280 for(
size_t i=0 ; i<N ; ++i) index[i] = i;
1290 size_t tmp = index[index_internal];
1291 if(index_internal == index.size()){
1300 if(index_internal == index.size()){
1305 return index[index_internal];
1315 std::vector<size_t> index;
1316 size_t index_internal;
1321 template <
typename T>
1322 void apply_permutation(
1324 const std::vector<std::size_t>& p)
1326 std::vector<T> copy(p.size());
1328 for(std::size_t i = 0; i < p.size(); ++i){
1332 for(std::size_t i = 0; i < p.size(); ++i){
1333 vec[i] = copy[p[i]];
1337 template <
typename T>
1338 void apply_permutation(
1339 std::vector<T>& vec,
1340 const std::vector<std::size_t>& p)
1342 apply_permutation(vec.data(),p);
1351 std::valarray<double>
const &aa
1352 ,std::valarray<double>
const &bb
1357 ,std::vector<double> &ll
1358 ,std::vector<double> &Pl
1359 ,
double zeropaddingfactor
1363 std::valarray<double> &aa
1368 ,std::vector<double> &ll
1369 ,std::vector<double> &Pl
1373 std::valarray<double> &aa
1378 ,
const std::vector<double> &ll
1379 ,std::vector<double> &Pl
1380 ,std::vector<double> &llave
1384 std::valarray<float>
const &aa
1385 ,std::valarray<float>
const &bb
1390 ,std::vector<double> &ll
1391 ,std::vector<double> &Pl
1392 ,
double zeropaddingfactor
1396 std::valarray<float> &aa
1401 ,std::vector<double> &ll
1402 ,std::vector<double> &Pl
1406 std::valarray<float> &aa
1411 ,
const std::vector<double> &ll
1412 ,std::vector<double> &Pl
1413 ,std::vector<double> &llave
1422 inline bool file_exists (
const std::string& name) {
1424 return (stat (name.c_str(), &buffer) == 0);
1429 std::string filename
1432 ,
bool verbose =
false
1437 std::ifstream file_in(filename.c_str());
1439 std::string space =
" ";
1443 std::stringstream buffer;
1446 std::cout <<
"Can't open file " << filename << std::endl;
1448 throw std::runtime_error(
" Cannot open file.");
1451 std::cout <<
"Reading from " << filename << std::endl;
1453 while(i < skiplines){
1454 getline(file_in,myline);
1457 while(file_in.peek() ==
'#'){
1458 file_in.ignore(10000,
'\n');
1461 std::cout <<
"skipped "<< i <<
" comment lines in " << filename << std::endl;
1465 while(getline(file_in,myline)){
1467 if(myline[0] ==
'#'){
1468 std::cout <<
"skipped line " << i << std::endl;
1472 pos= myline.find_first_not_of(space);
1473 myline.erase(0,pos);
1477 if(verbose) std::cout << myt1 <<
" ";
1485 std::cout <<
"Read " << x.size() <<
" lines from " << filename << std::endl;
1490 template <
class T1,
class T2>
1492 std::string filename
1495 ,std::string delineator =
" "
1497 ,
bool verbose =
false
1504 std::ifstream file_in(filename.c_str());
1506 std::string space =
" ";
1511 std::stringstream buffer;
1514 std::cout <<
"Can't open file " << filename << std::endl;
1516 throw std::runtime_error(
" Cannot open file.");
1519 std::cout <<
"Reading caustic information from " << filename << std::endl;
1521 while(i < skiplines){
1522 getline(file_in,myline);
1525 while(file_in.peek() ==
'#'){
1526 file_in.ignore(10000,
'\n');
1529 std::cout <<
"skipped "<< i <<
" comment lines in " << filename << std::endl;
1533 while(getline(file_in,myline)){
1535 if(myline[0] ==
'#'){
1536 std::cout <<
"skipped line " << i << std::endl;
1540 pos= myline.find_first_not_of(space);
1541 myline.erase(0,pos);
1544 pos = myline.find(delineator);
1545 strg.assign(myline,0,pos);
1548 if(verbose) std::cout << myt1 <<
" ";
1551 myline.erase(0,pos+1);
1552 pos= myline.find_first_not_of(space);
1553 myline.erase(0,pos);
1558 pos = myline.find(space);
1559 strg.assign(myline,0,pos);
1562 if(verbose) std::cout << myt2 << std::endl;
1570 std::cout <<
"Read " << x.size() <<
" lines from " << filename << std::endl;
1574 template <
class T1,
class T2,
class T3>
1576 std::string filename
1580 ,std::string delineator =
" "
1581 ,
bool verbose =
false
1591 std::ifstream file_in(filename.c_str());
1593 std::string space =
" ";
1599 std::stringstream buffer;
1602 std::cout <<
"Can't open file " << filename << std::endl;
1604 throw std::runtime_error(
" Cannot open file.");
1607 std::cout <<
"Reading caustic information from " << filename << std::endl;
1609 while(file_in.peek() ==
'#'){
1610 file_in.ignore(10000,
'\n');
1613 std::cout <<
"skipped "<< i <<
" comment lines in " << filename << std::endl;
1617 while(getline(file_in,myline)){
1619 if(myline[0] ==
'#'){
1620 std::cout <<
"skipped line " << i << std::endl;
1624 pos= myline.find_first_not_of(space);
1625 myline.erase(0,pos);
1628 pos = myline.find(delineator);
1629 strg.assign(myline,0,pos);
1632 if(verbose) std::cout << myt1 <<
" ";
1635 myline.erase(0,pos+1);
1636 pos= myline.find_first_not_of(space);
1637 myline.erase(0,pos);
1645 pos = myline.find(space);
1646 strg.assign(myline,0,pos);
1649 if(verbose) std::cout << myt2 << std::endl;
1652 myline.erase(0,pos+1);
1653 pos= myline.find_first_not_of(space);
1654 myline.erase(0,pos);
1660 pos = myline.find(space);
1661 strg.assign(myline,0,pos);
1664 if(verbose) std::cout << myt3 << std::endl;
1672 std::cout <<
"Read " << x.size() <<
" lines from " << filename << std::endl;
1675 int NumberOfEntries(
const std::string &
string,
char deliniator);
1679 ,
char comment_char =
'#'
1680 ,
char deliniator =
' '
1688 ,
const std::string filespec
1689 ,std::vector<std::string> & filenames
1707 template <
typename T>
1709 ,std::string filename
1712 ,
char comment_char =
'#'
1714 ,
size_t MaxNrows = std::numeric_limits<size_t>::max()
1715 ,
bool verbose =
true){
1717 std::ifstream file(filename);
1719 if (!file.is_open()){
1720 std::cerr <<
"file " << filename <<
" cann't be opened." << std::endl;
1721 throw std::runtime_error(
"no file");
1730 size_t first_data_line;
1734 while(i < skiplines){
1735 std::getline(file,line);
1742 first_data_line = file.tellg();
1743 std::getline(file,line);
1745 }
while(line[0] == comment_char);
1747 columns = NumberOfEntries(line,
' ');
1749 file.seekg(first_data_line);
1751 std::copy(std::istream_iterator<T>(file),
1752 std::istream_iterator<T>(),
1753 std::back_inserter(data));
1755 rows = data.size()/columns;
1757 std::cout <<
"Read " << rows <<
" rows of " << columns <<
" columns from file " << filename << std::endl;
1778 template <
typename T>
1780 ,std::vector<std::vector<T> > &data
1781 ,std::vector<std::string> &column_names
1782 ,
size_t MaxNumber = 100000000
1783 ,
char comment_char =
'#'
1784 ,
char deliniator =
','
1785 ,std::string replace =
"\\N"
1786 ,std::function<
bool(std::vector<T> &)> accept = [](std::vector<T> &v){
return true;}
1789 bool verbose =
false;
1791 std::ifstream file(filename);
1793 if (!file.is_open()){
1794 std::cerr <<
"file " << filename <<
" cann't be opened." << std::endl;
1795 throw std::runtime_error(
"no file");
1798 int count_preamble=0;
1802 std::getline(file,line);
1805 }
while(line[0] == comment_char);
1808 std::stringstream lineStream(line);
1810 column_names.empty();
1811 while(std::getline(lineStream,cell, deliniator))
1813 column_names.push_back(cell);
1816 if (!lineStream && cell.empty())
1818 column_names.push_back(
"");
1823 for(
auto st : column_names){
1824 std::cout << i++ <<
" " << st << std::endl;
1828 int ncolumns = NumberOfEntries(line,deliniator);
1831 std::getline(file,line);
1833 size_t n=0,m = 0,i=0;
1835 m = line.find(
",",n);
1836 cell = line.substr(n,m-n);
1838 cell.erase(remove_if(cell.begin(),cell.end(), isspace), cell.end());
1841 }
while(m != std::string::npos);
1845 size_t number_of_data_lines = 1;
1846 while(std::getline(file, line) && number_of_data_lines < MaxNumber ) ++number_of_data_lines;
1849 std::vector<std::vector<T> > tmp_data(ncolumns,std::vector<T>(0));
1850 swap(data,tmp_data);
1851 std::vector<T> tmp_row(ncolumns);
1857 file.open(filename);
1858 for(
int i=0; i < count_preamble; ++i){
1859 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
1863 while(std::getline(file,line) && rows < MaxNumber){
1865 std::stringstream lineStream(line);
1869 while(std::getline(lineStream,cell,deliniator))
1871 if(cell==replace) cell=
'0';
1874 cell.erase(remove_if(cell.begin(),cell.end(), isspace), cell.end());
1877 tmp_row[i] = to_numeric<T>(cell);
1880 if(accept(tmp_row)){
1882 for(
int i=0 ; i < ncolumns ; ++i) data[i].push_back(tmp_row[i]);
1892 template <
typename T>
1894 ,std::vector<std::vector<T> > &ranges
1895 ,std::vector<std::string> &column_names
1896 ,
size_t MaxNumber = 100000000
1897 ,
char comment_char =
'#'
1898 ,
char deliniator =
','
1899 ,std::string replace =
"\\N"
1900 ,std::function<
bool(std::vector<T> &)> accept = [](std::vector<T> &v){
return true;}
1903 bool verbose =
false;
1905 std::ifstream file(filename);
1907 if (!file.is_open()){
1908 std::cerr <<
"file " << filename <<
" cann't be opened." << std::endl;
1909 throw std::runtime_error(
"no file");
1912 int count_preamble=0;
1916 std::getline(file,line);
1919 }
while(line[0] == comment_char);
1922 std::stringstream lineStream(line);
1924 column_names.empty();
1925 while(std::getline(lineStream,cell,
','))
1927 column_names.push_back(cell);
1930 if (!lineStream && cell.empty())
1932 column_names.push_back(
"");
1937 for(
auto st : column_names){
1938 std::cout << i++ <<
" " << st << std::endl;
1942 int columns = NumberOfEntries(line,deliniator);
1944 std::vector<std::vector<T> > tmp_ranges(columns,std::vector<T>(2));
1945 swap(ranges,tmp_ranges);
1946 std::vector<T> tmp_row(columns);
1952 file.open(filename);
1953 for(
int i=0; i < count_preamble; ++i){
1954 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
1959 while(std::getline(file,line) && rows < MaxNumber){
1961 std::stringstream lineStream(line);
1965 while(std::getline(lineStream,cell,deliniator))
1967 if(cell==replace) cell=
'0';
1970 cell.erase(remove_if(cell.begin(),cell.end(), isspace), cell.end());
1971 tmp_row[i] = to_numeric<T>(cell);
1974 if(accept(tmp_row)){
1976 for(
int j=0 ; j < columns ; ++j){
1977 ranges[j][0] = tmp_row[j];
1978 ranges[j][1] = tmp_row[j];
1982 for(
int j=0 ; j < columns ; ++j){
1983 if(ranges[j][0] > tmp_row[j]) ranges[j][0] = tmp_row[j];
1984 if(ranges[j][1] < tmp_row[j]) ranges[j][1] = tmp_row[j];
2008 template <
typename T>
2010 ,std::vector<std::vector<T> > &data
2011 ,std::vector<std::string> &column_names
2012 ,
size_t Nmax = 1000000
2013 ,
char comment_char =
'#'
2014 ,
char deliniator =
','
2016 ,std::string reject =
""
2020 std::ifstream file(filename);
2022 if (!file.is_open()){
2023 std::cerr <<
"file " << filename <<
" cann't be opened." << std::endl;
2024 throw std::runtime_error(
"no file");
2029 std::getline(file,line);
2031 }
while(line[0] == comment_char);
2034 std::stringstream lineStream(line);
2036 column_names.empty();
2040 while(std::getline(lineStream,cell,deliniator))
2042 column_names.push_back(cell);
2046 if (!lineStream && cell.empty())
2048 column_names.push_back(
"");
2052 while(std::getline(lineStream,cell,deliniator))
2054 column_names.push_back(cell);
2057 data.emplace_back(column_names.size());
2059 for(
auto a : column_names){
2061 cell.erase(remove_if(cell.begin(),cell.end(), isspace), cell.end());
2062 data.back()[i++] = to_numeric<T>(a);
2067 int columns = NumberOfEntries(line,deliniator);
2069 for(
auto &v : data ) v.empty();
2071 while(std::getline(file,line) && ii < Nmax){
2073 data.emplace_back(columns);
2076 std::stringstream lineStream(line);
2080 while(std::getline(lineStream,cell, deliniator))
2082 assert(i < columns);
2083 cell.erase(remove_if(cell.begin(),cell.end(), isspace), cell.end());
2089 data.back()[i] = to_numeric<T>(cell);
2117 template<
typename T>
2119 ,
const std::vector<std::string> header
2120 ,std::vector<T *> &data
2123 std::ofstream s(filename +
".csv");
2125 long ncol = header.size();
2126 assert(ncol == data.size() );
2127 for(
int i = 0 ; i < header.size()-1 ; ++i){
2128 s << header[i] <<
",";
2130 s << header.back() << std::endl;
2132 size_t nrow = data[0]->size();
2133 for(
auto v : data) assert(nrow == v->size() );
2135 for(
size_t j = 0 ; j < nrow ; ++j){
2136 for(
int i = 0 ; i < ncol-1 ; ++i){
2137 s << data[i]->operator[](j) <<
",";
2139 s << data.back()->operator[](j) <<
"\n";
2155 XYcsvLookUp(std::string datafile
2159 ,
size_t MaxNumber = 1000000
2160 ,
bool verbose =
false);
2162 std::string datafile
2165 ,std::vector<double> Xbins
2166 ,
size_t MaxNumber = 1000000
2167 ,
bool verbose =
false);
2170 std::vector<double> find(
double x,
double y);
2173 double operator[](std::string label)
const;
2176 double operator[](
int i)
const{
2177 return (*current)[i];
2180 double getX()
const {
2181 return (*current)[Xindex];
2184 double getY()
const {
2185 return (*current)[Yindex];
2188 std::vector<double> record()
const{
2192 std::vector<double> operator*()
const {
2197 if(current != data.end() ) ++current;
2200 if(current != data.begin() ) --current;
2203 std::vector<std::string> labels()
const{
2204 return column_names;
2207 double Xmin()
const {
return xmin;}
2208 double Xmax()
const {
return xmax;}
2210 double Ymin(
double x)
const;
2212 double Ymax(
double x)
const;
2214 void printcurrent(){
2216 for(
auto label : column_names){
2217 std::cout << label <<
" : " << (*current)[i++] << std::endl;
2226 std::vector<std::vector<double> >::iterator current;
2227 std::vector<std::vector<std::vector<double> >::iterator> borders;
2228 std::vector<std::vector<double> > data;
2229 std::vector<std::string> column_names;
2230 std::vector<double> Xborders;
2232 std::string filename;
2237 void splitstring(std::string &line,std::vector<std::string> &vec,
const std::string &delimiter);
2247 template<
typename T>
2251 ,
size_t MaxNumber = 1000000
2252 ,
char comment_char =
'#'
2253 ,
char deliniator =
','
2254 ,std::string replace =
"\\N"
2255 ,std::function<
bool(std::vector<T> &)> accept = [](std::vector<T> &v){
return true;}
2256 ):filename(datafile){
2257 input(datafile,MaxNumber,comment_char,deliniator,replace,accept);
2263 ,
size_t MaxNumber = 1000000
2264 ,
char comment_char =
'#'
2265 ,
char deliniator =
','
2266 ,std::string replace =
"\\N"
2267 ,std::function<
bool(std::vector<T> &)> accept = [](std::vector<T> &v){
return true;}
2269 filename = datafile;
2271 for(
int i=0 ; i<column_names.size() ; ++i) datamap[column_names[i]] = i;
2275 void pop(std::string colname){
2277 int i=datamap[colname];
2279 swap(column_names[i],column_names.back());
2280 column_names.pop_back();
2281 swap(data[i],data.back());
2285 for(
int i=0 ; i<column_names.size() ; ++i) datamap[column_names[i]] = i;
2290 if(datamap.find(label) == datamap.end()){
2291 std::cerr <<
"No label - " << label <<
" - in " << filename <<std::endl;
2292 throw std::invalid_argument(
"no label");
2294 return data[datamap[label]];
2299 const std::vector<T>&
operator[](
const std::string &label)
const{
2300 if(datamap.find(label) == datamap.end()){
2301 std::cerr <<
"No label - " << label <<
" - in " << filename <<std::endl;
2302 throw std::invalid_argument(
"no label");
2304 return data[datamap[label]];
2308 void add_column(
const std::string &name,
const std::vector<T> &vec){
2309 if(vec.size() != data[0].size()){
2310 std::cerr <<
"Added column to DataFrame needs to have the same size." << std::endl;
2311 throw std::invalid_argument(
"wrong size");
2313 column_names.push_back(name);
2314 data.push_back(vec);
2315 data[name] = data.size()-1;
2322 const std::vector<T>&
operator[](
int i)
const{
2328 return column_names;
2332 void sortby(std::string name){
2333 std::vector<size_t> index(data[0].size());
2334 size_t N = index.size();
2335 for(
size_t i=0 ; i<N ; ++i) index[i] = i;
2338 std::vector<T> tmp_v(N);
2340 for(
size_t j=0 ; j<data.size() ; ++j){
2341 for(
size_t i=0 ; i<N ; ++i){
2342 tmp_v[i] = data[j][index[i]];
2344 swap(data[j],tmp_v);
2350 std::vector<size_t> index(data[0].size());
2351 size_t N = index.size();
2352 for(
size_t i=0 ; i<N ; ++i) index[i] = i;
2355 std::vector<T> tmp_v(N);
2357 for(
size_t j=0 ; j<data.size() ; ++j){
2358 for(
size_t i=0 ; i<N ; ++i){
2359 tmp_v[i] = data[j][index[i]];
2361 swap(data[j],tmp_v);
2365 size_t number_of_rows(){
return data[0].size();}
2366 size_t number_of_columns(){
return data.size();}
2368 std::vector<std::vector<T> > data;
2370 std::map<std::string,int> datamap;
2371 std::vector<std::string> column_names;
2372 std::string filename;
2376 template <
typename T>
2377 double KleinSum(std::vector<T> &input){
2378 double s = 0.0,cs = 0.0,ccs = 0.0,c,cc;
2379 size_t n = input.size();
2380 for(
size_t i = 0 ; i<n ; ++i){
2381 double t = s + input[i];
2382 if( abs(s) >= abs(input[i]) ){
2383 c = (s - t) + input[i];
2385 c = (input[i] - t) + s;
2389 if( abs(cs) >= abs(c) ){
2397 return s + cs + ccs;
2401 template <
typename T,
typename F>
2402 double PairWiseSum(T *begin,T *end,F value){
2404 size_t n = end - begin;
2406 for(T* p=begin ; p != end ; ++p){
2410 sum = PairWiseSum<T,F>(begin,begin + n/2,value) + PairWiseSum<T,F>(begin + n/2,end,value);
2416 template <
typename T>
2417 double PairWiseSum(T *begin,T *end){
2419 size_t n = end - begin;
2421 for(T* p=begin ; p != end ; ++p){
2425 sum = PairWiseSum(begin,begin + n/2) + PairWiseSum(begin + n/2,end);
2431 template <
typename T>
2432 double PairWiseSum(std::vector<T> &v){
2433 return PairWiseSum(v.data(),v.data() + v.size());
2437 template <
typename T,
typename F>
2438 double PairWiseSum(std::vector<T> &v,F value){
2439 return PairWiseSum(v.data(),v.data() + v.size(),value);
2443 template <
typename T>
2448 SUMMER():batch(1000),n(0),ntotal(0),current(0.0){};
2450 :batch(batchsize),n(0),current(0.0),ntotal(0){};
2458 v.push_back(current);
2466 if(v.size() ==0 )
return 0;
2467 v.push_back(current);
2473 std::vector<T> dump;
2497 template <
typename T =
double>
2498 double hypergeometric( T a, T b, T c, T x )
2502 throw std::invalid_argument(
"out of bounds");
2504 const double TOLERANCE = 1.0e-10;
2505 double term = a * b * x / c;
2506 double value = 1.0 + term;
2509 while ( abs( term ) > TOLERANCE )
2512 term *= a * b * x / c / n;
2522 typedef boost::variant<int,size_t,long,float,double,std::string,bool> MULTITYPE;
2523 typedef std::map<std::string,MULTITYPE> LINE;
2524 typedef std::map<std::string,std::vector<double> > VLINE;
2525 typedef std::map<std::string,std::vector<bool> > BLINE;
2533 std::map<std::string,std::string> input_lines;
2534 std::string filename;
2535 std::string blanck_val;
2538 LOGPARAMS(std::string file,std::string blanck_value =
"0"):filename(file),blanck_val(blanck_value){
2543 print_file(filename);
2546 void operator()(std::string label,MULTITYPE v){lines[label] = v;}
2547 void operator()(std::string label,
const std::vector<double> &v){vlines[label] = v;}
2548 void operator()(std::string label,
const std::vector<bool> &v){blines[label] = v;}
2549 void operator()(std::string label){lines[label] =
"-----";}
2552 MULTITYPE operator[](std::string label){
return lines[label];}
2553 void setlogfile(std::string name){filename = name;}
2560 void setinput(
const std::string &input_filename){
2561 std::ifstream input_file(input_filename);
2563 throw std::runtime_error(
"Unable to open parameter file: " + input_filename);
2566 input_lines.clear();
2568 const std::string whitespace =
" \t\r\n";
2569 while(std::getline(input_file,line)){
2570 const std::string::size_type value_end = line.find_last_not_of(whitespace);
2571 if(value_end == std::string::npos)
continue;
2573 const std::string::size_type value_begin = line.find_last_of(whitespace,value_end);
2574 if(value_begin == std::string::npos)
continue;
2576 const std::string::size_type label_end = line.find_last_not_of(whitespace,value_begin);
2577 if(label_end == std::string::npos)
continue;
2579 const std::string label = line.substr(0,label_end+1);
2580 const std::string value = line.substr(value_begin+1,value_end-value_begin);
2581 input_lines[label] = value;
2586 bool get(
const std::string &label) {
2587 const std::map<std::string,std::string>::const_iterator entry = input_lines.find(label);
2588 if(entry == input_lines.end())
return false;
2592 lines[label] = entry->second;
2600 template<
typename T>
2601 bool get(
const std::string &label,T &value) {
2602 const std::map<std::string,std::string>::const_iterator entry = input_lines.find(label);
2603 if(entry == input_lines.end()){
2604 lines[label] = value;
2608 std::istringstream stream(entry->second);
2612 throw std::runtime_error(
"Invalid value for input parameter '" + label +
"'");
2615 lines[label] = value;
2620 for(
auto a : lines){
2621 std::cout << std::left << std::setw(25) << a.first <<
" " << a.second << std::endl;
2623 for(
auto a : vlines){
2624 std::cout << std::left << std::setw(25) << a.first <<
" : ";
2625 for(
double x : a.second ) std::cout << x <<
" ";
2626 std::cout << std::endl;
2628 for(
auto a : blines){
2629 std::cout << std::left << a.first <<
" : ";
2630 for(
double x : a.second ) std::cout << x <<
" ";
2631 std::cout << std::endl;
2633 time_t now = time(0);
2634 std::cout <<
"log has existed for " << difftime(now,to)/60 <<
" min" << std::endl;
2637 void print_to_file(){
2638 print_file(filename);
2641 void print_file(std::string filename){
2644 std::ofstream logfile(filename);
2645 time_t now = time(0);
2646 struct tm date = *localtime(&now);
2647 logfile << date.tm_hour <<
":" << date.tm_min <<
" " << date.tm_mday <<
"/" << date.tm_mon <<
"/" << date.tm_year + 1900 << std::endl;
2648 logfile <<
"log has existed for " << difftime(now,to)/60 <<
" min" << std::endl;
2649 for(
auto a : lines){
2650 logfile << std::left << std::setw(23) << a.first <<
" " << a.second << std::endl;
2652 for(
auto a : vlines){
2653 logfile << std::left << std::setw(23) << a.first <<
" : ";
2654 for(
double x : a.second ) logfile << x <<
" ";
2655 logfile << std::endl;
2657 for(
auto a : blines){
2658 logfile << std::left << a.first <<
" : ";
2659 for(
double x : a.second ) logfile << x <<
" ";
2660 logfile << std::endl;
2670 typedef boost::variant<int,long,size_t,float,double,std::string,bool> MULTITYPE;
2671 typedef std::map<std::string,MULTITYPE> LINE;
2674 std::vector<LINE> lines;
2675 std::string filename;
2676 std::string blank_val;
2677 std::vector<std::string> header;
2680 long last_line_printed;
2682 std::set<std::string> labels;
2684 std::map<std::string,std::string> label_comments;
2690 const long default_precision = std::cout.precision();
2691 std::ofstream logfile;
2692 logfile.open(filename,std::ios_base::app);
2694 size_t n=labels.size();
2695 if(n == nlabels && last_line_printed > 0){
2696 std::cout << std::setprecision(precision);
2697 for(
size_t j=last_line_printed ; j<lines.size() ; ++j ){
2699 for(
auto &label : labels){
2701 if(label ==
"ID" || label ==
"id"
2702 || label ==
"galaxy_halo_id" ){
2703 logfile << std::setprecision(17) << lines[j].at(label);
2704 std::cout << std::setprecision(precision);
2705 }
else if(label ==
"RA" || label ==
"DEC" ){
2706 logfile << std::setprecision(8) << lines[j].at(label) ;
2707 std::cout << std::setprecision(precision);
2709 logfile << std::setprecision(precision) << lines[j].at(label) ;
2711 if(i<n-1) logfile <<
",";
2712 }
catch(std::exception& e){
2713 logfile << std::setprecision(precision) << blank_val ;
2714 if(i<n-1) logfile <<
",";
2718 logfile << std::endl;
2720 std::cout << std::setprecision(default_precision);
2722 last_line_printed = lines.size();
2731 LOGDATA(std::string file
2732 ,std::string blanck_value =
"0"
2734 ):filename(file),blank_val(blanck_value)
2735 ,nbatch(Nbatch),precision(std::cout.precision())
2736 ,last_line_printed(0),nlabels(0),nprints(0){
2740 ~LOGDATA(){print_to_file();}
2742 std::string output_file(){
return filename;}
2744 void set_precision(
int p){
2749 size_t ncol(){
return labels.size();}
2751 std::set<std::string> names = labels;
2753 void print_to_file(){
2755 const int default_precision = std::cout.precision();
2758 if(lines.size() == 0 )
return;
2760 std::ofstream logfile(filename);
2762 time_t now = time(0);
2763 struct tm date = *localtime(&now);
2764 logfile <<
"# " << date.tm_hour <<
":" << date.tm_min <<
" " << date.tm_mday <<
"/" << date.tm_mon <<
"/" << date.tm_year + 1900 << std::endl;
2765 for(std::string &s : header){
2766 logfile <<
"# " << s << std::endl;
2772 for(
auto &label : labels){
2773 auto it = label_comments.find(label);
2774 if(it == label_comments.end() ){
2775 logfile <<
"# " << i <<
" " << label <<
" : no comment" << std::endl;
2777 logfile <<
"# " << i <<
" " << it->first <<
" : " << it->second << std::endl;
2783 nlabels = labels.size();
2784 for(
auto &label : labels){
2786 if(i<nlabels-1) logfile <<
",";
2789 logfile << std::endl;
2791 std::cout << std::setprecision(precision);
2792 for(LINE &line : lines){
2794 for(
auto &label : labels){
2797 logfile << std::setprecision(17) << line.at(label) ;
2798 std::cout << std::setprecision(precision);
2799 }
else if(label ==
"RA" || label ==
"DEC" ){
2800 logfile << std::setprecision(8) << line.at(label) ;
2801 std::cout << std::setprecision(precision);
2803 logfile << std::setprecision(precision) << line.at(label);
2805 if(i<nlabels-1) logfile <<
",";
2806 }
catch(std::exception& e){
2807 logfile << std::setprecision(precision) << blank_val;
2808 if(i<nlabels-1) logfile <<
",";
2812 logfile << std::endl;
2814 std::cout << std::setprecision(default_precision);
2816 last_line_printed = lines.size();
2821 void add(
const LINE &line){
2822 for (
auto itr = line.begin(); itr != line.end(); ++itr) labels.insert(itr->first);
2823 lines.push_back(line);
2824 if(lines.size() % nbatch == 0) append_file();
2828 void replace(
const LINE &line){
2829 for (
auto itr = line.begin(); itr != line.end(); ++itr) labels.insert(itr->first);
2830 lines.back() = line;
2834 void header_comment(
const std::string &s){
2835 header.push_back(s);
2839 void label_description(
const std::string &name
2840 ,
const std::string &description
2842 label_comments[name] = description;
2883 template <
typename T>
2884 std::valarray<T>
AdaptiveSmooth(
const std::valarray<T> &map_in,
size_t Nx,
size_t Ny,T value){
2886 std::valarray<T> map_out(map_in.size());
2889 for(
long i=0;i<Nx;++i){
2890 for(
long j=0;j<Ny;++j){
2892 val = map_in[i+j*Nx];
2893 while(val < value && r < std::min(Nx, Ny) ){
2897 long imin,imax,jmin,jmax;
2899 imin = (i-r < 0) ? 0 : i-r;
2900 imax = (i+r > Nx-1) ? Nx-1 : i+r;
2902 jmin = (j-r < 0) ? 0 : j-r;
2903 jmax = (j+r > Ny-1) ? Ny-1 : j+r;
2906 for(
long ii=imin;ii<=imax;++ii){
2907 for(
long jj=jmin;jj<=jmax;++jj){
2908 if( (ii-i)*(ii-i) + (jj-j)*(jj-j) < r){
2909 val += map_in[ii+jj*Nx];
2917 map_out[i+j*Nx] = val/area;