SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MeanEigen3.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2016 Pan Deng
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * The views and conclusions contained in the software and documentation are those
26  * of the authors and should not be interpreted as representing official policies,
27  * either expressed or implied, of the Shogun Development Team.
28  */
29 
30 #ifndef MEAN_EIGEN_H_
31 #define MEAN_EIGEN_H_
32 
33 #include <shogun/lib/config.h>
34 #include <shogun/lib/SGVector.h>
35 #include <shogun/lib/SGMatrix.h>
37 
39 #include <numeric>
40 
41 namespace shogun
42 {
43 
44 namespace linalg
45 {
46 
47 namespace implementation
48 {
49 
50 #ifndef SWIG // SWIG should skip this part
51 
55 template <typename inputType>
56 struct int2float
57 {
59  using floatType = inputType;
60 };
61 
65 template <>
66 struct int2float<int32_t>
67 {
70 };
71 
75 template <>
76 struct int2float<int64_t>
77 {
80 };
81 #endif
82 
86 template <enum Backend, class Matrix>
87 struct mean
88 {
90  typedef typename Matrix::Scalar T;
91 
94 
101  static ReturnType compute(Matrix a);
102 
110  static ReturnType compute(Matrix a, bool no_diag=false);
111 };
112 
116 template <enum Backend, class Matrix>
118 {
120  typedef typename Matrix::Scalar T;
121 
124 
132  static SGVector<ReturnDataType> compute(SGMatrix<T> m, bool no_diag);
133 
141  static void compute(SGMatrix<T> mat, SGVector<ReturnDataType> result, bool no_diag);
142 };
143 
148 template <class Matrix>
149 struct mean<Backend::EIGEN3, Matrix>
150 {
152  typedef typename Matrix::Scalar T;
153 
156 
163  static ReturnType compute(SGVector<T> vec)
164  {
165  REQUIRE(vec.vlen > 0, "Vector cannot be empty!\n");
166  return (vector_sum<Backend::EIGEN3, SGVector<T> >::compute(vec)
167  / ReturnType(vec.vlen));
168  }
169 
177  static ReturnType compute(SGMatrix<T> mat, bool no_diag=false)
178  {
179  REQUIRE(mat.num_rows > 0, "Matrix row number cannot be zero!\n");
180  if (no_diag)
181  {
182  if (mat.num_rows > mat.num_cols)
183  {
184  return (sum<Backend::EIGEN3, SGMatrix<T> >::compute(mat, no_diag)
185  / ReturnType(mat.num_rows * mat.num_cols - mat.num_cols));
186  }
187  else
188  {
189  return (sum<Backend::EIGEN3, SGMatrix<T> >::compute(mat, no_diag)
190  / ReturnType(mat.num_rows * mat.num_cols - mat.num_rows));
191  }
192  }
193  else
194  {
195  return (sum<Backend::EIGEN3, SGMatrix<T> >::compute(mat, no_diag)
196  / ReturnType(mat.num_rows * mat.num_cols));
197  }
198  }
199 };
200 
201 
206 template <class Matrix>
207 struct rowwise_mean<Backend::EIGEN3, Matrix>
208 {
210  typedef typename Matrix::Scalar T;
211 
214 
217 
226  {
228  compute(m, result, no_diag);
229  return result;
230  }
231 
239  static void compute(SGMatrix<T> mat, SGVector<ReturnDataType> result, bool no_diag)
240  {
241  REQUIRE(mat.num_cols > 0, "Matrix column number cannot be zero!\n");
242 
243  Eigen::Map<MatrixXt> m = mat;
244  SGVector<T> temp(m.rows());
246 
247  if (!no_diag)
248  for (index_t i = 0; i < m.rows(); ++i)
249  result[i] = temp[i] / ReturnDataType(m.cols());
250  else if (m.rows() <= m.cols())
251  for (index_t i = 0; i < m.rows(); ++i)
252  result[i] = temp[i] / ReturnDataType(m.cols() - 1);
253  else
254  {
255  for (index_t i = 0; i < m.cols(); ++i)
256  result[i] = temp[i] / ReturnDataType(m.cols() - 1);
257  for (index_t i = m.cols(); i < m.rows(); ++i)
258  result[i] = temp[i] / ReturnDataType(m.cols());
259  }
260  }
261 
262 };
263 
264 }
265 
266 }
267 
268 }
269 
270 #endif
static ReturnType compute(Matrix a)
Generic class rowwise_sum which provides a static compute method. This class is specialized for diffe...
Definition: Sum.h:178
int32_t index_t
Definition: common.h:62
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixXt
Definition: MeanEigen3.h:216
#define REQUIRE(x,...)
Definition: SGIO.h:206
Generic class mean which provides a static compute method.
Definition: MeanEigen3.h:87
index_t num_cols
Definition: SGMatrix.h:376
index_t num_rows
Definition: SGMatrix.h:374
Generic class sum which provides a static compute method. This class is specialized for different typ...
Definition: Sum.h:69
shogun matrix
index_t vlen
Definition: SGVector.h:494
int2float< T >::floatType ReturnDataType
Definition: MeanEigen3.h:123
shogun vector
int2float< T >::floatType ReturnType
Definition: MeanEigen3.h:93
static SGVector< ReturnDataType > compute(SGMatrix< T > m, bool no_diag)
Definition: MeanEigen3.h:225
double float64_t
Definition: common.h:50
Generic class rowwise_mean which provides a static compute method.
Definition: MeanEigen3.h:117
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
Generic class vector_sum which provides a static compute method. This class is specialized for differ...
Definition: VectorSum.h:65
static void compute(SGMatrix< T > mat, SGVector< ReturnDataType > result, bool no_diag)
Definition: MeanEigen3.h:239
static ReturnType compute(SGMatrix< T > mat, bool no_diag=false)
Definition: MeanEigen3.h:177
Generic class int2float which converts different types of integer into float64 type.
Definition: MeanEigen3.h:56
static SGVector< ReturnDataType > compute(SGMatrix< T > m, bool no_diag)

SHOGUN Machine Learning Toolbox - Documentation