CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
Kernel.h
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2010 by Federico Marulli *
3  * federico.marulli3@unibo.it *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public *
16  * License along with this program; if not, write to the Free *
17  * Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ********************************************************************/
20 
34 #ifndef __KERNEL__
35 #define __KERNEL__
36 
37 #include "Path.h"
38 #include "Constants.h"
39 #include "EnumCast.h"
40 #include "Exception.h"
41 
42 // ============================================================================================
43 
44 
477 namespace cbl {
478 
483  enum class Dim {
484 
486  _1D_,
487 
489  _2D_
490 
491  };
492 
499  inline std::vector<std::string> DimNames () { return {"1D", "2D"}; }
500 
505  enum class BinType {
506 
508  _linear_,
509 
512 
514  _custom_
515 
516  };
517 
524  inline std::vector<std::string> BinTypeNames () { return {"linear", "logarithmic"}; }
525 
532  inline BinType BinTypeCast (const int binTypeIndex) { return castFromValue<BinType>(binTypeIndex); }
533 
540  inline BinType BinTypeCast (const std::string binTypeName) { return castFromName<BinType>(binTypeName, BinTypeNames()); }
541 
548  inline std::vector<BinType> BinTypeCast (const std::vector<int> binTypeIndeces) { return castFromValues<BinType>(binTypeIndeces); }
549 
556  inline std::vector<BinType> BinTypeCast (const std::vector<std::string> binTypeNames) { return castFromNames<BinType>(binTypeNames, BinTypeNames()); }
557 
562  enum class CoordinateUnits {
563 
565  _radians_,
566 
568  _degrees_,
569 
571  _arcseconds_,
572 
575 
576  };
577 
578 
585  inline std::vector<std::string> CoordinateUnitsNames () { return {"radians", "degrees", "arcseconds", "arcminutes"}; }
586 
593  inline CoordinateUnits CoordinateUnitsCast (const int coordinateUnitsIndex) { return castFromValue<CoordinateUnits>(coordinateUnitsIndex); }
594 
601  inline CoordinateUnits CoordinateUnitsCast (const std::string coordinateUnitsName) { return castFromName<CoordinateUnits>(coordinateUnitsName, CoordinateUnitsNames()); }
602 
609  inline std::vector<CoordinateUnits> CoordinateUnitsCast (const std::vector<int> coordinateUnitsIndeces) { return castFromValues<CoordinateUnits>(coordinateUnitsIndeces); }
610 
617  inline std::vector<CoordinateUnits> CoordinateUnitsCast (const std::vector<std::string> coordinateUnitsNames) { return castFromNames<CoordinateUnits>(coordinateUnitsNames, CoordinateUnitsNames()); }
618 
619 
624  enum class CoordinateType {
625 
627  _comoving_,
628 
630  _observed_
631 
632  };
633 
640  inline std::vector<std::string> CoordinateTypeNames () { return {"comoving", "observed"}; }
641 
648  inline CoordinateType CoordinateTypeCast (const int coordinateTypeIndex) { return castFromValue<CoordinateType>(coordinateTypeIndex); }
649 
656  inline CoordinateType CoordinateTypeCast (const std::string coordinateTypeName) { return castFromName<CoordinateType>(coordinateTypeName, CoordinateTypeNames()); }
657 
664  inline std::vector<CoordinateType> CoordinateTypeCast (const std::vector<int> coordinateTypeIndeces) { return castFromValues<CoordinateType>(coordinateTypeIndeces); }
665 
672  inline std::vector<CoordinateType> CoordinateTypeCast (const std::vector<std::string> coordinateTypeNames) { return castFromNames<CoordinateType>(coordinateTypeNames, CoordinateTypeNames()); }
673 
674  struct comovingCoordinates { double xx; double yy; double zz; };
675  struct observedCoordinates { double ra; double dec; double redshift; };
676 
678  typedef Eigen::Matrix<double, 3, 1> Vector3D;
679 
681  typedef Eigen::Matrix<double, 4, 1> Vector4D;
682 
684  typedef Eigen::Matrix<std::complex<double>, 1, Eigen::Dynamic> VectorComplex;
685 
687  typedef std::function<double(double)> FunctionDoubleDouble;
688 
690  typedef std::function<double(std::vector<double>)> FunctionDoubleVector;
691 
693  typedef std::function<double(std::vector<double> &)> FunctionDoubleVectorRef;
694 
696  typedef std::function< double(double, std::shared_ptr<void>, std::vector<double> &)> FunctionDoubleDoublePtrVectorRef;
697 
699  typedef std::function< double(double, double, std::shared_ptr<void>, std::vector<double> &)> FunctionDoubleDoubleDoublePtrVectorRef;
700 
702  typedef std::function< double(std::vector<double>, std::shared_ptr<void>, std::vector<double> &)> FunctionDoubleVectorPtrVectorRef;
703 
705  typedef std::function< std::vector<double>(std::vector<double>, std::shared_ptr<void>, std::vector<double> &)> FunctionVectorVectorPtrVectorRef;
706 
708  typedef std::vector<std::vector<std::vector<int>>> Tensor3Di;
709 
711  typedef std::vector<std::vector<std::vector<double>>> Tensor3Dd;
712 
714  typedef std::vector<std::vector<std::vector<std::vector<int>>>> Tensor4Di;
715 
716 
721 
727  inline std::ostream &headerCBL (std::ostream &stream)
728  {
729  stream << par::col_blue << "CBL > " << par::col_default;
730  return stream;
731  }
732 
734 #define coutCBL std::cout << headerCBL
735 
747  inline void WarningMsgCBL (const std::string msg, const std::string functionCBL, const std::string fileCBL)
748  { std::cerr << std::endl << par::col_bred << "CBL > Warning in the CBL function " << cbl::par::col_yellow << functionCBL << cbl::par::col_bred << " of " << fileCBL << ": " << cbl::par::col_default << msg << std::endl << std::endl; }
749 
761  inline int Error (const std::string msg, const cbl::glob::ExitCode exitCode=cbl::glob::ExitCode::_error_, const std::string header="\n")
762  { throw cbl::glob::Exception(msg, exitCode, header); }
763 
780  inline int ErrorCBL (const std::string msg, const std::string functionCBL, const std::string fileCBL, const cbl::glob::ExitCode exitCode=cbl::glob::ExitCode::_error_)
781  { throw cbl::glob::Exception(msg, exitCode, cbl::par::ErrorMsg, functionCBL, fileCBL); }
782 
791  inline void Beep (const std::string beep="beep")
792  { if (system(("command -v say >/dev/null && { say "+beep+"; }").c_str())) {} }
793 
803  inline bool isSet (const std::string var)
804  { return (var!=cbl::par::defaultString) ? true : false; }
805 
814  inline bool isSet (const int var)
815  { return (var>par::defaultInt) ? true : false; }
816 
825  inline bool isSet (const long var)
826  { return (var>par::defaultLong) ? true : false; }
827 
836  inline bool isSet (const double var)
837  { return (var>par::defaultDouble) ? true : false; }
838 
848  inline bool isSet (const std::vector<double> vect)
849  {
850  bool is = true;
851  size_t ind = 0;
852  while (is && ind<vect.size())
853  if (vect[ind++]<par::defaultDouble*1.000001) is = false;
854  return is;
855  }
856 
866  inline bool isSet (const std::vector<unsigned int> vect)
867  {
868  bool is = true;
869  size_t ind = 0;
870  while (is && ind<vect.size())
871  if (vect[ind++]<par::defaultInt*1.000001) is = false;
872  return is;
873  }
874 
884  inline bool isSet (const std::vector<std::vector<unsigned int>> vect)
885  {
886  bool is = true;
887  size_t ind = 0;
888  size_t i=0;
889  while (is && i<vect.size()) {
890  while (is && ind<vect[i].size())
891  if (vect[i][ind++]<par::defaultInt*1.000001) is = false;
892  i++;
893  }
894  return is;
895  }
896 
903  template <typename T> std::string conv (const T val, const char *fact)
904  {
905  char VAL[20]; sprintf(VAL, fact, val);
906  return std::string(VAL);
907  }
908 
914  template <typename T>
915  int nint (const T val)
916  { return (val<0) ? val-0.5 : val+0.5; }
917 
925  template <typename T>
926  T Log (const T val, const double fact=0.9)
927  { return (val>0) ? log10(val) : par::defaultDouble*fact; }
928 
936  template <typename T>
937  T Ln (const T val, const double fact=0.9)
938  { return (val>0) ? log(val) : par::defaultDouble*fact; }
939 
947  template <typename T>
948  T closest (T x, T a, T b)
949  {
950  if (a>b) ErrorCBL("the input parameter a must be <= than the input parameter b!", "closest", "Kernel.h");
951  else if (a==b) return a;
952  else return (fabs(x-a) < fabs(x-b)) ? a : b;
953  return 1;
954  }
955 
964  template <typename T>
965  T index_closest (T x, std::vector<T> vv)
966  {
967  if (vv.size()==0) ErrorCBL("vv is an empty std::vector!", "index_closest", "Kernel.h");
968  std::vector<double>::iterator low, up;
969  low = lower_bound(vv.begin(), vv.end(), x);
970  up = upper_bound(vv.begin(), vv.end(), x);
971  int index = (closest(x, *low, *up)==*low) ? low-vv.begin() : up-vv.begin();
972  return index;
973  }
974 
983  template <typename T>
984  T closest (T x, std::vector<T> values)
985  { return values[index_closest(x, values)]; }
986 
992  short ShortSwap (const short s);
993 
999  int IntSwap (const int i);
1000 
1006  long long LongSwap (const long long i);
1007 
1013  float FloatSwap (const float f);
1014 
1020  double DoubleSwap (const double d);
1021 
1033  double round_to_digits (const double num, const int ndigits);
1034 
1046  double round_to_precision (const double num, const int ndigits);
1047 
1053  void checkIO (const std::ifstream &fin, const std::string file="NULL");
1054 
1060  void checkIO (const std::ofstream &fout, const std::string file="NULL");
1061 
1067  void set_EnvVar (const std::vector<std::string> Var);
1068 
1073  void check_EnvVar (const std::string Var);
1074 
1086  int used_memory (const int type);
1087 
1109  int check_memory (const double frac, const bool exit=true, const std::string func="", const int type=1);
1110 
1112 
1113 
1114  // ============================================================================================
1115 
1116 
1121 
1141  template <typename T>
1142  void Print (const T value, const int prec, const int ww, const std::string header="", const std::string end="\n", const bool use_coutCBL=true, std::ostream &stream=std::cout, const std::string colour=cbl::par::col_default)
1143  {
1144  const int bp = std::cout.precision();
1145  if (fabs(value)<pow(10, -prec) || fabs(value)>pow(10, prec)) {
1146  if (use_coutCBL)
1147  coutCBL << header << colour << std::scientific << std::setprecision(prec) << std::setw(ww) << std::right << value << par::col_default << end;
1148  else
1149  stream << header << std::scientific << std::setprecision(prec) << std::setw(ww) << std::right << value << end;
1150  }
1151  else {
1152  if (use_coutCBL)
1153  coutCBL << header << colour << std::fixed << std::setprecision(prec) << std::setw(ww) << std::right << value << par::col_default << end;
1154  else
1155  stream << header << std::fixed << std::setprecision(prec) << std::setw(ww) << std::right << value << end;
1156  }
1157  std::cout.precision(bp);
1158  }
1159 
1168  template <typename T>
1169  void Print (const std::vector<T> vect, const int prec=4, const int ww=8)
1170  {
1171  for (auto &&vi : vect)
1172  Print(vi, prec, ww);
1173  }
1174 
1181  inline void Print (const std::vector<std::string> vect)
1182  {
1183  for (auto &&vi : vect)
1184  coutCBL << vi << std::endl;
1185  }
1186 
1187 
1197  template <typename T>
1198  void Print (const std::vector<T> vect1, const std::vector<T> vect2, const int prec=4, const int ww=8)
1199  {
1200  if (vect1.size()!=vect2.size())
1201  ErrorCBL("the two input vectors to be printed must have the same dimension!", "Print", "Kernel.h");
1202 
1203  for (size_t i=0; i<vect1.size(); i++) {
1204  Print(vect1[i], prec, ww, "", " ");
1205  Print(vect2[i], prec, ww);
1206  }
1207  }
1208 
1209 
1218  inline void Print (const std::vector<std::string> vect1, const std::vector<std::string> vect2)
1219  {
1220  if (vect1.size()!=vect2.size())
1221  ErrorCBL("the two input vectors to be printed must have the same dimension!", "Print", "Kernel.h");
1222 
1223  for (size_t i=0; i<vect1.size(); i++)
1224  coutCBL << vect1[i] << " " << vect2[i] << std::endl;
1225  }
1226 
1227 
1242  template <typename T>
1243  void Print (const std::vector<T> vect1, const std::vector<T> vect2, const std::vector<T> vect3, const int prec=4, const int ww=8)
1244  {
1245  if (vect1.size()!=vect2.size() || vect1.size()!=vect3.size())
1246  ErrorCBL("the three input vectors to be printed must have the same dimension!", "Print", "Kernel.h");
1247 
1248  for (size_t i=0; i<vect1.size(); i++) {
1249  Print(vect1[i], prec, ww, "", " ");
1250  Print(vect2[i], prec, ww, "", " ");
1251  Print(vect3[i], prec, ww);
1252  }
1253  }
1254 
1255 
1266  inline void Print (const std::vector<std::string> vect1, const std::vector<std::string> vect2, const std::vector<std::string> vect3)
1267  {
1268  if (vect1.size()!=vect2.size() || vect1.size()!=vect3.size())
1269  ErrorCBL("the three input vectors to be printed must have the same dimension!", "Print", "Kernel.h");
1270 
1271  for (size_t i=0; i<vect1.size(); i++)
1272  coutCBL << vect1[i] << " " << vect2[i] << " " << vect3[i] << std::endl;
1273  }
1274 
1275 
1286  template <typename T>
1287  void Print (const std::vector<std::vector<T>> mat, const int prec=4, const int ww=8)
1288  {
1289  for (size_t i=0; i<mat.size(); i++) {
1290  for (size_t j=0; j<mat[i].size(); j++)
1291  if (j==0)
1292  Print(mat[i][j], prec, ww, "", " ");
1293  else
1294  Print(mat[i][j], prec, ww, "", " ");
1295  std::cout << std::endl;
1296  }
1297  }
1298 
1305  inline void Print (const std::vector<std::vector<std::string>> mat)
1306  {
1307  for (size_t i=0; i<mat.size(); i++) {
1308  for (size_t j=0; j<mat[i].size(); j++)
1309  if (j==0)
1310  coutCBL << mat[i][j] << " ";
1311  else
1312  std::cout << mat[i][j] << " ";
1313  std::cout << std::endl;
1314  }
1315  }
1316 
1317 
1323  template <typename T>
1324  T Min (const std::vector<T> vect)
1325  {
1326  if (vect.size()==0) ErrorCBL("vect.size=0!", "Min", "Kernel.h");
1327  return *min_element(vect.begin(), vect.end());
1328  }
1329 
1335  template <typename T>
1336  T Max (const std::vector<T> vect)
1337  {
1338  if (vect.size()==0) ErrorCBL("vect.size=0!", "Max", "Kernel.h");
1339  return *max_element(vect.begin(), vect.end());
1340  }
1341 
1348  template <typename T>
1349  std::vector<T> different_elements (const std::vector<T> vect_input)
1350  {
1351  std::vector<T> vect = vect_input;
1352  sort(vect.begin(), vect.end());
1353  typename std::vector<T>::iterator it = unique(vect.begin(), vect.end());
1354  vect.resize(it-vect.begin());
1355  return vect;
1356  }
1357 
1363  template <typename T>
1364  int N_different_elements (const std::vector<T> vect_input)
1365  {
1366  std::vector<T> vect = different_elements<T>(vect_input);
1367  return vect.size();
1368  }
1369 
1374  void unique_unsorted (std::vector<int> & vv);
1375 
1380  void unique_unsorted (std::vector<double> & vv);
1381 
1386  void unique_unsorted (std::vector<std::string> & vv);
1387 
1394  template <typename T>
1395  void Erase (std::vector<T> &vv, std::vector<int> ind)
1396  {
1397  for (auto &&i : ind)
1398  if (i>=int(vv.size())) ErrorCBL("the input value of ind is too large!", "Erase", "Kernel.h");
1399 
1400  unique_unsorted(ind);
1401  int tt = 0;
1402  for (auto &&i : ind)
1403  vv.erase(vv.begin()+i-(tt++));
1404  }
1405 
1412  template <typename T>
1413  void Erase_lines (std::vector<std::vector<T> > &Mat, std::vector<int> ll)
1414  {
1415  for (auto &&i : ll)
1416  if (i>=int(Mat.size())) ErrorCBL("the dimension of the input vector ll is too large!", "Erase_lines", "Kernel.h");
1417 
1418  unique_unsorted(ll);
1419  int tt = 0;
1420  for (auto &&i : ll)
1421  Mat.erase(Mat.begin()+i-(tt++));
1422  }
1423 
1430  template <typename T>
1431  void Erase_columns (std::vector<std::vector<T> > &Mat, std::vector<int> col)
1432  {
1433  for (auto &&i : col)
1434  for (auto &&j : Mat)
1435  if (i>=int(j.size())) ErrorCBL("the dimension of the input vector col is too large!", "Erase_columns", "Kernel.h");
1436 
1437  unique_unsorted(col);
1438  int tt = 0;
1439  for (auto &&i : col) {
1440  for (auto &&j : Mat)
1441  j.erase(j.begin()+i-tt);
1442  tt ++;
1443  }
1444  }
1445 
1460  template <typename T>
1461  void SubMatrix (std::vector<T> &xx, std::vector<T> &yy, std::vector<std::vector<T> > &Mat, T val)
1462  {
1463  std::vector<int> line, column;
1464 
1465  for (unsigned int i=0; i<xx.size(); i++) {
1466  if (i>=Mat.size()) ErrorCBL("the dimension of the input vector xx is too large!", "SubMatrix", "Kernel.h");
1467  bool ll = 0;
1468 
1469  for (unsigned int j=0; j<yy.size(); j++) {
1470  if (j>=Mat[i].size()) ErrorCBL("the dimension of the input vector yy is too large!", "SubMatrix", "Kernel.h");
1471  if (Mat[i][j]<val) {
1472  if (j<int(yy.size()*0.5)) {line.push_back(i); ll = 1;}
1473  else if (ll==0) {column.push_back(j);}
1474  }
1475  }
1476  }
1477 
1478  Erase(xx, line);
1479  Erase_lines(Mat, line);
1480 
1481  Erase(yy, column);
1482  Erase_columns(Mat, column);
1483  }
1484 
1492  template <typename T>
1493  bool isDimEqual (const std::vector<T> vect1, const std::vector<T> vect2)
1494  {
1495  return (vect1.size()==vect2.size()) ? 1 : 0;
1496  }
1497 
1505  template <typename T>
1506  bool isDimEqual (const std::vector<std::vector<T> > mat1, const std::vector<std::vector<T> > mat2)
1507  {
1508  bool is = (mat1.size()==mat2.size()) ? 1 : 0;
1509  if (is)
1510  for (unsigned int i=0; i<mat1.size(); i++) {
1511  if (mat1[i].size()!=mat2[i].size()) is = 0;
1512  }
1513  return is;
1514  }
1515 
1531  template <typename T>
1532  void checkDim (const std::vector<T> vect, const int val, const std::string vector, bool equal=true)
1533  {
1534  if (equal) {
1535  if ((int)vect.size()!=val)
1536  ErrorCBL("the dimension of " + vector + " is: " + conv(vect.size(), par::fINT) + " ( != " + conv(val, par::fINT) + " )", "checkDim", "Kernel.h");
1537  }
1538  else {
1539  if ((int)vect.size()<val)
1540  ErrorCBL("the dimension of " + vector + " is: " + conv(vect.size(), par::fINT) + " ( < " + conv(val, par::fINT) + " )", "checkDim", "Kernel.h");
1541  }
1542  }
1543 
1555  template <typename T>
1556  void checkDim (const std::vector<T> mat, const int val_i, const int val_j, const std::string matrix, const bool equal=true)
1557  {
1558  if (equal) {
1559  if (int(mat.size())!=val_i)
1560  ErrorCBL("the dimension of " + matrix + " is: " + conv(mat.size(), par::fINT) + " != " + conv(val_i, par::fINT) + "!", "checkDim", "Kernel.h");
1561  else
1562  for (size_t k=0; k<mat.size(); k++)
1563  if (int(mat[k].size())!=val_j)
1564  ErrorCBL("the dimension of " + matrix + " is: " + conv(mat[k].size(), par::fINT) + " != " + conv(val_j, par::fINT) + "!", "checkDim", "Kernel.h");
1565  }
1566  else {
1567  if (int(mat.size())<val_i)
1568  ErrorCBL("the dimension of " + matrix + " is: " + conv(mat.size(), par::fINT) + " < " + conv(val_i, par::fINT) + "!", "checkDim", "Kernel.h");
1569  else
1570  for (size_t k=0; k<mat.size(); k++)
1571  if (int(mat[k].size())<val_j)
1572  ErrorCBL("the dimension of " + matrix + " is: " + conv(mat[k].size(), par::fINT) + " < " + conv(val_j, par::fINT) + "!", "checkDim", "Kernel.h");
1573  }
1574  }
1575 
1586  template <typename T>
1587  void checkEqual (const std::vector<T> vect1, const std::vector<T> vect2)
1588  {
1589  checkDim(vect2, vect1.size(), "vect2");
1590  for (size_t i=0; i<vect1.size(); i++)
1591  if (vect1[i]!=vect2[i])
1592  ErrorCBL("vect1 and vect2 are different!", "checkEqual", "Kernel.h");
1593  }
1594 
1603  template <typename T>
1604  std::vector<T> linear_bin_vector (const size_t nn, const T min, const T max)
1605  {
1606  std::vector<T> vv(nn);
1607  for (size_t i = 0; i<nn; i++)
1608  vv[i] = min+(max-min)*T(i)/T(nn-1);
1609  return vv;
1610  }
1611 
1620  template <typename T>
1621  std::vector<T> logarithmic_bin_vector (const size_t nn, const T min, const T max)
1622  {
1623  std::vector<T> vv(nn);
1624  for (size_t i=0; i<nn; i++)
1625  vv[i] = exp(log(min)+(log(max)-log(min))*T(i)/T(nn-1));
1626  return vv;
1627  }
1628 
1637  template <typename T>
1638  int locate (const std::vector<T> &vv, const T xx)
1639  {
1640  size_t nn = vv.size ();
1641  int jl = -1;
1642  int ju = nn;
1643  bool as = (vv[nn-1] >= vv[0]);
1644  while (ju-jl > 1)
1645  {
1646  int jm = (ju+jl)*0.5;
1647  if ((xx >= vv[jm]) == as)
1648  jl = jm;
1649  else
1650  ju = jm;
1651  }
1652  if (xx == vv[0])
1653  return 0;
1654  else if (xx == vv[nn-1])
1655  return nn-2;
1656  else
1657  return jl;
1658  }
1659 
1668  template <typename T>
1669  std::vector<T> extract_elements (std::vector<T> vec, std::vector<unsigned int> index)
1670  {
1671  std::vector<T> vv;
1672  for (unsigned int i=0; i< index.size(); i++)
1673  vv.push_back(vec[index[i]]);
1674  return vv;
1675  }
1676 
1685  template <typename T>
1686  std::vector<T> flatten(std::vector<std::vector<T>> matrix)
1687  {
1688  std::vector<T> flatten;
1689  for (size_t i=0; i<matrix.size(); i++)
1690  for (size_t j=0; j<matrix[i].size(); j++)
1691  flatten.push_back(matrix[i][j]);
1692 
1693  return flatten;
1694  }
1695 
1706  template <typename T>
1707  std::vector<std::vector<T>> reshape (std::vector<T> vec, const int size1, const int size2)
1708  {
1709  if (size1*size2!=int(vec.size()))
1710  ErrorCBL("sizes does not match! "+conv(size1*size2, par::fINT)+" should be equal to "+conv(int(vec.size()), par::fINT), "reshape", "Kernel.h");
1711 
1712  std::vector<std::vector<T>> matrix(size1, std::vector<T> (size2, 0));
1713 
1714  for (int i=0; i<size1; i++)
1715  for (int j=0; j<size2; j++)
1716  matrix[i][j] = vec[j+i*size2];
1717 
1718  return matrix;
1719  }
1720 
1728  template <typename T>
1729  std::vector<std::vector<T>> transpose (std::vector<std::vector<T>> matrix)
1730  {
1731  const int size1 = matrix.size();
1732  const int size2 = matrix[0].size();
1733 
1734  std::vector<std::vector<T>> TRmatrix(size2, std::vector<T> (size1, 0));
1735 
1736  for (int i=0; i<size1; i++)
1737  for (int j=0; j<size2; j++)
1738  TRmatrix[j][i] = matrix[i][j];
1739 
1740  return TRmatrix;
1741  }
1742 
1758  template <typename T>
1759  bool inRange (T value, T min, T max, bool include_limits = true)
1760  {
1761  if (include_limits){
1762  if (value>=min && max>=value)
1763  return true;
1764  }
1765  else{
1766  if (value>min && max>value)
1767  return true;
1768  }
1769 
1770  return false;
1771  }
1772 
1788  template <typename T>
1789  bool inRange (std::vector<T> value, std::vector<T> min, std::vector<T> max, bool include_limits = true)
1790  {
1791  int in_range = 1;
1792 
1793  for (size_t i=0; i<value.size(); i++)
1794  in_range *= int(inRange(value[i], min[i], max[i], include_limits));
1795 
1796  return bool(in_range);
1797  }
1798 
1813  template <typename T>
1814  bool inRange (std::vector<T> value, std::vector<std::vector<T>> ranges, bool include_limits = true)
1815  {
1816  int in_range = 1;
1817 
1818  for (size_t i=0; i<value.size(); i++)
1819  in_range *= int(inRange(value[i], ranges[i][0], ranges[i][1], include_limits));
1820 
1821  return bool(in_range);
1822  }
1823 
1832  template <typename T>
1833  T v_M_vt (const std::vector<T> vv, const std::vector<std::vector<T>> MM)
1834  {
1835  const int size = vv.size();
1836 
1837  std::vector<double> ivv(size, 0);
1838  for(int i=0; i<size; i++)
1839  for(int j=0; j<size; j++)
1840  ivv[i] += vv[j]*MM[i][j];
1841 
1842  double res=0;
1843  for(int i=0; i<size; i++)
1844  res += vv[i]*ivv[i];
1845 
1846  return res;
1847  }
1848 
1849  // sort two or more std::vectors at the same time
1850  namespace glob {
1851  class CL {
1852  public:
1853  std::vector<double> VV;
1854  CL (std::vector<double> vv) {VV = vv;};
1855  };
1857  bool operator<(const CL &, const CL &);
1859  }
1860 
1869  void sort_2vectors (std::vector<double>::iterator p1, std::vector<double>::iterator p2, const int dim);
1870 
1880  void sort_3vectors (std::vector<double>::iterator p1, std::vector<double>::iterator p2, std::vector<double>::iterator p3, const int dim);
1881 
1892  void sort_4vectors (std::vector<double>::iterator p1, std::vector<double>::iterator p2, std::vector<double>::iterator p3, std::vector<double>::iterator p4, const int dim);
1893 
1911  int makeDir (std::string path, const std::string rootPath=".", const mode_t mode=0777, const bool verbose=false);
1912 
1922  inline std::vector<std::vector<double> > operator * (const std::vector<std::vector<double> > &Mat1, const std::vector<std::vector<double> > &Mat2)
1923  {
1924  std::vector<std::vector<double> > MatP(Mat1.size(), std::vector<double>(Mat2[0].size(),0.));
1925 
1926  for (unsigned int i=0; i<Mat1.size(); i++)
1927  for (unsigned int j=0; j<Mat2[0].size(); j++) {
1928  double temp = 0.;
1929  for (unsigned int k=0; k<Mat1[0].size(); k++)
1930  temp += Mat1[i][k]*Mat2[k][j];
1931  MatP[i][j] = temp;
1932  }
1933 
1934  return MatP;
1935  }
1936 
1946  template <typename T>
1947  std::vector<T> slice (const std::vector<T> v, const int start=0, const int end=-1)
1948  {
1949  int oldlen = v.size();
1950  int newlen;
1951 
1952  if (end==-1 || end>=oldlen)
1953  newlen = oldlen-start;
1954  else
1955  newlen = end-start;
1956 
1957  std::vector<T> nv(newlen);
1958 
1959  for (int i=0; i<newlen; i++)
1960  nv[i] = v[start+i];
1961 
1962  return nv;
1963  }
1964 
1983  std::vector<std::vector<double>> read_file (const std::string file_name, const std::string path_name, const std::vector<int> column_data, const int skip_nlines=0);
1984 
2006  std::vector<std::vector<double>> read_file (const std::string file_name, const std::string path_name, const std::vector<int> column_data, const std::string delimiter, const char comment='#');
2007 
2009 }
2010 
2011 
2012 #endif
Constants of general use.
Classes used to cast integers and std::string into the enums used in the CosmoBolognaLib.
The class Exception Class used to handle the exceptions.
#define coutCBL
CBL print message.
Definition: Kernel.h:734
The class Path used to handle the Cosmobolognalib paths.
The class Exception.
Definition: Exception.h:111
static const std::string col_default
default colour (used when printing something on the screen)
Definition: Constants.h:297
static const std::string col_yellow
yellow colour (used when printing something on the screen)
Definition: Constants.h:315
static const std::string col_bred
bold high intensty red colour (used when printing something on the screen)
Definition: Constants.h:303
static const std::string col_blue
blue colour (used when printing something on the screen)
Definition: Constants.h:312
static const char fINT[]
conversion symbol for: int -> std::string
Definition: Constants.h:121
static const std::string defaultString
default std::string value
Definition: Constants.h:336
static const long defaultLong
default long value
Definition: Constants.h:342
static const double defaultDouble
default double value
Definition: Constants.h:348
static const int defaultInt
default integer value
Definition: Constants.h:339
static const std::string ErrorMsg
header of error messages for internal usage
Definition: Constants.h:366
Var
the catalogue variables
Definition: Catalogue.h:70
ExitCode
the exit status
Definition: Exception.h:45
@ _error_
generic error
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
T Log(const T val, const double fact=0.9)
common logarithm (i.e. logarithm to base 10)
Definition: Kernel.h:926
std::vector< std::string > CoordinateTypeNames()
return a std::vector containing the CoordinateType names
Definition: Kernel.h:640
T Min(const std::vector< T > vect)
minimum element of a std::vector
Definition: Kernel.h:1324
CoordinateType CoordinateTypeCast(const int coordinateTypeIndex)
cast an enum of type CoordinateType from its index
Definition: Kernel.h:648
int N_different_elements(const std::vector< T > vect_input)
get the number of unique elements of a std::vector
Definition: Kernel.h:1364
std::string conv(const T val, const char *fact)
convert a number to a std::string
Definition: Kernel.h:903
void Print(const T value, const int prec, const int ww, const std::string header="", const std::string end="\n", const bool use_coutCBL=true, std::ostream &stream=std::cout, const std::string colour=cbl::par::col_default)
function to print values with a proper homegenised format
Definition: Kernel.h:1142
void Erase_lines(std::vector< std::vector< T > > &Mat, std::vector< int > ll)
erase some lines of a matrix
Definition: Kernel.h:1413
std::function< double(std::vector< double >)> FunctionDoubleVector
typedef of a function returning a double with a vector in input
Definition: Kernel.h:690
std::vector< std::vector< double > > read_file(const std::string file_name, const std::string path_name, const std::vector< int > column_data, const int skip_nlines=0)
read a data from a file ASCII
Definition: Kernel.cpp:410
std::vector< std::vector< double > > operator*(const std::vector< std::vector< double > > &Mat1, const std::vector< std::vector< double > > &Mat2)
matrix multiplication
Definition: Kernel.h:1922
CoordinateType
the coordinate type
Definition: Kernel.h:624
@ _observed_
observed coordinates (R.A., Dec, redshift)
@ _comoving_
comoving coordinates (x, y, z)
std::ostream & headerCBL(std::ostream &stream)
provide the header for all internal messages
Definition: Kernel.h:727
std::vector< T > different_elements(const std::vector< T > vect_input)
get the unique elements of a std::vector
Definition: Kernel.h:1349
T index_closest(T x, std::vector< T > vv)
given a number x, return the index of the closest element to x in vv
Definition: Kernel.h:965
std::vector< T > slice(const std::vector< T > v, const int start=0, const int end=-1)
slice a std::vector from start to stop
Definition: Kernel.h:1947
void Erase_columns(std::vector< std::vector< T > > &Mat, std::vector< int > col)
erase some columns of a matrix
Definition: Kernel.h:1431
void Beep(const std::string beep="beep")
produce a beep
Definition: Kernel.h:791
bool isSet(const std::string var)
check if the value of a [string] variable has already been set
Definition: Kernel.h:803
std::vector< T > logarithmic_bin_vector(const size_t nn, const T min, const T max)
fill a std::vector with logarithmically spaced values
Definition: Kernel.h:1621
std::vector< T > linear_bin_vector(const size_t nn, const T min, const T max)
fill a std::vector with linearly spaced values
Definition: Kernel.h:1604
void checkDim(const std::vector< T > vect, const int val, const std::string vector, bool equal=true)
check if the dimension of a std::vector is equal/lower than an input value
Definition: Kernel.h:1532
void checkIO(const std::ifstream &fin, const std::string file="NULL")
check if an input file can be opened
Definition: Kernel.cpp:160
void sort_4vectors(std::vector< double >::iterator p1, std::vector< double >::iterator p2, std::vector< double >::iterator p3, std::vector< double >::iterator p4, const int dim)
sort the elements of a std::vectors, and the elements of three other std::vectors according to the fi...
Definition: Kernel.cpp:359
int locate(const std::vector< T > &vv, const T xx)
locate a value in a given std::vector
Definition: Kernel.h:1638
std::function< std::vector< double >std::vector< double >, std::shared_ptr< void >, std::vector< double > &)> FunctionVectorVectorPtrVectorRef
typedef of a function returning a vector with a vector, a pointer and a vector reference in input
Definition: Kernel.h:705
int used_memory(const int type)
get the memory used by current process in kB
Definition: Kernel.cpp:211
int ErrorCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL, const cbl::glob::ExitCode exitCode=cbl::glob::ExitCode::_error_)
throw an exception: it is used for handling exceptions inside the CosmoBolognaLib
Definition: Kernel.h:780
void unique_unsorted(std::vector< int > &vv)
erase all the equal elements of the input std::vector
Definition: Kernel.cpp:284
void set_EnvVar(const std::vector< std::string > Var)
set evironment variables
Definition: Kernel.cpp:186
short ShortSwap(const short s)
endian conversion of a short variable
Definition: Kernel.cpp:45
T closest(T x, T a, T b)
given a number x, return the closest of two values a, b
Definition: Kernel.h:948
std::vector< std::vector< std::vector< double > > > Tensor3Dd
typedef of a 3D Tensor of double
Definition: Kernel.h:711
std::vector< T > flatten(std::vector< std::vector< T >> matrix)
flatten a matrix in a vector of size
Definition: Kernel.h:1686
std::vector< std::string > DimNames()
return a vector containing the Dim names
Definition: Kernel.h:499
std::function< double(double, double, std::shared_ptr< void >, std::vector< double > &)> FunctionDoubleDoubleDoublePtrVectorRef
typedef of a function returning a double with two double, a pointer and a vector reference in input
Definition: Kernel.h:699
T v_M_vt(const std::vector< T > vv, const std::vector< std::vector< T >> MM)
return the value of
Definition: Kernel.h:1833
CoordinateUnits CoordinateUnitsCast(const int coordinateUnitsIndex)
cast an enum of type CoordinateUnits from its index
Definition: Kernel.h:593
std::vector< std::vector< std::vector< int > > > Tensor3Di
typedef of a 3D Tensor of int
Definition: Kernel.h:708
Eigen::Matrix< std::complex< double >, 1, Eigen::Dynamic > VectorComplex
Eigen complex std::vector.
Definition: Kernel.h:684
T Max(const std::vector< T > vect)
maximum element of a std::vector
Definition: Kernel.h:1336
bool inRange(T value, T min, T max, bool include_limits=true)
return false value outside the range; true value inside the range
Definition: Kernel.h:1759
std::function< double(double)> FunctionDoubleDouble
typedef of a function returning a double with a double in input
Definition: Kernel.h:687
BinType BinTypeCast(const int binTypeIndex)
cast an enum of type BinType from its index
Definition: Kernel.h:532
bool isDimEqual(const std::vector< T > vect1, const std::vector< T > vect2)
check if the dimensions of two std::vectors are equal
Definition: Kernel.h:1493
T Ln(const T val, const double fact=0.9)
natural logarithm
Definition: Kernel.h:937
std::vector< std::vector< T > > transpose(std::vector< std::vector< T >> matrix)
transpose a matrix
Definition: Kernel.h:1729
std::vector< std::vector< T > > reshape(std::vector< T > vec, const int size1, const int size2)
reshape a vector into a matrix of given number of rows and columns
Definition: Kernel.h:1707
double round_to_digits(const double num, const int ndigits)
reduce the digit figures of an input double
Definition: Kernel.cpp:133
double DoubleSwap(const double d)
endian conversion of a double variable
Definition: Kernel.cpp:109
std::vector< std::string > BinTypeNames()
return a vector containing the BinType names
Definition: Kernel.h:524
int Error(const std::string msg, const cbl::glob::ExitCode exitCode=cbl::glob::ExitCode::_error_, const std::string header="\n")
throw an exception
Definition: Kernel.h:761
std::vector< std::string > CoordinateUnitsNames()
return a std::vector containing the CoordinateUnits names
Definition: Kernel.h:585
void Erase(std::vector< T > &vv, std::vector< int > ind)
erase some elements of a std::vector
Definition: Kernel.h:1395
std::function< double(double, std::shared_ptr< void >, std::vector< double > &)> FunctionDoubleDoublePtrVectorRef
typedef of a function returning a double with a double, a pointer and a vector reference in input
Definition: Kernel.h:696
std::function< double(std::vector< double > &)> FunctionDoubleVectorRef
typedef of a function returning a double with a vector reference in input
Definition: Kernel.h:693
void checkEqual(const std::vector< T > vect1, const std::vector< T > vect2)
check if two std::vectors are equal
Definition: Kernel.h:1587
int nint(const T val)
the nearest integer
Definition: Kernel.h:915
void sort_2vectors(std::vector< double >::iterator p1, std::vector< double >::iterator p2, const int dim)
sort the elements of a std::vectors, and the elements of a second std::vector according to the first ...
Definition: Kernel.cpp:323
CoordinateUnits
the coordinate units
Definition: Kernel.h:562
@ _radians_
angle in radians
@ _arcminutes_
angle in arcminutes
@ _arcseconds_
angle in arcseconds
@ _degrees_
angle in degrees
BinType
the binning type
Definition: Kernel.h:505
@ _logarithmic_
logarithmic binning
@ _custom_
custom binning
@ _linear_
linear binning
void WarningMsgCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL)
internal CBL warning message
Definition: Kernel.h:747
long long LongSwap(const long long i)
endian conversion of a long integer variable
Definition: Kernel.cpp:71
int check_memory(const double frac, const bool exit=true, const std::string func="", const int type=1)
check if the memory used by current process is larger than a given fraction of the available memory
Definition: Kernel.cpp:252
std::function< double(std::vector< double >, std::shared_ptr< void >, std::vector< double > &)> FunctionDoubleVectorPtrVectorRef
typedef of a function returning a double with a vector, a pointer and a vector reference in input
Definition: Kernel.h:702
void check_EnvVar(const std::string Var)
check if an environment variable exists
Definition: Kernel.cpp:196
float FloatSwap(const float f)
endian conversion of a float variable
Definition: Kernel.cpp:89
double round_to_precision(const double num, const int ndigits)
reduce the precision of an input double
Definition: Kernel.cpp:150
int makeDir(std::string path, const std::string rootPath=".", const mode_t mode=0777, const bool verbose=false)
function to create multiple directories
Definition: Kernel.cpp:381
Dim
the dimension, used e.g. for pair and triplet vectors
Definition: Kernel.h:483
@ _2D_
2D pair, used e.g. for 2D pairs, in Cartesian or polar coordinates
@ _1D_
1D, used e.g. for 1D pairs, in angular or comoving separations
std::vector< T > extract_elements(std::vector< T > vec, std::vector< unsigned int > index)
extract elements from a given vector
Definition: Kernel.h:1669
double jl(const double xx, const int order)
the order l spherical Bessel function
Definition: Func.cpp:2066
void SubMatrix(std::vector< T > &xx, std::vector< T > &yy, std::vector< std::vector< T > > &Mat, T val)
select a submatrix containing lines and columns with all elements major than val
Definition: Kernel.h:1461
std::vector< std::vector< std::vector< std::vector< int > > > > Tensor4Di
typedef of a 4D Tensor of int
Definition: Kernel.h:714
void sort_3vectors(std::vector< double >::iterator p1, std::vector< double >::iterator p2, std::vector< double >::iterator p3, const int dim)
sort the elements of a std::vectors, and the elements of two other std::vectors according to the firs...
Definition: Kernel.cpp:341
Eigen::Matrix< double, 4, 1 > Vector4D
Eigen 4D matrix.
Definition: Kernel.h:681
int IntSwap(const int i)
endian conversion of an integer variable
Definition: Kernel.cpp:57
Eigen::Matrix< double, 3, 1 > Vector3D
Eigen 3D std::vector.
Definition: Kernel.h:678