SHOGUN  v2.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) 2012 Fernando José Iglesias García
8  * Written (W) 2010,2012 Soeren Sonnenburg
9  * Copyright (C) 2010 Berlin Institute of Technology
10  * Copyright (C) 2012 Soeren Sonnenburg
11  */
12 #ifndef __SGMATRIX_H__
13 #define __SGMATRIX_H__
14 
15 #include <shogun/lib/config.h>
16 #include <shogun/lib/DataType.h>
18 
19 namespace shogun
20 {
21  template<class T> class SGVector;
22  template<class T> class SGMatrixList;
24 template<class T> class SGMatrix : public SGReferencedData
25 {
26  public:
29  {
30  init_data();
31  }
32 
34  SGMatrix(T* m, index_t nrows, index_t ncols, bool ref_counting=true)
35  : SGReferencedData(ref_counting), matrix(m),
36  num_rows(nrows), num_cols(ncols) { }
37 
39  SGMatrix(index_t nrows, index_t ncols, bool ref_counting=true)
40  : SGReferencedData(ref_counting), num_rows(nrows), num_cols(ncols)
41  {
42  matrix=SG_MALLOC(T, ((int64_t) nrows)*ncols);
43  }
44 
46  SGMatrix(const SGMatrix &orig) : SGReferencedData(orig)
47  {
48  copy_data(orig);
49  }
50 
52  virtual ~SGMatrix()
53  {
54  unref();
55  }
56 
60  T* get_column_vector(index_t col) const
61  {
62  return &matrix[col*num_rows];
63  }
64 
69  inline const T& operator()(index_t i_row, index_t i_col) const
70  {
71  return matrix[i_col*num_rows + i_row];
72  }
73 
77  inline const T& operator[](index_t index) const
78  {
79  return matrix[index];
80  }
81 
86  inline T& operator()(index_t i_row, index_t i_col)
87  {
88  return matrix[i_col*num_rows + i_row];
89  }
90 
94  inline T& operator[](index_t index)
95  {
96  return matrix[index];
97  }
98 
100  void set_const(T const_elem)
101  {
102  for (index_t i=0; i<num_rows*num_cols; i++)
103  matrix[i]=const_elem ;
104  }
105 
107  void zero()
108  {
109  if (matrix && (num_rows*num_cols))
110  set_const(0);
111  }
112 
115  {
117  num_rows, num_cols);
118  }
119 
121  static T* clone_matrix(const T* matrix, int32_t nrows, int32_t ncols)
122  {
123  T* result = SG_MALLOC(T, int64_t(nrows)*ncols);
124  for (int64_t i=0; i<int64_t(nrows)*ncols; i++)
125  result[i]=matrix[i];
126 
127  return result;
128  }
129 
131  static void transpose_matrix(
132  T*& matrix, int32_t& num_feat, int32_t& num_vec);
133 
135  static void create_diagonal_matrix(T* matrix, T* v,int32_t size)
136  {
137  for(int32_t i=0;i<size;i++)
138  {
139  for(int32_t j=0;j<size;j++)
140  {
141  if(i==j)
142  matrix[j*size+i]=v[i];
143  else
144  matrix[j*size+i]=0;
145  }
146  }
147  }
148 
154  static SGMatrix<T> create_identity_matrix(index_t size, T scale);
155 
168 
169 #ifdef HAVE_LAPACK
170 
180 
188  static double* compute_eigenvectors(double* matrix, int n, int m);
189 
200  void compute_few_eigenvectors(double* matrix_, double*& eigenvalues, double*& eigenvectors,
201  int n, int il, int iu);
202 #endif
203 
213  bool transpose_A=false, bool transpose_B=false,
214  float64_t scale=1.0);
215 #ifdef HAVE_LAPACK
216 
217  static void inverse(SGMatrix<float64_t> matrix);
218 
222  static float64_t* pinv(
223  float64_t* matrix, int32_t rows, int32_t cols,
224  float64_t* target=NULL);
225 
226 #endif
227 
229  static inline float64_t trace(
230  float64_t* mat, int32_t cols, int32_t rows)
231  {
232  float64_t trace=0;
233  for (int32_t i=0; i<rows; i++)
234  trace+=mat[i*cols+i];
235  return trace;
236  }
237 
239  static T* get_row_sum(T* matrix, int32_t m, int32_t n)
240  {
241  T* rowsums=SG_CALLOC(T, n);
242 
243  for (int32_t i=0; i<n; i++)
244  {
245  for (int32_t j=0; j<m; j++)
246  rowsums[i]+=matrix[j+int64_t(i)*m];
247  }
248  return rowsums;
249  }
250 
252  static T* get_column_sum(T* matrix, int32_t m, int32_t n)
253  {
254  T* colsums=SG_CALLOC(T, m);
255 
256  for (int32_t i=0; i<n; i++)
257  {
258  for (int32_t j=0; j<m; j++)
259  colsums[j]+=matrix[j+int64_t(i)*m];
260  }
261  return colsums;
262  }
263 
265  void center()
266  {
268  }
269 
271  static void center_matrix(T* matrix, int32_t m, int32_t n);
272 
274  void remove_column_mean();
275 
277  void display_matrix(const char* name="matrix") const;
278 
280  static void display_matrix(
281  const T* matrix, int32_t rows, int32_t cols,
282  const char* name="matrix", const char* prefix="");
283 
285  static void display_matrix(
286  const SGMatrix<T> matrix, const char* name="matrix",
287  const char* prefix="");
288 
301  index_t num_cols, SGMatrix<T> pre_allocated=SGMatrix<T>());
302 
303  protected:
305  virtual void copy_data(const SGReferencedData &orig)
306  {
307  matrix=((SGMatrix*)(&orig))->matrix;
308  num_rows=((SGMatrix*)(&orig))->num_rows;
309  num_cols=((SGMatrix*)(&orig))->num_cols;
310  }
311 
313  virtual void init_data()
314  {
315  matrix=NULL;
316  num_rows=0;
317  num_cols=0;
318  }
319 
321  virtual void free_data()
322  {
323  SG_FREE(matrix);
324  matrix=NULL;
325  num_rows=0;
326  num_cols=0;
327  }
328 
329  public:
331  T* matrix;
336 };
337 }
338 #endif // __SGMATRIX_H__

SHOGUN Machine Learning Toolbox - Documentation