SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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:
40  typedef T Scalar;
41 
43  SGMatrix();
44 
47  SGMatrix(bool ref_counting);
48 
50  SGMatrix(T* m, index_t nrows, index_t ncols, bool ref_counting=true);
51 
53  SGMatrix(T* m, index_t nrows, index_t ncols, index_t offset)
54  : SGReferencedData(false), matrix(m+offset),
55  num_rows(nrows), num_cols(ncols) { }
56 
58  SGMatrix(index_t nrows, index_t ncols, bool ref_counting=true);
59 
60 #ifndef SWIG // SWIG should skip this part
61 #if defined(HAVE_CXX0X) || defined(HAVE_CXX11)
62 
64  template <typename ST> using container_type = SGMatrix<ST>;
65 
66 #endif // define (HAVE_CXX0X) || defined(HAVE_CXX11)
67 
78  SGMatrix(SGVector<T> vec);
79 
93  SGMatrix(SGVector<T> vec, index_t nrows, index_t ncols);
94 
95 #ifdef HAVE_EIGEN3
96 
97  SGMatrix(EigenMatrixXt& mat);
98 
100  operator EigenMatrixXtMap() const;
101 #endif // HAVE_EIGEN3
102 #endif // SWIG
103 
105  SGMatrix(const SGMatrix &orig);
106 
108  virtual ~SGMatrix();
109 
110 #ifndef SWIG // SWIG should skip this part
111 
116  {
117  const int64_t c = col;
118  return &matrix[c*num_rows];
119  }
120 
127 
133 
138  inline const T& operator()(index_t i_row, index_t i_col) const
139  {
140  const int64_t c = i_col;
141  return matrix[c*num_rows + i_row];
142  }
143 
147  inline const T& operator[](index_t index) const
148  {
149  return matrix[index];
150  }
151 
156  inline T& operator()(index_t i_row, index_t i_col)
157  {
158  const int64_t c = i_col;
159  return matrix[c*num_rows + i_row];
160  }
161 
165  inline T& operator[](index_t index)
166  {
167  return matrix[index];
168  }
169 
175  inline SGMatrix<T> get()
176  {
177  return *this;
178  }
179 
181  inline T* data() const
182  {
183  return matrix;
184  }
185 
187  inline uint64_t size() const
188  {
189  const uint64_t c=num_cols;
190  return num_rows*c;
191  }
192 
194  bool operator==(SGMatrix<T>& other);
195 
202  bool equals(SGMatrix<T>& other);
203 
205  void set_const(T const_elem);
206 
208  void zero();
209 
219  bool is_symmetric();
220 
222  T max_single();
223 
225  SGMatrix<T> clone();
226 
228  static T* clone_matrix(const T* matrix, int32_t nrows, int32_t ncols);
229 
231  static void transpose_matrix(
232  T*& matrix, int32_t& num_feat, int32_t& num_vec);
233 
235  static void create_diagonal_matrix(T* matrix, T* v,int32_t size);
236 
243 
244 #ifdef HAVE_LAPACK
245 
255 
263  static double* compute_eigenvectors(double* matrix, int n, int m);
264 
275  void compute_few_eigenvectors(double* matrix_, double*& eigenvalues, double*& eigenvectors,
276  int n, int il, int iu);
277 #endif
278 
288  bool transpose_A=false, bool transpose_B=false,
289  float64_t scale=1.0);
290 #ifdef HAVE_LAPACK
291 
292  static void inverse(SGMatrix<float64_t> matrix);
293 
297  static float64_t* pinv(
298  float64_t* matrix, int32_t rows, int32_t cols,
299  float64_t* target=NULL);
300 
301 #endif
302 
304  static float64_t trace(
305  float64_t* mat, int32_t cols, int32_t rows);
306 
308  static T* get_row_sum(T* matrix, int32_t m, int32_t n);
309 
311  static T* get_column_sum(T* matrix, int32_t m, int32_t n);
312 
314  void center();
315 
317  static void center_matrix(T* matrix, int32_t m, int32_t n);
318 
320  void remove_column_mean();
321 
323  void display_matrix(const char* name="matrix") const;
324 
326  static void display_matrix(
327  const T* matrix, int32_t rows, int32_t cols,
328  const char* name="matrix", const char* prefix="");
329 
331  static void display_matrix(
332  const SGMatrix<T> matrix, const char* name="matrix",
333  const char* prefix="");
334 
347  index_t num_cols, SGMatrix<T> pre_allocated=SGMatrix<T>());
348 
353  void load(CFile* loader);
354 
359  void save(CFile* saver);
360 #endif // #ifndef SWIG // SWIG should skip this part
361 
362  protected:
364  virtual void copy_data(const SGReferencedData &orig);
365 
367  virtual void init_data();
368 
370  virtual void free_data();
371 
372  public:
374  T* matrix;
379 };
380 }
381 #endif // __SGMATRIX_H__
void display_matrix(const char *name="matrix") const
Definition: SGMatrix.cpp:394
static void center_matrix(T *matrix, int32_t m, int32_t n)
Definition: SGMatrix.cpp:352
int32_t index_t
Definition: common.h:62
T * data() const
Definition: SGMatrix.h:181
T & operator()(index_t i_row, index_t i_col)
Definition: SGMatrix.h:156
void save(CFile *saver)
Definition: SGMatrix.cpp:1073
static T * get_column_sum(T *matrix, int32_t m, int32_t n)
Definition: SGMatrix.cpp:333
virtual void free_data()
Definition: SGMatrix.cpp:1043
Definition: SGMatrix.h:20
SGMatrix< T > clone()
Definition: SGMatrix.cpp:260
static void create_diagonal_matrix(T *matrix, T *v, int32_t size)
Definition: SGMatrix.cpp:295
index_t num_cols
Definition: SGMatrix.h:378
virtual ~SGMatrix()
Definition: SGMatrix.cpp:100
const T & operator[](index_t index) const
Definition: SGMatrix.h:147
bool operator==(SGMatrix< T > &other)
Definition: SGMatrix.cpp:106
static SGMatrix< T > get_allocated_matrix(index_t num_rows, index_t num_cols, SGMatrix< T > pre_allocated=SGMatrix< T >())
Definition: SGMatrix.cpp:997
index_t num_rows
Definition: SGMatrix.h:376
static void transpose_matrix(T *&matrix, int32_t &num_feat, int32_t &num_vec)
Definition: SGMatrix.cpp:277
static float64_t * pinv(float64_t *matrix, int32_t rows, int32_t cols, float64_t *target=NULL)
Definition: SGMatrix.cpp:846
static T * clone_matrix(const T *matrix, int32_t nrows, int32_t ncols)
Definition: SGMatrix.cpp:267
static SGMatrix< float64_t > matrix_multiply(SGMatrix< float64_t > A, SGMatrix< float64_t > B, bool transpose_A=false, bool transpose_B=false, float64_t scale=1.0)
Definition: SGMatrix.cpp:947
SGVector< T > get_diagonal_vector() const
Definition: SGMatrix.cpp:1099
shogun vector
SGMatrix(T *m, index_t nrows, index_t ncols, index_t offset)
Definition: SGMatrix.h:53
void remove_column_mean()
Definition: SGMatrix.cpp:377
shogun reference count managed data
double float64_t
Definition: common.h:50
void compute_few_eigenvectors(double *matrix_, double *&eigenvalues, double *&eigenvectors, int n, int il, int iu)
Definition: SGMatrix.cpp:934
A File access base class.
Definition: File.h:34
T & operator[](index_t index)
Definition: SGMatrix.h:165
T * get_column_vector(index_t col) const
Definition: SGMatrix.h:115
static float64_t trace(float64_t *mat, int32_t cols, int32_t rows)
Definition: SGMatrix.cpp:310
virtual void copy_data(const SGReferencedData &orig)
Definition: SGMatrix.cpp:1027
bool is_symmetric()
Definition: SGMatrix.cpp:154
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
uint64_t size() const
Definition: SGMatrix.h:187
void scale(Matrix A, Matrix B, typename Matrix::Scalar alpha)
Definition: Core.h:93
static T * get_row_sum(T *matrix, int32_t m, int32_t n)
Definition: SGMatrix.cpp:320
static void inverse(SGMatrix< float64_t > matrix)
inverses square matrix in-place
Definition: SGMatrix.cpp:885
bool equals(SGMatrix< T > &other)
Definition: SGMatrix.cpp:118
SGVector< T > get_row_vector(index_t row) const
Definition: SGMatrix.cpp:1088
void set_const(T const_elem)
Definition: SGMatrix.cpp:133
static SGMatrix< T > create_identity_matrix(index_t size, T scale)
static SGVector< float64_t > compute_eigenvectors(SGMatrix< float64_t > matrix)
Definition: SGMatrix.cpp:895
virtual void init_data()
Definition: SGMatrix.cpp:1035
const T & operator()(index_t i_row, index_t i_col) const
Definition: SGMatrix.h:138
void load(CFile *loader)
Definition: SGMatrix.cpp:1052

SHOGUN Machine Learning Toolbox - Documentation