00001 #ifndef GMATRIX_H 00002 #define GMATRIX_H 00003 00004 #include "GObject.h" 00005 00006 #include <iostream> 00007 #include <sstream> 00008 #include <string> 00009 00010 namespace GMathLib{ 00011 00018 class GMatrix : public GObject 00019 { 00020 public: 00025 GMatrix(); 00026 00027 00043 GMatrix(int mrow, int mcolumn, double* p_data = 0) 00044 : GObject() 00045 { 00046 allocate_initialize(mrow, mcolumn, p_data); 00047 } 00048 00060 GMatrix(std::string obj_name, int mrow, int mcolumn, double* p_data = 0) 00061 : GObject() 00062 { 00063 Object_Name(obj_name); 00064 allocate_initialize(mrow, mcolumn, p_data); 00065 } 00066 00074 virtual ~GMatrix(); 00075 00076 00080 GMatrix(const GMatrix& obj); 00081 00082 00093 virtual int Copy(const GMatrix& obj); 00094 00095 00101 virtual int Fill(double a){ 00102 if(p_data == 0){ 00103 return 1; 00104 } 00105 00106 for(int i=0; i < row; i++){ 00107 for(int j=0; j < column; j++){ 00108 Set(i, j, a); 00109 } 00110 } 00111 00112 return 0; 00113 } 00114 00115 00123 int Add(const GMatrix& a, const GMatrix& b); 00124 00125 00133 int Sub(const GMatrix& a, const GMatrix& b); 00134 00135 00143 int Multi(const GMatrix& a, const GMatrix& b); 00144 00145 00150 inline void Scalar_Product(double sc){ 00151 for(int i=0; i < row; i++){ 00152 for(int j=0; j < column; j++){ 00153 Set(i, j, sc * Get(i, j) ); 00154 } 00155 } 00156 } 00157 00158 00163 inline int Row() const{return row;} 00164 00165 00170 inline int Column() const{return column;} 00171 00172 00177 inline double* GetData(){return p_data;} 00178 00179 00186 inline double GetElement(int row_no, int column_no) const{ 00187 return p_data[column_no + row_no * column]; 00188 } 00189 00196 inline double Get(int row_no, int column_no) const{ 00197 return GetElement(row_no, column_no); 00198 } 00199 00200 00207 inline void SetElement(int row_no, int column_no, double value){ 00208 p_data[column_no + row_no * column] = value; 00209 } 00210 00211 00218 inline void Set(int row_no, int column_no, double value){ 00219 SetElement(row_no, column_no, value); 00220 } 00221 00222 virtual std::string ToString() 00223 { 00224 std::string text = GObject::ToString(); 00225 std::ostringstream oss; 00226 00227 text.append("Matrix Size: ("); 00228 oss << Row(); 00229 text.append(oss.str()); text.append(","); 00230 00231 oss.str(std::string()); 00232 oss << Column(); 00233 text.append(oss.str()); text.append(")\n"); 00234 00235 return text; 00236 } 00237 00243 void Print() { 00244 Print(-1); 00245 } 00246 00247 00255 void Print(int turn_down, bool scientific_format=false); 00256 00257 00262 inline void Vectorize(){ 00263 row = row * column; 00264 column = 1; 00265 } 00266 00274 virtual int Reshape(int new_row, int new_column); 00275 00282 inline double& operator()(int i, int j){ 00283 return p_data[j + i * column]; 00284 } 00285 00289 GMatrix operator=(const GMatrix &a); 00290 00294 GMatrix operator+(const GMatrix &a); 00295 00299 GMatrix operator-(const GMatrix &a); 00300 00304 GMatrix operator*(const GMatrix &a); 00305 00309 inline friend GMatrix operator*(double k, const GMatrix &a){ 00310 00311 GMatrix r(a.Row(), a.Column()); 00312 r.Copy(a); 00313 r.Scalar_Product(k); 00314 00315 return r; 00316 } 00317 00321 inline friend GMatrix operator*(const GMatrix &a, double k){ 00322 return k * a; 00323 } 00324 00325 protected: 00326 00333 void allocate_initialize(int mrow, int mcolumn, double* p_data = 0); 00334 00335 double* p_data; 00338 private: 00339 int row; 00340 int column; 00341 bool internal_new_flag; 00342 }; 00343 00344 } 00345 #endif 00346