SHOGUN  v3.0.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>
18 #include <shogun/lib/DataType.h>
20 
21 namespace shogun
22 {
23  template<class T> class SGVector;
24  class CFile;
25 
27 template<class T> class SGMatrix : public SGReferencedData
28 {
29  public:
31  SGMatrix();
32 
35  SGMatrix(bool ref_counting);
36 
38  SGMatrix(T* m, index_t nrows, index_t ncols, bool ref_counting=true);
39 
41  SGMatrix(index_t nrows, index_t ncols, bool ref_counting=true);
42 
44  SGMatrix(const SGMatrix &orig);
45 
47  virtual ~SGMatrix();
48 
52  T* get_column_vector(index_t col) const
53  {
54  return &matrix[col*num_rows];
55  }
56 
63 
69 
74  inline const T& operator()(index_t i_row, index_t i_col) const
75  {
76  return matrix[i_col*num_rows + i_row];
77  }
78 
82  inline const T& operator[](index_t index) const
83  {
84  return matrix[index];
85  }
86 
91  inline T& operator()(index_t i_row, index_t i_col)
92  {
93  return matrix[i_col*num_rows + i_row];
94  }
95 
99  inline T& operator[](index_t index)
100  {
101  return matrix[index];
102  }
103 
109  inline SGMatrix<T> get()
110  {
111  return *this;
112  }
113 
115  bool operator==(SGMatrix<T>& other);
116 
123  bool equals(SGMatrix<T>& other);
124 
126  void set_const(T const_elem);
127 
129  void zero();
130 
132  T max_single();
133 
135  SGMatrix<T> clone();
136 
138  static T* clone_matrix(const T* matrix, int32_t nrows, int32_t ncols);
139 
141  static void transpose_matrix(
142  T*& matrix, int32_t& num_feat, int32_t& num_vec);
143 
145  static void create_diagonal_matrix(T* matrix, T* v,int32_t size);
146 
152  static SGMatrix<T> create_identity_matrix(index_t size, T scale);
153 
166 
167 #ifdef HAVE_LAPACK
168 
178 
186  static double* compute_eigenvectors(double* matrix, int n, int m);
187 
198  void compute_few_eigenvectors(double* matrix_, double*& eigenvalues, double*& eigenvectors,
199  int n, int il, int iu);
200 #endif
201 
211  bool transpose_A=false, bool transpose_B=false,
212  float64_t scale=1.0);
213 #ifdef HAVE_LAPACK
214 
215  static void inverse(SGMatrix<float64_t> matrix);
216 
220  static float64_t* pinv(
221  float64_t* matrix, int32_t rows, int32_t cols,
222  float64_t* target=NULL);
223 
224 #endif
225 
227  static float64_t trace(
228  float64_t* mat, int32_t cols, int32_t rows);
229 
231  static T* get_row_sum(T* matrix, int32_t m, int32_t n);
232 
234  static T* get_column_sum(T* matrix, int32_t m, int32_t n);
235 
237  void center();
238 
240  static void center_matrix(T* matrix, int32_t m, int32_t n);
241 
243  void remove_column_mean();
244 
246  void display_matrix(const char* name="matrix") const;
247 
249  static void display_matrix(
250  const T* matrix, int32_t rows, int32_t cols,
251  const char* name="matrix", const char* prefix="");
252 
254  static void display_matrix(
255  const SGMatrix<T> matrix, const char* name="matrix",
256  const char* prefix="");
257 
270  index_t num_cols, SGMatrix<T> pre_allocated=SGMatrix<T>());
271 
276  void load(CFile* loader);
277 
282  void save(CFile* saver);
283 
284  protected:
286  virtual void copy_data(const SGReferencedData &orig);
287 
289  virtual void init_data();
290 
292  virtual void free_data();
293 
294  public:
296  T* matrix;
301 };
302 }
303 #endif // __SGMATRIX_H__

SHOGUN Machine Learning Toolbox - Documentation