SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SGMatrix.h
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) 2011-2013 Heiko Strathmann
8  * Written (W) 2012 Fernando Jose Iglesias Garcia
9  * Written (W) 2010,2012 Soeren Sonnenburg
10  * Copyright (C) 2010 Berlin Institute of Technology
11  * Copyright (C) 2012 Soeren Sonnenburg
12  */
13 #ifndef __SGMATRIX_H__
14 #define __SGMATRIX_H__
15 
16 #include <shogun/lib/config.h>
17 #include <shogun/lib/common.h>
19 
20 namespace Eigen
21 {
22  template <class, int, int, int, int, int> class Matrix;
23  template<int, int> class Stride;
24  template <class, int, class> class Map;
25 }
26 
27 namespace shogun
28 {
29  template<class T> class SGVector;
30  class CFile;
31 
33 template<class T> class SGMatrix : public SGReferencedData
34 {
35  typedef Eigen::Matrix<T,-1,-1,0,-1,-1> EigenMatrixXt;
36  typedef Eigen::Map<EigenMatrixXt,0,Eigen::Stride<0,0> > EigenMatrixXtMap;
37 
38  public:
40  typedef T Scalar;
41 
43  SGMatrix();
44 
47  SGMatrix(bool ref_counting);
48 
50  SGMatrix(T* m, index_t nrows, index_t ncols, bool ref_counting=true);
51 
53  SGMatrix(T* m, index_t nrows, index_t ncols, index_t offset)
54  : SGReferencedData(false), matrix(m+offset),
55  num_rows(nrows), num_cols(ncols) { }
56 
58  SGMatrix(index_t nrows, index_t ncols, bool ref_counting=true);
59 
60 #ifndef SWIG // SWIG should skip this part
61 
71  SGMatrix(SGVector<T> vec);
72 
86  SGMatrix(SGVector<T> vec, index_t nrows, index_t ncols);
87 
88 #ifdef HAVE_EIGEN3
89 
90  SGMatrix(EigenMatrixXt& mat);
91 
93  operator EigenMatrixXtMap() const;
94 #endif // HAVE_EIGEN3
95 #endif // SWIG
96 
98  SGMatrix(const SGMatrix &orig);
99 
101  virtual ~SGMatrix();
102 
103 #ifndef SWIG // SWIG should skip this part
104 
109  {
110  const int64_t c = col;
111  return &matrix[c*num_rows];
112  }
113 
120 
126 
131  inline const T& operator()(index_t i_row, index_t i_col) const
132  {
133  const int64_t c = i_col;
134  return matrix[c*num_rows + i_row];
135  }
136 
140  inline const T& operator[](index_t index) const
141  {
142  return matrix[index];
143  }
144 
149  inline T& operator()(index_t i_row, index_t i_col)
150  {
151  const int64_t c = i_col;
152  return matrix[c*num_rows + i_row];
153  }
154 
158  inline T& operator[](index_t index)
159  {
160  return matrix[index];
161  }
162 
168  inline SGMatrix<T> get()
169  {
170  return *this;
171  }
172 
174  bool operator==(SGMatrix<T>& other);
175 
182  bool equals(SGMatrix<T>& other);
183 
185  void set_const(T const_elem);
186 
188  void zero();
189 
199  bool is_symmetric();
200 
202  T max_single();
203 
205  SGMatrix<T> clone();
206 
208  static T* clone_matrix(const T* matrix, int32_t nrows, int32_t ncols);
209 
211  static void transpose_matrix(
212  T*& matrix, int32_t& num_feat, int32_t& num_vec);
213 
215  static void create_diagonal_matrix(T* matrix, T* v,int32_t size);
216 
223 
224 #ifdef HAVE_LAPACK
225 
235 
243  static double* compute_eigenvectors(double* matrix, int n, int m);
244 
255  void compute_few_eigenvectors(double* matrix_, double*& eigenvalues, double*& eigenvectors,
256  int n, int il, int iu);
257 #endif
258 
268  bool transpose_A=false, bool transpose_B=false,
269  float64_t scale=1.0);
270 #ifdef HAVE_LAPACK
271 
272  static void inverse(SGMatrix<float64_t> matrix);
273 
277  static float64_t* pinv(
278  float64_t* matrix, int32_t rows, int32_t cols,
279  float64_t* target=NULL);
280 
281 #endif
282 
284  static float64_t trace(
285  float64_t* mat, int32_t cols, int32_t rows);
286 
288  static T* get_row_sum(T* matrix, int32_t m, int32_t n);
289 
291  static T* get_column_sum(T* matrix, int32_t m, int32_t n);
292 
294  void center();
295 
297  static void center_matrix(T* matrix, int32_t m, int32_t n);
298 
300  void remove_column_mean();
301 
303  void display_matrix(const char* name="matrix") const;
304 
306  static void display_matrix(
307  const T* matrix, int32_t rows, int32_t cols,
308  const char* name="matrix", const char* prefix="");
309 
311  static void display_matrix(
312  const SGMatrix<T> matrix, const char* name="matrix",
313  const char* prefix="");
314 
327  index_t num_cols, SGMatrix<T> pre_allocated=SGMatrix<T>());
328 
333  void load(CFile* loader);
334 
339  void save(CFile* saver);
340 #endif // #ifndef SWIG // SWIG should skip this part
341 
342  protected:
344  virtual void copy_data(const SGReferencedData &orig);
345 
347  virtual void init_data();
348 
350  virtual void free_data();
351 
352  public:
354  T* matrix;
359 };
360 }
361 #endif // __SGMATRIX_H__

SHOGUN Machine Learning Toolbox - Documentation