Kernel Quantum Probability Library
The KQP library aims at providing tools for working with quantums probabilities
dense_direct.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_DENSE_DIRECT_BUILDER_H__
19 #define __KQP_DENSE_DIRECT_BUILDER_H__
20 
21 #include <boost/shared_ptr.hpp>
22 
23 #include <kqp/kqp.hpp>
24 
25 
26 #include <Eigen/Eigenvalues>
27 
28 #include <kqp/kernel_evd.hpp>
29 #include <kqp/feature_matrix/dense.hpp>
30 #include <kqp/evd_utils.hpp>
31 
32 #include <cassert>
33 
34 namespace kqp {
35 
41  template <class Scalar> class DenseDirectBuilder : public KernelEVD<Scalar> {
42  public:
43  KQP_SCALAR_TYPEDEFS(Scalar);
44  typedef Dense<Scalar> FDense;
45 
46  DenseDirectBuilder(int dimension) : KernelEVD<Scalar>(DenseSpace<Scalar>::create(dimension)), matrix(dimension, dimension) {
47  reset();
48  }
49 
50  virtual ~DenseDirectBuilder() {}
51 
52  void reset() {
53  matrix.setConstant(0);
55  }
56 
57  protected:
58 
59 
60  virtual void _add(Real alpha, const FMatrixCPtr &mX, const ScalarAltMatrix &mA) override {
61  rankUpdate2(matrix.template selfadjointView<Eigen::Lower>(), kqp::our_dynamic_cast<const FDense &>(*mX).getMatrix() * mA, (Scalar)alpha);
62  }
63 
64  virtual Decomposition<Scalar> _getDecomposition() const override {
65  Decomposition<Scalar> d(this->getFSpace());
66  Eigen::SelfAdjointEigenSolver<ScalarMatrix> evd(matrix.template selfadjointView<Eigen::Lower>());
67 
68  ScalarAltMatrix _mX;
69  kqp::ThinEVD<ScalarMatrix>::run(evd, _mX, d.mD);
70 
71  d.mX = FMatrixPtr(new Dense<Scalar>(std::move(ScalarMatrix(_mX))));
72  d.mY = Eigen::Identity<Scalar>(d.mX->size(), d.mX->size());
73  return d;
74  }
75 
76 
77  public:
78 
79  ScalarMatrix matrix;
80  };
81 
82 } // end namespace kqp
83 
84 #endif