Kernel Quantum Probability Library
The KQP library aims at providing tools for working with quantums probabilities
unused.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_REDUCED_SET_UNUSED_H__
19 #define __KQP_REDUCED_SET_UNUSED_H__
20 
21 #include <kqp/cleanup.hpp>
22 #include <kqp/feature_matrix.hpp>
23 #include <kqp/subset.hpp>
24 
25 namespace kqp {
26  template<typename Scalar>
27  class CleanerUnused : public Cleaner<Scalar> {
28  public:
29  KQP_SCALAR_TYPEDEFS(Scalar);
30  private:
31  template <typename T> static void _run(const FMatrixPtr &mF, T &mY) {
32  // Dimension of the problem
33  Index N = mY.rows();
34  assert(N == mF->size());
35 
36 
37  // Removes unused pre-images
38  RealVector v = mY.rowwise().squaredNorm();
39  Real threshold = v.sum() * Eigen::NumTraits<Scalar>::epsilon();
40 
41  bool change = false;
42  std::vector<bool> to_keep(N, true);
43  for(Index i = 0; i < N; i++)
44  if (v(i) < threshold) {
45  change = true;
46  to_keep[i] = false;
47  }
48 
49  if (!change) return;
50 
51  select_rows(to_keep, mY, mY);
52 
53  *mF = *mF->subset(to_keep);
54  }
55  public:
56 
57  virtual void cleanup(Decomposition<Scalar> &d) const override {
58  run(d.mX, d.mY);
59  }
60 
64  static void run(const FMatrixPtr &mF, ScalarMatrix &mY) {
66  }
67 
68  static void run(const FMatrixPtr &mF, ScalarAltMatrix &mY) {
70  }
71 
72  virtual picojson::value save() const override {
73  picojson::object json;
74  json["name"] = picojson::value("unused");
75  return picojson::value(json);
76  }
77 
78  };
79 
80 }
81 
82 #endif