SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 #include <shogun/lib/SGVector.h>
13 #include <shogun/lib/SGMatrix.h>
14 #include <shogun/base/Parameter.h>
17 
18 using namespace Eigen;
19 
20 namespace shogun
21 {
22 
23 template<class T>
25  : CMatrixOperator<T>()
26  {
27  init();
28 
29  SG_SGCDEBUG("%s created (%p)\n", this->get_name(), this);
30  }
31 
32 template<class T>
34  : CMatrixOperator<T>(op.num_cols),
35  m_operator(op)
36  {
37  init();
38 
39  SG_SGCDEBUG("%s created (%p)\n", this->get_name(), this);
40  }
41 
42 template<class T>
44  const CDenseMatrixOperator<T>& orig)
45  : CMatrixOperator<T>(orig.get_dimension())
46  {
47  init();
48 
49  m_operator=SGMatrix<T>(orig.m_operator.num_rows, orig.m_operator.num_cols);
50  for (index_t i=0; i<m_operator.num_cols; ++i)
51  {
52  for (index_t j=0; j<m_operator.num_rows; ++j)
53  m_operator(j,i)=orig.m_operator(j,i);
54  }
55 
56  SG_SGCDEBUG("%s deep copy created (%p)\n", this->get_name(), this);
57  }
58 
59 template<class T>
61  {
62  CSGObject::set_generic<T>();
63 
64  this->m_parameters->add(&m_operator, "dense_matrix",
65  "The dense matrix of the linear operator");
66  }
67 
68 template<class T>
70  {
71  SG_SGCDEBUG("%s destroyed (%p)\n", this->get_name(), this);
72  }
73 
74 template<class T>
76  {
77  return m_operator;
78  }
79 
80 template<class T>
82  {
83  REQUIRE(m_operator.matrix, "Operator not initialized!\n");
84 
85  typedef Matrix<T, Dynamic, 1> VectorXt;
86  typedef Matrix<T, Dynamic, Dynamic> MatrixXt;
87 
88  Map<MatrixXt> _op(m_operator.matrix, m_operator.num_rows,
89  m_operator.num_cols);
90 
91  SGVector<T> diag(static_cast<int32_t>(_op.diagonalSize()));
92  Map<VectorXt> _diag(diag.vector, diag.vlen);
93  _diag=_op.diagonal();
94 
95  return diag;
96  }
97 
98 template<class T>
100  {
101  REQUIRE(m_operator.matrix, "Operator not initialized!\n");
102  REQUIRE(diag.vector, "Diagonal not initialized!\n");
103 
104  typedef Matrix<T, Dynamic, 1> VectorXt;
105  typedef Matrix<T, Dynamic, Dynamic> MatrixXt;
106 
107  Map<MatrixXt> _op(m_operator.matrix, m_operator.num_rows,
108  m_operator.num_cols);
109 
110  REQUIRE(static_cast<int32_t>(_op.diagonalSize())==diag.vlen,
111  "Dimension mismatch!\n");
112 
113  Map<VectorXt> _diag(diag.vector, diag.vlen);
114  _op.diagonal()=_diag;
115  }
116 
117 template<class T>
119  {
120  REQUIRE(m_operator.matrix, "Operator not initialized!\n");
121  REQUIRE(this->get_dimension()==b.vlen,
122  "Number of rows of vector must be equal to the "
123  "number of cols of the operator!\n");
124 
125  typedef Matrix<T, Dynamic, 1> VectorXt;
126  typedef Matrix<T, Dynamic, Dynamic> MatrixXt;
127 
128  Map<VectorXt> _b(b.vector, b.vlen);
129  Map<MatrixXt> _op(m_operator.matrix, m_operator.num_rows,
130  m_operator.num_cols);
131 
132  SGVector<T> result(m_operator.num_rows);
133  Map<VectorXt> _result(result.vector, result.vlen);
134  _result=_op*_b;
135 
136  return result;
137  }
138 
139 #define UNDEFINED(type) \
140 template<> \
141 SGVector<type> CDenseMatrixOperator<type>::apply(SGVector<type> b) const \
142  { \
143  SG_SERROR("Not supported for %s\n", #type);\
144  return b; \
145  }
146 
147 UNDEFINED(bool)
148 UNDEFINED(char)
149 UNDEFINED(int8_t)
150 UNDEFINED(uint8_t)
151 #undef UNDEFINED
152 
153 template class CDenseMatrixOperator<bool>;
154 template class CDenseMatrixOperator<char>;
155 template class CDenseMatrixOperator<int8_t>;
156 template class CDenseMatrixOperator<uint8_t>;
157 template class CDenseMatrixOperator<int16_t>;
158 template class CDenseMatrixOperator<uint16_t>;
159 template class CDenseMatrixOperator<int32_t>;
160 template class CDenseMatrixOperator<uint32_t>;
161 template class CDenseMatrixOperator<int64_t>;
162 template class CDenseMatrixOperator<uint64_t>;
163 template class CDenseMatrixOperator<float32_t>;
164 template class CDenseMatrixOperator<float64_t>;
165 template class CDenseMatrixOperator<floatmax_t>;
166 template class CDenseMatrixOperator<complex128_t>;
167 }
SGMatrix< T > get_matrix_operator() const
int32_t index_t
Definition: common.h:62
Definition: SGMatrix.h:20
#define REQUIRE(x,...)
Definition: SGIO.h:206
#define UNDEFINED(type)
virtual SGVector< T > apply(SGVector< T > b) const
shogun matrix
index_t vlen
Definition: SGVector.h:492
shogun vector
virtual void set_diagonal(SGVector< T > diag)
virtual SGVector< T > get_diagonal() const
Class that represents a dense-matrix linear operator. It computes matrix-vector product in its apply...
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
#define SG_SGCDEBUG(...)
Definition: SGIO.h:163
virtual const char * get_name() const
Abstract base class that represents a matrix linear operator. It provides an interface to computes ma...

SHOGUN Machine Learning Toolbox - Documentation