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 #ifdef HAVE_EIGEN3
22 #endif
23 
24 namespace shogun
25 {
26  template<class T> class SGVector;
27  class CFile;
28 
30 template<class T> class SGMatrix : public SGReferencedData
31 {
32  public:
33  typedef T Scalar;
34 
36  SGMatrix();
37 
40  SGMatrix(bool ref_counting);
41 
43  SGMatrix(T* m, index_t nrows, index_t ncols, bool ref_counting=true);
44 
46  SGMatrix(T* m, index_t nrows, index_t ncols, index_t offset)
47  : SGReferencedData(false), matrix(m+offset),
48  num_rows(nrows), num_cols(ncols) { }
49 
51  SGMatrix(index_t nrows, index_t ncols, bool ref_counting=true);
52 
54  SGMatrix(const SGMatrix &orig);
55 
56 #ifndef SWIG // SWIG should skip this part
57 #ifdef HAVE_EIGEN3
58 
59  template <class Derived>
60  SGMatrix(Eigen::PlainObjectBase<Derived>& mat)
61  : SGReferencedData(false), matrix(mat.data()),
62  num_rows(mat.rows()), num_cols(mat.cols()) { }
63 
65  operator Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> >() const
66  {
67  return Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> >(
69  }
70 #endif
71 #endif
72 
74  virtual ~SGMatrix();
75 
79  T* get_column_vector(index_t col) const
80  {
81  const int64_t c = col;
82  return &matrix[c*num_rows];
83  }
84 
91 
97 
102  inline const T& operator()(index_t i_row, index_t i_col) const
103  {
104  const int64_t c = i_col;
105  return matrix[c*num_rows + i_row];
106  }
107 
111  inline const T& operator[](index_t index) const
112  {
113  return matrix[index];
114  }
115 
120  inline T& operator()(index_t i_row, index_t i_col)
121  {
122  const int64_t c = i_col;
123  return matrix[c*num_rows + i_row];
124  }
125 
129  inline T& operator[](index_t index)
130  {
131  return matrix[index];
132  }
133 
139  inline SGMatrix<T> get()
140  {
141  return *this;
142  }
143 
145  bool operator==(SGMatrix<T>& other);
146 
153  bool equals(SGMatrix<T>& other);
154 
156  void set_const(T const_elem);
157 
159  void zero();
160 
170  bool is_symmetric();
171 
173  T max_single();
174 
176  SGMatrix<T> clone();
177 
179  static T* clone_matrix(const T* matrix, int32_t nrows, int32_t ncols);
180 
182  static void transpose_matrix(
183  T*& matrix, int32_t& num_feat, int32_t& num_vec);
184 
186  static void create_diagonal_matrix(T* matrix, T* v,int32_t size);
187 
194 
195 #ifdef HAVE_LAPACK
196 
206 
214  static double* compute_eigenvectors(double* matrix, int n, int m);
215 
226  void compute_few_eigenvectors(double* matrix_, double*& eigenvalues, double*& eigenvectors,
227  int n, int il, int iu);
228 #endif
229 
239  bool transpose_A=false, bool transpose_B=false,
240  float64_t scale=1.0);
241 #ifdef HAVE_LAPACK
242 
243  static void inverse(SGMatrix<float64_t> matrix);
244 
248  static float64_t* pinv(
249  float64_t* matrix, int32_t rows, int32_t cols,
250  float64_t* target=NULL);
251 
252 #endif
253 
255  static float64_t trace(
256  float64_t* mat, int32_t cols, int32_t rows);
257 
259  static T* get_row_sum(T* matrix, int32_t m, int32_t n);
260 
262  static T* get_column_sum(T* matrix, int32_t m, int32_t n);
263 
265  void center();
266 
268  static void center_matrix(T* matrix, int32_t m, int32_t n);
269 
271  void remove_column_mean();
272 
274  void display_matrix(const char* name="matrix") const;
275 
277  static void display_matrix(
278  const T* matrix, int32_t rows, int32_t cols,
279  const char* name="matrix", const char* prefix="");
280 
282  static void display_matrix(
283  const SGMatrix<T> matrix, const char* name="matrix",
284  const char* prefix="");
285 
298  index_t num_cols, SGMatrix<T> pre_allocated=SGMatrix<T>());
299 
304  void load(CFile* loader);
305 
310  void save(CFile* saver);
311 
312  protected:
314  virtual void copy_data(const SGReferencedData &orig);
315 
317  virtual void init_data();
318 
320  virtual void free_data();
321 
322  public:
324  T* matrix;
329 };
330 }
331 #endif // __SGMATRIX_H__

SHOGUN Machine Learning Toolbox - Documentation