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