00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "realvec.h"
00020 #include "NumericLib.h"
00021 #include <algorithm>
00022 #include <limits>
00023
00024 #ifdef MARSYAS_MATLAB
00025
00026 #endif
00027
00028 using namespace std;
00029
00030 namespace Marsyas
00031 {
00032
00033
00034
00036 realvec::realvec()
00037 : size_(0),
00038 allocatedSize_(0),
00039 data_(NULL),
00040 rows_(1),
00041 cols_(0)
00042 {
00043 allocateData(size_);
00044 }
00045
00046 realvec::~realvec()
00047 {
00048 delete[] data_;
00049 data_ = NULL;
00050 }
00051
00052 realvec::realvec(mrs_natural size)
00053 : size_(size),
00054 allocatedSize_(0),
00055 data_(NULL),
00056 rows_(1),
00057 cols_(size)
00058 {
00059 allocateData(size_);
00060 }
00061
00062 realvec::realvec(mrs_natural rows, mrs_natural cols)
00063 : size_(rows * cols),
00064 allocatedSize_(0),
00065 data_(NULL),
00066 rows_(rows),
00067 cols_(cols)
00068 {
00069 allocateData(size_);
00070 }
00071
00072 realvec::realvec(const realvec& a)
00073 : size_(a.size_),
00074 allocatedSize_(0),
00075 data_(NULL),
00076 rows_(a.rows_),
00077 cols_(a.cols_)
00078 {
00079 allocateData(size_);
00080
00081 for (mrs_natural i=0; i<size_; ++i)
00082 data_[i] = a.data_[i];
00083
00084 }
00085
00086 realvec&
00087 realvec::operator=(const realvec& a)
00088 {
00089 if (this != &a)
00090 {
00091 size_ = a.size_;
00092 rows_ = a.rows_;
00093 cols_ = a.cols_;
00094
00095
00096 if (allocatedSize_ < size_)
00097 allocateData(size_);
00098
00099
00100
00101
00102
00103 memcpy (data_, a.data_, sizeof(mrs_real)*size_);
00104
00105 }
00106
00107 return *this;
00108 }
00109
00110 mrs_real *
00111 realvec::getData() const
00112 {
00113 return data_;
00114 }
00115
00116 void
00117 realvec::appendRealvec(const realvec newValues)
00118 {
00119 mrs_natural origSize = size_;
00120
00121 stretch(origSize + newValues.getSize());
00122
00123 for (mrs_natural i=0; i<newValues.getSize(); ++i)
00124 data_[origSize + i] = newValues.data_[i];
00125 }
00126
00127 realvec
00128 realvec::getSubVector(mrs_natural startPos, mrs_natural length) const
00129 {
00130 realvec subVector(length);
00131 for (mrs_natural i=0; i<length; ++i)
00132 subVector.data_[i] = data_[startPos + i];
00133 return subVector;
00134 }
00135
00136 void
00137 realvec::transpose()
00138 {
00139 mrs_real *tmp_ = new mrs_real[size_];
00140
00141 for (mrs_natural i=0; i < rows_; ++i)
00142 for (mrs_natural j=0; j < cols_; j++)
00143 tmp_[i * cols_ + j] = data_[j * rows_ + i];
00144
00145 mrs_natural tmp = rows_;
00146 rows_ = cols_;
00147 cols_ = tmp;
00148
00149 delete [] data_;
00150 data_ = tmp_;
00151 }
00152
00153 void
00154 realvec::fliplr()
00155 {
00156 mrs_natural i,j,jtmp;
00157
00158 for (i=0; i < rows_; i++)
00159 for (j=0, jtmp = cols_-1; j < cols_/2; j++, jtmp--)
00160 {
00161 mrs_real tmp = (*this)(i,j);
00162 (*this)(i,j) = (*this)(i,jtmp);
00163 (*this)(i,jtmp) = tmp;
00164 }
00165 }
00166
00167 void
00168 realvec::flipud()
00169 {
00170 mrs_natural i,j,itmp;
00171
00172 for (i=0, itmp = rows_-1; i < rows_/2; i++, itmp--)
00173 for (j=0; j < cols_; j++)
00174 {
00175 mrs_real tmp = (*this)(i,j);
00176 (*this)(i,j) = (*this)(itmp,j);
00177 (*this)(itmp,j) = tmp;
00178 }
00179 }
00180
00181 mrs_real
00182 realvec::median() const
00183 {
00184 if (size_ == 0)
00185 return 0;
00186 realvec tmp(*this);
00187 mrs_real *tmpData = tmp.data_;
00188 std::sort(tmpData, tmpData+size_);
00189 return tmpData[size_/2];
00190 }
00191
00192 mrs_real
00193 realvec::mean() const
00194 {
00195 mrs_real sum = 0.0;
00196 for (mrs_natural i=0; i < size_; ++i)
00197 {
00198 sum += data_[i];
00199 }
00200 if (sum != 0.0) sum /= size_;
00201 return sum;
00202 }
00203
00204 void
00205 realvec::sort()
00206 {
00207 std::sort(data_, data_+size_);
00208 }
00209
00210 mrs_real
00211 realvec::sum() const
00212 {
00213 mrs_real sum = 0.0;
00214 for (mrs_natural i=0; i < size_; ++i)
00215 {
00216 sum += data_[i];
00217 }
00218 return sum;
00219 }
00220
00221
00222 mrs_real
00223 realvec::var() const
00224 {
00225 mrs_real sum = 0.0;
00226 mrs_real sum_sq = 0.0;
00227 mrs_real val;
00228 mrs_real var;
00229
00230 for (mrs_natural i=0; i < size_; ++i)
00231 {
00232 val = data_[i];
00233 sum += val;
00234 sum_sq += (val * val);
00235 }
00236 if (sum != 0.0) sum /= size_;
00237 if (sum_sq != 0.0) sum_sq /= size_;
00238
00239 var = sum_sq - sum * sum;
00240
00241 if (var < 0.0)
00242 var = 0.0;
00243 return var;
00244 }
00245
00246 mrs_real
00247 realvec::std() const
00248 {
00249 mrs_real vr = var();
00250 if (vr != 0)
00251 return sqrt(vr);
00252 else
00253 return 0.0;
00254 }
00255
00256 mrs_natural
00257 realvec::getRows() const
00258 {
00259 return rows_;
00260 }
00261
00262 mrs_natural
00263 realvec::getCols() const
00264 {
00265 return cols_;
00266 }
00267
00268 mrs_natural
00269 realvec::getSize() const
00270 {
00271 return size_;
00272 }
00273
00274 void
00275 realvec::debug_info()
00276 {
00277 MRSERR("realvec information");
00278 MRSERR("size = " + size_);
00279 }
00280
00281
00282 void
00283 realvec::stretch(mrs_natural size)
00284 {
00285 if (size_ == size)
00286 return;
00287
00288 if (size < allocatedSize_)
00289 {
00290 size_ = size;
00291 rows_ = 1;
00292 cols_ = size_;
00293 return;
00294 }
00295
00296
00297
00298 mrs_real* ndata = new mrs_real[size];
00299
00300 mrs_natural i=0;
00301 for (; i < size_; ++i)
00302 ndata[i] = data_[i];
00303 for(; i < size; ++i)
00304 ndata[i] = 0.0;
00305
00306
00307 delete [] data_;
00308
00309 data_ = ndata;
00310
00311
00312 size_ = size;
00313 allocatedSize_ = size;
00314 rows_ = 1;
00315 cols_ = size_;
00316 }
00317
00318
00319 void
00320 realvec::stretch(mrs_natural rows, mrs_natural cols)
00321 {
00322 mrs_natural size = rows * cols;
00323
00324 if ((rows == rows_)&&(cols == cols_))
00325 return;
00326
00327 if(size == 0){
00328 size_ = size;
00329 rows_ = rows;
00330 cols_ = cols;
00331 return;
00332 }
00333
00334 if (size < allocatedSize_)
00335 {
00336 size_ = size;
00337 rows_ = rows;
00338 cols_ = cols;
00339 return;
00340 }
00341
00342
00343 mrs_real *ndata = new mrs_real[size];
00344
00345
00346 for (mrs_natural r=0; r < rows; ++r)
00347 for (mrs_natural c = 0; c < cols; ++c)
00348 {
00349 if ((r < rows_)&&(c < cols_))
00350 ndata[c * rows + r] = data_[c * rows_ + r];
00351 else
00352 ndata[c * rows + r] = 0.0;
00353 }
00354
00355 delete [] data_;
00356 data_ = ndata;
00357
00358 size_ = size;
00359 allocatedSize_ = size;
00360 rows_ = rows;
00361 cols_ = cols;
00362 }
00363
00364 void
00365 realvec::stretchWrite(const mrs_natural pos, const mrs_real val)
00366 {
00367
00368
00369 mrs_natural wantSize = pos+1;
00370 if (wantSize > size_){
00371 if ( wantSize < 2*size_ )
00372
00373 stretch( 2*size_ );
00374 else
00375
00376 stretch( wantSize );
00377 }
00378
00379
00380 data_[pos] = val;
00381 }
00382
00383 void
00384 realvec::stretchWrite(const mrs_natural r, const mrs_natural c, const mrs_real val)
00385 {
00386 mrs_natural nextR = rows_;
00387 mrs_natural nextC = cols_;
00388 mrs_natural wantR = r+1;
00389 mrs_natural wantC = c+1;
00390 if ( (wantR >= rows_) || (wantC >= cols_) )
00391 {
00392 if ( wantR >= rows_ ) {
00393 if ( wantR < 2*rows_ ) {
00394 nextR = 2*rows_;
00395 } else {
00396 nextR = wantR;
00397 }
00398 }
00399
00400 if ( wantC >= cols_ ) {
00401 if ( wantC < 2*cols_ ) {
00402 nextC = 2*cols_;
00403 } else {
00404 nextC = wantC;
00405 }
00406 }
00407
00408 stretch( nextR, nextC );
00409
00410
00411 }
00412 data_[c * rows_ + r] = val;
00413 }
00414
00415 void
00416 realvec::create(mrs_natural size)
00417 {
00418 size_ = size;
00419 allocateData(size_);
00420 rows_ = 1;
00421 cols_ = size_;
00422 }
00423
00424 void
00425 realvec::create(mrs_natural rows, mrs_natural cols)
00426 {
00427 size_ = rows * cols;
00428 rows_ = rows;
00429 cols_ = cols;
00430 allocateData(size_);
00431 }
00432
00433
00434 void
00435 realvec::create(mrs_real val, mrs_natural rows, mrs_natural cols)
00436 {
00437 size_ = rows * cols;
00438 rows_ = rows;
00439 cols_ = cols;
00440 delete [] data_;
00441 data_ = NULL;
00442 if (size_ > 0)
00443 data_ = new mrs_real[size_];
00444 for (mrs_natural i=0; i<size_; ++i)
00445 data_[i] = val;
00446 allocatedSize_ = size_;
00447 }
00448
00449
00450
00451 void
00452 realvec::allocate(mrs_natural size)
00453 {
00454 delete [] data_;
00455 data_ = NULL;
00456 size_ = size;
00457 cols_ = size_;
00458 rows_ = 1;
00459 allocatedSize_ = size;
00460 if (size_ > 0)
00461 data_ = new mrs_real[size_];
00462 }
00463
00464 void
00465 realvec::allocate(mrs_natural rows, mrs_natural cols)
00466 {
00467 delete [] data_;
00468 data_ = NULL;
00469 size_ = rows*cols;
00470 cols_ = cols;
00471 rows_ = rows;
00472 allocatedSize_ = size_;
00473 if (size_ > 0)
00474 data_ = new mrs_real[size_];
00475 }
00476
00477
00478 void
00479 realvec::setval(mrs_natural start, mrs_natural end, mrs_real val)
00480 {
00481 MRSASSERT(start <= (mrs_natural)size_);
00482 MRSASSERT(end <= (mrs_natural)size_);
00483
00484 for (mrs_natural i=start; i<end; ++i)
00485 {
00486 data_[i] = val;
00487 }
00488 }
00489
00490 void
00491 realvec::apply(mrs_real (*func) (mrs_real))
00492 {
00493 for (mrs_natural i=0; i<size_; ++i)
00494 {
00495 data_[i] = func(data_[i]);
00496 }
00497 }
00498
00499 void
00500 realvec::setval(mrs_real val)
00501 {
00502 for (mrs_natural i=0; i<size_; ++i)
00503 {
00504 data_[i] = val;
00505 }
00506 }
00507
00508 void
00509 realvec::abs()
00510 {
00511 for (mrs_natural i=0; i<size_; ++i)
00512 {
00513 data_[i] = fabs(data_[i]);
00514 }
00515 }
00516
00517 void
00518 realvec::pow (mrs_real exp)
00519 {
00520 for (mrs_natural i=0; i<size_; i++)
00521 {
00522 data_[i] = ::pow (data_[i], exp);
00523 }
00524 }
00525
00526 void
00527 realvec::norm()
00528 {
00529 mrs_real mean = this->mean();
00530 mrs_real std = this->std();
00531 for (mrs_natural i=0; i < size_; ++i)
00532 {
00533 data_[i] = (data_[i] - mean) / std;
00534 }
00535 }
00536
00537
00538 void
00539 realvec::normMaxMin()
00540 {
00541 mrs_real max = DBL_MIN;
00542 mrs_real min = DBL_MAX;
00543
00544 for (mrs_natural i=0; i < size_; ++i)
00545 {
00546 if (data_[i] > max)
00547 max = data_[i];
00548 if (data_[i] < min)
00549 min = data_[i];
00550 }
00551
00552
00553 for (mrs_natural i=0; i < size_; ++i)
00554 {
00555 data_[i] = (data_[i] - min) / (max - min);
00556 }
00557
00558
00559 }
00560
00561
00562 void
00563 realvec::norm(mrs_real mean, mrs_real std)
00564 {
00565 for (mrs_natural i=0; i < size_; ++i)
00566 {
00567 data_[i] = (data_[i] - mean) / std;
00568 }
00569 }
00570
00571 void
00572 realvec::renorm(mrs_real old_mean, mrs_real old_std, mrs_real new_mean, mrs_real new_std)
00573 {
00574 for (mrs_natural i=0; i < size_; ++i)
00575 {
00576 data_[i] = (data_[i] - old_mean) / old_std;
00577 data_[i] *= new_std;
00578 data_[i] += new_mean;
00579 }
00580 }
00581
00582 mrs_natural
00583 realvec::invert(realvec& res)
00584 {
00585 if (rows_ != cols_)
00586 {
00587 MRSERR("realvec::invert() - matrix should be square!");
00588 res.create(0);
00589 return -1;
00590 }
00591 if (this != &res)
00592 {
00593 mrs_natural rank;
00594 mrs_natural r,c,i;
00595 mrs_real temp;
00596
00597 res.create(rows_, cols_);
00598
00599 rank = 0;
00600 for (r = 0; r < rows_; ++r)
00601 for (c=0; c < cols_; ++c)
00602 {
00603 if (r == c)
00604 res(r,c) = 1.0;
00605 else
00606 res(r,c) = 0.0;
00607 }
00608 for (i = 0; i < rows_; ++i)
00609 {
00610 if ((*this)(i,i) == 0)
00611 {
00612 for (r = i; r < rows_; ++r)
00613 for (c = 0; c < cols_; ++c)
00614 {
00615 (*this)(i,c) += (*this)(r,c);
00616 res(i,c) += res(r,c);
00617 }
00618 }
00619 for (r = i; r < rows_; ++r)
00620 {
00621 temp = (*this)(r,i);
00622 if (temp != 0)
00623 for (c =0; c < cols_; ++c)
00624 {
00625 (*this)(r,c) /= temp;
00626 res(r,c) /= temp;
00627 }
00628 }
00629 if (i != rows_-1)
00630 {
00631 for (r = i+1; r < rows_; ++r)
00632 {
00633 temp = (*this)(r,i);
00634 if (temp != 0.0)
00635 for (c=0; c < cols_; ++c)
00636 {
00637 (*this)(r,c) -= (*this)(i,c);
00638 res(r,c) -= res(i,c);
00639 }
00640 }
00641 }
00642 }
00643 for (i=1; i < rows_; ++i)
00644 for (r=0; r < i; ++r)
00645 {
00646 temp = (*this)(r,i);
00647 for (c=0; c < cols_; ++c)
00648 {
00649 (*this)(r,c) -= (temp * (*this)(i,c));
00650 res(r,c) -= (temp * res(i,c));
00651 }
00652 }
00653 for (r =0; r < rows_; ++r)
00654 for (c=0; c < cols_; ++c)
00655 (*this)(r,c) = res(r,c);
00656 return rank;
00657 }
00658 else
00659 {
00660 res.create(0);
00661 MRSERR("realvec::invert() - inPlace operation not supported - returning empty result vector!");
00662 return -1;
00663 }
00664 }
00665
00666 void
00667 realvec::sqr()
00668 {
00669 for (mrs_natural i=0; i<size_; ++i)
00670 {
00671 data_[i] *= data_[i];
00672 }
00673 }
00674
00675 mrs_natural
00676 realvec::search(mrs_real val)
00677 {
00678 mrs_real minDiff = MAXREAL;
00679 mrs_natural index=-1;
00680 for (mrs_natural i=0; i<size_; ++i)
00681 if (fabs(data_[i]-val)< minDiff)
00682 {
00683 minDiff = fabs(data_[i]-val);
00684 index=i;
00685 }
00686 return index;
00687 }
00688
00689 void
00690 realvec::sqroot()
00691 {
00692 for (mrs_natural i=0; i<size_; ++i)
00693 {
00694 data_[i] = sqrt(data_[i]);
00695 }
00696 }
00697
00698 realvec
00699 operator+(const realvec& vec1, const realvec& vec2)
00700 {
00701 mrs_natural size;
00702 if (vec1.size_ != vec2.size_)
00703 MRSERR("Size of realvecs does not match");
00704 if (vec1.size_ >= vec2.size_)
00705 size = vec1.size_;
00706 else
00707 size = vec2.size_;
00708 realvec sum;
00709 sum.create(size);
00710
00711 for (mrs_natural i=0; i<vec1.size_; ++i)
00712 {
00713 sum.data_[i] = vec1.data_[i];
00714 }
00715 for (mrs_natural i=0; i<vec2.size_; ++i)
00716 {
00717 sum.data_[i] += vec2.data_[i];
00718 }
00719
00720 return sum;
00721 }
00722
00723 realvec
00724 operator-(const realvec& vec1, const realvec& vec2)
00725 {
00726 mrs_natural size;
00727 mrs_natural i;
00728 if (vec1.size_ != vec2.size_)
00729 MRSERR("Size of realvecs does not match");
00730 if (vec1.size_ >= vec2.size_)
00731 size = vec1.size_;
00732 else
00733 size = vec2.size_;
00734 realvec diff;
00735 diff.create(size);
00736
00737 for (i=0; i<vec1.size_; ++i)
00738 {
00739 diff.data_[i] = vec1.data_[i];
00740 }
00741 for (i=0; i<vec2.size_; ++i)
00742 {
00743 diff.data_[i] -= vec2.data_[i];
00744 }
00745
00746 return diff;
00747 }
00748
00749
00750
00751 void
00752 realvec::send(Communicator *com)
00753 {
00754 static char *buf = new char[256];
00755 mrs_string message;
00756 sprintf(buf, "%i\n", (int)size_);
00757 message = buf;
00758 com->send_message(message);
00759 for (mrs_natural i=0; i<size_; ++i)
00760 {
00761 sprintf(buf, "%f\n", data_[i]);
00762 message = buf;
00763 com->send_message(message);
00764 }
00765 MRSERR("realvec::send numbers were sent to the client");
00766
00767 }
00768
00769 bool
00770 realvec::read(mrs_string filename)
00771 {
00772 ifstream from(filename.c_str());
00773 if (from.is_open())
00774 {
00775 from >> (*this);
00776 return true;
00777 }
00778 else
00779 {
00780 MRSERR("realvec::read: failed to open file: " + filename);
00781 return false;
00782 }
00783 }
00784
00785
00786 void
00787 realvec::shuffle()
00788 {
00789
00790 unsigned int n = cols_;
00791 while (n > 1)
00792 {
00793
00794 unsigned int k = (unsigned int)((mrs_real)n * (mrs_real)rand() / (mrs_real)(RAND_MAX + 1.0));
00795
00796 n--;
00797
00798
00799 if (k != n)
00800 {
00801 for (int r = 0; r < rows_; ++r)
00802 swap(data_[k * rows_ + r], data_[n * rows_ + r]);
00803 }
00804 }
00805 }
00806
00807
00808 bool
00809 realvec::write(mrs_string filename) const
00810 {
00811 ofstream os(filename.c_str());
00812 if (os.is_open())
00813 {
00814 os << (*this) << endl;
00815 return true;
00816 }
00817 else
00818 {
00819 MRSERR("realvec::write: failed to open file to write: filename");
00820 return false;
00821 }
00822 }
00823
00824 void
00825 realvec::dump()
00826 {
00827 for (mrs_natural i =0 ; i< size_ ; ++i)
00828 MRSMSG(data_[i] << " ") ;
00829 MRSMSG(endl);
00830 }
00831
00832 bool
00833 realvec::readText(mrs_string filename)
00834 {
00835 ifstream infile(filename.c_str());
00836 if (infile.is_open())
00837 {
00838 int i = 0;
00839 if (size_ == 0)
00840 create(1);
00841
00842 mrs_real value;
00843 while (infile >> value)
00844 {
00845
00846 stretchWrite(i,value);
00847 ++i;
00848 }
00849 stretch(i-1);
00850 infile.close();
00851 return true;
00852 }
00853 else
00854 {
00855 MRSERR("realvec::readText: failed to open file: " + filename);
00856 return false;
00857 }
00858 }
00859
00860 bool
00861 realvec::writeText(mrs_string filename)
00862 {
00863 if (size_ == 0)
00864 return true;
00865
00866 ofstream outfile(filename.c_str());
00867 if (outfile.is_open())
00868 {
00869 for (mrs_natural i=0; i<size_; ++i)
00870 {
00871 outfile << data_[i] <<endl;
00872 }
00873 outfile.close();
00874 return true;
00875 }
00876 else
00877 {
00878 MRSERR("realvec::writeText: failed to open file: " + filename);
00879 return false;
00880 }
00881 }
00882
00887 void
00888 realvec::dumpDataOnly(std::ostream& o, std::string columnSep, std::string rowSep) const
00889 {
00890 for (mrs_natural r = 0; r < this->rows_; ++r)
00891 {
00892 for (mrs_natural c = 0; c < this->cols_; ++c)
00893 {
00894 o << this->data_[c * this->rows_ + r];
00895 if (c < this->cols_ - 1)
00896 {
00897 o << columnSep ;
00898 }
00899 }
00900 if (r < this->rows_ - 1)
00901 {
00902 o << rowSep;
00903 }
00904 }
00905 }
00906
00907 ostream&
00908 operator<< (ostream& o, const realvec& vec)
00909 {
00910 o << "# MARSYAS mrs_realvec" << endl;
00911 o << "# Size = " << vec.size_ << endl << endl;
00912 o << endl;
00913
00914 o << "# type: matrix" << endl;
00915 o << "# rows: " << vec.rows_ << endl;
00916 o << "# columns: " << vec.cols_ << endl;
00917
00918 vec.dumpDataOnly(o, " ", "\n");
00919 o << endl;
00920
00921
00922
00923 o << endl;
00924 o << "# Size = " << vec.size_ << endl;
00925 o << "# MARSYAS mrs_realvec" << endl;
00926 return o;
00927 }
00928
00929 istream&
00930 operator>>(istream& is, realvec& vec)
00931 {
00932
00933
00934
00935
00936
00937
00938
00939
00940 mrs_string str0,str1,str2;
00941 mrs_natural size;
00942 mrs_natural i;
00943
00944 is >> str0;
00945 is >> str1;
00946 is >> str2;
00947
00948
00949 if ((str0 != "#") || (str1 != "MARSYAS") || (str2 != "mrs_realvec"))
00950 {
00951 MRSERR("realvec::operator>>: Problem1 reading realvec object from istream");
00952 MRSERR("-str0 = " << str0 << endl);
00953 MRSERR("-str1 = " << str1 << endl);
00954 MRSERR("-str2 = " << str2 << endl);
00955 return is;
00956 }
00957 is >> str0;
00958 is >> str1;
00959 is >> str2;
00960
00961
00962 if ((str0 != "#") || (str1 != "Size") || (str2 != "="))
00963 {
00964 MRSERR("realvec::operator>>: Problem2 reading realvec object from istream");
00965 MRSERR("-str0 = " << str0 << endl);
00966 MRSERR("-str1 = " << str1 << endl);
00967 MRSERR("-str2 = " << str2 << endl);
00968 return is;
00969 }
00970 is >> size;
00971
00972 vec.create(size);
00973 for (i=0; i<3; ++i)
00974 {
00975 is >> str0;
00976 }
00977 is >> str0 >> str1 >> vec.rows_;
00978 is >> str0 >> str1 >> vec.cols_;
00979
00980 for (mrs_natural r = 0; r < vec.rows_; ++r)
00981 for (mrs_natural c = 0; c < vec.cols_; ++c)
00982 {
00983 is >> str0;
00984 if (str0 == "nan") {
00985 vec.data_[c*vec.rows_ + r] = std::numeric_limits<double>::quiet_NaN();
00986 } else {
00987 std::stringstream s(str0);
00988 s >> vec.data_[c * vec.rows_ + r];
00989 }
00990 }
00991
00992 is >> str0;
00993 is >> str1;
00994 is >> str2;
00995 if ((str0 != "#") || (str1 != "Size") || (str2 != "="))
00996 {
00997 MRSERR("realvec::operator>>: Problem3 reading realvec object from istream");
00998 MRSERR("-str0 = " << str0 << endl);
00999 MRSERR("-str1 = " << str1 << endl);
01000 MRSERR("-str2 = " << str2 << endl);
01001 is >> str0;
01002 is >> str1;
01003 is >> str2;
01004 MRSERR("-str0 = " << str0 << endl);
01005 MRSERR("-str1 = " << str1 << endl);
01006 MRSERR("-str2 = " << str2 << endl);
01007 return is;
01008 }
01009 is >> size;
01010 is >> str0;
01011 is >> str1;
01012 is >> str2;
01013
01014 if ((str0 != "#") || (str1 != "MARSYAS") || (str2 != "mrs_realvec"))
01015 {
01016 MRSERR("realvec::operator>>: Problem4 reading realvec object from istream");
01017 MRSERR("-str0 = " << str0 << endl);
01018 MRSERR("-str1 = " << str1 << endl);
01019 MRSERR("-str2 = " << str2 << endl);
01020 return is;
01021 }
01022 return is;
01023 }
01024
01025 realvec
01026 realvec::operator()(std::string r, std::string c)
01027 {
01028 mrs_string::size_type r_l = r.length();
01029 mrs_string::size_type c_l = c.length();
01030
01031 mrs_string::size_type r_c = r.find(":");
01032 mrs_string::size_type c_c = c.find(":");
01033
01034 mrs_string::size_type r_a;
01035 mrs_string::size_type r_b;
01036
01037 mrs_string::size_type c_a;
01038 mrs_string::size_type c_b;
01039
01040 char *endptr;
01041
01042 MRSASSERT( (r_c == 0 && r_l == 1) || (r_c == mrs_string::npos) || (r_c>0 && r_l-r_c>1) );
01043 MRSASSERT( (c_c == 0 && c_l == 1) || (c_c == mrs_string::npos) || (c_c>0 && c_l-c_c>1) );
01044
01045 if ( r_c != mrs_string::npos && r_l > 1 )
01046 {
01047 r_a = strtol( r.substr(0,r_c).c_str() , &endptr , 10 );
01048 MRSASSERT( *endptr == '\0' );
01049 r_b = strtol( r.substr(r_c+1,r_l-r_c-1).c_str() , &endptr , 10 );
01050 MRSASSERT( *endptr == '\0' );
01051 }
01052 else if ( r_c == mrs_string::npos )
01053 {
01054 r_a = r_b = strtol( r.c_str() , &endptr , 10 );
01055 MRSASSERT( *endptr == '\0' );
01056 }
01057 else
01058 {
01059 r_a = 0;
01060 r_b = rows_-1;
01061 }
01062
01063 MRSASSERT( (mrs_natural)r_b < rows_ );
01064
01065 if ( c_c != mrs_string::npos && c_l > 1 )
01066 {
01067 c_a = strtol( c.substr(0,c_c).c_str() , &endptr , 10 );
01068 MRSASSERT( *endptr == '\0' );
01069 c_b = strtol( c.substr(c_c+1,c_l-c_c-1).c_str() , &endptr , 10 );
01070 MRSASSERT( *endptr == '\0' );
01071 }
01072 else if ( c_c == mrs_string::npos )
01073 {
01074 c_a = c_b = strtol( c.c_str() , &endptr , 10 );
01075 MRSASSERT( *endptr == '\0' );
01076 }
01077 else
01078 {
01079 c_a = 0;
01080 c_b = cols_-1;
01081 }
01082
01083 MRSASSERT( (mrs_natural)c_b < cols_ );
01084
01085 r_l = r_b - r_a + 1;
01086 c_l = c_b - c_a + 1;
01087
01088 realvec matrix;
01089
01090 matrix.create( r_l , c_l );
01091
01092 for ( c_c = c_a ; c_c <= c_b ; c_c++ )
01093 {
01094 for ( r_c = r_a ; r_c <= r_b ; r_c++ )
01095 {
01096 matrix.data_[(c_c-c_a) * r_l + (r_c-r_a)] = data_[c_c * rows_ + r_c];
01097 }
01098 }
01099
01100 return matrix;
01101 }
01102
01103 realvec
01104 realvec::operator()(std::string c)
01105 {
01106 mrs_string::size_type c_l = c.length();
01107 mrs_string::size_type c_c = c.find(":");
01108 mrs_string::size_type c_a;
01109 mrs_string::size_type c_b;
01110 char *endptr;
01111
01112 MRSASSERT( (c_c == 0 && c_l == 1) || (c_c == mrs_string::npos) || (c_c>0 && c_l-c_c>1) );
01113
01114 if ( c_c != mrs_string::npos && c_l > 1 )
01115 {
01116 c_a = strtol( c.substr(0,c_c).c_str() , &endptr , 10 );
01117 MRSASSERT( *endptr == '\0' );
01118 c_b = strtol( c.substr(c_c+1,c_l-c_c-1).c_str() , &endptr , 10 );
01119 MRSASSERT( *endptr == '\0' );
01120 }
01121 else if ( c_c == mrs_string::npos )
01122 {
01123 c_a = c_b = strtol( c.c_str() , &endptr , 10 );
01124 MRSASSERT( *endptr == '\0' );
01125 }
01126 else
01127 {
01128 c_a = 0;
01129 c_b = (rows_*cols_)-1;
01130 }
01131
01132 MRSASSERT( (mrs_natural)c_b < rows_*cols_ );
01133 c_l = c_b - c_a + 1;
01134
01135 realvec matrix;
01136 matrix.create( c_l );
01137
01138 for ( c_c = c_a ; c_c <= c_b ; c_c++ )
01139 {
01140 matrix.data_[(c_c-c_a)] = data_[c_c];
01141 }
01142 return matrix;
01143 }
01144
01145 void
01146 realvec::getRow(const mrs_natural r, realvec& res) const
01147 {
01148 if (this != &res)
01149 {
01150 if (r >= rows_ )
01151 {
01152 MRSERR("realvec::getRow() - row index greater than realvec number of rows! Returning empty result vector.");
01153 res.create(0);
01154 return;
01155 }
01156 res.stretch(cols_);
01157 for (mrs_natural c=0; c < cols_; ++c)
01158 {
01159 res(c) = (*this)(r,c);
01160 }
01161 }
01162 else
01163 {
01164 res.create(0);
01165 MRSERR("realvec::getRow() - inPlace operation not supported - returning empty result vector!");
01166 }
01167 }
01168
01169 void
01170 realvec::getCol(const mrs_natural c, realvec& res) const
01171 {
01172 if (this != &res)
01173 {
01174 if (c >= cols_)
01175 {
01176 MRSERR("realvec::getCol() - row index greater than realvec number of rows! Returning empty result vector.");
01177 res.create(0);
01178 return;
01179 }
01180 res.stretch(rows_,1);
01181 for (mrs_natural r=0; r < rows_; ++r)
01182 {
01183 res(r) = (*this)(r,c);
01184 }
01185 }
01186 else
01187 {
01188 res.create(0);
01189 MRSERR("realvec::getCol() - inPlace operation not supported - returning empty result vector!");
01190 }
01191 }
01192 void
01193 realvec::getSubMatrix (const mrs_natural r, const mrs_natural c, realvec& res)
01194 {
01195 if (this != &res)
01196 {
01197 mrs_natural numRows = res.getRows (),
01198 numCols = res.getCols ();
01199
01200 if (c >= cols_ || r >= rows_)
01201 {
01202 MRSERR("realvec::getSubMatrix() - index larger than realvec number of rows/cols! Returning empty result vector.");
01203 res.create(0);
01204 return;
01205 }
01206 mrs_natural m,n,i,j;
01207 mrs_natural endRow = min(rows_, r + numRows),
01208 endCol = min(cols_, c + numCols);
01209 for (m=r, i=0; m < endRow; m++,++i)
01210 {
01211 for (n=c, j=0; n < endCol; n++, j++)
01212 res(i,j) = (*this)(m,n);
01213 }
01214
01215
01216 for (i=endRow-r; i < numRows; ++i)
01217 for (j=0; j < numCols; j++)
01218 res(i,j) = 0;
01219 for (j=endCol-c; j < numCols; j++)
01220 for (i=0; i < numRows; ++i)
01221 res(i,j) = 0;
01222 }
01223 else
01224 {
01225 res.create(0);
01226 MRSERR("realvec::getSubMatrix() - inPlace operation not supported - returning empty result vector!");
01227 }
01228 }
01229
01230 void
01231 realvec::setRow (const mrs_natural r, const realvec src)
01232 {
01233 setSubMatrix (r,0, src);
01234 }
01235 void
01236 realvec::setCol (const mrs_natural c, const realvec src)
01237 {
01238 setSubMatrix (0,c,src);
01239 }
01240 void
01241 realvec::setSubMatrix (const mrs_natural r, const mrs_natural c, const realvec src)
01242 {
01243 mrs_natural m,n;
01244 mrs_natural numRows = src.getRows (),
01245 numCols = src.getCols ();
01246 if (c+numCols > cols_ || r+numRows > rows_)
01247 {
01248 MRSERR("realvec::setSubMatrix() - dimension mismatch! Abort.");
01249 return;
01250 }
01251
01252 mrs_natural endRow = min(rows_, r + numRows),
01253 endCol = min(cols_, c + numCols);
01254 for (m=r; m < endRow; m++)
01255 {
01256 for (n=c; n < endCol; n++)
01257 (*this)(m,n) = src(m-r,n-c);
01258 }
01259 }
01260
01261
01262 mrs_real
01263 realvec::maxval(mrs_natural* index) const
01264 {
01265 mrs_real max = numeric_limits<mrs_real>::max() * -1.0;
01266 mrs_natural ind = 0;
01267 for (mrs_natural i=0; i < size_; ++i)
01268 {
01269 if (data_[i] > max)
01270 {
01271 max = data_[i];
01272 ind = i;
01273 }
01274 }
01275 if (index)
01276 *index = ind;
01277 return max;
01278 }
01279
01280 mrs_real
01281 realvec::minval() const
01282 {
01283 mrs_real min = numeric_limits<mrs_real>::max();
01284 for (mrs_natural i=0; i < size_; ++i)
01285 {
01286 if (data_[i] < min)
01287 min = data_[i];
01288 }
01289 return min;
01290 }
01291
01292 void
01293 realvec::meanObs(realvec& res) const
01294 {
01295 if (this != &res)
01296 {
01297 realvec obsrow(cols_);
01298 res.stretch(rows_, 1);
01299
01300 for (mrs_natural r=0; r<rows_; ++r)
01301 {
01302
01303 for (mrs_natural c=0; c<cols_; ++c)
01304 {
01305 obsrow(c) = (*this)(r,c);
01306 }
01307 res(r,0) = obsrow.mean();
01308 }
01309 }
01310 else
01311 {
01312 res.create(0);
01313 MRSERR("realvec::meanObs() - inPlace operation not supported - returning empty result vector!");
01314 }
01315 }
01316
01317 void
01318 realvec::varObs(realvec& res) const
01319 {
01320 if (this != &res)
01321 {
01322 res.create(rows_, 1);
01323 realvec obsrow(cols_);
01324
01325 for (mrs_natural r=0; r<rows_; ++r)
01326 {
01327
01328 for (mrs_natural c=0; c<cols_; ++c)
01329 {
01330 obsrow(c) = (*this)(r,c);
01331 }
01332 res(r,0) = obsrow.var();
01333 }
01334 }
01335 else
01336 {
01337 res.create(0);
01338 MRSERR("realvec::varObs() - inPlace operation not supported - returning empty result vector!");
01339 }
01340 }
01341
01342 void
01343 realvec::stdObs(realvec& res) const
01344 {
01345 if (this != &res)
01346 {
01347 realvec obsrow(cols_);
01348 res.stretch(rows_, 1);
01349 for (mrs_natural r=0; r<rows_; ++r)
01350 {
01351
01352 for (mrs_natural c=0; c < cols_; ++c)
01353 {
01354 obsrow(c) = (*this)(r,c);
01355 }
01356 res(r,0) = obsrow.std();
01357 }
01358 }
01359 else
01360 {
01361 res.create(0);
01362 MRSERR("realvec::stdObs() - inPlace operation not supported - returning empty result vector!");
01363 }
01364 }
01365
01366 void
01367 realvec::normObs()
01368 {
01369
01370
01371 realvec obsrow(cols_);
01372 mrs_real mean, std;
01373
01374 for (mrs_natural r=0; r < rows_; ++r)
01375 {
01376 for (mrs_natural c=0; c < cols_; ++c)
01377 {
01378 obsrow(c) = (*this)(r,c);
01379 }
01380
01381 mean = obsrow.mean();
01382 std = obsrow.std();
01383 for (mrs_natural c=0; c < cols_; ++c)
01384 {
01385 (*this)(r,c) -= mean;
01386 (*this)(r,c) /= std;
01387 }
01388 }
01389 }
01390
01391 void
01392 realvec::normObsMinMax()
01393 {
01394
01395
01396
01397 realvec obsrow(cols_);
01398 mrs_real min, max, dif;
01399
01400 for (mrs_natural r=0; r < rows_; ++r)
01401 {
01402
01403
01404
01405
01406 getRow(r, obsrow);
01407 min = obsrow.minval();
01408 max = obsrow.maxval();
01409 dif = max-min;
01410 if (dif ==0)
01411 dif=1.0;
01412 for (mrs_natural c=0; c < cols_; ++c)
01413 {
01414 (*this)(r,c) -= min;
01415 (*this)(r,c) /= dif;
01416 }
01417 }
01418 }
01419
01420 void
01421 realvec::normSpl(mrs_natural index)
01422 {
01423
01424
01425 mrs_real mean;
01426 mrs_real std;
01427
01428 realvec colVec;
01429
01430 if (!index)
01431 index=cols_;
01432 for (mrs_natural r=0; r < index; ++r)
01433 {
01434
01435
01436
01437
01438
01439 getCol(r, colVec);
01440 mean = colVec.mean();
01441 std = colVec.std();
01442
01443 if (std)
01444 for (mrs_natural c=0; c < rows_; ++c)
01445 {
01446 (*this)(c, r) -= mean;
01447 (*this)(c, r) /= std;
01448 }
01449 }
01450 }
01451
01452 void
01453 realvec::normSplMinMax(mrs_natural index)
01454 {
01455
01456
01457 mrs_real min;
01458 mrs_real max, dif;
01459 realvec colVec;
01460
01461 if (!index)
01462 index=cols_;
01463 for (mrs_natural r=0; r < index; ++r)
01464 {
01465
01466
01467
01468
01469 getCol(r, colVec);
01470 min = colVec.minval();
01471 max = colVec.maxval();
01472 dif = max-min;
01473 if (dif ==0)
01474 dif=1.0;
01475 if (max)
01476 for (mrs_natural c=0; c < rows_; ++c)
01477 {
01478 (*this)(c, r) -= min;
01479 (*this)(c, r) /= dif;
01480 }
01481 }
01482 }
01483
01484 void
01485 realvec::correlation(realvec& res) const
01486 {
01487
01488
01489
01490 if (size_ == 0)
01491 {
01492 MRSERR("realvec::correlation() : empty input matrix! returning empty correlation matrix!");
01493 res.create(0);
01494 return;
01495 }
01496 if (this != &res)
01497 {
01498 res.stretch(rows_, rows_);
01499
01500 realvec temp = (*this);
01501 temp.normObs();
01502
01503 mrs_real sum = 0.0;
01504 for (mrs_natural r1=0; r1< rows_; ++r1)
01505 {
01506 for (mrs_natural r2=0; r2 < rows_; ++r2)
01507 {
01508 sum = 0.0;
01509
01510 for (mrs_natural c=0; c < cols_; ++c)
01511 sum += (temp(r1,c) * temp(r2,c));
01512
01513 sum /= cols_;
01514 res(r1,r2) = sum;
01515 }
01516 }
01517 }
01518 else
01519 {
01520 res.create(0);
01521 MRSERR("realvec::correlation() - inPlace operation not supported - returning empty result vector!");
01522 }
01523 }
01524
01525 void
01526 realvec::covariance(realvec& res) const
01527 {
01528
01529
01530
01531 if (size_ == 0)
01532 {
01533 MRSERR("realvec::covariance(): empty input matrix! returning empty covariance matrix!");
01534 res.create(0);
01535 return;
01536 }
01537
01538 if (this != &res)
01539 {
01540 res.stretch(rows_, rows_);
01541
01542 if (cols_ < (rows_ + 1))
01543 {
01544 MRSWARN("realvec::covariance() : nr. data points < nr. observations + 1 => covariance matrix is SINGULAR!");
01545 }
01546 if ( (mrs_real)cols_ < ((mrs_real)rows_*(mrs_real)(rows_-1.0)/2.0))
01547 {
01548 MRSWARN("realvec::covariance() : too few data points => ill-calculation of covariance matrix!");
01549 }
01550
01551 realvec meanobs;
01552 this->meanObs(meanobs);
01553
01554 mrs_real sum = 0.0;
01555 for (mrs_natural r1=0; r1< rows_; ++r1)
01556 {
01557 for (mrs_natural r2=0; r2 < rows_; ++r2)
01558 {
01559 sum = 0.0;
01560 for (mrs_natural c=0; c < cols_; ++c)
01561 sum += ((data_[c * rows_ + r1] - meanobs(r1)) * (data_[c * rows_ + r2]- meanobs(r2)));
01562
01563 if (cols_ > 1)
01564 sum /= (cols_ - 1);
01565 else
01566 sum /= cols_;
01567
01568 res(r1,r2) = sum;
01569 }
01570 }
01571 }
01572 else
01573 {
01574 res.create(0);
01575 MRSERR("realvec::covariance() - inPlace operation not supported - returning empty result vector!");
01576 }
01577 }
01578
01579 void
01580 realvec::covariance2(realvec& res) const
01581 {
01582
01583
01584
01585
01586 if (size_ == 0)
01587 {
01588 MRSERR("realvec::covariance() : empty input matrix! returning empty and invalid covariance matrix!");
01589 res.create(0);
01590 return;
01591 }
01592 if (this != &res)
01593 {
01594 res.stretch(rows_, rows_);
01595
01596 if (cols_ < (rows_ + 1))
01597 {
01598 MRSWARN("realvec::covariance() : nr. data points < nr. observations + 1 => covariance matrix is SINGULAR!");
01599 }
01600 if ( (mrs_real)cols_ < ((mrs_real)rows_*(mrs_real)(rows_-1.0)/2.0))
01601 {
01602 MRSWARN("realvec::covariance() : too few data points => ill-calculation of covariance matrix!");
01603 }
01604
01605 for (mrs_natural r1=0; r1< rows_; ++r1)
01606 {
01607 for (mrs_natural r2=0; r2 < rows_; ++r2)
01608 {
01609 mrs_real sum(0);
01610
01611 for (mrs_natural c=0; c < cols_; ++c)
01612 sum += (data_[c * rows_ + r1] * data_[c * rows_ + r2]);
01613
01614 sum /= cols_;
01615 res(r1,r2) = sum;
01616 }
01617 }
01618 }
01619 else
01620 {
01621 res.create(0);
01622 MRSERR("realvec::covariance2() - inPlace operation not supported - returning empty result vector!");
01623 }
01624 }
01625
01626 mrs_real
01627 realvec::trace() const
01628 {
01629 if (cols_ != rows_)
01630 {
01631 MRSWARN("realvec::trace() - matrix is not square!");
01632 }
01633
01634 mrs_real res(0);
01635
01636 for (mrs_natural i = 0; i < size_;)
01637 {
01638 res += data_[i];
01639 i += cols_+1;
01640 }
01641
01642 return res;
01643 }
01644
01645 mrs_real
01646 realvec::det() const
01647 {
01648 NumericLib numlib;
01649 return numlib.determinant(*this);
01650 }
01651
01652
01653
01654
01655
01656
01657
01658 void realvec::allocateData(mrs_natural size)
01659 {
01660
01661 delete[] data_;
01662 data_ = NULL;
01663
01664 allocatedSize_ = size > 0 ? size : 1;
01665 data_ = new mrs_real[allocatedSize_];
01666
01667 for (mrs_natural i=0; i<allocatedSize_; ++i)
01668 data_[i] = 0.0;
01669 }
01670
01671
01673
01675
01676
01677
01678
01679
01680
01681 bool realvec::operator==(const realvec &v1) const
01682 {
01683
01684 if (v1.getRows()!=rows_) return false;
01685 if (v1.getCols()!=cols_) return false;
01686
01687 for(int r=0;r<v1.getRows();++r)
01688 {
01689 for(int c=0;c<v1.getCols();++c)
01690 {
01691 if(v1(r,c)!=data_[c * rows_ + r])return false;
01692 }
01693 }
01694 return true;
01695 }
01696
01697
01698 bool realvec::operator!=(const realvec &v1) const
01699 {
01700 return !(*this == v1);
01701 }
01702
01703
01704 realvec&
01705 realvec::operator/=(const mrs_real val)
01706 {
01707 for (mrs_natural i=0; i<size_; ++i)
01708 data_[i] /= val;
01709 return *this;
01710 }
01711
01712
01713
01714 realvec&
01715 realvec::operator*=(const mrs_real val)
01716 {
01717 for (mrs_natural i=0; i<size_; ++i)
01718 data_[i] *= val;
01719 return *this;
01720 }
01721
01722
01723 realvec&
01724 realvec::operator-=(const mrs_real val)
01725 {
01726 for (mrs_natural i=0; i<size_; ++i)
01727 data_[i] -= val;
01728 return *this;
01729 }
01730
01731
01732 realvec&
01733 realvec::operator+=(const mrs_real val)
01734 {
01735 for (mrs_natural i=0; i<size_; ++i)
01736 data_[i] += val;
01737 return *this;
01738 }
01739
01740
01741 realvec&
01742 realvec::operator+=(const realvec& vec)
01743 {
01744 for (mrs_natural i=0; i<size_; ++i)
01745 data_[i] += vec.data_[i];
01746 return *this;
01747 }
01748
01749
01750 realvec&
01751 realvec::operator-=(const realvec& vec)
01752 {
01753 for (mrs_natural i=0; i<size_; ++i)
01754 data_[i] -= vec.data_[i];
01755 return *this;
01756 }
01757
01758
01759 realvec&
01760 realvec::operator*=(const realvec& vec)
01761 {
01762 for (mrs_natural i=0; i<size_; ++i)
01763 data_[i] *= vec.data_[i];
01764 return *this;
01765 }
01766
01767
01768 realvec&
01769 realvec::operator/=(const realvec& vec)
01770 {
01771 for (mrs_natural i=0; i<size_; ++i)
01772 {
01773 MRSASSERT(vec.data_[i] != 0);
01774 data_[i] /= vec.data_[i];
01775 }
01776 return *this;
01777 }
01778
01789 void realvec::matrixMulti(const mrs_realvec& a,const mrs_realvec& b,mrs_realvec& out)
01790 {
01791
01792
01793 MRSASSERT(a.getCols()==b.getRows());
01794 MRSASSERT(out.getRows()==a.getRows());
01795 MRSASSERT(out.getCols()==b.getCols());
01796
01797 out.setval (0.);
01798
01799 for (mrs_natural r=0;r<out.getRows();++r)
01800 {
01801 for (mrs_natural c=0;c<out.getCols();++c)
01802 {
01803 for (mrs_natural i=0;i<a.getCols();++i)
01804 {
01805 out(r,c)+=a(r,i)*b(i,c);
01806 }
01807 }
01808 }
01809 }
01810
01811
01812
01821 mrs_real realvec::getValueFenced(const mrs_natural i) const
01822 {
01823
01824 if (i < 0 || i >= (mrs_natural)size_) {
01825
01826 throw std::out_of_range("realvec indexing out of bounds.");
01827 }
01828 return data_[i];
01829 }
01830
01840 mrs_real realvec::getValueFenced(const mrs_natural r, const mrs_natural c) const
01841 {
01842 if (r < 0 || r >= rows_ || c < 0 || c >= cols_) {
01843
01844 throw std::out_of_range("realvec indexing out of bounds.");
01845 }
01846 return data_[c * rows_ + r];
01847 }
01848
01859 mrs_real& realvec::getValueFenced(const mrs_natural i)
01860 {
01861 if (i < 0 || i >= (mrs_natural)size_) {
01862
01863 throw std::out_of_range("realvec indexing out of bounds.");
01864 }
01865 return data_[i];
01866 }
01867
01879 mrs_real& realvec::getValueFenced(const mrs_natural r, const mrs_natural c)
01880 {
01881 if (r < 0 || r >= rows_ || c < 0 || c >= cols_) {
01882
01883 throw std::out_of_range("realvec indexing out of bounds.");
01884 }
01885 return data_[c * rows_ + r];
01886 }
01887
01888
01889 }