SHOGUN  3.2.1
 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 
61  SGMatrix(const SGMatrix &orig);
62 
63 #ifndef SWIG // SWIG should skip this part
64 #ifdef HAVE_EIGEN3
65 
66  SGMatrix(EigenMatrixXt& mat);
67 
69  operator EigenMatrixXtMap() const;
70 #endif
71 #endif
72 
74  virtual ~SGMatrix();
75 
76 #ifndef SWIG // SWIG should skip this part
77 
81  T* get_column_vector(index_t col) const
82  {
83  const int64_t c = col;
84  return &matrix[c*num_rows];
85  }
86 
93 
99 
104  inline const T& operator()(index_t i_row, index_t i_col) const
105  {
106  const int64_t c = i_col;
107  return matrix[c*num_rows + i_row];
108  }
109 
113  inline const T& operator[](index_t index) const
114  {
115  return matrix[index];
116  }
117 
122  inline T& operator()(index_t i_row, index_t i_col)
123  {
124  const int64_t c = i_col;
125  return matrix[c*num_rows + i_row];
126  }
127 
131  inline T& operator[](index_t index)
132  {
133  return matrix[index];
134  }
135 
141  inline SGMatrix<T> get()
142  {
143  return *this;
144  }
145 
147  bool operator==(SGMatrix<T>& other);
148 
155  bool equals(SGMatrix<T>& other);
156 
158  void set_const(T const_elem);
159 
161  void zero();
162 
172  bool is_symmetric();
173 
175  T max_single();
176 
178  SGMatrix<T> clone();
179 
181  static T* clone_matrix(const T* matrix, int32_t nrows, int32_t ncols);
182 
184  static void transpose_matrix(
185  T*& matrix, int32_t& num_feat, int32_t& num_vec);
186 
188  static void create_diagonal_matrix(T* matrix, T* v,int32_t size);
189 
196 
197 #ifdef HAVE_LAPACK
198 
208 
216  static double* compute_eigenvectors(double* matrix, int n, int m);
217 
228  void compute_few_eigenvectors(double* matrix_, double*& eigenvalues, double*& eigenvectors,
229  int n, int il, int iu);
230 #endif
231 
241  bool transpose_A=false, bool transpose_B=false,
242  float64_t scale=1.0);
243 #ifdef HAVE_LAPACK
244 
245  static void inverse(SGMatrix<float64_t> matrix);
246 
250  static float64_t* pinv(
251  float64_t* matrix, int32_t rows, int32_t cols,
252  float64_t* target=NULL);
253 
254 #endif
255 
257  static float64_t trace(
258  float64_t* mat, int32_t cols, int32_t rows);
259 
261  static T* get_row_sum(T* matrix, int32_t m, int32_t n);
262 
264  static T* get_column_sum(T* matrix, int32_t m, int32_t n);
265 
267  void center();
268 
270  static void center_matrix(T* matrix, int32_t m, int32_t n);
271 
273  void remove_column_mean();
274 
276  void display_matrix(const char* name="matrix") const;
277 
279  static void display_matrix(
280  const T* matrix, int32_t rows, int32_t cols,
281  const char* name="matrix", const char* prefix="");
282 
284  static void display_matrix(
285  const SGMatrix<T> matrix, const char* name="matrix",
286  const char* prefix="");
287 
300  index_t num_cols, SGMatrix<T> pre_allocated=SGMatrix<T>());
301 
306  void load(CFile* loader);
307 
312  void save(CFile* saver);
313 #endif // #ifndef SWIG // SWIG should skip this part
314 
315  protected:
317  virtual void copy_data(const SGReferencedData &orig);
318 
320  virtual void init_data();
321 
323  virtual void free_data();
324 
325  public:
327  T* matrix;
332 };
333 }
334 #endif // __SGMATRIX_H__

SHOGUN Machine Learning Toolbox - Documentation