00001
00002
00003 template<long m, long n>
00004 inline _dgematrix dgematrix_small<m,n>::to_dgematrix() const
00005 {VERBOSE_REPORT;
00006 dgematrix mat(m,n);
00007 for(long i=0; i<m; i++){
00008 for(long j=0; j<n; j++){
00009 mat(i,j) =(*this)(i,j);
00010 }
00011 }
00012 return _(mat);
00013 }
00014
00015
00016
00017
00018
00019
00020
00021 template<long m, long n>
00022 inline double& dgematrix_small<m,n>::operator()(const long& i, const long& j)
00023 {VERBOSE_REPORT;
00024 return array[i+m*j];
00025 }
00026
00027
00028
00029 template<long m, long n>
00030 inline double dgematrix_small<m,n>::operator()(const long& i, const long& j) const
00031 {VERBOSE_REPORT;
00032 return array[i+m*j];
00033 }
00034
00035
00036
00037 template<long m, long n>
00038 inline dgematrix_small<m,n>& dgematrix_small<m,n>::set(const long& i, const long& j, const double& v)
00039 {VERBOSE_REPORT;
00040 (*this)(i,j) =v;
00041 return *this;
00042 }
00043
00044
00045
00046 template<long m, long n>
00047 inline std::ostream& operator<<(std::ostream& s, const dgematrix_small<m,n>& A)
00048 {VERBOSE_REPORT;
00049 s << std::setiosflags(std::ios::showpos);
00050 for(long i=0; i<m; i++){
00051 for(long j=0; j<n; j++){
00052 s << " " << A(i,j);
00053 }
00054 s << std::endl;
00055 }
00056 return s;
00057 }
00058
00059
00060
00061 template<long m, long n>
00062 inline void dgematrix_small<m,n>::write(const char* filename) const
00063 {VERBOSE_REPORT;
00064 std::ofstream ofs(filename, std::ios::trunc);
00065 ofs.setf(std::cout.flags());
00066 ofs.precision(std::cout.precision());
00067 ofs.width(std::cout.width());
00068 ofs.fill(std::cout.fill());
00069 ofs << "#dgematrix" << " " << m << " " << n << std::endl;
00070 for(long i=0; i<m; i++){
00071 for(long j=0; j<n; j++){
00072 ofs << (*this)()(i,j) << " ";
00073 }
00074 ofs << std::endl;
00075 }
00076 ofs.close();
00077 }
00078
00079
00080
00081 template<long m, long n>
00082 inline void dgematrix_small<m,n>::read(const char* filename)
00083 {VERBOSE_REPORT;
00084 std::ifstream s( filename );
00085 if(!s){
00086 ERROR_REPORT;
00087 std::cerr << "The file \"" << filename << "\" can not be opened." << std::endl;
00088 exit(1);
00089 }
00090
00091 std::string id;
00092 s >> id;
00093 if( id != "dgematrix" && id != "#dgematrix" ){
00094 ERROR_REPORT;
00095 std::cerr << "The type name of the file \"" << filename << "\" is not dgematrix." << std::endl
00096 << "Its type name was " << id << " ." << std::endl;
00097 exit(1);
00098 }
00099
00100 long _m, _n;
00101 s >> _m >> _n;
00102 if(m!=_m || n!=_n){
00103 ERROR_REPORT;
00104 std::cerr << "Matrix size is invalid." << std::endl;
00105 exit(1);
00106 }
00107 for(long i=0; i<m; i++){
00108 for(long j=0; j<n; j++ ){
00109 s >> operator()(i,j);
00110 }
00111 }
00112 if(s.eof()){
00113 ERROR_REPORT;
00114 std::cerr << "There is something is wrong with the file \"" << filename << "\"." << std::endl
00115 << "Most likely, there is not enough data components, or a linefeed code or space code is missing at the end of the last line." << std::endl;
00116 exit(1);
00117 }
00118
00119 s >> id;
00120 if(!s.eof()){
00121 ERROR_REPORT;
00122 std::cerr << "There is something is wrong with the file \"" << filename << "\"." << std::endl
00123 << "Most likely, there are extra data components." << std::endl;
00124 exit(1);
00125 }
00126
00127 s.close();
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 template<long m, long n>
00140 inline dgematrix_small<n,m> t(const dgematrix_small<m,n>& A)
00141 {VERBOSE_REPORT;
00142 dgematrix_small<n,m> X;
00143 for(long i=0; i<m; i++){
00144 for(long j=0; j<n; j++){
00145 X(j,i) =A(i,j);
00146 }
00147 }
00148 return X;
00149 }
00150
00151
00152
00153
00154
00155
00156
00157 template<long m, long n>
00158 inline dgematrix_small<m,n>& dgematrix_small<m,n>::zero()
00159 {VERBOSE_REPORT;
00160 for(long k=0; k<m*n; k++){ array[k]=0.; }
00161 return *this;
00162 }
00163
00164
00165
00166 template<long m, long n>
00167 inline dgematrix_small<m,n>& dgematrix_small<m,n>::identity()
00168 {VERBOSE_REPORT;
00169 zero();
00170 for(long k=0; k<std::min(m,n); k++){ (*this)(k,k)=1.; }
00171 return *this;
00172 }
00173
00174
00175
00176 template<long m, long n>
00177 inline dcovector_small<m> dgematrix_small<m,n>::col(const long& j) const
00178 {VERBOSE_REPORT;
00179 dcovector_small<m> vec;
00180 for(long i=0; i<m; i++){ vec(i)=(*this)(i,j); }
00181 return vec;
00182 }
00183
00184
00185
00186 template<long m, long n>
00187 inline drovector_small<n> dgematrix_small<m,n>::row(const long& i) const
00188 {VERBOSE_REPORT;
00189 drovector_small<n> vec;
00190 for(long j=0; j<n; j++){ vec(j)=(*this)(i,j); }
00191 return vec;
00192 }
00193
00194
00195
00196
00197
00198
00199
00200 template<long m, long n>
00201 inline dgematrix_small<m,n>& operator+=(dgematrix_small<m,n>& A, const dgematrix_small<m,n>& B)
00202 {VERBOSE_REPORT;
00203 for(long k=0; k<m*n; k++){
00204 A.array[k] +=B.array[k];
00205 }
00206 return A;
00207 }
00208
00209
00210
00211 template<long m, long n>
00212 inline dgematrix_small<m,n>& operator-=(dgematrix_small<m,n>& A, const dgematrix_small<m,n>& B)
00213 {VERBOSE_REPORT;
00214 for(long k=0; k<m*n; k++){
00215 A.array[k] -=B.array[k];
00216 }
00217 return A;
00218 }
00219
00220
00221
00222 template<long m, long l, long n>
00223 inline dgematrix_small<m,n>& operator*=(dgematrix_small<m,l>& A, const dgematrix_small<l,n>& B)
00224 {VERBOSE_REPORT;
00225 dgematrix_small<m,n> X(0.);
00226 for(long i=0; i<m; i++){
00227 for(long j=0; j<n; j++){
00228 for(long k=0; k<l; k++){
00229 X(i,j) += A(i,k)*B(k,j);
00230 }
00231 }
00232 }
00233 return X;
00234 }
00235
00236
00237
00238 template<long m, long n>
00239 inline dgematrix_small<m,n>& operator*=(dgematrix_small<m,n>& A, const double& d)
00240 {VERBOSE_REPORT;
00241 for(long k=0; k<m*n; k++){
00242 A.array[k] *=d;
00243 }
00244 return A;
00245 }
00246
00247
00248
00249 template<long m, long n>
00250 inline dgematrix_small<m,n>& operator/=(dgematrix_small<m,n>& A, const double& d)
00251 {VERBOSE_REPORT;
00252 for(long k=0; k<m*n; k++){
00253 A.array[k] /=d;
00254 }
00255 return A;
00256 }
00257
00258
00259
00260
00261
00262
00263
00264 template<long m, long n>
00265 inline const dgematrix_small<m,n>& operator+(const dgematrix_small<m,n>& A)
00266 {VERBOSE_REPORT;
00267 return A;
00268 }
00269
00270
00271
00272 template<long m, long n>
00273 inline dgematrix_small<m,n> operator-(const dgematrix_small<m,n>& A)
00274 {VERBOSE_REPORT;
00275 dgematrix_small<m,n> X;
00276 for(long i=0; i<m; i++){
00277 for(long j=0; j<n; j++){
00278 X(i,j) =-A(i,j);
00279 }
00280 }
00281 return X;
00282 }
00283
00284
00285
00286
00287
00288
00289
00290 template<long m, long n>
00291 inline dgematrix_small<m,n> operator+(const dgematrix_small<m,n>& A, const dgematrix_small<m,n>& B)
00292 {VERBOSE_REPORT;
00293 dgematrix_small<m,n> C;
00294 for(int i=0; i<m; i++){
00295 for(int j=0; j<n; j++){
00296 C(i,j) =A(i,j)+B(i,j);
00297 }
00298 }
00299 return C;
00300 }
00301
00302
00303
00304 template<long n>
00305 inline dgematrix_small<n,n> operator+(const dgematrix_small<n,n>& A, const dsymatrix_small<n>& B)
00306 {VERBOSE_REPORT;
00307 dgematrix_small<n,n> X;
00308 for(long i=0; i<n; i++){
00309 for(long j=0; j<i; j++){
00310 X(i,j) =A(i,j)+B(i,j);
00311 }
00312 for(long j=i; j<n; j++){
00313 X(i,j) =A(i,j)+B(j,i);
00314 }
00315 }
00316 return X;
00317 }
00318
00319
00320
00321 template<long m, long n>
00322 inline dgematrix_small<m,n> operator-(const dgematrix_small<m,n>& A, const dgematrix_small<m,n>& B)
00323 {VERBOSE_REPORT;
00324 dgematrix_small<m,n> C;
00325 for(int i=0; i<m; i++){
00326 for(int j=0; j<n; j++){
00327 C(i,j)=A(i,j)-B(i,j);
00328 }
00329 }
00330 return C;
00331 }
00332
00333
00334
00335 template<long n>
00336 inline dgematrix_small<n,n> operator-(const dgematrix_small<n,n>& A, const dsymatrix_small<n>& B)
00337 {VERBOSE_REPORT;
00338 dgematrix_small<n,n> X;
00339 for(long i=0; i<n; i++){
00340 for(long j=0; j<=i; j++){ X(i,j)=A(i,j)-B(i,j); }
00341 for(long j=i+1; j<n; j++){ X(i,j)=A(i,j)-B(j,i); }
00342 }
00343 return X;
00344 }
00345
00346
00347
00348 template<long m, long n>
00349 inline dcovector_small<m> operator*(const dgematrix_small<m,n>& A, const dcovector_small<n>& B)
00350 {VERBOSE_REPORT;
00351 dcovector_small<m> C(0.);
00352 for(long i=0; i<m; i++){
00353 for(long j=0; j<n; j++){
00354 C(i) +=A(i,j)*B(j);
00355 }
00356 }
00357 return C;
00358 }
00359
00360
00361
00362 template<long m, long l, long n>
00363 inline dgematrix_small<m,n> operator*(const dgematrix_small<m,l>& A, const dgematrix_small<l,n>& B)
00364 {VERBOSE_REPORT;
00365 dgematrix_small<m,n> C(0.);
00366 for(int i=0; i<m; i++){
00367 for(int j=0; j<n; j++){
00368 for(int k=0; k<l; k++){
00369 C(i,j) +=A(i,k)*B(k,j);
00370 }
00371 }
00372 }
00373 return C;
00374 }
00375
00376
00377
00378 template<long m, long n>
00379 inline dgematrix_small<m,n> operator*(const dgematrix_small<m,n>& A, const dsymatrix_small<n>& B)
00380 {VERBOSE_REPORT;
00381 dgematrix_small<m,n> X(0.);
00382 for(long i=0; i<m; i++){
00383 for(long j=0; j<n; j++){
00384 for(long k=0; k<j; k++){ X(i,j)+=A(i,k)*B(j,k); }
00385 for(long k=j; k<n; k++){ X(i,j)+=A(i,k)*B(k,j); }
00386 }
00387 }
00388 return X;
00389 }
00390
00391
00392
00393 template<long m, long n>
00394 inline dgematrix_small<m,n> operator*(const dgematrix_small<m,n>& A, const double& v)
00395 {VERBOSE_REPORT;
00396 dgematrix_small<m,n> C;
00397 for(long i=0; i<m; i++){
00398 for(long j=0; j<n; j++){
00399 C(i,j) =A(i,j)*v;
00400 }
00401 }
00402 return C;
00403 }
00404
00405
00406
00407 template<long m, long n>
00408 inline dgematrix_small<m,n> operator/(const dgematrix_small<m,n>& A, const double& v)
00409 {VERBOSE_REPORT;
00410 dgematrix_small<m,n> C;
00411 for(long i=0; i<m; i++){
00412 for(long j=0; j<n; j++){
00413 C(i,j) =A(i,j)/v;
00414 }
00415 }
00416 return C;
00417 }