Marsyas  0.5.0-beta1
/Users/jleben/code/marsyas/src/marsyas/realvec.h
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2010 George Tzanetakis <gtzan@cs.uvic.ca>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #ifndef MARSYAS_REALVEC_H
00020 #define MARSYAS_REALVEC_H
00021 
00022 #include <marsyas/common_header.h>
00023 
00024 #include <cmath>
00025 #include <cfloat>
00026 #include <cstring>
00027 #include <string>
00028 #include <iostream>
00029 #include <fstream>
00030 #include <stdexcept>
00031 
00032 namespace Marsyas
00033 {
00034   class Communicator;
00035 
00071 class marsyas_EXPORT realvec
00072 {
00073 
00074 public:
00075   realvec();
00076   explicit realvec(mrs_natural size);
00077   explicit realvec(mrs_natural rows, mrs_natural cols, mrs_real value = 0.0);
00078 
00079   realvec(const realvec& a);
00080   ~realvec(); // not mean to be inherited from
00081 
00082   realvec& operator=(const realvec& a);
00083 
00086   void allocate(mrs_natural size);
00087   void allocate(mrs_natural rows, mrs_natural cols);
00088 
00090   void create(mrs_natural size);
00092   void create(mrs_natural rows, mrs_natural cols);
00094   void create(mrs_real val, mrs_natural rows, mrs_natural cols);
00095 
00097   void stretch(mrs_natural rows, mrs_natural cols);
00099   void stretch(mrs_natural size);
00100 
00102   void stretchWrite(const mrs_natural pos, const mrs_real val);
00104   void stretchWrite(const mrs_natural r, const mrs_natural c, const mrs_real val);
00106 
00109 
00110   void setval(mrs_natural start, mrs_natural end, mrs_real val);
00112   void setval(mrs_real val);
00115   void appendRealvec(const realvec newValues);
00117 
00120 
00121   void apply(mrs_real (*func) (mrs_real));
00123 
00126   mrs_natural getSize() const;
00127   mrs_natural getCols() const;
00128   mrs_natural getRows() const;
00130   realvec getSubVector(mrs_natural startPos, mrs_natural length) const;
00132   mrs_real *getData() const;
00134 
00135 
00136   void shuffle();
00137 
00140 
00141   bool operator!=(const realvec& v1) const;
00142   bool operator==(const realvec &v1) const;
00143   realvec& operator+=(const realvec& vec);
00144   realvec& operator-=(const realvec& vec);
00145   realvec& operator*=(const realvec& vec);
00146   realvec& operator/=(const realvec& vec);
00147 
00148   template <typename T>
00149   realvec& operator*=(const T & rhs)
00150   {
00151     for (mrs_natural i = 0; i < size_; ++i)
00152       data_[i] *= rhs;
00153     return *this;
00154   }
00155 
00156   template <typename T>
00157   realvec& operator/=(const T & rhs)
00158   {
00159     for (mrs_natural i = 0; i < size_; ++i)
00160       data_[i] /= rhs;
00161     return *this;
00162   }
00163 
00164   template <typename T>
00165   realvec& operator+=(const T & rhs)
00166   {
00167     for (mrs_natural i = 0; i < size_; ++i)
00168       data_[i] += rhs;
00169     return *this;
00170   }
00171 
00172   template <typename T>
00173   realvec& operator-=(const T & rhs)
00174   {
00175     for (mrs_natural i = 0; i < size_; ++i)
00176       data_[i] -= rhs;
00177     return *this;
00178   }
00179 
00181 
00184 
00185 
00186   mrs_real& operator()(const mrs_natural i);
00187   mrs_real operator()(const mrs_natural i) const;
00188   mrs_real& operator()(const mrs_natural r, const mrs_natural c);
00189   mrs_real operator()(const mrs_natural r, const mrs_natural c) const;
00190 
00191   mrs_real getValueFenced(const mrs_natural i) const;
00192   mrs_real getValueFenced(const mrs_natural r, const mrs_natural c) const;
00193   mrs_real& getValueFenced(const mrs_natural i);
00194   mrs_real& getValueFenced(const mrs_natural r, const mrs_natural c);
00195 
00196 
00198 
00199 
00202 
00203   realvec operator()(std::string r, std::string c);
00205   realvec operator()(std::string r);
00207   void getRow(const mrs_natural r, realvec& res) const;
00209   void getCol(const mrs_natural c, realvec& res) const;
00211   void getSubMatrix (const mrs_natural r, const mrs_natural c, realvec& res);
00212   // set row in matrix
00213   void setRow (const mrs_natural r, const realvec src);
00214   // set col in matrix
00215   void setCol (const mrs_natural c, const realvec src);
00216   // set submatrix
00217   void setSubMatrix (const mrs_natural r, const mrs_natural c, const realvec src) ;
00219 
00222   void debug_info();
00223   void dump();
00224   bool write(std::string filename) const;
00225   bool read(std::string filename);
00226 
00228   void dumpDataOnly(std::ostream& o=std::cout, std::string columnSep=" ", std::string rowSep="\n") const;
00229 
00230   marsyas_EXPORT friend std::ostream& operator<<(std::ostream&, const realvec&);
00231   marsyas_EXPORT friend std::istream& operator>>(std::istream&, realvec&);
00233   bool readText(std::string filename);
00235   bool writeText(std::string filename);
00237 
00240   void meanObs(realvec& res) const;
00241   void stdObs(realvec& res) const;
00242   void varObs(realvec& res) const;
00243   void normObs();
00244   void normSpl(mrs_natural=0);
00245   void normObsMinMax();
00246   void normSplMinMax(mrs_natural=0);
00248 
00251   mrs_real maxval(mrs_natural *index=NULL) const;
00252   mrs_real minval() const;
00253   mrs_real mean() const;
00254   mrs_real median() const;
00255   mrs_real sum() const;
00256   mrs_real std() const;
00257   mrs_real var() const;
00258   void sort();//non-descending sort - assumes one dimensional vector only!
00259   void abs();
00260   void sqr();
00261   void sqroot();
00262   void norm();
00263   void normMaxMin();
00264   void pow (mrs_real exp);
00265 
00266   static void matrixMulti(const mrs_realvec& a,const mrs_realvec& b,mrs_realvec& out);
00267   void norm(mrs_real mean, mrs_real std);
00268   void renorm(mrs_real old_mean, mrs_real old_std, mrs_real new_mean, mrs_real new_std);
00269   mrs_natural invert(realvec& res);//lmartins: this seems to be both inplace and returning the inverse matrix in "res"... why both?!? [!][?]
00270   void transpose();
00271   void fliplr ();
00272   void flipud ();
00273   void covariance(realvec& res) const; //Typical covariance calculation, as performed by MATLAB cov().
00274   void covariance2(realvec& res) const;//Marsyas0.1 covariance calculation (assumes standardized data, and uses biased estimation)
00275   void correlation(realvec& res) const;
00276   mrs_real trace() const;
00277   mrs_real det() const;
00279 
00282   void send(Communicator *com);
00283   mrs_natural search(mrs_real val);
00285 
00286 
00287 private:
00288   void allocateData(mrs_natural size);
00290   mrs_natural  size_;
00292   mrs_natural allocatedSize_;
00294   mrs_real *data_;
00296   mrs_natural rows_;
00298   mrs_natural cols_;
00299 
00300 
00301 
00302 };
00303 
00304 inline
00305 mrs_real realvec::operator()(const mrs_natural r, const mrs_natural c) const
00306 {
00307 
00308   MRSASSERT(r < rows_);
00309   MRSASSERT(c < cols_);
00310 
00311   return data_[c * rows_ + r];
00312 }
00313 
00314 inline
00315 mrs_real& realvec::operator()(const mrs_natural r, const mrs_natural c)
00316 {
00317   MRSASSERT(r < rows_);
00318   MRSASSERT(c < cols_);
00319   MRSASSERT(r >= 0);
00320   MRSASSERT(c >= 0);
00321 
00322   return data_[c * rows_ + r];
00323 }
00324 
00325 
00326 inline
00327 mrs_real realvec::operator()(const mrs_natural i) const
00328 {
00329   MRSASSERT(i < size_);
00330   return data_[i];
00331 }
00332 
00333 
00334 inline
00335 mrs_real& realvec::operator()(const mrs_natural i)
00336 {
00337 
00338   MRSASSERT(i < size_);
00339   return data_[i];
00340 }
00341 
00342 // Non-template operators between two realvecs are required
00343 // to avoid ambiguity among the templates below for lhs or rhs
00344 // operands.
00345 
00346 inline
00347 realvec operator*( const realvec & lhs, const realvec & rhs )
00348 {
00349   realvec result(lhs);
00350   result *= rhs;
00351   return result;
00352 }
00353 
00354 inline
00355 realvec operator/( const realvec & lhs, const realvec & rhs )
00356 {
00357   realvec result(lhs);
00358   result /= rhs;
00359   return result;
00360 }
00361 
00362 inline
00363 realvec operator+( const realvec & lhs, const realvec & rhs )
00364 {
00365   realvec result(lhs);
00366   result += rhs;
00367   return result;
00368 }
00369 
00370 inline
00371 realvec operator-( const realvec & lhs, const realvec & rhs )
00372 {
00373   realvec result(lhs);
00374   result -= rhs;
00375   return result;
00376 }
00377 
00378 // Operators with right-hand-side operand as template parameter
00379 
00380 template <typename RHS> inline
00381 realvec operator*( const realvec & lhs, const RHS & rhs )
00382 {
00383   realvec result(lhs);
00384   result *= rhs;
00385   return result;
00386 }
00387 
00388 template <typename RHS> inline
00389 realvec operator/( const realvec & lhs, const RHS & rhs )
00390 {
00391   realvec result(lhs);
00392   result /= rhs;
00393   return result;
00394 }
00395 
00396 template <typename RHS> inline
00397 realvec operator+( const realvec & lhs, const RHS & rhs )
00398 {
00399   realvec result(lhs);
00400   result += rhs;
00401   return result;
00402 }
00403 
00404 template <typename RHS> inline
00405 realvec operator-( const realvec & lhs, const RHS & rhs )
00406 {
00407   realvec result(lhs);
00408   result -= rhs;
00409   return result;
00410 }
00411 
00412 // Operators with left-hand-side operand as template parameter
00413 
00414 template <typename LHS>
00415 inline
00416 realvec operator*( const LHS & lhs, const realvec & rhs )
00417 {
00418   realvec result;
00419   result.allocate(rhs.getRows(), rhs.getCols());
00420   for (mrs_natural i = 0; i < result.getSize(); ++i)
00421     result(i) = lhs * rhs(i);
00422   return result;
00423 }
00424 
00425 template <typename LHS>
00426 inline
00427 realvec operator/( const LHS & lhs, const realvec & rhs )
00428 {
00429   realvec result;
00430   result.allocate(rhs.getRows(), rhs.getCols());
00431   for (mrs_natural i = 0; i < result.getSize(); ++i)
00432     result(i) = lhs / rhs(i);
00433   return result;
00434 }
00435 
00436 template <typename LHS>
00437 inline
00438 realvec operator+( const LHS & lhs, const realvec & rhs )
00439 {
00440   realvec result;
00441   result.allocate(rhs.getRows(), rhs.getCols());
00442   for (mrs_natural i = 0; i < result.getSize(); ++i)
00443     result(i) = lhs + rhs(i);
00444   return result;
00445 }
00446 
00447 template <typename LHS>
00448 inline
00449 realvec operator-( const LHS & lhs, const realvec & rhs )
00450 {
00451   realvec result;
00452   result.allocate(rhs.getRows(), rhs.getCols());
00453   for (mrs_natural i = 0; i < result.getSize(); ++i)
00454     result(i) = lhs - rhs(i);
00455   return result;
00456 }
00457 
00458 }//namespace Marsyas
00459 
00460 
00461 #endif /* !MARSYAS_REALVEC_H */
00462