diff options
Diffstat (limited to 'test/main.h')
-rw-r--r-- | test/main.h | 102 |
1 files changed, 71 insertions, 31 deletions
diff --git a/test/main.h b/test/main.h index 0b9b0bc4c..06c17a9ae 100644 --- a/test/main.h +++ b/test/main.h @@ -24,6 +24,7 @@ // Eigen. If not, see <http://www.gnu.org/licenses/>. #include <cstdlib> +#include <cerrno> #include <ctime> #include <iostream> #include <string> @@ -49,6 +50,8 @@ namespace Eigen { static std::vector<std::string> g_test_stack; static int g_repeat; + static unsigned int g_seed; + static bool g_has_set_repeat, g_has_set_seed; } #define EI_PP_MAKE_STRING2(S) #S @@ -350,17 +353,30 @@ void createRandomMatrixOfRank(int desired_rank, int rows, int cols, MatrixType& typedef Matrix<Scalar, Rows, Rows> MatrixAType; typedef Matrix<Scalar, Cols, Cols> MatrixBType; + if(desired_rank == 0) + { + m.setZero(rows,cols); + return; + } + + if(desired_rank == 1) + { + m = VectorType::Random(rows) * VectorType::Random(cols).transpose(); + return; + } + MatrixAType a = MatrixAType::Random(rows,rows); MatrixType d = MatrixType::Identity(rows,cols); MatrixBType b = MatrixBType::Random(cols,cols); // set the diagonal such that only desired_rank non-zero entries reamain const int diag_size = std::min(d.rows(),d.cols()); - d.diagonal().segment(desired_rank, diag_size-desired_rank) = VectorType::Zero(diag_size-desired_rank); + if(diag_size != desired_rank) + d.diagonal().segment(desired_rank, diag_size-desired_rank) = VectorType::Zero(diag_size-desired_rank); HouseholderQR<MatrixAType> qra(a); HouseholderQR<MatrixBType> qrb(b); - m = qra.matrixQ() * d * qrb.matrixQ(); + m = qra.householderQ() * d * qrb.householderQ(); } } // end namespace Eigen @@ -372,51 +388,68 @@ template<> struct GetDifferentType<double> { typedef float type; }; template<typename T> struct GetDifferentType<std::complex<T> > { typedef std::complex<typename GetDifferentType<T>::type> type; }; +template<typename T> std::string type_name() { return "other"; } +template<> std::string type_name<float>() { return "float"; } +template<> std::string type_name<double>() { return "double"; } +template<> std::string type_name<int>() { return "int"; } +template<> std::string type_name<std::complex<float> >() { return "complex<float>"; } +template<> std::string type_name<std::complex<double> >() { return "complex<double>"; } +template<> std::string type_name<std::complex<int> >() { return "complex<int>"; } + // forward declaration of the main test function void EIGEN_CAT(test_,EIGEN_TEST_FUNC)(); using namespace Eigen; +void set_repeat_from_string(const char *str) +{ + errno = 0; + g_repeat = int(strtoul(str, 0, 10)); + if(errno || g_repeat <= 0) + { + std::cout << "Invalid repeat value " << str << std::endl; + exit(EXIT_FAILURE); + } + g_has_set_repeat = true; +} + +void set_seed_from_string(const char *str) +{ + errno = 0; + g_seed = strtoul(str, 0, 10); + if(errno || g_seed == 0) + { + std::cout << "Invalid seed value " << str << std::endl; + exit(EXIT_FAILURE); + } + g_has_set_seed = true; +} + int main(int argc, char *argv[]) { - bool has_set_repeat = false; - bool has_set_seed = false; + g_has_set_repeat = false; + g_has_set_seed = false; bool need_help = false; - unsigned int seed = 0; - int repeat = DEFAULT_REPEAT; for(int i = 1; i < argc; i++) { if(argv[i][0] == 'r') { - if(has_set_repeat) + if(g_has_set_repeat) { std::cout << "Argument " << argv[i] << " conflicting with a former argument" << std::endl; return 1; } - repeat = atoi(argv[i]+1); - has_set_repeat = true; - if(repeat <= 0) - { - std::cout << "Invalid \'repeat\' value " << argv[i]+1 << std::endl; - return 1; - } + set_repeat_from_string(argv[i]+1); } else if(argv[i][0] == 's') { - if(has_set_seed) + if(g_has_set_seed) { std::cout << "Argument " << argv[i] << " conflicting with a former argument" << std::endl; return 1; } - seed = int(strtoul(argv[i]+1, 0, 10)); - has_set_seed = true; - bool ok = seed!=0; - if(!ok) - { - std::cout << "Invalid \'seed\' value " << argv[i]+1 << std::endl; - return 1; - } + set_seed_from_string(argv[i]+1); } else { @@ -429,22 +462,29 @@ int main(int argc, char *argv[]) std::cout << "This test application takes the following optional arguments:" << std::endl; std::cout << " rN Repeat each test N times (default: " << DEFAULT_REPEAT << ")" << std::endl; std::cout << " sN Use N as seed for random numbers (default: based on current time)" << std::endl; + std::cout << std::endl; + std::cout << "If defined, the environment variables EIGEN_REPEAT and EIGEN_SEED" << std::endl; + std::cout << "will be used as default values for these parameters." << std::endl; return 1; } - if(!has_set_seed) seed = (unsigned int) time(NULL); - if(!has_set_repeat) repeat = DEFAULT_REPEAT; + char *env_EIGEN_REPEAT = getenv("EIGEN_REPEAT"); + if(!g_has_set_repeat && env_EIGEN_REPEAT) + set_repeat_from_string(env_EIGEN_REPEAT); + char *env_EIGEN_SEED = getenv("EIGEN_SEED"); + if(!g_has_set_seed && env_EIGEN_SEED) + set_seed_from_string(env_EIGEN_SEED); - std::cout << "Initializing random number generator with seed " << seed << std::endl; - srand(seed); - std::cout << "Repeating each test " << repeat << " times" << std::endl; + if(!g_has_set_seed) g_seed = (unsigned int) time(NULL); + if(!g_has_set_repeat) g_repeat = DEFAULT_REPEAT; + + std::cout << "Initializing random number generator with seed " << g_seed << std::endl; + srand(g_seed); + std::cout << "Repeating each test " << g_repeat << " times" << std::endl; - Eigen::g_repeat = repeat; Eigen::g_test_stack.push_back(EI_PP_MAKE_STRING(EIGEN_TEST_FUNC)); EIGEN_CAT(test_,EIGEN_TEST_FUNC)(); return 0; } - - |