Kernel Quantum Probability Library
The KQP library aims at providing tools for working with quantums probabilities
kernel_evd.hpp
1 /*
2  This file is part of the Kernel Quantum Probability library (KQP).
3 
4  KQP is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  KQP is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with KQP. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __KQP_KERNEL_EVD_H__
19 #define __KQP_KERNEL_EVD_H__
20 
21 #include <utility>
22 
23 
24 #include <kqp/decomposition.hpp>
25 #include <kqp/feature_matrix.hpp>
26 #include <kqp/rank_selector.hpp>
27 
28 namespace kqp {
29 
30  template<typename Scalar> class Decomposition;
31 
33  class KernelEVDBase {
34  public:
35  KernelEVDBase() {}
36  virtual ~KernelEVDBase() {}
37  };
38 
53  template <class Scalar> class KernelEVD : public KernelEVDBase {
54  public:
55  KQP_SCALAR_TYPEDEFS(Scalar);
56 
57  KernelEVD(const FSpaceCPtr &featureSpace) : nbUpdates(0), m_featureSpace(featureSpace) {
58  }
59 
60 
61  virtual ~KernelEVD() {}
62 
63 
74  virtual void add(Real alpha, const FMatrixCPtr &mX, const ScalarAltMatrix &mA) {
75  // Check consistency
76  if (mX->size() != mA.rows())
77  KQP_THROW_EXCEPTION_F(illegal_argument_exception, "Cannot combine %d pre-images with a %d rows matrix", %mX->size() %mA.rows());
78 
79  _add(alpha, mX, mA);
80  nbUpdates += mA.cols();
81  }
82 
87  inline void add(const FMatrixCPtr &mU) {
88  add(1., mU, Eigen::Identity<Scalar>(mU->size(),mU->size()));
89  nbUpdates += mU->size();
90  }
91 
97  d.updateCount = nbUpdates;
98  d.fs = this->m_featureSpace;
99  // Creates a new empty feature matrix when nothing is returned
100  if (!d.mX)
101  d.mX = d.fs->newMatrix();
102  if (!d.check())
103  KQP_THROW_EXCEPTION_F(assertion_exception, "Decomposition in an invalid state (%d, %dx%d, %d) for KEVD %s",
104  %d.mX->size() %d.mY.rows() %d.mY.cols() %d.mD.rows() %KQP_DEMANGLE(*this));
105 
106  return d;
107  }
108 
110  virtual void reset() {
111  nbUpdates = 0;
112  }
113 
115  Index getUpdateCount() const {
116  return nbUpdates;
117  }
118 
119  const FSpaceCPtr &getFSpace() const { return m_featureSpace; }
120 
121  protected:
132  virtual void _add(Real alpha, const FMatrixCPtr &mU, const ScalarAltMatrix &mA) = 0;
133 
135  virtual Decomposition<Scalar> _getDecomposition() const = 0;
136 
137  private:
138  Index nbUpdates;
139  FSpaceCPtr m_featureSpace;
140  };
141 
142 
143 
144 }
145 
146 
147 #endif