SHOGUN
v3.2.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2011-2013 Heiko Strathmann 00008 * Written (W) 2012 Fernando Jose Iglesias Garcia 00009 * Written (W) 2010,2012 Soeren Sonnenburg 00010 * Copyright (C) 2010 Berlin Institute of Technology 00011 * Copyright (C) 2012 Soeren Sonnenburg 00012 */ 00013 #ifndef __SGMATRIX_H__ 00014 #define __SGMATRIX_H__ 00015 00016 #include <shogun/lib/config.h> 00017 #include <shogun/lib/common.h> 00018 #include <shogun/lib/DataType.h> 00019 #include <shogun/lib/SGReferencedData.h> 00020 00021 namespace shogun 00022 { 00023 template<class T> class SGVector; 00024 class CFile; 00025 00027 template<class T> class SGMatrix : public SGReferencedData 00028 { 00029 public: 00031 SGMatrix(); 00032 00035 SGMatrix(bool ref_counting); 00036 00038 SGMatrix(T* m, index_t nrows, index_t ncols, bool ref_counting=true); 00039 00041 SGMatrix(index_t nrows, index_t ncols, bool ref_counting=true); 00042 00044 SGMatrix(const SGMatrix &orig); 00045 00047 virtual ~SGMatrix(); 00048 00052 T* get_column_vector(index_t col) const 00053 { 00054 const int64_t c = col; 00055 return &matrix[c*num_rows]; 00056 } 00057 00063 SGVector<T> get_row_vector(index_t row) const; 00064 00069 SGVector<T> get_diagonal_vector() const; 00070 00075 inline const T& operator()(index_t i_row, index_t i_col) const 00076 { 00077 const int64_t c = i_col; 00078 return matrix[c*num_rows + i_row]; 00079 } 00080 00084 inline const T& operator[](index_t index) const 00085 { 00086 return matrix[index]; 00087 } 00088 00093 inline T& operator()(index_t i_row, index_t i_col) 00094 { 00095 const int64_t c = i_col; 00096 return matrix[c*num_rows + i_row]; 00097 } 00098 00102 inline T& operator[](index_t index) 00103 { 00104 return matrix[index]; 00105 } 00106 00112 inline SGMatrix<T> get() 00113 { 00114 return *this; 00115 } 00116 00118 bool operator==(SGMatrix<T>& other); 00119 00126 bool equals(SGMatrix<T>& other); 00127 00129 void set_const(T const_elem); 00130 00132 void zero(); 00133 00135 T max_single(); 00136 00138 SGMatrix<T> clone(); 00139 00141 static T* clone_matrix(const T* matrix, int32_t nrows, int32_t ncols); 00142 00144 static void transpose_matrix( 00145 T*& matrix, int32_t& num_feat, int32_t& num_vec); 00146 00148 static void create_diagonal_matrix(T* matrix, T* v,int32_t size); 00149 00155 static SGMatrix<T> create_identity_matrix(index_t size, T scale); 00156 00168 static SGMatrix<float64_t> create_centering_matrix(index_t size); 00169 00170 #ifdef HAVE_LAPACK 00171 00179 static SGVector<float64_t> compute_eigenvectors( 00180 SGMatrix<float64_t> matrix); 00181 00189 static double* compute_eigenvectors(double* matrix, int n, int m); 00190 00201 void compute_few_eigenvectors(double* matrix_, double*& eigenvalues, double*& eigenvectors, 00202 int n, int il, int iu); 00203 #endif 00204 00212 static SGMatrix<float64_t> matrix_multiply( 00213 SGMatrix<float64_t> A, SGMatrix<float64_t> B, 00214 bool transpose_A=false, bool transpose_B=false, 00215 float64_t scale=1.0); 00216 #ifdef HAVE_LAPACK 00217 00218 static void inverse(SGMatrix<float64_t> matrix); 00219 00223 static float64_t* pinv( 00224 float64_t* matrix, int32_t rows, int32_t cols, 00225 float64_t* target=NULL); 00226 00227 #endif 00228 00230 static float64_t trace( 00231 float64_t* mat, int32_t cols, int32_t rows); 00232 00234 static T* get_row_sum(T* matrix, int32_t m, int32_t n); 00235 00237 static T* get_column_sum(T* matrix, int32_t m, int32_t n); 00238 00240 void center(); 00241 00243 static void center_matrix(T* matrix, int32_t m, int32_t n); 00244 00246 void remove_column_mean(); 00247 00249 void display_matrix(const char* name="matrix") const; 00250 00252 static void display_matrix( 00253 const T* matrix, int32_t rows, int32_t cols, 00254 const char* name="matrix", const char* prefix=""); 00255 00257 static void display_matrix( 00258 const SGMatrix<T> matrix, const char* name="matrix", 00259 const char* prefix=""); 00260 00272 static SGMatrix<T> get_allocated_matrix(index_t num_rows, 00273 index_t num_cols, SGMatrix<T> pre_allocated=SGMatrix<T>()); 00274 00279 void load(CFile* loader); 00280 00285 void save(CFile* saver); 00286 00287 protected: 00289 virtual void copy_data(const SGReferencedData &orig); 00290 00292 virtual void init_data(); 00293 00295 virtual void free_data(); 00296 00297 public: 00299 T* matrix; 00301 index_t num_rows; 00303 index_t num_cols; 00304 }; 00305 } 00306 #endif // __SGMATRIX_H__