diff --git a/Test/matBasic_real_test.cpp b/Test/matBasic_real_test.cpp index 2a35497..b9ca1b3 100644 --- a/Test/matBasic_real_test.cpp +++ b/Test/matBasic_real_test.cpp @@ -79,6 +79,27 @@ i_real_matrix genTestMatrixb(const std::size_t nAnt, const std::size_t nEq) return resMat; } + +bool CheckIdentity(const i_real_matrix& mat) +{ + const float EPS = 1.0e-20; + + double sumErrSqr = 0; + for (std::size_t r = 0; r < mat.size(); r++) + { + const i_real_vector& row = mat[r]; + for (std::size_t c = 0; c < row.size(); c++) + { + i_float_t diff = c == r ? row[c] - i_float_t(1.0) : row[c]; + sumErrSqr = diff * diff; + } + } + + return sumErrSqr < EPS; +} + + + void pinvTest(bool doLargeMatTest = true) { std::cout << "\n\n******************** pinv test ********************\n\n"; @@ -106,7 +127,13 @@ void pinvTest(bool doLargeMatTest = true) {-6.0, 3.0, 9.0, 1.0}}; std::cout << "rank(matC) = " << rank(matC) << "\n"; showMatrix(matC, "matC"); - showMatrix(inv(matC), "inv(matC)"); + + i_real_matrix invMatC = inv(matC); + showMatrix(invMatC, "inv(matC)"); + bool isIdentity = CheckIdentity(matMul(matC, invMatC)); + if (!isIdentity) + std::cout << "not identity"; + showMatrix(pinv(matC), "pinv(matC)"); showMatrix(pinv2(matC), "pinv2(matC)"); std::cout << "\n\n"; diff --git a/Test/matBasic_testUtil.hpp b/Test/matBasic_testUtil.hpp index 12d327d..c8ece44 100644 --- a/Test/matBasic_testUtil.hpp +++ b/Test/matBasic_testUtil.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include // Performance test timer +#include class TestTimer @@ -13,7 +13,7 @@ class TestTimer void tic() { m_running = true; - gettimeofday(&m_time_start, NULL); + m_time_start = std::chrono::steady_clock::now(); } double toc(const char *printInfo = nullptr) @@ -21,9 +21,11 @@ class TestTimer double res = 0.0; if (m_running) { - gettimeofday(&m_time_end, NULL); + m_time_end = std::chrono::steady_clock::now(); m_running = false; - res = static_cast(m_time_end.tv_sec - m_time_start.tv_sec) + static_cast(m_time_end.tv_usec - m_time_start.tv_usec) / 1000000.0; + auto time_diff = m_time_end - m_time_start; + auto microSecs = std::chrono::duration_cast(time_diff).count(); + res = static_cast(microSecs) / 1.0e6; if (nullptr != printInfo) { std::cout << "Time elapsed - " << printInfo << ": "; @@ -45,15 +47,8 @@ class TestTimer } private: - struct timeval m_time_start - { - 0, 0 - }; - - struct timeval m_time_end - { - 0, 0 - }; + std::chrono::steady_clock::time_point m_time_start; + std::chrono::steady_clock::time_point m_time_end; bool m_running{false}; };