00001
00002
00003 template<long n>
00004 inline zgematrix_small<n,n> zhematrix_small<n>::to_zgematrix_small() const
00005 {VERBOSE_REPORT;
00006 zgematrix_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 zhematrix zhematrix_small<n>::to_zhematrix() const
00018 {VERBOSE_REPORT;
00019 zhematrix 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 comple& zhematrix_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 comple zhematrix_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 zhematrix_small<n>& zhematrix_small<n>::set(const long& i, const long& j, const comple& 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 zhematrix_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 zhematrix_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 << "#zhematrix" << " " << 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 zhematrix_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 != "zhematrix" && id != "#zhematrix" ){
00127 ERROR_REPORT;
00128 std::cerr << "The type name of the file \"" << filename << "\" is not zhematrix." << 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 zhematrix_small<n>& zhematrix_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 zhematrix_small<n>& zhematrix_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 zhematrix_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 comple damax(const zhematrix_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 zhematrix_small<n>& operator+=(zhematrix_small<n>& A, const zhematrix_small<n>& B)
00228 {VERBOSE_REPORT;
00229 for(long k=0; k<(n*(n+1))/2; k++){
00230 A.array[k] +=B.array[k];
00231 }
00232 return A;
00233 }
00234
00235
00236
00237 template<long n>
00238 inline zhematrix_small<n>& operator-=(zhematrix_small<n>& A, const zhematrix_small<n>& B)
00239 {VERBOSE_REPORT;
00240 for(long k=0; k<(n*(n+1))/2; k++){
00241 A.array[k] -=B.array[k];
00242 }
00243 return A;
00244 }
00245
00246
00247
00248 template<long n>
00249 inline zhematrix_small<n>& operator*=(zhematrix_small<n>& A, const double& v)
00250 {VERBOSE_REPORT;
00251 for(long i=0; i<n; i++){
00252 for(long j=0; j<=i; j++){
00253 A(i,j) *=v;
00254 }
00255 }
00256 return A;
00257 }
00258
00259
00260
00261 template<long n>
00262 inline zhematrix_small<n>& operator/=(zhematrix_small<n>& A, const double& v)
00263 {VERBOSE_REPORT;
00264 for(long i=0; i<n; i++){
00265 for(long j=0; j<=i; j++){
00266 A(i,j) /=v;
00267 }
00268 }
00269 return A;
00270 }
00271
00272
00273
00274
00275
00276
00277
00278 template<long n>
00279 inline const zhematrix_small<n>& operator+(const zhematrix_small<n>& A)
00280 {VERBOSE_REPORT;
00281 return A;
00282 }
00283
00284
00285
00286 template<long n>
00287 inline zhematrix_small<n> operator-(const zhematrix_small<n>& A)
00288 {VERBOSE_REPORT;
00289 zhematrix_small<n> X;
00290 for(long i=0; i<n; i++){
00291 for(long j=0; j<=i; j++){
00292 X(i,j) =-A(i,j);
00293 }
00294 }
00295 return X;
00296 }
00297
00298
00299
00300
00301
00302
00303
00304 template<long n>
00305 inline zgematrix_small<n,n> operator+(const zhematrix_small<n>& A, const zgematrix_small<n,n>& B)
00306 {VERBOSE_REPORT;
00307 zgematrix_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(j,i)+B(i,j);
00314 }
00315 }
00316 return X;
00317 }
00318
00319
00320
00321 template<long n>
00322 inline zhematrix_small<n> operator+(const zhematrix_small<n>& A, const zhematrix_small<n>& B)
00323 {VERBOSE_REPORT;
00324 zhematrix_small<n> X;
00325 for(long i=0; i<n; i++){
00326 for(long j=0; j<=i; j++){
00327 X(i,j) =A(i,j)+B(i,j);
00328 }
00329 }
00330 return X;
00331 }
00332
00333
00334
00335
00336 template<long n>
00337 inline zgematrix_small<n,n> operator-(const zhematrix_small<n>& A, const zgematrix_small<n,n>& B)
00338 {VERBOSE_REPORT;
00339 zgematrix_small<n,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 for(long j=i; j<n; j++){
00345 X(i,j) =A(j,i)-B(i,j);
00346 }
00347 }
00348 return X;
00349 }
00350
00351
00352
00353 template<long n>
00354 inline zhematrix_small<n> operator-(const zhematrix_small<n>& A, const zhematrix_small<n>& B)
00355 {VERBOSE_REPORT;
00356 zhematrix_small<n> X;
00357 for(long i=0; i<n; i++){
00358 for(long j=0; j<=i; j++){
00359 X(i,j) =A(i,j)-B(i,j);
00360 }
00361 }
00362 return X;
00363 }
00364
00365
00366
00367 template<long n>
00368 inline zcovector_small<n> operator*(const zhematrix_small<n>& A, const zcovector_small<n>& B)
00369 {VERBOSE_REPORT;
00370 zcovector_small<n> C(0.);
00371 for(long i=0; i<n; i++){
00372 for(long j=0; j<i; j++){
00373 C(i) +=A(i,j)*B(j);
00374 }
00375 for(long j=i; j<n; j++){
00376 C(i) +=A(j,i)*B(j);
00377 }
00378 }
00379 return C;
00380 }
00381
00382
00383
00384 template<long m, long n>
00385 inline zgematrix_small<m,n> operator*(const zhematrix_small<m>& A, const zgematrix_small<m,n>& B)
00386 {VERBOSE_REPORT;
00387 zgematrix_small<m,n> X(0.);
00388 for(long i=0; i<m; i++){
00389 for(long j=0; j<n; j++){
00390 for(long k=0; k<i; k++){
00391 X(i,j) +=A(i,k)*B(k,j);
00392 }
00393 for(long k=i; k<m; k++){
00394 X(i,j) +=A(k,i)*B(k,j);
00395 }
00396 }
00397 }
00398 return X;
00399 }
00400
00401
00402
00403 template<long n>
00404 inline zgematrix_small<n,n> operator*(const zhematrix_small<n>& A, const zhematrix_small<n>& B)
00405 {VERBOSE_REPORT;
00406 zgematrix_small<n,n> X(0.);
00407 for(long i=0; i<n; i++){
00408 for(long j=0; j<i; j++){
00409 for(long k=0; k<j; k++){
00410 X(i,j) +=A(i,k)*B(j,k);
00411 }
00412 for(long k=j; k<i; k++){
00413 X(i,j) +=A(i,k)*B(k,j);
00414 }
00415 for(long k=i; k<n; k++){
00416 X(i,j) +=A(k,i)*B(k,j);
00417 }
00418 }
00419 for(long j=i; j<n; j++){
00420 for(long k=0; k<i; k++){
00421 X(i,j) +=A(i,k)*B(j,k);
00422 }
00423 for(long k=i; k<j; k++){
00424 X(i,j) +=A(k,i)*B(j,k);
00425 }
00426 for(long k=j; k<n; k++){
00427 X(i,j) +=A(k,i)*B(k,j);
00428 }
00429 }
00430 }
00431 return X;
00432 }
00433
00434
00435
00436 template<long n>
00437 inline zhematrix_small<n> operator*(const zhematrix_small<n>& A, const double& v)
00438 {VERBOSE_REPORT;
00439 zhematrix_small<n> C;
00440 for(long i=0; i<n; i++){
00441 for(long j=0; j<=i; j++){
00442 C(i,j) =A(i,j)*v;
00443 }
00444 }
00445 return C;
00446 }
00447
00448
00449
00450 template<long n>
00451 inline zhematrix_small<n> operator/(const zhematrix_small<n>& A, const double& v)
00452 {VERBOSE_REPORT;
00453 zhematrix_small<n> C;
00454 for(long i=0; i<n; i++){
00455 for(long j=0; j<=i; j++){
00456 C(i,j) =A(i,j)/v;
00457 }
00458 }
00459 return C;
00460 }