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:
39  typedef T Scalar;
40 
42  SGMatrix();
43 
46  SGMatrix(bool ref_counting);
47 
49  SGMatrix(T* m, index_t nrows, index_t ncols, bool ref_counting=true);
50 
52  SGMatrix(T* m, index_t nrows, index_t ncols, index_t offset)
53  : SGReferencedData(false), matrix(m+offset),
54  num_rows(nrows), num_cols(ncols) { }
55 
57  SGMatrix(index_t nrows, index_t ncols, bool ref_counting=true);
58 
60  SGMatrix(const SGMatrix &orig);
61 
62 #ifndef SWIG // SWIG should skip this part
63 #ifdef HAVE_EIGEN3
64 
65  SGMatrix(EigenMatrixXt& mat);
66 
68  operator EigenMatrixXtMap() const;
69 #endif
70 #endif
71 
73  virtual ~SGMatrix();
74 
78  T* get_column_vector(index_t col) const
79  {
80  const int64_t c = col;
81  return &matrix[c*num_rows];
82  }
83 
90 
96 
101  inline const T& operator()(index_t i_row, index_t i_col) const
102  {
103  const int64_t c = i_col;
104  return matrix[c*num_rows + i_row];
105  }
106 
110  inline const T& operator[](index_t index) const
111  {
112  return matrix[index];
113  }
114 
119  inline T& operator()(index_t i_row, index_t i_col)
120  {
121  const int64_t c = i_col;
122  return matrix[c*num_rows + i_row];
123  }
124 
128  inline T& operator[](index_t index)
129  {
130  return matrix[index];
131  }
132 
138  inline SGMatrix<T> get()
139  {
140  return *this;
141  }
142 
144  bool operator==(SGMatrix<T>& other);
145 
152  bool equals(SGMatrix<T>& other);
153 
155  void set_const(T const_elem);
156 
158  void zero();
159 
169  bool is_symmetric();
170 
172  T max_single();
173 
175  SGMatrix<T> clone();
176 
178  static T* clone_matrix(const T* matrix, int32_t nrows, int32_t ncols);
179 
181  static void transpose_matrix(
182  T*& matrix, int32_t& num_feat, int32_t& num_vec);
183 
185  static void create_diagonal_matrix(T* matrix, T* v,int32_t size);
186 
193 
194 #ifdef HAVE_LAPACK
195 
205 
213  static double* compute_eigenvectors(double* matrix, int n, int m);
214 
225  void compute_few_eigenvectors(double* matrix_, double*& eigenvalues, double*& eigenvectors,
226  int n, int il, int iu);
227 #endif
228 
238  bool transpose_A=false, bool transpose_B=false,
239  float64_t scale=1.0);
240 #ifdef HAVE_LAPACK
241 
242  static void inverse(SGMatrix<float64_t> matrix);
243 
247  static float64_t* pinv(
248  float64_t* matrix, int32_t rows, int32_t cols,
249  float64_t* target=NULL);
250 
251 #endif
252 
254  static float64_t trace(
255  float64_t* mat, int32_t cols, int32_t rows);
256 
258  static T* get_row_sum(T* matrix, int32_t m, int32_t n);
259 
261  static T* get_column_sum(T* matrix, int32_t m, int32_t n);
262 
264  void center();
265 
267  static void center_matrix(T* matrix, int32_t m, int32_t n);
268 
270  void remove_column_mean();
271 
273  void display_matrix(const char* name="matrix") const;
274 
276  static void display_matrix(
277  const T* matrix, int32_t rows, int32_t cols,
278  const char* name="matrix", const char* prefix="");
279 
281  static void display_matrix(
282  const SGMatrix<T> matrix, const char* name="matrix",
283  const char* prefix="");
284 
297  index_t num_cols, SGMatrix<T> pre_allocated=SGMatrix<T>());
298 
303  void load(CFile* loader);
304 
309  void save(CFile* saver);
310 
311  protected:
313  virtual void copy_data(const SGReferencedData &orig);
314 
316  virtual void init_data();
317 
319  virtual void free_data();
320 
321  public:
323  T* matrix;
328 };
329 }
330 #endif // __SGMATRIX_H__

SHOGUN Machine Learning Toolbox - Documentation