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