Kernel Quantum Probability Library
The KQP library aims at providing tools for working with quantums probabilities
cleanup.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 #ifndef _KQP_CLEANUP_H_
18 #define _KQP_CLEANUP_H_
19 
20 #include <boost/shared_ptr.hpp>
21 
22 #include <kqp/decomposition.hpp>
23 #include <kqp/rank_selector.hpp>
24 #include <kqp/subset.hpp>
25 
26 namespace kqp {
27 
28 # include <kqp/define_header_logger.hpp>
29 DEFINE_KQP_HLOGGER("kqp.cleaner");
30 
31  class CleanerBase {
32  public:
33  CleanerBase() {}
34  virtual ~CleanerBase() {}
35  virtual picojson::value save() const = 0;
36  };
37 
38  template<typename Scalar> class Cleaner : public CleanerBase {
39  public:
40  virtual ~Cleaner() {}
41 
43  virtual void cleanup(Decomposition<Scalar> &) const {}
44 
45  };
46 
47 
52  template<typename Scalar>
53  class CleanerList : public Cleaner<Scalar> {
54  public:
55  typedef boost::shared_ptr< Cleaner<Scalar> > Ptr;
56  typedef boost::shared_ptr< const Cleaner<Scalar> > CPtr;
57 
58  virtual void cleanup(Decomposition<Scalar> &d) const override {
59  KQP_HLOG_DEBUG_F("Before cleaning: pre-images=%d, rank=%d", %d.mX->size() %d.mD.rows());
60  for(auto i = list.begin(); i != list.end(); ++i) {
61  (*i)->cleanup(d);
62 
63  KQP_HLOG_DEBUG_F("After cleaner %s: pre-images=%d, rank=%d (%d, %dx%d, %d)", %KQP_DEMANGLE(**i) %d.mX->size() %d.mD.rows() %d.mX->size() %d.mY.rows() %d.mY.cols() %d.mD.rows());
64 
65  // Sanity check
66  if (!d.check())
67  KQP_THROW_EXCEPTION_F(assertion_exception, "Decomposition in an invalid state (%d, %dx%d, %d) after cleaner %s",
68  %d.mX->size() %d.mY.rows() %d.mY.cols() %d.mD.rows() % KQP_DEMANGLE(**i));
69  }
70  }
71 
72  void add(const Ptr &item) {
73  list.push_back(item);
74  }
75 
76  virtual picojson::value save() const override {
77  picojson::object json;
78  json["name"] = picojson::value("list");
79  json["scalar"] = picojson::value(ScalarInfo<Scalar>::name());
80  picojson::array array;
81  for(auto cleaner: this->list) {
82  array.push_back(picojson::value(cleaner->save()));
83  }
84  json["list"] = picojson::value(array);
85  return picojson::value(json);
86  }
87  private:
88  std::vector< Ptr > list;
89  };
90 
95  template<typename Scalar>
96  class CleanerRank : public Cleaner<Scalar> {
97  public:
98  KQP_SCALAR_TYPEDEFS(Scalar);
99 
101  CleanerRank(const boost::shared_ptr< const Selector<Real> > &selector): selector(selector) {
102  }
103 
104  virtual void cleanup(Decomposition<Scalar> &d) const override {
105  DecompositionList<Real> list(d.mD);
106  this->selector->selection(list);
107 
108  // Remove corresponding entries
109  select_rows(list.getSelected(), d.mD, d.mD);
110 
111  // Case where mY is the identity matrix
112  if (d.mY.getTypeId() == typeid(typename AltDense<Scalar>::IdentityType)) {
113  d.mX = d.mX->subset(list.getSelected());
114  d.mY.conservativeResize(list.getRank(), list.getRank());
115  } else {
116  select_columns(list.getSelected(), d.mY, d.mY);
117  }
118 
119  }
120 
121  virtual picojson::value save() const override {
122  picojson::object json;
123  json["name"] = picojson::value("selector");
124  json["scalar"] = picojson::value(ScalarInfo<Scalar>::name());
125  json["selector"] = picojson::value(selector->save());
126  return picojson::value(json);
127  }
128 
129  private:
131  boost::shared_ptr< const Selector<Real> > selector;
132  };
133 
134 
135 }
136 
137 #endif
138 
139