planc
Parallel Lowrank Approximation with Non-negativity Constraints
nnls.hpp
Go to the documentation of this file.
1 /* Copyright 2016 Ramakrishnan Kannan */
2 
3 #ifndef NNLS_NNLS_HPP_
4 #define NNLS_NNLS_HPP_
5 #include "utils.hpp"
6 
7 // #ifndef _VERBOSE
8 // #define _VERBOSE 1;
9 // #endif
10 
11 template <class MATTYPE, class VECTYPE>
12 class NNLS {
13  protected:
14  MATTYPE CtC; // input matrix is pxq. Hence CtC is qxq.
15  VECTYPE Ctb; // right hand side vector b is px1. Hence Ctb is qx1.
16  MATTYPE CtB; // multiple RHS B is pxr. Hence CtB is qxr.
17  UINT p, q, r; // dimension of matrix.
18  VECTYPE x; // solution vector qx1;
19  MATTYPE X; // solution matrix qxr;
20  // If true The C matrix is CtC and b vector is Ctb.
21  bool inputProd;
22  bool cleared;
23  public:
24  NNLS(MATTYPE& inputMat, VECTYPE& rhs, bool prodSent) {
25  this->inputProd = prodSent;
26  if (inputProd) {
27  this->CtC = inputMat;
28  this->Ctb = rhs;
29  this->q = rhs.n_rows;
30  } else {
31  this->CtC = inputMat.t() * inputMat;
32  this->Ctb = inputMat.t() * rhs;
33  this->p = inputMat.n_rows;
34  this->q = inputMat.n_cols;
35  }
36  this->r = 1;
37  x.zeros(this->q);
38 #ifdef _VERBOSE
39  INFO << "NNLS::Constructor with RHS vector" << endl;
40 #endif
41  this->cleared = false;
42  }
43  NNLS(MATTYPE& inputMat, MATTYPE& RHS, bool prodSent) {
44  this->inputProd = prodSent;
45  if (this->inputProd) {
46  this->CtC = inputMat;
47  // bug raised by oguz
48  if (RHS.n_cols==1){
49  // user has called RHS as mattype instead
50  // of vec type. Take just the first col
51  // of CtB in this case.
52  this->Ctb = RHS.col(0);
53  } else {
54  this->CtB = RHS;
55  }
56  this->q = RHS.n_rows;
57  } else {
58  this->CtC = inputMat.t() * inputMat;
59  this->CtB = inputMat.t() * RHS;
60  this->p = inputMat.n_rows;
61  this->q = inputMat.n_cols;
62  }
63  this->r = RHS.n_cols;
64  X.resize(this->q, this->r);
65  X.zeros();
66 #ifdef _VERBOSE
67  INFO << "NNLS::Constructor with multiple RHS vector" << "r=" << r << endl;
68 #endif
69  this->cleared = false;
70  }
71  ~NNLS() {
72  }
73 
74  virtual int solveNNLS() = 0;
75 
76  VECTYPE getSolutionVector() {
77  return this->x;
78  }
79  MATTYPE getSolutionMatrix() {
80  return this->X;
81  }
82  void clear() {
83  if (!this->cleared) {
84  this->CtC.clear();
85  this->Ctb.clear();
86  this->CtB.clear();
87  this->x.clear();
88  this->X.clear();
89  this->cleared = true;
90  }
91  }
92 };
93 #endif // NNLS_NNLS_HPP_
MATTYPE getSolutionMatrix()
Definition: nnls.hpp:79
NNLS(MATTYPE &inputMat, MATTYPE &RHS, bool prodSent)
Definition: nnls.hpp:43
NNLS(MATTYPE &inputMat, VECTYPE &rhs, bool prodSent)
Definition: nnls.hpp:24
#define INFO
Definition: utils.h:36
VECTYPE getSolutionVector()
Definition: nnls.hpp:76
Definition: nnls.hpp:12
~NNLS()
Definition: nnls.hpp:71
unsigned int UINT
Definition: utils.h:68
void clear()
Definition: nnls.hpp:82
virtual int solveNNLS()=0