GURLS++  2.0.00
C++ Implementation of GURLS Matlab Toolbox
gvec.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_GVEC_H_
00043 #define  _GURLS_GVEC_H_
00044 
00045 #include <iostream>
00046 #include <cstring>
00047 #include <vector>
00048 #include <cassert>
00049 #include <queue>
00050 #include <sstream>
00051 #include <typeinfo>
00052 
00053 
00054 #include "gurls++/basearray.h"
00055 #include "gurls++/gmath.h"
00056 #include "gurls++/exceptions.h"
00057 
00058 
00059 namespace gurls {
00060 
00061 template <typename T>
00062 class gMat2D;
00063 
00069 template <typename T>
00070 class gVec : public BaseArray<T> {
00071 
00072 public:
00076     gVec(unsigned long n = 0);
00077 
00081     gVec(T* buf, unsigned long n, bool ownership = true);
00082 
00086     gVec(const gVec<T>& other);
00087 
00091     gVec<T>& operator=(const gVec<T>& other);
00092 
00096     static gVec<T> zeros(unsigned long n = 0);
00097 
00101     static gVec<T> rand(unsigned long n = 0);
00102 
00106     gVec<unsigned long> nonzeros() const;
00107 
00111     void clear();
00112 
00116     gVec<T>& operator=(const T& val) { return static_cast<gVec<T>&>(this->BaseArray<T>::operator=(val)); }
00117 
00118     using BaseArray<T>::set;
00119 
00123     void set(const gVec<T>& v, unsigned long start=0) { this->set(v.data(), v.size(), start); }
00124 
00128     const T& at(unsigned long i) const { return this->data[i]; }
00129 
00133     T& at(unsigned long i) { return this->data[i]; }
00134 
00138     const T& operator[](unsigned long i) const {
00139 #ifdef DEBUG
00140         if (i < 0 || i >= this->size)
00141             throw gException("Index exceeds array dimension.");
00142 #endif // DEBUG
00143         return at(i);
00144     }
00145 
00149     T& operator[](unsigned long i) {
00150 #ifdef DEBUG
00151         if (i < 0 || i >= this->size)
00152             throw gException("Index exceeds array dimension.");
00153 #endif // DEBUG
00154         return at(i);
00155     }
00156 
00160     gVec<T> subvec(unsigned int len, unsigned int start=0) const;
00161 
00165     gVec<T> operator-() const { return static_cast<T>(-1)*(*this); }
00166 
00167     using BaseArray<T>::operator+=;
00168 
00172     gVec<T> operator+(T) const;
00173 
00177     template <typename U>
00178     friend gVec<U> operator+(U val, const gVec<U>& v);
00179 
00180 
00181     using BaseArray<T>::operator-=;
00182 
00186     gVec<T> operator-(T val) const { return *this + (-val); }
00187 
00191     template <typename U>
00192     friend gVec<U> operator-(U val, const gVec<U>& v);
00193 
00194     using BaseArray<T>::operator*=;
00195 
00199     gVec<T> operator*(T) const;
00200 
00204     template <typename U>
00205     friend gVec<U> operator*(U val, const gVec<U>& v);
00206 
00207     using BaseArray<T>::operator/=;
00208 
00212     gVec<T> operator/(T val) const { return *this * (static_cast<T>(1)/val); }
00213 
00217     template <typename U>
00218     friend gVec<U> operator/(U val, const gVec<U>& v);
00219 
00223     gVec<T>& operator+=(const gVec<T>&);
00224 
00228     gVec<T> operator+(const gVec<T>&) const;
00229 
00233     gVec<T>& operator-=(const gVec<T>& v);
00234 
00238     gVec<T> operator-(const gVec<T>& v) const;
00239 
00243     gVec<T>& operator*=(const gVec<T>&);
00244 
00248     gVec<T> operator*(const gVec<T>&) const;
00249 
00253     gVec<T>& operator/=(const gVec<T>&);
00254 
00258     gVec<T> operator/(const gVec<T>&) const;
00259 
00263     gVec<T>& reciprocal() const;
00264 
00268     gMat2D<T>& asMatrix(bool ascolumn = true){
00269         unsigned long int r = this->size;
00270         unsigned long int c = 1;
00271         if (!ascolumn){
00272             r = 1;
00273             c = this->size;
00274         }
00275         gMat2D<T>* mat = new gMat2D<T>(this->data, r, c, true);
00276         return *mat;
00277     }
00278 
00282     void isequal(const T& value, std::vector<int>& indices);
00283 
00287     template <typename U>
00288     friend bool operator== (const gVec<U>&, const U&);
00289 
00293     template <typename U>
00294     friend std::ostream& operator<<(std::ostream&, const gVec<U>&);
00295 
00296 //    template <typename U>
00297 //    friend void dot(const gMat2D<U>&, const gVec<U>&, gVec<U>&);
00298 
00299 //    template <typename U>
00300 //    friend void dot(const gVec<T>& x, const gVec<T>& y);
00301 
00305     virtual std::string what() const {
00306         std::stringstream v("gVec:");
00307         v << std::string(" vector of length ");
00308         v << this->getSize();
00309         v <<  std::string("and type ");
00310         v << typeid(T).name();
00311         return v.str();
00312     }
00313 
00317     T argmin() const;
00318 
00322     T argmax() const;
00323 
00327     gVec<T>& copyLocations(const gVec<T> locs);
00328 
00329 };
00330 
00331 
00332 }
00333 
00334 #include "gvec.hpp"
00335 
00336 #endif //  _GURLS_GVEC_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends