Skip to content

Commit 913df63

Browse files
committed
Merge branch 'eigenvalues'
Calculate eigenvalues & check for PSD
2 parents 28763d5 + 92ff36b commit 913df63

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/kernel.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ calculate_pairwise(std::vector<std::string> &hash_fnames)
113113
if (verbosity > 0) {
114114
*outstream << "Done all!" << std::endl;
115115
}
116+
117+
if (!matrix_is_pos_semidef(_kernel_m)) {
118+
*outstream << "WARNING: The kernel matrix is not positive semidefinite."
119+
<< std::endl;
120+
} else {
121+
*outstream << "The kernel matrix is positive semidefinite."
122+
<< std::endl;
123+
}
116124
}
117125

118126
void

src/kwip-utils.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212

1313
#include "kwip-utils.hh"
14+
#include <Eigen/Eigenvalues>
15+
1416

1517
namespace kwip
1618
{
@@ -120,4 +122,11 @@ kernel_to_distance(MatrixXd &dist, MatrixXd &kernel, bool normalise)
120122
}
121123
}
122124

125+
bool
126+
matrix_is_pos_semidef(MatrixXd &mat)
127+
{
128+
VectorXd eigenvalues = mat.eigenvalues().real();
129+
return eigenvalues.minCoeff() > -1e-5;
130+
}
131+
123132
} // end namespace kwip

src/kwip-utils.hh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <Eigen/Core>
2323

2424
using Eigen::MatrixXd;
25+
using Eigen::VectorXd;
2526

2627
#include <kwip-config.hh>
2728

@@ -37,6 +38,10 @@ void print_lsmat(MatrixXd &mat, std::ostream &outstream,
3738
void normalise_matrix(MatrixXd &norm, MatrixXd &input);
3839
void kernel_to_distance(MatrixXd &dist, MatrixXd &kernel, bool normalise=true);
3940

41+
// Checks if a matrix is postitive semi-definite. Specifically, that all
42+
// eigenvalues are > -1e-5.
43+
bool matrix_is_pos_semidef(MatrixXd &mat);
44+
4045
template <typename eltype>
4146
eltype
4247
vec_min(std::vector<eltype> &vec)

0 commit comments

Comments
 (0)