From c43449b8125cfdc653bc3f6981c5da69c8c7a127 Mon Sep 17 00:00:00 2001 From: atrayees <121290945+atrayees@users.noreply.github.com> Date: Fri, 5 Jul 2024 17:25:08 +0530 Subject: [PATCH 1/5] new function is_correlation_matrix --- .../matrix_operations/EigenvaluesProblems.h | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/include/matrix_operations/EigenvaluesProblems.h b/include/matrix_operations/EigenvaluesProblems.h index 178d91666..5f61fdbbf 100644 --- a/include/matrix_operations/EigenvaluesProblems.h +++ b/include/matrix_operations/EigenvaluesProblems.h @@ -439,6 +439,27 @@ 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 From ad8ddabd517f47f014b9a009b360ddb106688e7f Mon Sep 17 00:00:00 2001 From: atrayees <121290945+atrayees@users.noreply.github.com> Date: Fri, 5 Jul 2024 17:34:25 +0530 Subject: [PATCH 2/5] update example function call this example calls the is_correlation_matrix() function that is present in include/matrix_operations/EigenvaluesProblems.h --- examples/correlation_matrices/sampler.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/correlation_matrices/sampler.cpp b/examples/correlation_matrices/sampler.cpp index f18f90a1b..1a4508cd7 100644 --- a/examples/correlation_matrices/sampler.cpp +++ b/examples/correlation_matrices/sampler.cpp @@ -150,11 +150,13 @@ 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; write_to_file(walkname + "_matrices_MT" + std::to_string(n) + ".txt", randPoints); From 6b0467352b9c99fe0c9f0338e4ab4b13d84e245a Mon Sep 17 00:00:00 2001 From: atrayees <121290945+atrayees@users.noreply.github.com> Date: Fri, 5 Jul 2024 17:36:48 +0530 Subject: [PATCH 3/5] remove blank line --- examples/correlation_matrices/sampler.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/correlation_matrices/sampler.cpp b/examples/correlation_matrices/sampler.cpp index 1a4508cd7..0655767ea 100644 --- a/examples/correlation_matrices/sampler.cpp +++ b/examples/correlation_matrices/sampler.cpp @@ -156,7 +156,6 @@ void correlation_matrix_uniform_sampling_MT(const unsigned int n, const unsigned valid_points++; } } - std::cout << "Number of valid points = " << valid_points << std::endl; write_to_file(walkname + "_matrices_MT" + std::to_string(n) + ".txt", randPoints); From f70e4477ec400fa6e8231dd76343468ce88d322b Mon Sep 17 00:00:00 2001 From: atrayees <121290945+atrayees@users.noreply.github.com> Date: Fri, 5 Jul 2024 17:37:51 +0530 Subject: [PATCH 4/5] remove blank line --- include/matrix_operations/EigenvaluesProblems.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/matrix_operations/EigenvaluesProblems.h b/include/matrix_operations/EigenvaluesProblems.h index 5f61fdbbf..33a8ef2e5 100644 --- a/include/matrix_operations/EigenvaluesProblems.h +++ b/include/matrix_operations/EigenvaluesProblems.h @@ -459,7 +459,6 @@ class EigenvaluesProblems, E 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 From 1ee58d1a5cf9ff3553a9f7f2b4e4b606721f99a2 Mon Sep 17 00:00:00 2001 From: atrayees <121290945+atrayees@users.noreply.github.com> Date: Fri, 5 Jul 2024 20:10:28 +0530 Subject: [PATCH 5/5] fix spaces, add function parameter --- include/matrix_operations/EigenvaluesProblems.h | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/include/matrix_operations/EigenvaluesProblems.h b/include/matrix_operations/EigenvaluesProblems.h index 33a8ef2e5..7bad48cbc 100644 --- a/include/matrix_operations/EigenvaluesProblems.h +++ b/include/matrix_operations/EigenvaluesProblems.h @@ -442,19 +442,17 @@ class EigenvaluesProblems, E /// 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; + 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){ + for (int i=0 ; i tol){ return false; } } //check if the matrix is positive definite - if(isPositiveSemidefinite(matrix)) return true; + if (isPositiveSemidefinite(matrix)) return true; return false; }