SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DenseMatrixOperator.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2013 Soumyajit De
8  */
9 
10 #include <shogun/lib/config.h>
11 
12 #ifdef HAVE_EIGEN3
13 #include <shogun/lib/SGVector.h>
14 #include <shogun/lib/SGMatrix.h>
15 #include <shogun/base/Parameter.h>
18 
19 using namespace Eigen;
20 
21 namespace shogun
22 {
23 
24 template<class T>
26  : CMatrixOperator<T>()
27  {
28  init();
29 
30  SG_SGCDEBUG("%s created (%p)\n", this->get_name(), this);
31  }
32 
33 template<class T>
35  : CMatrixOperator<T>(op.num_cols),
36  m_operator(op)
37  {
38  init();
39 
40  SG_SGCDEBUG("%s created (%p)\n", this->get_name(), this);
41  }
42 
43 template<class T>
45  const CDenseMatrixOperator<T>& orig)
46  : CMatrixOperator<T>(orig.get_dimension())
47  {
48  init();
49 
50  m_operator=SGMatrix<T>(orig.m_operator.num_rows, orig.m_operator.num_cols);
51  for (index_t i=0; i<m_operator.num_cols; ++i)
52  {
53  for (index_t j=0; j<m_operator.num_rows; ++j)
54  m_operator(j,i)=orig.m_operator(j,i);
55  }
56 
57  SG_SGCDEBUG("%s deep copy created (%p)\n", this->get_name(), this);
58  }
59 
60 template<class T>
62  {
63  CSGObject::set_generic<T>();
64 
65  this->m_parameters->add(&m_operator, "dense_matrix",
66  "The dense matrix of the linear operator");
67  }
68 
69 template<class T>
71  {
72  SG_SGCDEBUG("%s destroyed (%p)\n", this->get_name(), this);
73  }
74 
75 template<class T>
77  {
78  return m_operator;
79  }
80 
81 template<class T>
83  {
84  REQUIRE(m_operator.matrix, "Operator not initialized!\n");
85 
86  typedef Matrix<T, Dynamic, 1> VectorXt;
87  typedef Matrix<T, Dynamic, Dynamic> MatrixXt;
88 
89  Map<MatrixXt> _op(m_operator.matrix, m_operator.num_rows,
90  m_operator.num_cols);
91 
92  SGVector<T> diag(static_cast<int32_t>(_op.diagonalSize()));
93  Map<VectorXt> _diag(diag.vector, diag.vlen);
94  _diag=_op.diagonal();
95 
96  return diag;
97  }
98 
99 template<class T>
101  {
102  REQUIRE(m_operator.matrix, "Operator not initialized!\n");
103  REQUIRE(diag.vector, "Diagonal not initialized!\n");
104 
105  typedef Matrix<T, Dynamic, 1> VectorXt;
106  typedef Matrix<T, Dynamic, Dynamic> MatrixXt;
107 
108  Map<MatrixXt> _op(m_operator.matrix, m_operator.num_rows,
109  m_operator.num_cols);
110 
111  REQUIRE(static_cast<int32_t>(_op.diagonalSize())==diag.vlen,
112  "Dimension mismatch!\n");
113 
114  Map<VectorXt> _diag(diag.vector, diag.vlen);
115  _op.diagonal()=_diag;
116  }
117 
118 template<class T>
120  {
121  REQUIRE(m_operator.matrix, "Operator not initialized!\n");
122  REQUIRE(this->get_dimension()==b.vlen,
123  "Number of rows of vector must be equal to the "
124  "number of cols of the operator!\n");
125 
126  typedef Matrix<T, Dynamic, 1> VectorXt;
127  typedef Matrix<T, Dynamic, Dynamic> MatrixXt;
128 
129  Map<VectorXt> _b(b.vector, b.vlen);
130  Map<MatrixXt> _op(m_operator.matrix, m_operator.num_rows,
131  m_operator.num_cols);
132 
133  SGVector<T> result(m_operator.num_rows);
134  Map<VectorXt> _result(result.vector, result.vlen);
135  _result=_op*_b;
136 
137  return result;
138  }
139 
140 #define UNDEFINED(type) \
141 template<> \
142 SGVector<type> CDenseMatrixOperator<type>::apply(SGVector<type> b) const \
143  { \
144  SG_SERROR("Not supported for %s\n", #type);\
145  return b; \
146  }
147 
148 UNDEFINED(bool)
149 UNDEFINED(char)
150 UNDEFINED(int8_t)
151 UNDEFINED(uint8_t)
152 #undef UNDEFINED
153 
154 template class CDenseMatrixOperator<bool>;
155 template class CDenseMatrixOperator<char>;
156 template class CDenseMatrixOperator<int8_t>;
157 template class CDenseMatrixOperator<uint8_t>;
158 template class CDenseMatrixOperator<int16_t>;
159 template class CDenseMatrixOperator<uint16_t>;
160 template class CDenseMatrixOperator<int32_t>;
161 template class CDenseMatrixOperator<uint32_t>;
162 template class CDenseMatrixOperator<int64_t>;
163 template class CDenseMatrixOperator<uint64_t>;
164 template class CDenseMatrixOperator<float32_t>;
165 template class CDenseMatrixOperator<float64_t>;
166 template class CDenseMatrixOperator<floatmax_t>;
167 template class CDenseMatrixOperator<complex128_t>;
168 }
169 #endif // HAVE_EIGEN3

SHOGUN Machine Learning Toolbox - Documentation