Skip to content

Commit

Permalink
Function/is correlation (GeomScale#315)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
atrayees authored and TolisChal committed Jul 5, 2024
1 parent c65b2ef commit b5be6f2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
7 changes: 4 additions & 3 deletions examples/correlation_matrices/sampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<NT, MT, Eigen::Matrix<NT, Eigen::Dynamic, 1>> 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;

Expand Down
18 changes: 18 additions & 0 deletions include/matrix_operations/EigenvaluesProblems.h
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,24 @@ class EigenvaluesProblems<NT, Eigen::Matrix<NT,Eigen::Dynamic,Eigen::Dynamic>, 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<matrix.rows() ; i++){
if (std::abs(matrix(i, i)-1.0) > 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<MT> ges(B,A) (faster)
/// \param[in] A: symmetric positive definite matrix
Expand Down

0 comments on commit b5be6f2

Please sign in to comment.