SHOGUN
v2.0.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) 2012 Fernando José Iglesias García 00008 * Written (W) 2010,2012 Soeren Sonnenburg 00009 * Copyright (C) 2010 Berlin Institute of Technology 00010 * Copyright (C) 2012 Soeren Sonnenburg 00011 */ 00012 #ifndef __SGMATRIX_H__ 00013 #define __SGMATRIX_H__ 00014 00015 #include <shogun/lib/config.h> 00016 #include <shogun/lib/DataType.h> 00017 #include <shogun/lib/SGReferencedData.h> 00018 00019 namespace shogun 00020 { 00021 template<class T> class SGVector; 00022 template<class T> class SGMatrixList; 00024 template<class T> class SGMatrix : public SGReferencedData 00025 { 00026 public: 00028 SGMatrix() : SGReferencedData() 00029 { 00030 init_data(); 00031 } 00032 00034 SGMatrix(T* m, index_t nrows, index_t ncols, bool ref_counting=true) 00035 : SGReferencedData(ref_counting), matrix(m), 00036 num_rows(nrows), num_cols(ncols) { } 00037 00039 SGMatrix(index_t nrows, index_t ncols, bool ref_counting=true) 00040 : SGReferencedData(ref_counting), num_rows(nrows), num_cols(ncols) 00041 { 00042 matrix=SG_MALLOC(T, ((int64_t) nrows)*ncols); 00043 } 00044 00046 SGMatrix(const SGMatrix &orig) : SGReferencedData(orig) 00047 { 00048 copy_data(orig); 00049 } 00050 00052 virtual ~SGMatrix() 00053 { 00054 unref(); 00055 } 00056 00060 T* get_column_vector(index_t col) const 00061 { 00062 return &matrix[col*num_rows]; 00063 } 00064 00069 inline const T& operator()(index_t i_row, index_t i_col) const 00070 { 00071 return matrix[i_col*num_rows + i_row]; 00072 } 00073 00077 inline const T& operator[](index_t index) const 00078 { 00079 return matrix[index]; 00080 } 00081 00086 inline T& operator()(index_t i_row, index_t i_col) 00087 { 00088 return matrix[i_col*num_rows + i_row]; 00089 } 00090 00094 inline T& operator[](index_t index) 00095 { 00096 return matrix[index]; 00097 } 00098 00100 void set_const(T const_elem) 00101 { 00102 for (index_t i=0; i<num_rows*num_cols; i++) 00103 matrix[i]=const_elem ; 00104 } 00105 00107 void zero() 00108 { 00109 if (matrix && (num_rows*num_cols)) 00110 set_const(0); 00111 } 00112 00114 SGMatrix<T> clone() 00115 { 00116 return SGMatrix<T>(clone_matrix(matrix, num_rows, num_cols), 00117 num_rows, num_cols); 00118 } 00119 00121 static T* clone_matrix(const T* matrix, int32_t nrows, int32_t ncols) 00122 { 00123 T* result = SG_MALLOC(T, int64_t(nrows)*ncols); 00124 for (int64_t i=0; i<int64_t(nrows)*ncols; i++) 00125 result[i]=matrix[i]; 00126 00127 return result; 00128 } 00129 00131 static void transpose_matrix( 00132 T*& matrix, int32_t& num_feat, int32_t& num_vec); 00133 00135 static void create_diagonal_matrix(T* matrix, T* v,int32_t size) 00136 { 00137 for(int32_t i=0;i<size;i++) 00138 { 00139 for(int32_t j=0;j<size;j++) 00140 { 00141 if(i==j) 00142 matrix[j*size+i]=v[i]; 00143 else 00144 matrix[j*size+i]=0; 00145 } 00146 } 00147 } 00148 00154 static SGMatrix<T> create_identity_matrix(index_t size, T scale); 00155 00167 static SGMatrix<float64_t> create_centering_matrix(index_t size); 00168 00169 #ifdef HAVE_LAPACK 00170 00178 static SGVector<float64_t> compute_eigenvectors( 00179 SGMatrix<float64_t> matrix); 00180 00188 static double* compute_eigenvectors(double* matrix, int n, int m); 00189 00200 void compute_few_eigenvectors(double* matrix_, double*& eigenvalues, double*& eigenvectors, 00201 int n, int il, int iu); 00202 #endif 00203 00211 static SGMatrix<float64_t> matrix_multiply( 00212 SGMatrix<float64_t> A, SGMatrix<float64_t> B, 00213 bool transpose_A=false, bool transpose_B=false, 00214 float64_t scale=1.0); 00215 #ifdef HAVE_LAPACK 00216 00217 static void inverse(SGMatrix<float64_t> matrix); 00218 00222 static float64_t* pinv( 00223 float64_t* matrix, int32_t rows, int32_t cols, 00224 float64_t* target=NULL); 00225 00226 #endif 00227 00229 static inline float64_t trace( 00230 float64_t* mat, int32_t cols, int32_t rows) 00231 { 00232 float64_t trace=0; 00233 for (int32_t i=0; i<rows; i++) 00234 trace+=mat[i*cols+i]; 00235 return trace; 00236 } 00237 00239 static T* get_row_sum(T* matrix, int32_t m, int32_t n) 00240 { 00241 T* rowsums=SG_CALLOC(T, n); 00242 00243 for (int32_t i=0; i<n; i++) 00244 { 00245 for (int32_t j=0; j<m; j++) 00246 rowsums[i]+=matrix[j+int64_t(i)*m]; 00247 } 00248 return rowsums; 00249 } 00250 00252 static T* get_column_sum(T* matrix, int32_t m, int32_t n) 00253 { 00254 T* colsums=SG_CALLOC(T, m); 00255 00256 for (int32_t i=0; i<n; i++) 00257 { 00258 for (int32_t j=0; j<m; j++) 00259 colsums[j]+=matrix[j+int64_t(i)*m]; 00260 } 00261 return colsums; 00262 } 00263 00265 void center() 00266 { 00267 center_matrix(matrix, num_rows, num_cols); 00268 } 00269 00271 static void center_matrix(T* matrix, int32_t m, int32_t n); 00272 00274 void remove_column_mean(); 00275 00277 void display_matrix(const char* name="matrix") const; 00278 00280 static void display_matrix( 00281 const T* matrix, int32_t rows, int32_t cols, 00282 const char* name="matrix", const char* prefix=""); 00283 00285 static void display_matrix( 00286 const SGMatrix<T> matrix, const char* name="matrix", 00287 const char* prefix=""); 00288 00300 static SGMatrix<T> get_allocated_matrix(index_t num_rows, 00301 index_t num_cols, SGMatrix<T> pre_allocated=SGMatrix<T>()); 00302 00303 protected: 00305 virtual void copy_data(const SGReferencedData &orig) 00306 { 00307 matrix=((SGMatrix*)(&orig))->matrix; 00308 num_rows=((SGMatrix*)(&orig))->num_rows; 00309 num_cols=((SGMatrix*)(&orig))->num_cols; 00310 } 00311 00313 virtual void init_data() 00314 { 00315 matrix=NULL; 00316 num_rows=0; 00317 num_cols=0; 00318 } 00319 00321 virtual void free_data() 00322 { 00323 SG_FREE(matrix); 00324 matrix=NULL; 00325 num_rows=0; 00326 num_cols=0; 00327 } 00328 00329 public: 00331 T* matrix; 00333 index_t num_rows; 00335 index_t num_cols; 00336 }; 00337 } 00338 #endif // __SGMATRIX_H__