00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef O2SCL_UMATRIX_TLATE_H
00024 #define O2SCL_UMATRIX_TLATE_H
00025
00026
00027
00028
00029
00030 #include <iostream>
00031 #include <cstdlib>
00032 #include <string>
00033 #include <fstream>
00034 #include <sstream>
00035
00036 #include <gsl/gsl_matrix.h>
00037 #include <gsl/gsl_ieee_utils.h>
00038
00039 #include <o2scl/err_hnd.h>
00040 #include <o2scl/uvector_tlate.h>
00041
00042 #ifndef DOXYGENP
00043 namespace o2scl {
00044 #endif
00045
00046
00047
00048
00049 template<class data_t> class umatrix_view_tlate {
00050
00051 #ifndef DOXYGEN_INTERNAL
00052
00053 protected:
00054
00055
00056 data_t *data;
00057
00058 size_t size1;
00059
00060 size_t size2;
00061
00062 int owner;
00063
00064 #endif
00065
00066 public:
00067
00068
00069
00070
00071 umatrix_view_tlate(const umatrix_view_tlate &v) {
00072 data=v.data;
00073 size1=v.size1;
00074 size2=v.size2;
00075 owner=0;
00076 }
00077
00078
00079 umatrix_view_tlate& operator=(const umatrix_view_tlate &v) {
00080 data=v.data;
00081 size1=v.size1;
00082 size2=v.size2;
00083 owner=0;
00084
00085 return *this;
00086 }
00087
00088
00089 ~umatrix_view_tlate() {};
00090
00091
00092
00093
00094
00095
00096 data_t *operator[](size_t i) {
00097 #if GSL_RANGE_CHECK
00098 if (i>=size1) {
00099 set_err((((std::string)"Array index ")+itos(i)+" out of bounds"
00100 +" in umatrix_view_tlate::operator[]. Size: "+
00101 itos(size1)+
00102 " (index should be less than size).").c_str(),gsl_index);
00103 return data;
00104 }
00105 #endif
00106 return data+i*size2;
00107 }
00108
00109
00110
00111
00112 const data_t *operator[](size_t i) const {
00113 #if GSL_RANGE_CHECK
00114 if (i>=size1) {
00115 set_err((((std::string)"Array index ")+itos(i)+" out of bounds"
00116 +" in umatrix_view_tlate::operator[] const. Size: "+
00117 itos(size1)+
00118 " (index should be less than size).").c_str(),gsl_index);
00119 return data;
00120 }
00121 #endif
00122 return data+i*size2;
00123 }
00124
00125
00126
00127
00128 data_t &operator()(size_t i, size_t j) {
00129 #if GSL_RANGE_CHECK
00130 if (i>=size1 || j>=size2) {
00131 set_err((((std::string)"Indices (")+itos(i)+","+itos(j)+
00132 ") out of bounds"
00133 +" in umatrix_view_tlate::operator(). Sizes: ("+
00134 itos(size1)+","+itos(size2)+
00135 ") (index should be less than size).").c_str(),gsl_index);
00136 return *data;
00137 }
00138 #endif
00139 return *(data+i*size2+j);
00140 }
00141
00142
00143
00144
00145 const data_t &operator()(size_t i, size_t j) const {
00146 #if GSL_RANGE_CHECK
00147 if (i>=size1 || j>=size2) {
00148 set_err((((std::string)"Indices (")+itos(i)+","+itos(j)+
00149 ") out of bounds"
00150 +" in umatrix_view_tlate::operator() const. Sizes: ("+
00151 itos(size1)+","+itos(size2)+
00152 ") (index should be less than size).").c_str(),gsl_index);
00153 return *data;
00154 }
00155 #endif
00156 return *(data+i*size2+j);
00157 }
00158
00159
00160 data_t get(size_t i, size_t j) const {
00161 #if GSL_RANGE_CHECK
00162 if (i>=size1 || j>=size2) {
00163 set_err((((std::string)"Indices (")+itos(i)+","+itos(j)+
00164 ") out of bounds"
00165 +" in umatrix_view_tlate::get(). Sizes: ("+
00166 itos(size1)+","+itos(size2)+
00167 ") (index should be less than size).").c_str(),gsl_index);
00168 return *data;
00169 }
00170 #endif
00171 return (data+i*size2+j);
00172 }
00173
00174
00175 data_t *get_ptr(size_t i, size_t j) {
00176 #if GSL_RANGE_CHECK
00177 if (i>=size1 || j>=size2) {
00178 set_err((((std::string)"Indices (")+itos(i)+","+itos(j)+
00179 ") out of bounds"
00180 +" in umatrix_view_tlate::get_ptr(). Sizes: ("+
00181 itos(size1)+","+itos(size2)+
00182 ") (index should be less than size).").c_str(),gsl_index);
00183 return data;
00184 }
00185 #endif
00186 return data+i*size2+j;
00187 }
00188
00189
00190 const data_t *get_const_ptr(size_t i, size_t j) const {
00191 #if GSL_RANGE_CHECK
00192 if (i>=size1 || j>=size2) {
00193 set_err((((std::string)"Indices (")+itos(i)+","+itos(j)+
00194 ") out of bounds"
00195 +" in umatrix_view_tlate::get_const_ptr(). Sizes: ("+
00196 itos(size1)+","+itos(size2)+
00197 ") (index should be less than size).").c_str(),gsl_index);
00198 return (const data_t *)data;
00199 }
00200 #endif
00201 return (const data_t *)(data+i*size2+j);
00202 }
00203
00204
00205 int set(size_t i, size_t j, data_t val) {
00206 #if GSL_RANGE_CHECK
00207 if (i>=size1 || j>=size2) {
00208 set_err_ret((((std::string)"Indices (")+itos(i)+","+itos(j)+
00209 ") out of bounds"
00210 +" in umatrix_view_tlate::set(). Sizes: ("+
00211 itos(size1)+","+itos(size2)+
00212 ") (index should be less than size).").c_str(),gsl_index);
00213 }
00214 #endif
00215 *(data+i*size2+j)=val;
00216 return 0;
00217 }
00218
00219
00220 int set_all(double val) {
00221 for(size_t i=0;i<size1;i++) {
00222 for(size_t j=0;j<size2;j++) {
00223 *(data+i*size2+j)=val;
00224 }
00225 }
00226 return 0;
00227 }
00228
00229
00230
00231
00232
00233
00234
00235 size_t rows() const {
00236 return size1;
00237 }
00238
00239
00240
00241
00242
00243
00244
00245 size_t cols() const {
00246 return size2;
00247 }
00248
00249
00250
00251
00252
00253 bool is_owner() const {
00254 if (owner==1) return true;
00255 return false;
00256 }
00257
00258
00259
00260
00261
00262 umatrix_view_tlate<data_t> &operator+=
00263 (const umatrix_view_tlate<data_t> &x) {
00264 size_t lsize=x.size1;
00265 if (lsize>size1) lsize=size1;
00266 size_t lsize2=x.size2;
00267 if (lsize2>size2) lsize2=size2;
00268 for(size_t i=0;i<lsize;i++) {
00269 for(size_t j=0;j<lsize2;j++) {
00270 (*this)[i][j]+=x[i][j];
00271 }
00272 }
00273
00274 return *this;
00275 }
00276
00277
00278 umatrix_view_tlate<data_t> &operator-=
00279 (const umatrix_view_tlate<data_t> &x) {
00280 size_t lsize=x.size1;
00281 if (lsize>size1) lsize=size1;
00282 size_t lsize2=x.size2;
00283 if (lsize2>size2) lsize2=size2;
00284 for(size_t i=0;i<lsize;i++) {
00285 for(size_t j=0;j<lsize2;j++) {
00286 (*this)[i][j]+=x[i][j];
00287 }
00288 }
00289
00290 return *this;
00291 }
00292
00293
00294 umatrix_view_tlate<data_t> &operator+=(const data_t &y) {
00295 for(size_t i=0;i<size1;i++) {
00296 for(size_t j=0;j<size2;j++) {
00297 (*this)[i][j]+=y;
00298 }
00299 }
00300
00301 return *this;
00302 }
00303
00304
00305 umatrix_view_tlate<data_t> &operator-=(const data_t &y) {
00306 for(size_t i=0;i<size1;i++) {
00307 for(size_t j=0;j<size2;j++) {
00308 (*this)[i][j]-=y;
00309 }
00310 }
00311
00312 return *this;
00313 }
00314
00315
00316 umatrix_view_tlate<data_t> &operator*=(const data_t &y) {
00317 for(size_t i=0;i<size1;i++) {
00318 for(size_t j=0;j<size2;j++) {
00319 (*this)[i][j]*=y;
00320 }
00321 }
00322
00323 return *this;
00324 }
00325
00326
00327 #ifndef DOXYGEN_INTERNAL
00328
00329 protected:
00330
00331
00332
00333
00334 umatrix_view_tlate() {};
00335
00336 #endif
00337
00338 };
00339
00340
00341
00342
00343
00344 template<class data_t> class umatrix_tlate :
00345 public umatrix_view_tlate<data_t> {
00346 public:
00347
00348
00349
00350
00351
00352 umatrix_tlate(size_t r=0, size_t c=0) {
00353
00354 this->data=0;
00355 this->size1=0;
00356 this->size2=0;
00357
00358
00359
00360 this->owner=1;
00361
00362 if (r>0 && c>0) {
00363 this->data=(data_t *)malloc(r*c*sizeof(data_t));
00364 if (this->data) {
00365 this->size1=r;
00366 this->size2=c;
00367 } else {
00368 set_err("No memory for data in umatrix_tlate constructor",
00369 gsl_enomem);
00370 }
00371 }
00372 }
00373
00374
00375
00376
00377
00378 umatrix_tlate(const umatrix_tlate &v) :
00379 umatrix_view_tlate<data_t>() {
00380 size_t n=v.size1;
00381 size_t n2=v.size2;
00382 if (n>0 && n2>0) {
00383 this->data=(data_t *)malloc(n*n2*sizeof(data_t));
00384 if (this->data) {
00385 this->size1=n;
00386 this->size2=n2;
00387 this->owner=1;
00388 for(size_t i=0;i<n;i++) {
00389 for(size_t j=0;j<n2;j++) {
00390 *(this->data+i*this->size2+j)=v[i][j];
00391 }
00392 }
00393 } else {
00394 set_err("No memory for data in umatrix_tlate constructor",
00395 gsl_enomem);
00396 }
00397 } else {
00398 this->size1=0;
00399 this->size2=0;
00400 }
00401 }
00402
00403
00404 umatrix_tlate
00405 (const umatrix_view_tlate<data_t> &v) :
00406 umatrix_view_tlate<data_t>() {
00407 size_t r=v.rows();
00408 size_t c=v.cols();
00409 if (r>0 && c>0) {
00410 this->data=(data_t *)malloc(r*c*sizeof(data_t));
00411 if (this->data) {
00412 this->size1=v.size1;
00413 this->size2=v.size2;
00414 this->owner=1;
00415 for(size_t i=0;i<r;i++) {
00416 for(size_t j=0;i<c;j++) {
00417 *(this->data+i*this->size2+j)=v[i][j];
00418 }
00419 }
00420 } else {
00421 set_err("No memory for data in umatrix_tlate constructor",
00422 gsl_enomem);
00423 }
00424 } else {
00425 this->size1=0;
00426 this->size2=0;
00427 }
00428 }
00429
00430
00431
00432
00433 umatrix_tlate& operator=(const umatrix_tlate &v) {
00434 size_t sze=v.size1;
00435 size_t sze2=v.size2;
00436 if (this->owner) {
00437 allocate(sze,sze2);
00438 } else {
00439 if (this->size1!=sze || this->size2!=sze2) {
00440 set_err("Sizes don't match in umatrix_tlate::operator=()",
00441 gsl_ebadlen);
00442 return *this;
00443 }
00444 }
00445 for(size_t i=0;i<sze;i++) {
00446 for(size_t j=0;j<sze2;j++) {
00447 *(this->data+i*this->size2+j)=v[i][j];
00448 }
00449 }
00450 return *this;
00451 }
00452
00453
00454
00455
00456 umatrix_tlate& operator=
00457 (const umatrix_view_tlate<data_t> &v) {
00458 size_t sze=v.rows();
00459 size_t sze2=v.cols();
00460 if (this->owner) {
00461 allocate(sze,sze2);
00462 } else {
00463 if (this->size1!=sze || this->size2!=sze2) {
00464 set_err("Sizes don't match in umatrix_tlate::operator=()",
00465 gsl_ebadlen);
00466 return *this;
00467 }
00468 }
00469 for(size_t i=0;i<sze;i++) {
00470 for(size_t j=0;j<sze2;j++) {
00471 *(this->data+i*this->size2+j)=v[i][j];
00472 }
00473 }
00474 return *this;
00475 }
00476
00477
00478
00479 umatrix_tlate(size_t n, uvector_view_tlate<data_t> uva[]) {
00480 if (n>0) {
00481 size_t n2=uva[0];
00482 if (n2>0) {
00483 allocate(n,n2);
00484 for(size_t i=0;i<n;i++) {
00485 for(size_t j=0;j<n2;j++) {
00486 (*this)[i][j]=uva[i][j];
00487 }
00488 }
00489 }
00490 }
00491 }
00492
00493
00494
00495 umatrix_tlate(size_t n, size_t n2, data_t **csa) {
00496 if (n>0 && n2>0) {
00497 allocate(n,n2);
00498 for(size_t i=0;i<n;i++) {
00499 for(size_t j=0;j<n2;j++) {
00500 (*this)[i][j]=csa[i][j];
00501 }
00502 }
00503 }
00504 }
00505
00506
00507
00508 ~umatrix_tlate() {
00509 if (this->size1>0) {
00510 if (this->owner==1) {
00511 std::free(this->data);
00512 this->size1=0;
00513 this->size2=0;
00514 }
00515 }
00516 }
00517
00518
00519
00520
00521
00522
00523 int allocate(size_t nrows, size_t ncols) {
00524 if (this->size1>0 || this->size2>0) free();
00525
00526 if (nrows>0 && ncols>0) {
00527 this->data=(data_t *)malloc(nrows*ncols*sizeof(data_t));
00528 if (this->data) {
00529 this->size1=nrows;
00530 this->size2=ncols;
00531 this->owner=1;
00532 } else {
00533 set_err_ret("No memory for data in umatrix_tlate::allocate()",
00534 gsl_enomem);
00535 }
00536 } else {
00537 set_err_ret("Zero size in umatrix::allocate()",gsl_einval);
00538 }
00539 return 0;
00540 }
00541
00542
00543
00544
00545
00546
00547
00548 int free() {
00549 if (this->size1>0) {
00550 if (this->owner==1) {
00551 std::free(this->data);
00552 }
00553 this->size1=0;
00554 this->size2=0;
00555 }
00556 return 0;
00557 }
00558
00559
00560
00561
00562
00563 umatrix_tlate<data_t> transpose() {
00564 umatrix_tlate<data_t> result(this->size2,this->size1);
00565 for(size_t i=0;i<this->size1;i++) {
00566 for(size_t j=0;j<this->size2;j++) {
00567 result[j][i]=(*this)[i][j];
00568 }
00569 }
00570 }
00571
00572
00573 };
00574
00575
00576
00577 template<class data_t> class umatrix_row_tlate :
00578 public uvector_view_tlate<data_t> {
00579 public:
00580
00581 umatrix_row_tlate(umatrix_view_tlate<data_t> &m,
00582 size_t i) {
00583 if (i<m.rows()) {
00584 this->sz=m.cols();
00585 this->data=m[0]+m.cols()*i;
00586 this->owner=0;
00587 }
00588 }
00589 };
00590
00591
00592
00593 template<class data_t> class umatrix_const_row_tlate :
00594 public uvector_view_tlate<data_t> {
00595 public:
00596
00597 umatrix_const_row_tlate
00598 (const umatrix_view_tlate<data_t> &m,
00599 size_t i) {
00600 if (i<m.size1) {
00601 this->sz=m.cols();
00602 this->data=m[0]+m.cols()*i;
00603 this->owner=0;
00604 }
00605 }
00606 };
00607
00608
00609 typedef umatrix_tlate<double> umatrix;
00610
00611 typedef umatrix_view_tlate<double> umatrix_view;
00612
00613 typedef umatrix_row_tlate<double> umatrix_row;
00614
00615 typedef umatrix_const_row_tlate<double> umatrix_const_row;
00616
00617
00618 typedef umatrix_tlate<int> umatrix_int;
00619
00620 typedef umatrix_view_tlate<int> umatrix_int_view;
00621
00622 typedef umatrix_row_tlate<int> umatrix_int_row;
00623
00624 typedef umatrix_const_row_tlate<int> umatrix_int_const_row;
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645 template<class data_t> std::ostream &operator<<
00646 (std::ostream &os, const umatrix_view_tlate<data_t> &v) {
00647 size_t i;
00648 gsl_ieee_double_rep r;
00649 for(i=0;i<v.rows()-1;i++) {
00650 for(size_t j=0;j<v.cols();j++) {
00651 gsl_ieee_double_to_rep(&(v[i][j]), &r);
00652 if (r.sign==1) os << v[i][j] << ' ';
00653 else os << ' ' << v[i][j] << ' ';
00654 }
00655 os << '\n';
00656 }
00657 i=v.rows()-1;
00658 if (i>0) {
00659 for(size_t j=0;j<v.cols();j++) {
00660 gsl_ieee_double_to_rep(&(v[i][j]), &r);
00661 if (r.sign==1) os << v[i][j] << ' ';
00662 else os << ' ' << v[i][j] << ' ';
00663 }
00664 }
00665 return os;
00666 }
00667
00668
00669
00670
00671
00672
00673 class umatrix_alloc {
00674 public:
00675
00676 void allocate(umatrix &o, int i, int j) { o.allocate(i,j); }
00677
00678 void free(umatrix &o) { o.free(); }
00679 };
00680
00681
00682
00683
00684 #ifdef DOXYGENP
00685 template<size_t N, size_t M> class ufmatrix :
00686 public umatrix_tlate<data_t>
00687 #else
00688 template<size_t N, size_t M> class ufmatrix :
00689 public umatrix_tlate<double>
00690 #endif
00691 {
00692 public:
00693 ufmatrix() : umatrix_tlate<double>(N,M) {
00694 }
00695 };
00696
00697
00698 #ifndef DOXYGENP
00699 }
00700 #endif
00701
00702 #endif
00703
00704
00705