GURLS++  2.0.00
C++ Implementation of GURLS Matlab Toolbox
gmat2d.h
00001 /*
00002  * The GURLS Package in C++
00003  *
00004  * Copyright (C) 2011-1013, IIT@MIT Lab
00005  * All rights reserved.
00006  *
00007  * author:  M. Santoro
00008  * email:   msantoro@mit.edu
00009  * website: http://cbcl.mit.edu/IIT@MIT/IIT@MIT.html
00010  *
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions
00013  * are met:
00014  *
00015  *     * Redistributions of source code must retain the above
00016  *       copyright notice, this list of conditions and the following
00017  *       disclaimer.
00018  *     * Redistributions in binary form must reproduce the above
00019  *       copyright notice, this list of conditions and the following
00020  *       disclaimer in the documentation and/or other materials
00021  *       provided with the distribution.
00022  *     * Neither the name(s) of the copyright holders nor the names
00023  *       of its contributors or of the Massacusetts Institute of
00024  *       Technology or of the Italian Institute of Technology may be
00025  *       used to endorse or promote products derived from this software
00026  *       without specific prior written permission.
00027  *
00028  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00029  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00030  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00031  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00032  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00033  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00034  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00035  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00036  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00037  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00038  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00039  * POSSIBILITY OF SUCH DAMAGE.
00040  */
00041 
00042 #ifndef  _GURLS_GMAT2D_H_
00043 #define  _GURLS_GMAT2D_H_
00044 
00045 
00046 #include <iostream>
00047 #include <string>
00048 #include <sstream>
00049 #include <vector>
00050 #include <cassert>
00051 #include <queue>
00052 #include <limits>
00053 #include <typeinfo>
00054 
00055 #include <boost/serialization/base_object.hpp>
00056 #include <boost/serialization/split_member.hpp>
00057 
00058 #include "gurls++/basearray.h"
00059 #include "gurls++/exceptions.h"
00060 #include "gurls++/gvec.h"
00061 #include "gurls++/gmath.h"
00062 
00063 namespace gurls {
00064 
00065 static const int COLUMNWISE = 0; 
00066 static const int ROWWISE = 1;    
00067 
00068 
00069 template <typename T>
00070 class gVec;
00071 
00077 template <typename T>
00078 class gMat2D : public BaseArray<T> {
00079 
00080 protected:
00081 
00082     unsigned long numcols;  
00083     unsigned long numrows;  
00084 
00085 public:
00086 
00090     gMat2D(unsigned long r = 0, unsigned long c = 0);
00091 
00095     gMat2D(T* buf, unsigned long r, unsigned long c, bool owner);
00096 
00100     gMat2D(const gMat2D<T>& other);
00101 
00105     gMat2D<T>& operator=(const gMat2D<T>& other);
00106 
00110     static gMat2D<T> zeros(unsigned long r = 0, unsigned long c = 0);
00111 
00115     static gMat2D<T> rand(unsigned long r = 0, unsigned long c = 0);
00116 
00120     static gMat2D<T> diag(gVec<T> &diagonal);
00121 
00125     static gMat2D<T> eye(unsigned long n = 0);
00126 
00130     void resize(unsigned long r, unsigned long c);
00131 
00135     void reshape(unsigned long r, unsigned long c);
00136 
00140     unsigned long cols() const {return numcols; }
00141 
00145     unsigned long rows() const {return numrows; }
00146 
00150     gMat2D<T>& operator=(const T& val)
00151     {
00152         return static_cast<gMat2D<T>&>(this->BaseArray<T>::operator=(val));
00153     }
00154 
00160     void submatrix(const gMat2D<T>& other, unsigned long r, unsigned long c);
00161 
00168     void transpose(gMat2D <T>& transposed) const;
00169 
00173     gVec<T> asvector() const
00174     {
00175         return gVec<T>(this->data, this->size, true);
00176     }
00177 
00181     gVec<T> operator[](unsigned long i) const
00182     {
00183         gVec<T> ret(this->numcols);
00184         gurls::getRow(this->data, this->numrows, this->numcols, i, ret.getData());
00185 
00186         return ret;
00187     }
00188 
00192     gVec<T> operator[](unsigned long i)
00193     {
00194         gVec<T> ret(this->numcols);
00195         gurls::getRow(this->data, this->numrows, this->numcols, i, ret.getData());
00196 
00197         return ret;
00198     }
00199 
00200     //-------------------------------------------------------------------------
00201 
00205     T& operator() (unsigned long row, unsigned long col)
00206     {
00207         return this->data[ row + this->numrows*col];
00208     }
00209 
00213     gVec<T> operator() (unsigned long i)
00214     {
00215         return gVec<T>(this->data+(i*this->numrows), this->numrows, true);
00216     }
00217 
00221     gVec<T> operator() (unsigned long i) const
00222     {
00223         return gVec<T>(this->data+(i*this->numrows), this->numrows, true);
00224     }
00225 
00229     void setRow(gVec<T>& vec, unsigned long i)
00230     {
00231         if(vec.getSize() != this->numcols)
00232             throw gException(Exception_Inconsistent_Size);
00233 
00234         gurls::copy(this->data+i, vec.getData(), this->numcols, this->numrows, 1);
00235     }
00236 
00240     void setColumn(gVec<T>& vec, unsigned long i)
00241     {
00242         if(vec.getSize() != this->numrows)
00243             throw gException(Exception_Inconsistent_Size);
00244 
00245         gurls::copy(this->data+(i*this->numrows), vec.getData(), this->numrows, 1, 1, 1, 1, 1, 1, 1);
00246     }
00247 
00251     void setDiag(gVec<T>& vec)
00252     {
00253         unsigned long int k = std::min(this->numcols, this->numrows);
00254 
00255         if (vec.getSize() < k)
00256             throw gException("The lenght of the vector must be at least equal to the minimum dimension of the matrix");
00257 
00258         gurls::copy(this->data, vec.getData(), std::min(vec.getSize(), k), this->numrows+1, 1);
00259     }
00260 
00264     gMat2D<T> operator-() const { return static_cast<T>(-1)*(*this); }
00265 
00266     using BaseArray<T>::operator+=;
00267 
00271     gMat2D<T> operator+(T) const;
00272 
00276     template <typename U>
00277     friend gMat2D<U> operator+(U val, const gMat2D<U>& v);
00278 
00279     using BaseArray<T>::operator-=;
00280 
00284     gMat2D<T> operator-(T val) const { return *this + (-val); }
00285 
00289     template <typename U>
00290     friend gMat2D<U> operator-(U val, const gMat2D<U>& v);
00291 
00292 
00293     using BaseArray<T>::operator*=;
00297     gMat2D<T> operator*(T) const;
00298 
00302     template <typename U>
00303     friend gMat2D<U> operator*(U val, const gMat2D<U>& v);
00304 
00305     using BaseArray<T>::operator/=;
00306 
00310     gMat2D<T> operator/(T val) const { return *this * (static_cast<T>(1)/val); }
00311 
00315     template <typename U>
00316     friend gMat2D<U> operator/(U val, const gMat2D<U>& v);
00317 
00321     gMat2D<T>& operator+=(const gMat2D<T>&);
00322 
00326     gMat2D<T> operator+(const gMat2D<T>&) const;
00327 
00331     gMat2D<T>& operator-=(const gMat2D<T>& v);
00332 
00336     gMat2D<T> operator-(const gMat2D<T>& v) const;
00337 
00341     gMat2D<T>& operator*=(const gMat2D<T>&);
00342 
00346     gMat2D<T> operator*(const gMat2D<T>&) const;
00347 
00351     gMat2D<T>& operator/=(const gMat2D<T>&);
00352 
00356     gMat2D<T> operator/(const gMat2D<T>&) const;
00357 
00361     bool allEqualsTo(const T& val)  const;
00362 
00366     template <typename U>
00367     friend std::ostream& operator<<(std::ostream&, const gMat2D<U>&);
00368 
00372     template <typename U>
00373     friend void dot(const gMat2D<U>&, const gMat2D<U>&, gMat2D<U>&);
00374 
00378     template <typename U>
00379     friend void dot(const gMat2D<U>&, const gVec<U>&, gVec<U>&);
00380 
00384     void uppertriangular(gMat2D<T>& up) const;
00385 
00389     void lowertriangular(gMat2D<T>& lo) const;
00390 
00391 
00392     static const std::string Less;          
00393     static const std::string Greater;       
00394     static const std::string LessEq;        
00395     static const std::string GreaterEq;     
00396     static const std::string Equal;         
00397 
00403     gMat2D<bool> & operator ==(T threshold) const;
00404 
00410     gMat2D<bool> & operator <(T threshold) const;
00411 
00417     gMat2D<bool> & operator <=(T threshold) const;
00418 
00424     gMat2D<bool> &  operator >(T threshold) const;
00425 
00431     gMat2D<bool> & operator >=(T threshold) const;
00432 
00438     gMat2D<bool> & compare(T& threshold, std::string logical_operator = GreaterEq) const;
00439 
00444     gVec<T>& where(const gMat2D<bool> & comparison) const;
00445 
00449     gMat2D<T>& reciprocal() const;
00450 
00454     virtual std::string what() const
00455     {
00456         std::stringstream v;
00457         v << "gMat2D: (" << this->numrows;
00458         v << " x " << this->numcols;
00459         v << ") matrix of type " << typeid(T).name();
00460         return v.str();
00461     }
00462 
00463     using BaseArray<T>::max;
00464     using BaseArray<T>::min;
00465     using BaseArray<T>::sum;
00466 
00471     gVec<T>* min(int order);
00472 
00477     gVec<T>* max(int order);
00478 
00483     gVec<T>* argmin(int order);
00484 
00489     gVec<T>* argmax(int order);
00490 
00495     gVec<T>* sum(int order) const;
00496 
00497     friend class boost::serialization::access;
00498 
00502     template<class Archive>
00503     void save(Archive & , const unsigned int) const;
00504 
00508     template<class Archive>
00509     void load(Archive & , const unsigned int);
00510 
00511     BOOST_SERIALIZATION_SPLIT_MEMBER()
00512 
00513     
00516     void save(const std::string& fileName) const;
00517 
00521     void load(const std::string& fileName);
00522 
00526     void readCSV(const std::string& fileName, bool colMajor = true);
00527 
00531     void saveCSV(const std::string& fileName) const;
00532 
00533 };
00534 
00535 }
00536 
00537 #include "gmat2d.hpp"
00538 
00539 
00540 #endif // _GURLS_GMAT2D_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends