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 shogun
21 {
22  template<class T> class SGVector;
23  class CFile;
24 
26 template<class T> class SGMatrix : public SGReferencedData
27 {
28  public:
30  SGMatrix();
31 
34  SGMatrix(bool ref_counting);
35 
37  SGMatrix(T* m, index_t nrows, index_t ncols, bool ref_counting=true);
38 
40  SGMatrix(index_t nrows, index_t ncols, bool ref_counting=true);
41 
43  SGMatrix(const SGMatrix &orig);
44 
46  virtual ~SGMatrix();
47 
51  T* get_column_vector(index_t col) const
52  {
53  const int64_t c = col;
54  return &matrix[c*num_rows];
55  }
56 
63 
69 
74  inline const T& operator()(index_t i_row, index_t i_col) const
75  {
76  const int64_t c = i_col;
77  return matrix[c*num_rows + i_row];
78  }
79 
83  inline const T& operator[](index_t index) const
84  {
85  return matrix[index];
86  }
87 
92  inline T& operator()(index_t i_row, index_t i_col)
93  {
94  const int64_t c = i_col;
95  return matrix[c*num_rows + i_row];
96  }
97 
101  inline T& operator[](index_t index)
102  {
103  return matrix[index];
104  }
105 
111  inline SGMatrix<T> get()
112  {
113  return *this;
114  }
115 
117  bool operator==(SGMatrix<T>& other);
118 
125  bool equals(SGMatrix<T>& other);
126 
128  void set_const(T const_elem);
129 
131  void zero();
132 
134  T max_single();
135 
137  SGMatrix<T> clone();
138 
140  static T* clone_matrix(const T* matrix, int32_t nrows, int32_t ncols);
141 
143  static void transpose_matrix(
144  T*& matrix, int32_t& num_feat, int32_t& num_vec);
145 
147  static void create_diagonal_matrix(T* matrix, T* v,int32_t size);
148 
154  static SGMatrix<T> create_identity_matrix(index_t size, T scale);
155 
156 #ifdef HAVE_LAPACK
157 
167 
175  static double* compute_eigenvectors(double* matrix, int n, int m);
176 
187  void compute_few_eigenvectors(double* matrix_, double*& eigenvalues, double*& eigenvectors,
188  int n, int il, int iu);
189 #endif
190 
200  bool transpose_A=false, bool transpose_B=false,
201  float64_t scale=1.0);
202 #ifdef HAVE_LAPACK
203 
204  static void inverse(SGMatrix<float64_t> matrix);
205 
209  static float64_t* pinv(
210  float64_t* matrix, int32_t rows, int32_t cols,
211  float64_t* target=NULL);
212 
213 #endif
214 
216  static float64_t trace(
217  float64_t* mat, int32_t cols, int32_t rows);
218 
220  static T* get_row_sum(T* matrix, int32_t m, int32_t n);
221 
223  static T* get_column_sum(T* matrix, int32_t m, int32_t n);
224 
226  void center();
227 
229  static void center_matrix(T* matrix, int32_t m, int32_t n);
230 
232  void remove_column_mean();
233 
235  void display_matrix(const char* name="matrix") const;
236 
238  static void display_matrix(
239  const T* matrix, int32_t rows, int32_t cols,
240  const char* name="matrix", const char* prefix="");
241 
243  static void display_matrix(
244  const SGMatrix<T> matrix, const char* name="matrix",
245  const char* prefix="");
246 
259  index_t num_cols, SGMatrix<T> pre_allocated=SGMatrix<T>());
260 
265  void load(CFile* loader);
266 
271  void save(CFile* saver);
272 
273  protected:
275  virtual void copy_data(const SGReferencedData &orig);
276 
278  virtual void init_data();
279 
281  virtual void free_data();
282 
283  public:
285  T* matrix;
290 };
291 }
292 #endif // __SGMATRIX_H__

SHOGUN Machine Learning Toolbox - Documentation