SHOGUN  4.2.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  public:
36  typedef Eigen::Matrix<T,-1,-1,0,-1,-1> EigenMatrixXt;
38 
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 
96  SGMatrix(EigenMatrixXt& mat);
97 
99  operator EigenMatrixXtMap() const;
100 #endif // SWIG
101 
103  SGMatrix(const SGMatrix &orig);
104 
106  virtual ~SGMatrix();
107 
108 #ifndef SWIG // SWIG should skip this part
109 
114  {
115  const int64_t c = col;
116  return &matrix[c*num_rows];
117  }
118 
125 
131 
136  inline const T& operator()(index_t i_row, index_t i_col) const
137  {
138  const int64_t c = i_col;
139  return matrix[c*num_rows + i_row];
140  }
141 
145  inline const T& operator[](index_t index) const
146  {
147  return matrix[index];
148  }
149 
154  inline T& operator()(index_t i_row, index_t i_col)
155  {
156  const int64_t c = i_col;
157  return matrix[c*num_rows + i_row];
158  }
159 
163  inline T& operator[](index_t index)
164  {
165  return matrix[index];
166  }
167 
173  inline SGMatrix<T> get()
174  {
175  return *this;
176  }
177 
179  inline T* data() const
180  {
181  return matrix;
182  }
183 
185  inline uint64_t size() const
186  {
187  const uint64_t c=num_cols;
188  return num_rows*c;
189  }
190 
192  bool operator==(SGMatrix<T>& other);
193 
200  bool equals(SGMatrix<T>& other);
201 
203  void set_const(T const_elem);
204 
206  void zero();
207 
217  bool is_symmetric();
218 
220  T max_single();
221 
223  SGMatrix<T> clone();
224 
226  static T* clone_matrix(const T* matrix, int32_t nrows, int32_t ncols);
227 
229  static void transpose_matrix(
230  T*& matrix, int32_t& num_feat, int32_t& num_vec);
231 
233  static void create_diagonal_matrix(T* matrix, T* v,int32_t size);
234 
241 
242 #ifdef HAVE_LAPACK
243 
253 
261  static double* compute_eigenvectors(double* matrix, int n, int m);
262 
273  void compute_few_eigenvectors(double* matrix_, double*& eigenvalues, double*& eigenvectors,
274  int n, int il, int iu);
275 #endif
276 
286  bool transpose_A=false, bool transpose_B=false,
287  float64_t scale=1.0);
288 #ifdef HAVE_LAPACK
289 
290  static void inverse(SGMatrix<float64_t> matrix);
291 
295  static float64_t* pinv(
296  float64_t* matrix, int32_t rows, int32_t cols,
297  float64_t* target=NULL);
298 
299 #endif
300 
302  static float64_t trace(
303  float64_t* mat, int32_t cols, int32_t rows);
304 
306  static T* get_row_sum(T* matrix, int32_t m, int32_t n);
307 
309  static T* get_column_sum(T* matrix, int32_t m, int32_t n);
310 
312  void center();
313 
315  static void center_matrix(T* matrix, int32_t m, int32_t n);
316 
318  void remove_column_mean();
319 
321  void display_matrix(const char* name="matrix") const;
322 
324  static void display_matrix(
325  const T* matrix, int32_t rows, int32_t cols,
326  const char* name="matrix", const char* prefix="");
327 
329  static void display_matrix(
330  const SGMatrix<T> matrix, const char* name="matrix",
331  const char* prefix="");
332 
345  index_t num_cols, SGMatrix<T> pre_allocated=SGMatrix<T>());
346 
351  void load(CFile* loader);
352 
357  void save(CFile* saver);
358 #endif // #ifndef SWIG // SWIG should skip this part
359 
360  protected:
362  virtual void copy_data(const SGReferencedData &orig);
363 
365  virtual void init_data();
366 
368  virtual void free_data();
369 
370  public:
372  T* matrix;
377 };
378 }
379 #endif // __SGMATRIX_H__
void display_matrix(const char *name="matrix") const
Definition: SGMatrix.cpp:390
Eigen::Map< EigenMatrixXt, 0, Eigen::Stride< 0, 0 > > EigenMatrixXtMap
Definition: SGMatrix.h:37
static void center_matrix(T *matrix, int32_t m, int32_t n)
Definition: SGMatrix.cpp:348
int32_t index_t
Definition: common.h:62
T * data() const
Definition: SGMatrix.h:179
T & operator()(index_t i_row, index_t i_col)
Definition: SGMatrix.h:154
void save(CFile *saver)
Definition: SGMatrix.cpp:1069
static T * get_column_sum(T *matrix, int32_t m, int32_t n)
Definition: SGMatrix.cpp:329
virtual void free_data()
Definition: SGMatrix.cpp:1039
Definition: SGMatrix.h:20
SGMatrix< T > clone()
Definition: SGMatrix.cpp:256
static void create_diagonal_matrix(T *matrix, T *v, int32_t size)
Definition: SGMatrix.cpp:291
Eigen::Matrix< T,-1,-1, 0,-1,-1 > EigenMatrixXt
Definition: SGMatrix.h:36
index_t num_cols
Definition: SGMatrix.h:376
virtual ~SGMatrix()
Definition: SGMatrix.cpp:96
const T & operator[](index_t index) const
Definition: SGMatrix.h:145
bool operator==(SGMatrix< T > &other)
Definition: SGMatrix.cpp:102
static SGMatrix< T > get_allocated_matrix(index_t num_rows, index_t num_cols, SGMatrix< T > pre_allocated=SGMatrix< T >())
Definition: SGMatrix.cpp:993
index_t num_rows
Definition: SGMatrix.h:374
static void transpose_matrix(T *&matrix, int32_t &num_feat, int32_t &num_vec)
Definition: SGMatrix.cpp:273
static float64_t * pinv(float64_t *matrix, int32_t rows, int32_t cols, float64_t *target=NULL)
Definition: SGMatrix.cpp:842
static T * clone_matrix(const T *matrix, int32_t nrows, int32_t ncols)
Definition: SGMatrix.cpp:263
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:943
SGVector< T > get_diagonal_vector() const
Definition: SGMatrix.cpp:1095
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:373
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:930
A File access base class.
Definition: File.h:34
T & operator[](index_t index)
Definition: SGMatrix.h:163
T * get_column_vector(index_t col) const
Definition: SGMatrix.h:113
static float64_t trace(float64_t *mat, int32_t cols, int32_t rows)
Definition: SGMatrix.cpp:306
virtual void copy_data(const SGReferencedData &orig)
Definition: SGMatrix.cpp:1023
bool is_symmetric()
Definition: SGMatrix.cpp:150
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
uint64_t size() const
Definition: SGMatrix.h:185
void scale(Matrix A, Matrix B, typename Matrix::Scalar alpha)
Definition: Core.h:94
static T * get_row_sum(T *matrix, int32_t m, int32_t n)
Definition: SGMatrix.cpp:316
static void inverse(SGMatrix< float64_t > matrix)
inverses square matrix in-place
Definition: SGMatrix.cpp:881
bool equals(SGMatrix< T > &other)
Definition: SGMatrix.cpp:114
SGVector< T > get_row_vector(index_t row) const
Definition: SGMatrix.cpp:1084
void set_const(T const_elem)
Definition: SGMatrix.cpp:129
static SGMatrix< T > create_identity_matrix(index_t size, T scale)
static SGVector< float64_t > compute_eigenvectors(SGMatrix< float64_t > matrix)
Definition: SGMatrix.cpp:891
virtual void init_data()
Definition: SGMatrix.cpp:1031
const T & operator()(index_t i_row, index_t i_col) const
Definition: SGMatrix.h:136
void load(CFile *loader)
Definition: SGMatrix.cpp:1048

SHOGUN Machine Learning Toolbox - Documentation