diff --git a/src/mlpack/core/util/io.cpp b/src/mlpack/core/util/io.cpp index fcd6a002e2..2f1b4792ad 100644 --- a/src/mlpack/core/util/io.cpp +++ b/src/mlpack/core/util/io.cpp @@ -277,65 +277,21 @@ void IO::CheckInputMatrices() { std::string paramName = itr->first; std::string paramType = itr->second.cppType; - std::string errMsg1 = "The input " + paramName + " has NaN values."; - std::string errMsg2 = "The input " + paramName + " has inf values."; if (paramType == "arma::mat") - { - if (IO::GetParam>(paramName).has_nan()) - Log::Fatal << errMsg1 << std::endl; - - if (IO::GetParam>(paramName).has_inf()) - Log::Fatal << errMsg2 << std::endl; - } + IO::CheckInputMatrix>(paramName); else if (paramType == "arma::Mat") - { - if (IO::GetParam>(paramName).has_nan()) - Log::Fatal << errMsg1 << std::endl; - - if (IO::GetParam>(paramName).has_inf()) - Log::Fatal << errMsg2 << std::endl; - } + IO::CheckInputMatrix>(paramName); else if (paramType == "arma::colvec") - { - if (IO::GetParam>(paramName).has_nan()) - Log::Fatal << errMsg1 << std::endl; - - if (IO::GetParam>(paramName).has_inf()) - Log::Fatal << errMsg2 << std::endl; - } + IO::CheckInputMatrix>(paramName); else if (paramType == "arma::Col") - { - if (IO::GetParam>(paramName).has_nan()) - Log::Fatal << errMsg1 << std::endl; - - if (IO::GetParam>(paramName).has_inf()) - Log::Fatal << errMsg2 << std::endl; - } + IO::CheckInputMatrix>(paramName); else if (paramType == "arma::rowvec") - { - if (IO::GetParam>(paramName).has_nan()) - Log::Fatal << errMsg1 << std::endl; - - if (IO::GetParam>(paramName).has_inf()) - Log::Fatal << errMsg2 << std::endl; - } + IO::CheckInputMatrix>(paramName); else if (paramType == "arma::Row") - { - if (IO::GetParam>(paramName).has_nan()) - Log::Fatal << errMsg1 << std::endl; - - if (IO::GetParam>(paramName).has_inf()) - Log::Fatal << errMsg2 << std::endl; - } + IO::CheckInputMatrix>(paramName); else if (paramType == "std::tuple") - { - if (std::get<1>(IO::GetParam(paramName)).has_nan()) - Log::Fatal << errMsg1 << std::endl; - - if (std::get<1>(IO::GetParam(paramName)).has_inf()) - Log::Fatal << errMsg2 << std::endl; - } + IO::CheckInputMatrix(paramName); } } diff --git a/src/mlpack/core/util/io.hpp b/src/mlpack/core/util/io.hpp index dbe75da481..0633fd8c5d 100644 --- a/src/mlpack/core/util/io.hpp +++ b/src/mlpack/core/util/io.hpp @@ -285,6 +285,14 @@ class IO */ static void ClearSettings(); + /** + * Utility function for CheckInputMatrices(). + * + * @param matrix Matrix to check for NaN or Inf values. + */ + template + static void CheckInputMatrix(T& matrix); + /** * Checks all input matrices for NaN and inf values, exits if found any. */ diff --git a/src/mlpack/core/util/io_impl.hpp b/src/mlpack/core/util/io_impl.hpp index feb892325c..e40d5824dc 100644 --- a/src/mlpack/core/util/io_impl.hpp +++ b/src/mlpack/core/util/io_impl.hpp @@ -145,6 +145,19 @@ T& IO::GetRawParam(const std::string& identifier) } } +template +void CheckInputMatrix(const std::string paramName) +{ + std::string errMsg1 = "The input " + paramName + " has NaN values."; + std::string errMsg2 = "The input " + paramName + " has inf values."; + + if (IO::GetParam(paramName).has_nan()) + Log::Fatal << errMsg1 << std::endl; + + if (IO::GetParam(paramName).has_inf()) + Log::Fatal << errMsg2 << std::endl; +} + } // namespace mlpack #endif