/* * Toolbox.hh * * Created on: Jun 23, 2018 * Author: benjamin */ #ifndef TOOLBOX_HH_ #define TOOLBOX_HH_ #include "ParamData.hh" namespace AMDA { namespace Parameters { namespace StatisticFunctions { template Type min(Type a, Type b) { if (!std::isfinite(a) || isNAN(a)) return b; if (!std::isfinite(b) || isNAN(b)) return a; return std::min(a,b); } template std::vector min(std::vector a, std::vector b) { std::vector res = a; if (a.size() != b.size()) { BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Vectors don't have the same size")); } for (unsigned int i = 0; i < a.size(); ++i) { res[i] = StatisticFunctions::min(a[i],b[i]); } return res; } template Tab2DData min(Tab2DData a, Tab2DData b) { Tab2DData res(a); if (a.getDim1Size() != b.getDim1Size() || a.getDim2Size() != b.getDim2Size()) { BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Tab2DData don't have the same dimensions")); } for (int i = 0; i < a.getDim1Size(); ++i) { for (int j = 0; j < a.getDim2Size(); ++j) { res[i][j] = StatisticFunctions::min(a[i][j],b[i][j]); } } return res; } template Type max(Type a, Type b) { if (!std::isfinite(a) || isNAN(a)) return b; if (!std::isfinite(b) || isNAN(b)) return a; return std::max(a,b); } template std::vector max(std::vector a, std::vector b) { std::vector res = a; if (a.size() != b.size()) { BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Vectors don't have the same size")); } for (unsigned int i = 0; i < a.size(); ++i) { res[i] = StatisticFunctions::max(a[i],b[i]); } return res; } template Tab2DData max(Tab2DData a, Tab2DData b) { Tab2DData res(a); if (a.getDim1Size() != b.getDim1Size() || a.getDim2Size() != b.getDim2Size()) { BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Tab2DData don't have the same dimensions")); } for (int i = 0; i < a.getDim1Size(); ++i) { for (int j = 0; j < a.getDim2Size(); ++j) { res[i][j] = StatisticFunctions::max(a[i][j],b[i][j]); } } return res; } template Type pow(Type a, double exp) { return std::pow(a,exp); } template std::vector pow(std::vector a, double exp) { std::vector res = a; for (unsigned int i = 0; i < a.size(); ++i) { res[i] = StatisticFunctions::pow(a[i],exp); } return res; } template Tab2DData pow(Tab2DData a, double exp) { Tab2DData res = a; for (int i = 0; i < a.getDim1Size(); ++i) { for (int j = 0; j < a.getDim2Size(); ++j) { res[i][j] = StatisticFunctions::pow(a[i][j],exp); } } return res; } template Type square(Type a) { return StatisticFunctions::pow(a,2); } template std::vector square(std::vector a) { return StatisticFunctions::pow(a,2); } template Tab2DData square(Tab2DData a) { return StatisticFunctions::pow(a,2); } template Type root_square(Type a) { if (isNAN(a) || a < 0) { Type res; res << NotANumber(); return res; } return std::sqrt(a); } template std::vector root_square(std::vector a) { std::vector res = a; for (unsigned int i = 0; i < a.size(); ++i) { res[i] = StatisticFunctions::root_square(a[i]); } return res; } template Tab2DData root_square(Tab2DData a) { Tab2DData res = a; for (int i = 0; i < a.getDim1Size(); ++i) { for (int j = 0; j < a.getDim2Size(); ++j) { res[i][j] = StatisticFunctions::root_square(a[i][j]); } } return res; } template Type div(Type a, Type b) { if (isNAN(a) || isNAN(b) || (b == 0)) { Type res; res << NotANumber(); return res; } return a/b; } template std::vector div(std::vector a, std::vector b) { std::vector res = a; if (a.size() != b.size()) { BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Vectors don't have the same size")); } for (unsigned int i = 0; i < a.size(); ++i) { res[i] = StatisticFunctions::div(a[i],b[i]); } return res; } template Tab2DData div(Tab2DData a, Tab2DData b) { Tab2DData res(a); if (a.getDim1Size() != b.getDim1Size() || a.getDim2Size() != b.getDim2Size()) { BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Tab2DData don't have the same dimensions")); } for (int i = 0; i < a.getDim1Size(); ++i) { for (int j = 0; j < a.getDim2Size(); ++j) { res[i][j] = StatisticFunctions::div(a[i][j],b[i][j]); } } return res; } } /* namespace StatisticFunctions */ } /* namespace Parameters */ } /* namespace AMDA */ #endif /* TOOLBOX_HH_ */