SHOGUN
v2.0.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2012 Fernando José Iglesias García 00008 * Written (W) 2010,2012 Soeren Sonnenburg 00009 * Copyright (C) 2010 Berlin Institute of Technology 00010 * Copyright (C) 2012 Soeren Sonnenburg 00011 */ 00012 #ifndef __SGVECTOR_H__ 00013 #define __SGVECTOR_H__ 00014 00015 #include <algorithm> 00016 00017 #include <shogun/io/SGIO.h> 00018 #include <shogun/lib/DataType.h> 00019 #include <shogun/lib/SGReferencedData.h> 00020 00021 namespace shogun 00022 { 00024 template<class T> class SGVector : public SGReferencedData 00025 { 00026 public: 00028 SGVector(); 00029 00031 SGVector(T* v, index_t len, bool ref_counting=true); 00032 00034 SGVector(index_t len, bool ref_counting=true); 00035 00037 SGVector(const SGVector &orig); 00038 00040 virtual ~SGVector(); 00041 00043 inline int32_t size() const { return vlen; } 00044 00046 operator T*() { return vector; }; 00047 00049 void zero(); 00050 00055 void set_const(T const_elem); 00056 00061 void range_fill(T start=0); 00062 00068 void random(T min_value, T max_value); 00069 00071 void randperm(); 00072 00074 SGVector<T> clone() const; 00075 00077 static T* clone_vector(const T* vec, int32_t len) 00078 { 00079 T* result = SG_MALLOC(T, len); 00080 memcpy(result, vec, sizeof(T)*len); 00081 return result; 00082 } 00083 00085 static void fill_vector(T* vec, int32_t len, T value) 00086 { 00087 for (int32_t i=0; i<len; i++) 00088 vec[i]=value; 00089 } 00090 00092 static void range_fill_vector(T* vec, int32_t len, T start=0) 00093 { 00094 for (int32_t i=0; i<len; i++) 00095 vec[i]=i+start; 00096 } 00097 00099 static void random_vector(T* vec, int32_t len, T min_value, T max_value); 00100 00102 static void randperm(T* perm, int32_t n); 00103 00105 static void permute(T* vec, int32_t n); 00106 00112 const T& get_element(index_t index); 00113 00120 void set_element(const T& p_element, index_t index); 00121 00127 void resize_vector(int32_t n); 00128 00134 inline const T& operator[](index_t index) const 00135 { 00136 return vector[index]; 00137 } 00138 00144 inline T& operator[](index_t index) 00145 { 00146 return vector[index]; 00147 } 00148 00153 void add(const SGVector<T> x); 00154 00159 void add(const T x); 00160 00162 SGVector<T> operator+ (SGVector<T> x) 00163 { 00164 ASSERT(x.vector && vector); 00165 ASSERT(x.vlen == vlen); 00166 00167 SGVector<T> result=clone(); 00168 result.add(x); 00169 return result; 00170 } 00171 00173 SGVector<T> operator+= (SGVector<T> x) 00174 { 00175 add(x); 00176 return *this; 00177 } 00178 00180 static void permute_vector(SGVector<T> vec); 00181 00183 void permute(); 00184 00188 static inline void resize(T* &data, int64_t old_size, int64_t new_size) 00189 { 00190 if (old_size==new_size) 00191 return; 00192 00193 data = SG_REALLOC(T, data, new_size); 00194 } 00195 00197 static T twonorm(const T* x, int32_t len); 00198 00200 static float64_t onenorm(T* x, int32_t len); 00201 00203 static T qsq(T* x, int32_t len, float64_t q); 00204 00206 static T qnorm(T* x, int32_t len, float64_t q); 00207 00208 // /// x=x+alpha*y 00209 // static inline void vec1_plus_scalar_times_vec2(T* vec1, 00210 // T scalar, const T* vec2, int32_t n) 00211 // { 00212 // for (int32_t i=0; i<n; i++) 00213 // vec1[i]+=scalar*vec2[i]; 00214 // } 00215 00217 static void vec1_plus_scalar_times_vec2(float64_t* vec1, 00218 const float64_t scalar, const float64_t* vec2, int32_t n); 00219 00221 static void vec1_plus_scalar_times_vec2(float32_t* vec1, 00222 const float32_t scalar, const float32_t* vec2, int32_t n); 00223 00225 static inline float64_t dot(const bool* v1, const bool* v2, int32_t n) 00226 { 00227 float64_t r=0; 00228 for (int32_t i=0; i<n; i++) 00229 r+=((v1[i]) ? 1 : 0) * ((v2[i]) ? 1 : 0); 00230 return r; 00231 } 00232 00234 static inline floatmax_t dot(const floatmax_t* v1, const floatmax_t* v2, int32_t n) 00235 { 00236 floatmax_t r=0; 00237 for (int32_t i=0; i<n; i++) 00238 r+=v1[i]*v2[i]; 00239 return r; 00240 } 00241 00242 00244 static float64_t dot(const float64_t* v1, const float64_t* v2, int32_t n); 00245 00247 static float32_t dot(const float32_t* v1, const float32_t* v2, int32_t n); 00248 00250 static inline float64_t dot( 00251 const uint64_t* v1, const uint64_t* v2, int32_t n) 00252 { 00253 float64_t r=0; 00254 for (int32_t i=0; i<n; i++) 00255 r+=((float64_t) v1[i])*v2[i]; 00256 00257 return r; 00258 } 00260 static inline float64_t dot( 00261 const int64_t* v1, const int64_t* v2, int32_t n) 00262 { 00263 float64_t r=0; 00264 for (int32_t i=0; i<n; i++) 00265 r+=((float64_t) v1[i])*v2[i]; 00266 00267 return r; 00268 } 00269 00271 static inline float64_t dot( 00272 const int32_t* v1, const int32_t* v2, int32_t n) 00273 { 00274 float64_t r=0; 00275 for (int32_t i=0; i<n; i++) 00276 r+=((float64_t) v1[i])*v2[i]; 00277 00278 return r; 00279 } 00280 00282 static inline float64_t dot( 00283 const uint32_t* v1, const uint32_t* v2, int32_t n) 00284 { 00285 float64_t r=0; 00286 for (int32_t i=0; i<n; i++) 00287 r+=((float64_t) v1[i])*v2[i]; 00288 00289 return r; 00290 } 00291 00293 static inline float64_t dot( 00294 const uint16_t* v1, const uint16_t* v2, int32_t n) 00295 { 00296 float64_t r=0; 00297 for (int32_t i=0; i<n; i++) 00298 r+=((float64_t) v1[i])*v2[i]; 00299 00300 return r; 00301 } 00302 00304 static inline float64_t dot( 00305 const int16_t* v1, const int16_t* v2, int32_t n) 00306 { 00307 float64_t r=0; 00308 for (int32_t i=0; i<n; i++) 00309 r+=((float64_t) v1[i])*v2[i]; 00310 00311 return r; 00312 } 00313 00315 static inline float64_t dot( 00316 const char* v1, const char* v2, int32_t n) 00317 { 00318 float64_t r=0; 00319 for (int32_t i=0; i<n; i++) 00320 r+=((float64_t) v1[i])*v2[i]; 00321 00322 return r; 00323 } 00324 00326 static inline float64_t dot( 00327 const uint8_t* v1, const uint8_t* v2, int32_t n) 00328 { 00329 float64_t r=0; 00330 for (int32_t i=0; i<n; i++) 00331 r+=((float64_t) v1[i])*v2[i]; 00332 00333 return r; 00334 } 00335 00337 static inline float64_t dot( 00338 const int8_t* v1, const int8_t* v2, int32_t n) 00339 { 00340 float64_t r=0; 00341 for (int32_t i=0; i<n; i++) 00342 r+=((float64_t) v1[i])*v2[i]; 00343 00344 return r; 00345 } 00346 00348 static inline float64_t dot( 00349 const float64_t* v1, const char* v2, int32_t n) 00350 { 00351 float64_t r=0; 00352 for (int32_t i=0; i<n; i++) 00353 r+=((float64_t) v1[i])*v2[i]; 00354 00355 return r; 00356 } 00357 00359 static inline void vector_multiply( 00360 T* target, const T* v1, const T* v2,int32_t len) 00361 { 00362 for (int32_t i=0; i<len; i++) 00363 target[i]=v1[i]*v2[i]; 00364 } 00365 00366 00368 static inline void add( 00369 T* target, T alpha, const T* v1, T beta, const T* v2, 00370 int32_t len) 00371 { 00372 for (int32_t i=0; i<len; i++) 00373 target[i]=alpha*v1[i]+beta*v2[i]; 00374 } 00375 00377 static inline void add_scalar(T alpha, T* vec, int32_t len) 00378 { 00379 for (int32_t i=0; i<len; i++) 00380 vec[i]+=alpha; 00381 } 00382 00384 static inline void scale_vector(T alpha, T* vec, int32_t len) 00385 { 00386 for (int32_t i=0; i<len; i++) 00387 vec[i]*=alpha; 00388 } 00389 00391 static inline T sum(T* vec, int32_t len) 00392 { 00393 T result=0; 00394 for (int32_t i=0; i<len; i++) 00395 result+=vec[i]; 00396 00397 return result; 00398 } 00399 00401 static inline T sum(SGVector<T> vec) 00402 { 00403 return sum(vec.vector, vec.vlen); 00404 } 00405 00406 00408 static T min(T* vec, int32_t len); 00409 00411 static T max(T* vec, int32_t len); 00412 00414 static inline int32_t arg_max(T * vec, int32_t inc, int32_t len, T * maxv_ptr = NULL) 00415 { 00416 ASSERT(len > 0 || inc > 0); 00417 00418 T maxv = vec[0]; 00419 int32_t maxIdx = 0; 00420 00421 for (int32_t i = 1, j = inc ; i < len ; i++, j += inc) 00422 { 00423 if (vec[j] > maxv) 00424 maxv = vec[j], maxIdx = i; 00425 } 00426 00427 if (maxv_ptr != NULL) 00428 *maxv_ptr = maxv; 00429 00430 return maxIdx; 00431 } 00432 00434 static inline int32_t arg_min(T * vec, int32_t inc, int32_t len, T * minv_ptr = NULL) 00435 { 00436 ASSERT(len > 0 || inc > 0); 00437 00438 T minv = vec[0]; 00439 int32_t minIdx = 0; 00440 00441 for (int32_t i = 1, j = inc ; i < len ; i++, j += inc) 00442 { 00443 if (vec[j] < minv) 00444 minv = vec[j], minIdx = i; 00445 } 00446 00447 if (minv_ptr != NULL) 00448 *minv_ptr = minv; 00449 00450 return minIdx; 00451 } 00452 00454 static T sum_abs(T* vec, int32_t len); 00455 00457 static bool fequal(T x, T y, float64_t precision=1e-6); 00458 00462 static int32_t unique(T* output, int32_t size); 00463 00465 void display_size() const; 00466 00468 void display_vector(const char* name="vector", 00469 const char* prefix="") const; 00470 00472 static void display_vector( 00473 const T* vector, int32_t n, const char* name="vector", 00474 const char* prefix=""); 00475 00477 static void display_vector( 00478 const SGVector<T>, const char* name="vector", 00479 const char* prefix=""); 00480 00484 SGVector<index_t> find(T elem); 00485 00489 template <typename Predicate> 00490 SGVector<index_t> find_if(Predicate p) 00491 { 00492 SGVector<index_t> idx(vlen); 00493 index_t k=0; 00494 00495 for (index_t i=0; i < vlen; ++i) 00496 if (p(vector[i])) 00497 idx[k++] = i; 00498 00499 idx.vlen = k; 00500 return idx; 00501 } 00502 00504 struct IndexSorter 00505 { 00507 IndexSorter(const SGVector<T> *vec) { data = vec->vector; } 00508 00510 bool operator() (index_t i, index_t j) const 00511 { 00512 return data[i] < data[j]; 00513 } 00514 00516 const T* data; 00517 }; 00524 SGVector<index_t> sorted_index() 00525 { 00526 IndexSorter cmp(this); 00527 SGVector<index_t> idx(vlen); 00528 for (index_t i=0; i < vlen; ++i) 00529 idx[i] = i; 00530 00531 std::sort(idx.vector, idx.vector+vlen, cmp); 00532 00533 return idx; 00534 } 00535 00537 void scale(T alpha); 00538 00543 float64_t mean() const; 00544 00545 protected: 00547 virtual void copy_data(const SGReferencedData &orig); 00548 00550 virtual void init_data(); 00551 00553 virtual void free_data(); 00554 00555 public: 00557 T* vector; 00559 index_t vlen; 00560 }; 00561 } 00562 #endif // __SGVECTOR_H__