Test matrix Cholesky's decomposition.
#include <visp3/core/vpConfig.h>
#if defined(VISP_HAVE_CATCH2)
#include <iostream>
#include <stdlib.h>
#include <visp3/core/vpMatrix.h>
#include <catch_amalgamated.hpp>
#ifdef ENABLE_VISP_NAMESPACE
#endif
{
bool areEqual = (rowsA == rowsB) && (colsA == colsB);
if (!areEqual) {
return false;
}
for (unsigned int r = 0; r < rowsA; ++r) {
for (unsigned int c = 0; c < colsA; ++c) {
if (!areEqual) {
return false;
}
}
}
return areEqual;
}
const std::string &title, const bool &verbose)
{
if (verbose) {
std::cout << "-------------------------" << std::endl;
std::cout << "Test: " << title << std::endl;
std::cout << std::endl;
}
bool areEqual = (gtRows == resRows) && (gtCols == resCols);
if (!areEqual) {
if (verbose) {
std::cout << "Failed: dimensions mismatch (" << gtRows << " x " << gtCols << ")";
std::cout << " vs (" << resRows << " x " << resCols << ")" << std::endl;
}
return false;
}
areEqual = equal(gtResult, result);
if ((!areEqual) && verbose) {
std::cout << "Failed: L matrices differ." << std::endl;
std::cout << "Result =\n" << result << std::endl;
std::cout << "GT =\n" << gtResult << std::endl;
return false;
}
areEqual = equal(M, LLt);
if ((!areEqual) && verbose) {
std::cout << "Failed: LL^T differ from M." << std::endl;
std::cout << "LL^T =\n" << LLt << std::endl;
std::cout << "GT M =\n" << M << std::endl;
}
if (areEqual && verbose) {
std::cout << "Test " << title << " succeeded" << std::endl;
}
return areEqual;
}
TEST_CASE("3 x 3 input", "[cholesky]")
{
M[0][0] = 4; M[0][1] = 12; M[0][2] = -16;
M[1][0] = 12; M[1][1] = 37; M[1][2] = -43;
M[2][0] = -16; M[2][1] = -43; M[2][2] = 98;
gtL[0][0] = 2;
gtL[1][0] = 6; gtL[1][1] = 1;
gtL[2][0] = -8; gtL[2][1] = 5; gtL[2][2] = 3;
#if defined(VISP_HAVE_LAPACK)
SECTION("LAPACK")
{
CHECK(testCholeskyDecomposition(M, gtL, L, "Test Cholesky's decomposition using Lapack", true));
}
#endif
#if defined(VISP_HAVE_EIGEN3)
SECTION("EIGEN3")
{
CHECK(testCholeskyDecomposition(M, gtL, L, "Test Cholesky's decomposition using Eigen3", true));
}
#endif
#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION != 0x030100)
SECTION("OPENCV")
{
CHECK(testCholeskyDecomposition(M, gtL, L, "Test Cholesky's decomposition using OpenCV", true));
}
#endif
}
TEST_CASE("4 x 4 input", "[cholesky]")
{
M[0][0] = 4.16; M[0][1] = -3.12; M[0][2] = 0.56; M[0][3] = -0.10;
M[1][0] = -3.12; M[1][1] = 5.03; M[1][2] = -0.83; M[1][3] = 1.18;
M[2][0] = 0.56; M[2][1] = -0.83; M[2][2] = 0.76; M[2][3] = 0.34;
M[3][0] = -0.10; M[3][1] = 1.18; M[3][2] = 0.34; M[3][3] = 1.18;
gtL[0][0] = 2.0396;
gtL[1][0] = -1.5297; gtL[1][1] = 1.6401;
gtL[2][0] = 0.2746; gtL[2][1] = -0.2500; gtL[2][2] = 0.7887;
gtL[3][0] = -0.0490; gtL[3][1] = 0.6737; gtL[3][2] = 0.6617; gtL[3][3] = 0.5347;
#if defined(VISP_HAVE_LAPACK)
SECTION("LAPACK")
{
CHECK(testCholeskyDecomposition(M, gtL, L, "Test Cholesky's decomposition using Lapack", true));
}
#endif
#if defined(VISP_HAVE_EIGEN3)
SECTION("EIGEN3")
{
CHECK(testCholeskyDecomposition(M, gtL, L, "Test Cholesky's decomposition using Eigen3", true));
}
#endif
#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION != 0x030100)
SECTION("OPENCV")
{
CHECK(testCholeskyDecomposition(M, gtL, L, "Test Cholesky's decomposition using OpenCV", true));
}
#endif
}
int main(int argc, char *argv[])
{
Catch::Session session;
session.applyCommandLine(argc, argv);
int numFailed = session.run();
return numFailed;
}
#else
#include <iostream>
int main()
{
std::cout << "Test ignored: Catch2 is not available" << std::endl;
return EXIT_SUCCESS;
}
#endif
unsigned int getCols() const
unsigned int getRows() const
static bool equal(double x, double y, double threshold=0.001)
Implementation of a matrix and operations on matrices.
vpMatrix choleskyByOpenCV() const
Compute the Cholesky decomposition of a Hermitian positive-definite matrix using OpenCV library.
vpMatrix choleskyByEigen3() const
Compute the Cholesky decomposition of a Hermitian positive-definite matrix using Eigen3 library.
vpMatrix choleskyByLapack() const
Compute the Cholesky decomposition of a Hermitian positive-definite matrix using Lapack library.
vpMatrix transpose() const