00001
00002
00003 #ifndef JMATH_PCA_T_HPP
00004 #define JMATH_PCA_T_HPP
00005
00006 #include "jafarConfig.h"
00007
00008 #ifdef HAVE_BOOST_SANDBOX
00009 #ifdef HAVE_LAPACK
00010
00011 #include "jmath/jblas.hpp"
00012 #include "kernel/jafarDebug.hpp"
00013
00014 namespace jafar {
00015 namespace jmath {
00016
00027 template<typename NUMTYPE>
00028 class PCA_T {
00029 public:
00030
00033 enum UFlag {
00035 increase,
00037 preserve,
00040 ore
00041 };
00042
00043
00047 PCA_T(bool basisOnly_=false) : basis_only(basisOnly_) {};
00048
00054 PCA_T(const ublas::matrix<NUMTYPE>& X_, int dim_=0, bool basisOnly_ = false) {
00055 basis_only = basisOnly_;
00056 batchPCA(X_,dim_);
00057 };
00058
00062 PCA_T(PCA_T const & pca_) {
00063 mean = pca_.mean;
00064 eigenvalues = pca_.eigenvalues;
00065 eigenvectors = pca_.eigenvectors;
00066 coefficients = pca_.coefficients;
00067 }
00068
00072 PCA_T& operator= (PCA_T const & pca_) {
00073 mean = pca_.mean;
00074 eigenvalues = pca_.eigenvalues;
00075 eigenvectors = pca_.eigenvectors;
00076 coefficients = pca_.coefficients;
00077 return *this;
00078 };
00079
00080
00082 ublas::vector<NUMTYPE>& getMean() {
00083 JFR_PRECOND(mean.size() != 0, "PCA::getMean: no results available");
00084 return mean;
00085 };
00086
00088 ublas::matrix<NUMTYPE>& getEigenVectors() {
00089 JFR_PRECOND(eigenvectors.size2() != 0, "PCA::getEigenVectors: no results available");
00090 return eigenvectors;
00091 };
00092
00094 ublas::vector<NUMTYPE>& getEigenValues() {
00095 JFR_PRECOND(eigenvalues.size() != 0, "PCA::getEigenValues: no results available");
00096 return eigenvalues;
00097 }
00098
00100 ublas::matrix<NUMTYPE>& getCoefficients() {
00101 JFR_PRECOND(coefficients.size2() != 0, "PCA::getEigenValues: no results available");
00102 return coefficients;
00103 };
00104
00105
00110 void batchPCA(const ublas::matrix<NUMTYPE>& X_, int dim_=0);
00111
00112
00118 void updatePCA(const ublas::vector<NUMTYPE>& I_, UFlag f_=preserve, NUMTYPE thd_=0.25);
00119
00124 ublas::vector<NUMTYPE> project(const ublas::vector<NUMTYPE>& I_) const;
00125
00130 ublas::vector<NUMTYPE> reconstruct(const ublas::vector<NUMTYPE>& I_) const;
00131
00132 private:
00133
00134 #ifdef USE_JMATH_SERIALIZATION
00135 friend class boost::serialization::access;
00136 template<class Archive>
00137 void serialize(Archive & ar, const unsigned int version) {
00138 ar & BOOST_SERIALIZATION_NVP(basis_only);
00139 ar & BOOST_SERIALIZATION_NVP(eigenvectors);
00140 ar & BOOST_SERIALIZATION_NVP(coefficients);
00141 ar & BOOST_SERIALIZATION_NVP(mean);
00142 ar & BOOST_SERIALIZATION_NVP(eigenvalues);
00143 };
00144 #endif
00145 bool basis_only;
00146 ublas::matrix<NUMTYPE> eigenvectors,coefficients;
00147 ublas::vector<NUMTYPE> mean, eigenvalues;
00148
00149 };
00150 }
00151 }
00152
00153 #include <jmath/pca.hxx>
00154
00155 namespace jafar{
00156 namespace jmath {
00157 typedef PCA_T<double> PCA;
00158 }
00159 }
00160 #endif // HAVE_LAPACK
00161 #endif // HAVE_BOOST_SANDBOX
00162 #endif // JMATH_PCA_T_HPP
00163