mlpack  1.0.12
eigenvalue_ratio_constraint.hpp
Go to the documentation of this file.
1 
14 #ifndef __MLPACK_METHODS_GMM_EIGENVALUE_RATIO_CONSTRAINT_HPP
15 #define __MLPACK_METHODS_GMM_EIGENVALUE_RATIO_CONSTRAINT_HPP
16 
17 #include <mlpack/core.hpp>
18 
19 namespace mlpack {
20 namespace gmm {
21 
29 {
30  public:
37  EigenvalueRatioConstraint(const arma::vec& ratios) :
38  ratios(ratios)
39  {
40  // Check validity of ratios.
41  if (std::abs(ratios[0] - 1.0) > 1e-20)
42  Log::Fatal << "EigenvalueRatioConstraint::EigenvalueRatioConstraint(): "
43  << "first element of ratio vector is not 1.0!" << std::endl;
44 
45  for (size_t i = 1; i < ratios.n_elem; ++i)
46  {
47  if (ratios[i] > 1.0)
48  Log::Fatal << "EigenvalueRatioConstraint::EigenvalueRatioConstraint(): "
49  << "element " << i << " of ratio vector is greater than 1.0!"
50  << std::endl;
51  if (ratios[i] < 0.0)
52  Log::Warn << "EigenvalueRatioConstraint::EigenvalueRatioConstraint(): "
53  << "element " << i << " of ratio vectors is negative and will "
54  << "probably cause the covariance to be non-invertible..."
55  << std::endl;
56  }
57  }
58 
62  void ApplyConstraint(arma::mat& covariance) const
63  {
64  // Eigendecompose the matrix.
65  arma::vec eigenvalues;
66  arma::mat eigenvectors;
67  arma::eig_sym(eigenvalues, eigenvectors, covariance);
68 
69  // Change the eigenvalues to what we are forcing them to be. There
70  // shouldn't be any negative eigenvalues anyway, so it doesn't matter if we
71  // are suddenly forcing them to be positive. If the first eigenvalue is
72  // negative, well, there are going to be some problems later...
73  eigenvalues = (eigenvalues[0] * ratios);
74 
75  // Reassemble the matrix.
76  covariance = eigenvectors * arma::diagmat(eigenvalues) * eigenvectors.t();
77  }
78 
79  private:
81  const arma::vec& ratios;
82 };
83 
84 }; // namespace gmm
85 }; // namespace mlpack
86 
87 #endif
void ApplyConstraint(arma::mat &covariance) const
Apply the eigenvalue ratio constraint to the given covariance matrix.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: load.hpp:23
static util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:87
const arma::vec & ratios
Ratios for eigenvalues.
EigenvalueRatioConstraint(const arma::vec &ratios)
Create the EigenvalueRatioConstraint object with the given vector of eigenvalue ratios.
static util::PrefixedOutStream Warn
Prints warning messages prefixed with [WARN ].
Definition: log.hpp:84
Given a vector of eigenvalue ratios, ensure that the covariance matrix always has those eigenvalue ra...