From b77813fb79d2752d86bde3f9e118793a582b7ffe Mon Sep 17 00:00:00 2001 From: atrayees <121290945+atrayees@users.noreply.github.com> Date: Fri, 5 Jul 2024 21:21:23 +0530 Subject: [PATCH] Function/is correlation (#315) * new function is_correlation_matrix * update example function call this example calls the is_correlation_matrix() function that is present in include/matrix_operations/EigenvaluesProblems.h * remove blank line * remove blank line * fix spaces, add function parameter --- examples/correlation_matrices/sampler.cpp | 7 ++++--- .../matrix_operations/EigenvaluesProblems.h | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/examples/correlation_matrices/sampler.cpp b/examples/correlation_matrices/sampler.cpp index f18f90a1b..0655767ea 100644 --- a/examples/correlation_matrices/sampler.cpp +++ b/examples/correlation_matrices/sampler.cpp @@ -150,10 +150,11 @@ void correlation_matrix_uniform_sampling_MT(const unsigned int n, const unsigned std::cout << "Elapsed time : " << time << " (ms)" << std::endl; int valid_points = 0; + EigenvaluesProblems> solver; for(const auto& points : randPoints){ - if(is_correlation_matrix(points.mat)){ - valid_points++; - } + if(solver.is_correlation_matrix(points.mat)){ + valid_points++; + } } std::cout << "Number of valid points = " << valid_points << std::endl; diff --git a/include/matrix_operations/EigenvaluesProblems.h b/include/matrix_operations/EigenvaluesProblems.h index 178d91666..7bad48cbc 100644 --- a/include/matrix_operations/EigenvaluesProblems.h +++ b/include/matrix_operations/EigenvaluesProblems.h @@ -439,6 +439,24 @@ class EigenvaluesProblems, E return false; } + /// Check if a matrix is indeed a correlation matrix + /// return true if input matrix is found to be a correlation matrix + /// |param[in] matrix + bool is_correlation_matrix(const MT& matrix, const double tol = 1e-8){ + + //check if all the diagonal elements are ones + for (int i=0 ; i tol){ + return false; + } + } + + //check if the matrix is positive definite + if (isPositiveSemidefinite(matrix)) return true; + + return false; + } + /// Minimum positive eigenvalue of the generalized eigenvalue problem A - lB /// Use Eigen::GeneralizedSelfAdjointEigenSolver ges(B,A) (faster) /// \param[in] A: symmetric positive definite matrix