Marsyas  0.2
/home/gperciva/src/marsyas/src/marsyas/realvec.h
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 <cmath>
00023 #include <cfloat>
00024 #include <cstring>
00025 #include <string>
00026 #include <iostream>
00027 #include <fstream>
00028 #include <stdexcept>
00029 
00030 
00031 #include "common_header.h" 
00032 #include "MrsLog.h"
00033 #include "Communicator.h"
00034 
00035 namespace Marsyas
00036 {
00072     class marsyas_EXPORT realvec
00073     {
00074 
00075       public:
00076         realvec();
00077         realvec(mrs_natural size);
00078         realvec(mrs_natural rows, mrs_natural cols);
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         realvec& operator+=(const realvec& vec);
00141         realvec& operator-=(const realvec& vec);
00142         realvec& operator*=(const realvec& vec);
00143         realvec& operator/=(const realvec& vec);
00144         realvec& operator*=(const mrs_real val);
00145         realvec& operator/=(const mrs_real val);
00146         realvec& operator+=(const mrs_real val);
00147         realvec& operator-=(const mrs_real val);
00148 
00149         marsyas_EXPORT friend realvec operator+(const realvec& vec1, const realvec& vec2);
00150         marsyas_EXPORT friend realvec operator-(const realvec& vec1, const realvec& vec2);
00151         marsyas_EXPORT friend realvec operator*(const realvec& vec1, const realvec& vec2);
00152         marsyas_EXPORT friend realvec operator/(const realvec& vec1, const realvec& vec2);
00153     
00154         bool operator!=(const realvec& v1) const;
00155         bool operator==(const realvec &v1) const;
00156     
00158 
00161 
00162     
00163         mrs_real& operator()(const mrs_natural i);
00164         mrs_real operator()(const mrs_natural i) const;
00165         mrs_real& operator()(const mrs_natural r, const mrs_natural c);
00166         mrs_real operator()(const mrs_natural r, const mrs_natural c) const;
00167 
00168         mrs_real getValueFenced(const mrs_natural i) const;
00169         mrs_real getValueFenced(const mrs_natural r, const mrs_natural c) const;
00170         mrs_real& getValueFenced(const mrs_natural i);
00171         mrs_real& getValueFenced(const mrs_natural r, const mrs_natural c);
00172 
00173 
00175 
00176 
00179 
00180         realvec operator()(std::string r, std::string c);
00182         realvec operator()(std::string r);
00184         void getRow(const mrs_natural r, realvec& res) const;
00186         void getCol(const mrs_natural c, realvec& res) const;
00188         void getSubMatrix (const mrs_natural r, const mrs_natural c, realvec& res);
00189         // set row in matrix
00190         void setRow (const mrs_natural r, const realvec src);
00191         // set col in matrix
00192         void setCol (const mrs_natural c, const realvec src);
00193         // set submatrix
00194         void setSubMatrix (const mrs_natural r, const mrs_natural c, const realvec src) ;
00196 
00199         void debug_info();
00200         void dump();
00201         bool write(std::string filename) const;
00202         bool read(std::string filename);
00203 
00205         void dumpDataOnly(std::ostream& o=std::cout, std::string columnSep=" ", std::string rowSep="\n") const;
00206 
00207         marsyas_EXPORT friend std::ostream& operator<<(std::ostream&, const realvec&);
00208         marsyas_EXPORT friend std::istream& operator>>(std::istream&, realvec&);
00210         bool readText(std::string filename);
00212         bool writeText(std::string filename);
00214 
00217         void meanObs(realvec& res) const;
00218         void stdObs(realvec& res) const;
00219         void varObs(realvec& res) const;
00220         void normObs();
00221         void normSpl(mrs_natural=0);
00222         void normObsMinMax();
00223         void normSplMinMax(mrs_natural=0);
00225 
00228         mrs_real maxval(mrs_natural *index=NULL) const;
00229         mrs_real minval() const;
00230         mrs_real mean() const;
00231         mrs_real median() const;
00232         mrs_real sum() const;
00233         mrs_real std() const;
00234         mrs_real var() const;
00235         void sort();//non-descending sort - assumes one dimensional vector only!
00236         void abs();
00237         void sqr();
00238         void sqroot();
00239         void norm();
00240         void normMaxMin();
00241         void pow (mrs_real exp);
00242 
00243         static void matrixMulti(const mrs_realvec& a,const mrs_realvec& b,mrs_realvec& out);
00244         void norm(mrs_real mean, mrs_real std);
00245         void renorm(mrs_real old_mean, mrs_real old_std, mrs_real new_mean, mrs_real new_std);
00246         mrs_natural invert(realvec& res);//lmartins: this seems to be both inplace and returning the inverse matrix in "res"... why both?!? [!][?]
00247         void transpose();
00248         void fliplr ();
00249         void flipud ();
00250         void covariance(realvec& res) const; //Typical covariance calculation, as performed by MATLAB cov().
00251         void covariance2(realvec& res) const;//Marsyas0.1 covariance calculation (assumes standardized data, and uses biased estimation)
00252         void correlation(realvec& res) const;
00253         mrs_real trace() const;
00254         mrs_real det() const;
00256 
00259         void send(Communicator *com);
00260         mrs_natural search(mrs_real val);
00262     
00263 
00264       private:
00265         void allocateData(mrs_natural size);
00267         mrs_natural  size_;
00269         mrs_natural allocatedSize_;
00271         mrs_real *data_;
00273         mrs_natural rows_;
00275         mrs_natural cols_;
00276     
00277 
00278     
00279     };
00280     
00281     inline
00282     mrs_real realvec::operator()(const mrs_natural r, const mrs_natural c) const
00283     {
00284         
00285         MRSASSERT(r < rows_);
00286         MRSASSERT(c < cols_);
00287         
00288         return data_[c * rows_ + r];
00289     }
00290     
00291     inline
00292     mrs_real& realvec::operator()(const mrs_natural r, const mrs_natural c)
00293     {
00294         MRSASSERT(r < rows_);
00295         MRSASSERT(c < cols_);
00296         MRSASSERT(r >= 0);
00297         MRSASSERT(c >= 0);
00298         
00299         return data_[c * rows_ + r];
00300     }
00301     
00302     
00303     inline 
00304     mrs_real realvec::operator()(const mrs_natural i) const
00305     {
00306         MRSASSERT(i < size_);
00307         return data_[i];
00308     }
00309     
00310     
00311     inline 
00312     mrs_real& realvec::operator()(const mrs_natural i)
00313     {
00314         
00315         MRSASSERT(i < size_);
00316         return data_[i];
00317     }
00318 
00319 
00320     
00321 
00322 
00323 }//namespace Marsyas
00324 
00325 
00326 #endif /* !MARSYAS_REALVEC_H */
00327