SHOGUN  v3.1.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>
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  const int64_t c = col;
55  return &matrix[c*num_rows];
56  }
57 
64 
70 
75  inline const T& operator()(index_t i_row, index_t i_col) const
76  {
77  const int64_t c = i_col;
78  return matrix[c*num_rows + i_row];
79  }
80 
84  inline const T& operator[](index_t index) const
85  {
86  return matrix[index];
87  }
88 
93  inline T& operator()(index_t i_row, index_t i_col)
94  {
95  const int64_t c = i_col;
96  return matrix[c*num_rows + i_row];
97  }
98 
102  inline T& operator[](index_t index)
103  {
104  return matrix[index];
105  }
106 
112  inline SGMatrix<T> get()
113  {
114  return *this;
115  }
116 
118  bool operator==(SGMatrix<T>& other);
119 
126  bool equals(SGMatrix<T>& other);
127 
129  void set_const(T const_elem);
130 
132  void zero();
133 
135  T max_single();
136 
138  SGMatrix<T> clone();
139 
141  static T* clone_matrix(const T* matrix, int32_t nrows, int32_t ncols);
142 
144  static void transpose_matrix(
145  T*& matrix, int32_t& num_feat, int32_t& num_vec);
146 
148  static void create_diagonal_matrix(T* matrix, T* v,int32_t size);
149 
155  static SGMatrix<T> create_identity_matrix(index_t size, T scale);
156 
169 
170 #ifdef HAVE_LAPACK
171 
181 
189  static double* compute_eigenvectors(double* matrix, int n, int m);
190 
201  void compute_few_eigenvectors(double* matrix_, double*& eigenvalues, double*& eigenvectors,
202  int n, int il, int iu);
203 #endif
204 
214  bool transpose_A=false, bool transpose_B=false,
215  float64_t scale=1.0);
216 #ifdef HAVE_LAPACK
217 
218  static void inverse(SGMatrix<float64_t> matrix);
219 
223  static float64_t* pinv(
224  float64_t* matrix, int32_t rows, int32_t cols,
225  float64_t* target=NULL);
226 
227 #endif
228 
230  static float64_t trace(
231  float64_t* mat, int32_t cols, int32_t rows);
232 
234  static T* get_row_sum(T* matrix, int32_t m, int32_t n);
235 
237  static T* get_column_sum(T* matrix, int32_t m, int32_t n);
238 
240  void center();
241 
243  static void center_matrix(T* matrix, int32_t m, int32_t n);
244 
246  void remove_column_mean();
247 
249  void display_matrix(const char* name="matrix") const;
250 
252  static void display_matrix(
253  const T* matrix, int32_t rows, int32_t cols,
254  const char* name="matrix", const char* prefix="");
255 
257  static void display_matrix(
258  const SGMatrix<T> matrix, const char* name="matrix",
259  const char* prefix="");
260 
273  index_t num_cols, SGMatrix<T> pre_allocated=SGMatrix<T>());
274 
279  void load(CFile* loader);
280 
285  void save(CFile* saver);
286 
287  protected:
289  virtual void copy_data(const SGReferencedData &orig);
290 
292  virtual void init_data();
293 
295  virtual void free_data();
296 
297  public:
299  T* matrix;
304 };
305 }
306 #endif // __SGMATRIX_H__
T & operator[](index_t index)
Definition: SGMatrix.h:102
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:833
SGMatrix< T > clone()
Definition: SGMatrix.cpp:130
SGVector< T > get_row_vector(index_t row) const
Definition: SGMatrix.cpp:967
bool equals(SGMatrix< T > &other)
Definition: SGMatrix.cpp:74
static T * get_row_sum(T *matrix, int32_t m, int32_t n)
Definition: SGMatrix.cpp:190
void display_matrix(const char *name="matrix") const
Definition: SGMatrix.cpp:264
int32_t index_t
Definition: common.h:60
static void transpose_matrix(T *&matrix, int32_t &num_feat, int32_t &num_vec)
Definition: SGMatrix.cpp:147
void load(CFile *loader)
Definition: SGMatrix.cpp:931
static float64_t * pinv(float64_t *matrix, int32_t rows, int32_t cols, float64_t *target=NULL)
Definition: SGMatrix.cpp:732
static T * get_column_sum(T *matrix, int32_t m, int32_t n)
Definition: SGMatrix.cpp:203
void save(CFile *saver)
Definition: SGMatrix.cpp:952
virtual void init_data()
Definition: SGMatrix.cpp:914
static SGVector< float64_t > compute_eigenvectors(SGMatrix< float64_t > matrix)
Definition: SGMatrix.cpp:781
static T * clone_matrix(const T *matrix, int32_t nrows, int32_t ncols)
Definition: SGMatrix.cpp:137
shogun reference count managed data
double float64_t
Definition: common.h:48
static SGMatrix< T > create_identity_matrix(index_t size, T scale)
A File access base class.
Definition: File.h:34
bool operator==(SGMatrix< T > &other)
Definition: SGMatrix.cpp:62
const T & operator[](index_t index) const
Definition: SGMatrix.h:84
static void create_diagonal_matrix(T *matrix, T *v, int32_t size)
Definition: SGMatrix.cpp:165
index_t num_rows
Definition: SGMatrix.h:301
virtual ~SGMatrix()
Definition: SGMatrix.cpp:56
const T & operator()(index_t i_row, index_t i_col) const
Definition: SGMatrix.h:75
void remove_column_mean()
Definition: SGMatrix.cpp:247
static float64_t trace(float64_t *mat, int32_t cols, int32_t rows)
Definition: SGMatrix.cpp:180
index_t num_cols
Definition: SGMatrix.h:303
static void inverse(SGMatrix< float64_t > matrix)
inverses square matrix in-place
Definition: SGMatrix.cpp:771
void compute_few_eigenvectors(double *matrix_, double *&eigenvalues, double *&eigenvectors, int n, int il, int iu)
Definition: SGMatrix.cpp:820
static SGMatrix< float64_t > create_centering_matrix(index_t size)
Definition: SGMatrix.cpp:707
T & operator()(index_t i_row, index_t i_col)
Definition: SGMatrix.h:93
void set_const(T const_elem)
Definition: SGMatrix.cpp:89
static SGMatrix< T > get_allocated_matrix(index_t num_rows, index_t num_cols, SGMatrix< T > pre_allocated=SGMatrix< T >())
Definition: SGMatrix.cpp:876
SGVector< T > get_diagonal_vector() const
Definition: SGMatrix.cpp:978
virtual void copy_data(const SGReferencedData &orig)
Definition: SGMatrix.cpp:906
T * get_column_vector(index_t col) const
Definition: SGMatrix.h:52
static void center_matrix(T *matrix, int32_t m, int32_t n)
Definition: SGMatrix.cpp:222
virtual void free_data()
Definition: SGMatrix.cpp:922

SHOGUN Machine Learning Toolbox - Documentation