![]() |
GURLS++
2.0.00
C++ Implementation of GURLS Matlab Toolbox
|
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 namespace gurls { 00043 00044 00045 template <typename T> 00046 gVec<T>::gVec(unsigned long n) { 00047 this->alloc(n); 00048 } 00049 00050 template <typename T> 00051 gVec<T>::gVec(T* buf, unsigned long n, bool owner) { 00052 if (owner) { 00053 this->alloc(n); 00054 this->set(buf, n); 00055 } else { 00056 this->isowner = owner; 00057 this->size = n; 00058 this->data = buf; 00059 }; 00060 } 00061 00062 template <typename T> 00063 gVec<T>::gVec(const gVec<T>& other) { 00064 this->alloc(other.size); 00065 *this = other; 00066 } 00067 00068 template <typename T> 00069 gVec<T>& gVec<T>::operator=(const gVec<T>& other) { 00070 00071 if (this != &other) 00072 this->set(other.data, other.size); 00073 00074 return *this; 00075 } 00076 00077 template <typename T> 00078 gVec<T> gVec<T>::zeros(unsigned long n) { 00079 gVec<T> v(n); 00080 memset(v.data, 0, n*sizeof(T)); 00081 return v; 00082 } 00083 00084 template <typename T> 00085 gVec<T> gVec<T>::rand(unsigned long n) { 00086 gVec<T> v(n); 00087 T* d = v.data; 00088 for (unsigned long i = 0; i < n; i++) { 00089 *d++ = (T)std::rand()/RAND_MAX; 00090 } 00091 return v; 00092 } 00093 00094 template <typename T> 00095 gVec<unsigned long> gVec<T>::nonzeros() const 00096 { 00097 std::queue<unsigned> que; 00098 00099 for(unsigned i=0;i<this->size();i++) 00100 { 00101 if(this->at(i)!=0) que.push(i); 00102 } 00103 gVec<unsigned> indexes(que.size()); 00104 unsigned dim=0; 00105 while(!que.empty()) 00106 { 00107 indexes[dim++]=que.front(); 00108 que.pop(); 00109 } 00110 00111 return indexes; 00112 } 00113 00114 00115 template <typename T> 00116 void gVec<T>::isequal(const T& value, std::vector<int>& indices) { 00117 const T* p = this->data; 00118 for (unsigned int i = 0; i < this->size; ++i, ++p) { 00119 if (*p == value) 00120 indices.push_back(i); 00121 } 00122 } 00123 00124 /* 00125 template <typename T> 00126 void gVec<T>::resize(unsigned long n) { 00127 if (! this->m_owner) { 00128 assert(false); 00129 } 00130 else if (n == this->size) { 00131 return; 00132 } else { 00133 delete[] this->data; 00134 this->alloc(n); 00135 }; 00136 }; 00137 */ 00138 00139 00140 template <typename T> 00141 gVec<T> gVec<T>::subvec(unsigned int len, unsigned int start) const { 00142 gVec<T> v(len); 00143 v.set(&(this->data()[start]), len); 00144 return v; 00145 } 00146 00147 00148 template <typename T> 00149 gVec<T> gVec<T>::operator+(T val) const { 00150 gVec<T> w(*this); 00151 w += val; 00152 return w; 00153 } 00154 00158 template <typename T> 00159 gVec<T> operator+(T val, const gVec<T>& v) { 00160 gVec<T> w(v); 00161 w += val; 00162 return w; 00163 } 00164 00168 template <typename T> 00169 gVec<T> operator-(T val, const gVec<T>& v) { 00170 gVec<T> w(-v); 00171 w += val; 00172 return w; 00173 } 00174 00175 // ------------ MULT SCALAR ------------------------------------------- 00176 00177 /* 00178 template <typename T> 00179 gVec<T>& gVec<T>::operator*=(T val) { 00180 T* ptr = this->data; 00181 for (int i = 0; i < this->size; ++i, ++ptr) { 00182 *ptr *= val; 00183 } 00184 return *this; 00185 }; 00186 */ 00187 00188 template <typename T> 00189 gVec<T> gVec<T>::operator*(T val) const { 00190 gVec<T> w(*this); 00191 w *= val; 00192 return w; 00193 } 00194 00198 template <typename T> 00199 gVec<T> operator*(T val, const gVec<T>& v) { 00200 gVec<T> w(v); 00201 w *= val; 00202 return w; 00203 } 00204 00205 /* 00206 template <typename T> 00207 gVec<T>& gVec<T>::operator/=(T val) { 00208 std::cout << "bla" << std::endl; 00209 T* ptr = this->data; 00210 for (int i = 0; i < this->size; ++i, ++ptr) { 00211 *ptr /= val; 00212 } 00213 return *this; 00214 }; 00215 */ 00216 00220 template <typename T> 00221 gVec<T> operator/(T val, const gVec<T>& v) { 00222 gVec<T> w(v); 00223 w *= (static_cast<T>(1)/val); 00224 return w; 00225 } 00226 00227 // ----------------- SUM OF VECTORS -------------------------- 00228 00229 /* 00230 template <typename T> 00231 gVec<T>& gVec<T>::operator+=(const gVec<T>& v) { 00232 T *ptr = this->data, *ptr_v = v.data; 00233 for (int i = 0; i < this->size; ++i, ++ptr, ++ptr_v) { 00234 *ptr += *ptr_v; 00235 } 00236 return *this; 00237 } 00238 */ 00239 00240 template <typename T> 00241 gVec<T>& gVec<T>::operator+=(const gVec<T>& v) { 00242 return static_cast<gVec<T>&>(BaseArray<T>::add(v)); 00243 } 00244 00245 template <typename T> 00246 gVec<T> gVec<T>::operator+(const gVec<T>& v) const { 00247 gVec<T> w(v); 00248 w += *this; 00249 return w; 00250 } 00251 00252 // ----------------- SUBTRACTION OF VECTORS -------------------------- 00253 00254 template <typename T> 00255 gVec<T>& gVec<T>::operator-=(const gVec<T>& v) { 00256 /* 00257 T *ptr = this->data, *ptr_v = v.data; 00258 for (int i = 0; i < this->size; ++i, ++ptr, ++ptr_v) { 00259 *ptr -= *ptr_v; 00260 } 00261 return *this; 00262 */ 00263 return static_cast<gVec<T>&>(BaseArray<T>::subtract(v)); 00264 } 00265 00266 template <typename T> 00267 gVec<T> gVec<T>::operator-(const gVec<T>& v) const { 00268 gVec<T> w(*this); 00269 w -= v; 00270 return w; 00271 } 00272 00273 // ------------------- MULT TWO VECTORS ------------------------------------ 00274 00275 template <typename T> 00276 gVec<T>& gVec<T>::operator*=(const gVec<T>& v) { 00277 /* 00278 T *ptr = this->data, *ptr_v = v.data; 00279 for (int i = 0; i < this->size; ++i, ++ptr, ++ptr_v) { 00280 *ptr *= *ptr_v; 00281 } 00282 return *this; 00283 */ 00284 return static_cast<gVec<T>&>(BaseArray<T>::multiply(v)); 00285 } 00286 00287 template <typename T> 00288 gVec<T> gVec<T>::operator*(const gVec<T>& v) const { 00289 gVec<T> w(v); 00290 w *= *this; 00291 return w; 00292 } 00293 00294 // ------------------- DIVIDE TWO VECTORS ------------------------------------ 00295 00296 template <typename T> 00297 gVec<T>& gVec<T>::operator/=(const gVec<T>& v) { 00298 /* 00299 T *ptr = this->data, *ptr_v = v.data; 00300 for (int i = 0; i < this->size; ++i, ++ptr, ++ptr_v) { 00301 *ptr /= *ptr_v; 00302 } 00303 return *this; 00304 */ 00305 return static_cast<gVec<T>&>(BaseArray<T>::divide(v)); 00306 } 00307 00308 template <typename T> 00309 gVec<T> gVec<T>::operator/(const gVec<T>& v) const { 00310 gVec<T> w(v); 00311 w /= *this; 00312 return w; 00313 } 00314 00315 00316 // TODO: to implement the comparison between two vectors!! 00317 00321 template <typename U> 00322 bool operator== (const gVec<U>& v, const U& val) { 00323 U *ptr = v.data; 00324 for (int i = 0; i < v.size; ++i, ++ptr) { 00325 if (*ptr != val){ 00326 return false; 00327 } 00328 } 00329 return true; 00330 } 00331 00332 00333 // ----------------------- PRINTOUT -------------------------------- 00334 00338 template <typename T> 00339 std::ostream& operator<<(std::ostream& os, const gVec<T>& v) { 00340 if (v.getSize() >= (unsigned long)gurls::MAX_PRINTABLE_SIZE){ 00341 os << v.what() << std::endl; 00342 return os; 00343 } 00344 os << "[" << v.data[0]; 00345 for (unsigned long i = 1; i < v.size; ++i) { 00346 os << " " << v.data[i]; 00347 } 00348 os << "]" << std::endl; 00349 return os; 00350 } 00351 00352 template <typename T> 00353 gVec<T>& gVec<T>::reciprocal() const { 00354 gVec<T> *w = new gVec(*this); 00355 w->setReciprocal(); 00356 return *w; 00357 } 00358 00359 00360 template <typename T> 00361 T gVec<T>::argmin() const { 00362 const T* val = std::min_element(this->data, this->data+this->size); 00363 return static_cast<T>(val-this->data); 00364 } 00365 00366 template <typename T> 00367 T gVec<T>::argmax() const { 00368 const T* val = std::max_element(this->data, this->data+this->size); 00369 return static_cast<T>(val-this->data); 00370 } 00371 00372 template <typename T> 00373 gVec<T>& gVec<T>::copyLocations(const gVec<T> locs){ 00374 00375 gVec<T>* v = new gVec<T>(locs.getSize()); 00376 const T* ptr_locs = locs.getData(); 00377 T* ptr_v = v->data; 00378 unsigned long val; 00379 for (unsigned long i = 0; i < locs.getSize(); i++) { 00380 val = static_cast<int>(*(ptr_locs+i)); 00381 if ( (val < 0) || (val > this->size) ){ 00382 throw gException(gurls::Exception_Index_Out_of_Bound); 00383 } 00384 *(ptr_v+i) = *(this->data+val); 00385 } 00386 return *v; 00387 } 00388 00389 //template <typename T> 00390 //template<class Archive> 00391 //void gVec<T>::save(Archive & ar, const unsigned int /* file_version */) const{ 00392 // ar & this->numrows & this->numcols & this->isowner; 00393 // T* ptr = this->data; 00394 // T* ptr_end = this->data+(this->numrows*this->numcols); 00395 // while (ptr!=ptr_end){ 00396 // ar & *ptr++; 00397 // } 00398 //} 00399 00400 //template <typename T> 00401 //template<class Archive> 00402 //void gMat2D<T>::load(Archive & ar, const unsigned int /* file_version */){ 00403 // ar & this->numrows; 00404 // ar & this->numcols; 00405 // ar & this->isowner; 00406 // this->size = this->numrows*this->numcols; 00407 // this->data = new T[this->size]; 00408 // T* ptr = this->data; 00409 // T* ptr_end = this->data+this->size; 00410 // while (ptr!=ptr_end){ 00411 // ar & *ptr++; 00412 // } 00413 //} 00414 00415 00416 00417 }