SGVector.h

Go to the documentation of this file.
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/SGSparseVector.h>
00020 #include <shogun/lib/SGReferencedData.h>
00021 
00022 
00023 namespace shogun
00024 {
00025     class CFile;
00026 
00028 template<class T> class SGVector : public SGReferencedData
00029 {
00030     public:
00032         SGVector();
00033 
00035         SGVector(T* v, index_t len, bool ref_counting=true);
00036 
00038         SGVector(index_t len, bool ref_counting=true);
00039 
00041         SGVector(const SGVector &orig);
00042 
00044         virtual ~SGVector();
00045 
00047         inline int32_t size() const { return vlen; }
00048 
00050         operator T*() { return vector; };
00051 
00053         void zero();
00054 
00059         void set_const(T const_elem);
00060 
00065         void range_fill(T start=0);
00066 
00072         void random(T min_value, T max_value);
00073 
00075         void randperm();
00076 
00078         SGVector<T> clone() const;
00079 
00081         static T* clone_vector(const T* vec, int32_t len)
00082         {
00083             T* result = SG_MALLOC(T, len);
00084             memcpy(result, vec, sizeof(T)*len);
00085             return result;
00086         }
00087 
00089         static void fill_vector(T* vec, int32_t len, T value)
00090         {
00091             for (int32_t i=0; i<len; i++)
00092                 vec[i]=value;
00093         }
00094 
00096         static void range_fill_vector(T* vec, int32_t len, T start=0)
00097         {
00098             for (int32_t i=0; i<len; i++)
00099                 vec[i]=i+start;
00100         }
00101 
00103         static void random_vector(T* vec, int32_t len, T min_value, T max_value);
00104 
00106         static void randperm(T* perm, int32_t n);
00107 
00109         static void permute(T* vec, int32_t n);
00110 
00116         const T& get_element(index_t index);
00117 
00124         void set_element(const T& p_element, index_t index);
00125 
00131         void resize_vector(int32_t n);
00132 
00138         inline const T& operator[](index_t index) const
00139         {
00140             return vector[index];
00141         }
00142 
00148         inline T& operator[](index_t index)
00149         {
00150             return vector[index];
00151         }
00152 
00157         void add(const SGVector<T> x);
00158 
00163         void add(const SGSparseVector<T>& x);
00164 
00169         void add(const T x);
00170 
00172         SGVector<T> operator+ (SGVector<T> x)
00173         {
00174             ASSERT(x.vector && vector);
00175             ASSERT(x.vlen == vlen);
00176 
00177             SGVector<T> result=clone();
00178             result.add(x);
00179             return result;
00180         }
00181 
00183         SGVector<T> operator+= (SGVector<T> x)
00184         {
00185             add(x);
00186             return *this;
00187         }
00188 
00190         SGVector<T> operator+= (SGSparseVector<T>& x)
00191         {
00192             add(x);
00193             return *this;
00194         }
00195 
00197         static void permute_vector(SGVector<T> vec);
00198 
00200         void permute();
00201 
00205         static inline void resize(T* &data, int64_t old_size, int64_t new_size)
00206         {
00207             if (old_size==new_size)
00208                 return;
00209 
00210             data = SG_REALLOC(T, data, new_size);
00211         }
00212 
00214         static T twonorm(const T* x, int32_t len);
00215 
00217         static float64_t onenorm(T* x, int32_t len);
00218 
00220         static T qsq(T* x, int32_t len, float64_t q);
00221 
00223         static T qnorm(T* x, int32_t len, float64_t q);
00224 
00225 //      /// x=x+alpha*y
00226 //      static inline void vec1_plus_scalar_times_vec2(T* vec1,
00227 //              T scalar, const T* vec2, int32_t n)
00228 //      {
00229 //          for (int32_t i=0; i<n; i++)
00230 //              vec1[i]+=scalar*vec2[i];
00231 //      }
00232 
00234         static void vec1_plus_scalar_times_vec2(float64_t* vec1,
00235                 const float64_t scalar, const float64_t* vec2, int32_t n);
00236 
00238         static void vec1_plus_scalar_times_vec2(float32_t* vec1,
00239                 const float32_t scalar, const float32_t* vec2, int32_t n);
00240 
00242         static inline float64_t dot(const bool* v1, const bool* v2, int32_t n)
00243         {
00244             float64_t r=0;
00245             for (int32_t i=0; i<n; i++)
00246                 r+=((v1[i]) ? 1 : 0) * ((v2[i]) ? 1 : 0);
00247             return r;
00248         }
00249 
00251         static inline floatmax_t dot(const floatmax_t* v1, const floatmax_t* v2, int32_t n)
00252         {
00253             floatmax_t r=0;
00254             for (int32_t i=0; i<n; i++)
00255                 r+=v1[i]*v2[i];
00256             return r;
00257         }
00258 
00259 
00261         static float64_t dot(const float64_t* v1, const float64_t* v2, int32_t n);
00262 
00264         static float32_t dot(const float32_t* v1, const float32_t* v2, int32_t n);
00265 
00267         static inline float64_t dot(
00268             const uint64_t* v1, const uint64_t* v2, int32_t n)
00269         {
00270             float64_t r=0;
00271             for (int32_t i=0; i<n; i++)
00272                 r+=((float64_t) v1[i])*v2[i];
00273 
00274             return r;
00275         }
00277         static inline float64_t dot(
00278             const int64_t* v1, const int64_t* v2, int32_t n)
00279         {
00280             float64_t r=0;
00281             for (int32_t i=0; i<n; i++)
00282                 r+=((float64_t) v1[i])*v2[i];
00283 
00284             return r;
00285         }
00286 
00288         static inline float64_t dot(
00289             const int32_t* v1, const int32_t* v2, int32_t n)
00290         {
00291             float64_t r=0;
00292             for (int32_t i=0; i<n; i++)
00293                 r+=((float64_t) v1[i])*v2[i];
00294 
00295             return r;
00296         }
00297 
00299         static inline float64_t dot(
00300             const uint32_t* v1, const uint32_t* v2, int32_t n)
00301         {
00302             float64_t r=0;
00303             for (int32_t i=0; i<n; i++)
00304                 r+=((float64_t) v1[i])*v2[i];
00305 
00306             return r;
00307         }
00308 
00310         static inline float64_t dot(
00311             const uint16_t* v1, const uint16_t* v2, int32_t n)
00312         {
00313             float64_t r=0;
00314             for (int32_t i=0; i<n; i++)
00315                 r+=((float64_t) v1[i])*v2[i];
00316 
00317             return r;
00318         }
00319 
00321         static inline float64_t dot(
00322             const int16_t* v1, const int16_t* v2, int32_t n)
00323         {
00324             float64_t r=0;
00325             for (int32_t i=0; i<n; i++)
00326                 r+=((float64_t) v1[i])*v2[i];
00327 
00328             return r;
00329         }
00330 
00332         static inline float64_t dot(
00333             const char* v1, const char* v2, int32_t n)
00334         {
00335             float64_t r=0;
00336             for (int32_t i=0; i<n; i++)
00337                 r+=((float64_t) v1[i])*v2[i];
00338 
00339             return r;
00340         }
00341 
00343         static inline float64_t dot(
00344             const uint8_t* v1, const uint8_t* v2, int32_t n)
00345         {
00346             float64_t r=0;
00347             for (int32_t i=0; i<n; i++)
00348                 r+=((float64_t) v1[i])*v2[i];
00349 
00350             return r;
00351         }
00352 
00354         static inline float64_t dot(
00355             const int8_t* v1, const int8_t* v2, int32_t n)
00356         {
00357             float64_t r=0;
00358             for (int32_t i=0; i<n; i++)
00359                 r+=((float64_t) v1[i])*v2[i];
00360 
00361             return r;
00362         }
00363 
00365         static inline float64_t dot(
00366             const float64_t* v1, const char* v2, int32_t n)
00367         {
00368             float64_t r=0;
00369             for (int32_t i=0; i<n; i++)
00370                 r+=((float64_t) v1[i])*v2[i];
00371 
00372             return r;
00373         }
00374 
00376         static inline void vector_multiply(
00377                 T* target, const T* v1, const T* v2,int32_t len)
00378             {
00379                 for (int32_t i=0; i<len; i++)
00380                     target[i]=v1[i]*v2[i];
00381             }
00382 
00383 
00385         static inline void add(
00386             T* target, T alpha, const T* v1, T beta, const T* v2,
00387             int32_t len)
00388         {
00389             for (int32_t i=0; i<len; i++)
00390                 target[i]=alpha*v1[i]+beta*v2[i];
00391         }
00392 
00394         static inline void add_scalar(T alpha, T* vec, int32_t len)
00395         {
00396             for (int32_t i=0; i<len; i++)
00397                 vec[i]+=alpha;
00398         }
00399 
00401         static inline void scale_vector(T alpha, T* vec, int32_t len)
00402         {
00403             for (int32_t i=0; i<len; i++)
00404                 vec[i]*=alpha;
00405         }
00406 
00408         static inline T sum(T* vec, int32_t len)
00409         {
00410             T result=0;
00411             for (int32_t i=0; i<len; i++)
00412                 result+=vec[i];
00413 
00414             return result;
00415         }
00416 
00418         static inline T sum(SGVector<T> vec)
00419         {
00420             return sum(vec.vector, vec.vlen);
00421         }
00422 
00423 
00425         static T min(T* vec, int32_t len);
00426 
00428         static T max(T* vec, int32_t len);
00429 
00431         static inline int32_t arg_max(T * vec, int32_t inc, int32_t len, T * maxv_ptr = NULL)
00432         {
00433             ASSERT(len > 0 || inc > 0);
00434 
00435             T maxv = vec[0];
00436             int32_t maxIdx = 0;
00437 
00438             for (int32_t i = 1, j = inc ; i < len ; i++, j += inc)
00439             {
00440                 if (vec[j] > maxv)
00441                     maxv = vec[j], maxIdx = i;
00442             }
00443 
00444             if (maxv_ptr != NULL)
00445                 *maxv_ptr = maxv;
00446 
00447             return maxIdx;
00448         }
00449 
00451         static inline int32_t arg_min(T * vec, int32_t inc, int32_t len, T * minv_ptr = NULL)
00452         {
00453             ASSERT(len > 0 || inc > 0);
00454 
00455             T minv = vec[0];
00456             int32_t minIdx = 0;
00457 
00458             for (int32_t i = 1, j = inc ; i < len ; i++, j += inc)
00459             {
00460                 if (vec[j] < minv)
00461                     minv = vec[j], minIdx = i;
00462             }
00463 
00464             if (minv_ptr != NULL)
00465                 *minv_ptr = minv;
00466 
00467             return minIdx;
00468         }
00469 
00471         static T sum_abs(T* vec, int32_t len);
00472 
00474         static bool fequal(T x, T y, float64_t precision=1e-6);
00475 
00479         static int32_t unique(T* output, int32_t size);
00480 
00482         void display_size() const;
00483 
00485         void display_vector(const char* name="vector",
00486                 const char* prefix="") const;
00487 
00489         static void display_vector(
00490             const T* vector, int32_t n, const char* name="vector",
00491             const char* prefix="");
00492 
00494         static void display_vector(
00495             const SGVector<T>, const char* name="vector",
00496             const char* prefix="");
00497 
00501         SGVector<index_t> find(T elem);
00502 
00506         template <typename Predicate>
00507             SGVector<index_t> find_if(Predicate p)
00508             {
00509                 SGVector<index_t> idx(vlen);
00510                 index_t k=0; 
00511 
00512                 for (index_t i=0; i < vlen; ++i)
00513                     if (p(vector[i]))
00514                         idx[k++] = i;
00515 
00516                 idx.vlen = k;
00517                 return idx;
00518             }
00519 
00521         struct IndexSorter
00522         {
00524             IndexSorter(const SGVector<T> *vec) { data = vec->vector; }
00525 
00527             bool operator() (index_t i, index_t j) const
00528             {
00529                 return data[i] < data[j];
00530             }
00531 
00533             const T* data;
00534         };
00541         SGVector<index_t> sorted_index()
00542         {
00543             IndexSorter cmp(this);
00544             SGVector<index_t> idx(vlen);
00545             for (index_t i=0; i < vlen; ++i)
00546                 idx[i] = i;
00547 
00548             std::sort(idx.vector, idx.vector+vlen, cmp);
00549 
00550             return idx;
00551         }
00552 
00554         void scale(T alpha);
00555 
00560         float64_t mean() const;
00561 
00566         void load(CFile* loader);
00567 
00572         void save(CFile* saver);
00573 
00574     protected:
00576         virtual void copy_data(const SGReferencedData &orig);
00577 
00579         virtual void init_data();
00580 
00582         virtual void free_data();
00583 
00584     public:
00586         T* vector;
00588         index_t vlen;
00589 };
00590 }
00591 #endif // __SGVECTOR_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation