SHOGUN
v3.2.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) 2011-2013 Heiko Strathmann 00008 * Written (W) 2013 Soumyajit De 00009 * Written (W) 2012 Fernando Jose Iglesias Garcia 00010 * Written (W) 2010,2012 Soeren Sonnenburg 00011 * Copyright (C) 2010 Berlin Institute of Technology 00012 * Copyright (C) 2012 Soeren Sonnenburg 00013 */ 00014 #ifndef __SGVECTOR_H__ 00015 #define __SGVECTOR_H__ 00016 00017 #include <shogun/lib/common.h> 00018 #include <shogun/lib/DataType.h> 00019 #include <shogun/lib/SGReferencedData.h> 00020 00021 00022 namespace shogun 00023 { 00024 template <class T> class SGSparseVector; 00025 template <class T> class SGMatrix; 00026 class CFile; 00027 00029 template<class T> class SGVector : public SGReferencedData 00030 { 00031 public: 00033 SGVector(); 00034 00036 SGVector(T* v, index_t len, bool ref_counting=true); 00037 00039 SGVector(index_t len, bool ref_counting=true); 00040 00042 SGVector(const SGVector &orig); 00043 00048 void set(SGVector<T> orig); 00049 00051 virtual ~SGVector(); 00052 00054 inline int32_t size() const { return vlen; } 00055 00057 operator T*() { return vector; }; 00058 00060 void zero(); 00061 00066 void set_const(T const_elem); 00067 00072 void range_fill(T start=0); 00073 00079 void random(T min_value, T max_value); 00080 00082 void randperm(); 00083 00085 static SGVector<T> randperm_vec(int32_t n); 00086 00093 static T* randperm(int32_t n); 00094 00102 static SGVector<float64_t> linspace_vec(T start, T end, int32_t n); 00103 00111 static float64_t* linspace(T start, T end, int32_t n); 00112 00119 index_t find_position_to_insert(T element); 00120 00124 void qsort(); 00125 00132 SGVector<index_t> argsort(); 00133 00138 bool is_sorted() const; 00139 00141 SGVector<T> clone() const; 00142 00144 static T* clone_vector(const T* vec, int32_t len); 00145 00147 static void fill_vector(T* vec, int32_t len, T value); 00148 00150 static void range_fill_vector(T* vec, int32_t len, T start=0); 00151 00153 static void random_vector(T* vec, int32_t len, T min_value, T max_value); 00154 00156 static void randperm(T* perm, int32_t n); 00157 00159 static void permute(T* vec, int32_t n); 00160 00162 static void permute(T* vec, int32_t n, CRandom * rand); 00163 00169 SGVector<T> get() 00170 { 00171 return *this; 00172 } 00173 00179 const T& get_element(index_t index); 00180 00187 void set_element(const T& p_element, index_t index); 00188 00194 void resize_vector(int32_t n); 00195 00201 inline const T& operator[](uint64_t index) const 00202 { 00203 return vector[index]; 00204 } 00205 00211 inline const T& operator[](int64_t index) const 00212 { 00213 return vector[index]; 00214 } 00215 00221 inline const T& operator[](uint32_t index) const 00222 { 00223 return vector[index]; 00224 } 00225 00231 inline const T& operator[](int32_t index) const 00232 { 00233 return vector[index]; 00234 } 00235 00241 inline T& operator[](uint64_t index) 00242 { 00243 return vector[index]; 00244 } 00245 00251 inline T& operator[](int64_t index) 00252 { 00253 return vector[index]; 00254 } 00255 00261 inline T& operator[](uint32_t index) 00262 { 00263 return vector[index]; 00264 } 00265 00271 inline T& operator[](int32_t index) 00272 { 00273 return vector[index]; 00274 } 00275 00280 void add(const SGVector<T> x); 00281 00286 void add(const SGSparseVector<T>& x); 00287 00292 void add(const T x); 00293 00295 SGVector<T> operator+ (SGVector<T> x); 00296 00298 SGVector<T> operator+= (SGVector<T> x) 00299 { 00300 add(x); 00301 return *this; 00302 } 00303 00305 SGVector<T> operator+= (SGSparseVector<T>& x) 00306 { 00307 add(x); 00308 return *this; 00309 } 00310 00316 bool equals(SGVector<T>& other); 00317 00319 static void permute_vector(SGVector<T> vec); 00320 00322 void permute(); 00323 00325 void permute(CRandom * rand); 00326 00328 static T twonorm(const T* x, int32_t len); 00329 00331 static float64_t onenorm(T* x, int32_t len); 00332 00334 static T qsq(T* x, int32_t len, float64_t q); 00335 00337 static T qnorm(T* x, int32_t len, float64_t q); 00338 00340 static void vec1_plus_scalar_times_vec2(T* vec1, 00341 const T scalar, const T* vec2, int32_t n); 00342 00344 static inline float64_t dot(const bool* v1, const bool* v2, int32_t n) 00345 { 00346 float64_t r=0; 00347 for (int32_t i=0; i<n; i++) 00348 r+=((v1[i]) ? 1 : 0) * ((v2[i]) ? 1 : 0); 00349 return r; 00350 } 00351 00353 static inline floatmax_t dot(const floatmax_t* v1, const floatmax_t* v2, int32_t n) 00354 { 00355 floatmax_t r=0; 00356 for (int32_t i=0; i<n; i++) 00357 r+=v1[i]*v2[i]; 00358 return r; 00359 } 00360 00361 00363 static float64_t dot(const float64_t* v1, const float64_t* v2, int32_t n); 00364 00366 static float32_t dot(const float32_t* v1, const float32_t* v2, int32_t n); 00367 00369 static inline float64_t dot( 00370 const uint64_t* v1, const uint64_t* v2, int32_t n) 00371 { 00372 float64_t r=0; 00373 for (int32_t i=0; i<n; i++) 00374 r+=((float64_t) v1[i])*v2[i]; 00375 00376 return r; 00377 } 00379 static inline float64_t dot( 00380 const int64_t* v1, const int64_t* v2, int32_t n) 00381 { 00382 float64_t r=0; 00383 for (int32_t i=0; i<n; i++) 00384 r+=((float64_t) v1[i])*v2[i]; 00385 00386 return r; 00387 } 00388 00390 static inline float64_t dot( 00391 const int32_t* v1, const int32_t* v2, int32_t n) 00392 { 00393 float64_t r=0; 00394 for (int32_t i=0; i<n; i++) 00395 r+=((float64_t) v1[i])*v2[i]; 00396 00397 return r; 00398 } 00399 00401 static inline float64_t dot( 00402 const uint32_t* v1, const uint32_t* v2, int32_t n) 00403 { 00404 float64_t r=0; 00405 for (int32_t i=0; i<n; i++) 00406 r+=((float64_t) v1[i])*v2[i]; 00407 00408 return r; 00409 } 00410 00412 static inline float64_t dot( 00413 const uint16_t* v1, const uint16_t* v2, int32_t n) 00414 { 00415 float64_t r=0; 00416 for (int32_t i=0; i<n; i++) 00417 r+=((float64_t) v1[i])*v2[i]; 00418 00419 return r; 00420 } 00421 00423 static inline float64_t dot( 00424 const int16_t* v1, const int16_t* v2, int32_t n) 00425 { 00426 float64_t r=0; 00427 for (int32_t i=0; i<n; i++) 00428 r+=((float64_t) v1[i])*v2[i]; 00429 00430 return r; 00431 } 00432 00434 static inline float64_t dot( 00435 const char* v1, const char* v2, int32_t n) 00436 { 00437 float64_t r=0; 00438 for (int32_t i=0; i<n; i++) 00439 r+=((float64_t) v1[i])*v2[i]; 00440 00441 return r; 00442 } 00443 00445 static inline float64_t dot( 00446 const uint8_t* v1, const uint8_t* v2, int32_t n) 00447 { 00448 float64_t r=0; 00449 for (int32_t i=0; i<n; i++) 00450 r+=((float64_t) v1[i])*v2[i]; 00451 00452 return r; 00453 } 00454 00456 static inline float64_t dot( 00457 const int8_t* v1, const int8_t* v2, int32_t n) 00458 { 00459 float64_t r=0; 00460 for (int32_t i=0; i<n; i++) 00461 r+=((float64_t) v1[i])*v2[i]; 00462 00463 return r; 00464 } 00465 00467 static inline float64_t dot( 00468 const float64_t* v1, const char* v2, int32_t n) 00469 { 00470 float64_t r=0; 00471 for (int32_t i=0; i<n; i++) 00472 r+=((float64_t) v1[i])*v2[i]; 00473 00474 return r; 00475 } 00476 00478 static inline void vector_multiply( 00479 T* target, const T* v1, const T* v2,int32_t len) 00480 { 00481 for (int32_t i=0; i<len; i++) 00482 target[i]=v1[i]*v2[i]; 00483 } 00484 00485 00487 static inline void add( 00488 T* target, T alpha, const T* v1, T beta, const T* v2, 00489 int32_t len) 00490 { 00491 for (int32_t i=0; i<len; i++) 00492 target[i]=alpha*v1[i]+beta*v2[i]; 00493 } 00494 00496 static inline void add_scalar(T alpha, T* vec, int32_t len) 00497 { 00498 for (int32_t i=0; i<len; i++) 00499 vec[i]+=alpha; 00500 } 00501 00503 static void scale_vector(T alpha, T* vec, int32_t len); 00504 00506 static inline T sum(T* vec, int32_t len) 00507 { 00508 T result=0; 00509 for (int32_t i=0; i<len; i++) 00510 result+=vec[i]; 00511 00512 return result; 00513 } 00514 00516 static inline T sum(SGVector<T> vec) 00517 { 00518 return sum(vec.vector, vec.vlen); 00519 } 00520 00522 static inline T product(T* vec, int32_t len) 00523 { 00524 T result=1; 00525 for (int32_t i=0; i<len; i++) 00526 result*=vec[i]; 00527 00528 return result; 00529 } 00530 00532 inline T product() 00533 { 00534 return product(vector, vlen); 00535 } 00536 00538 static T min(T* vec, int32_t len); 00539 00541 static T max_abs(T* vec, int32_t len); 00542 00544 static T max(T* vec, int32_t len); 00545 00547 static int32_t arg_max(T * vec, int32_t inc, int32_t len, T * maxv_ptr = NULL); 00548 00550 static int32_t arg_max_abs(T * vec, int32_t inc, int32_t len, T * maxv_ptr = NULL); 00551 00553 static int32_t arg_min(T * vec, int32_t inc, int32_t len, T * minv_ptr = NULL); 00554 00556 static T sum_abs(T* vec, int32_t len); 00557 00559 static bool fequal(T x, T y, float64_t precision=1e-6); 00560 00564 static int32_t unique(T* output, int32_t size); 00565 00567 void display_size() const; 00568 00570 void display_vector(const char* name="vector", 00571 const char* prefix="") const; 00572 00574 static void display_vector( 00575 const T* vector, int32_t n, const char* name="vector", 00576 const char* prefix=""); 00577 00579 static void display_vector( 00580 const SGVector<T>, const char* name="vector", 00581 const char* prefix=""); 00582 00586 SGVector<index_t> find(T elem); 00587 00591 template <typename Predicate> 00592 SGVector<index_t> find_if(Predicate p) 00593 { 00594 SGVector<index_t> idx(vlen); 00595 index_t k=0; 00596 00597 for (index_t i=0; i < vlen; ++i) 00598 if (p(vector[i])) 00599 idx[k++] = i; 00600 00601 idx.vlen = k; 00602 return idx; 00603 } 00604 00606 void scale(T alpha); 00607 00612 float64_t mean() const; 00613 00618 void load(CFile* loader); 00619 00624 void save(CFile* saver); 00625 00627 void abs(); 00629 void acos(); 00631 void asin(); 00633 void atan(); 00635 void atan2(T x); 00637 void cos(); 00639 void cosh(); 00641 void exp(); 00643 void log(); 00645 void log10(); 00647 void pow(T q); 00649 void sin(); 00651 void sinh(); 00653 void sqrt(); 00655 void tan(); 00657 void tanh(); 00658 00660 SGVector<float64_t> get_real(); 00661 00663 SGVector<float64_t> get_imag(); 00664 00675 static SGMatrix<T> convert_to_matrix(SGVector<T> vector, index_t nrows, index_t ncols, bool fortran_order); 00676 00677 00690 static void convert_to_matrix(T*& matrix, index_t nrows, index_t ncols, const T* vector, int32_t vlen, bool fortran_order); 00691 00692 protected: 00694 virtual void copy_data(const SGReferencedData &orig); 00695 00697 virtual void init_data(); 00698 00700 virtual void free_data(); 00701 00702 public: 00704 T* vector; 00706 index_t vlen; 00707 }; 00708 00709 #ifndef DOXYGEN_SHOULD_SKIP_THIS 00710 template<> void SGVector<float64_t>::vec1_plus_scalar_times_vec2(float64_t* vec1, 00711 const float64_t scalar, const float64_t* vec2, int32_t n); 00712 00713 template<> void SGVector<float32_t>::vec1_plus_scalar_times_vec2(float32_t* vec1, 00714 const float32_t scalar, const float32_t* vec2, int32_t n); 00715 #endif // DOXYGEN_SHOULD_SKIP_THIS 00716 } 00717 #endif // __SGVECTOR_H__