SHOGUN  v3.2.0
SGVector.h
浏览该文件的文档.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2011-2013 Heiko Strathmann
8  * Written (W) 2013 Soumyajit De
9  * Written (W) 2012 Fernando Jose Iglesias Garcia
10  * Written (W) 2010,2012 Soeren Sonnenburg
11  * Copyright (C) 2010 Berlin Institute of Technology
12  * Copyright (C) 2012 Soeren Sonnenburg
13  */
14 #ifndef __SGVECTOR_H__
15 #define __SGVECTOR_H__
16 
17 #include <shogun/lib/common.h>
18 #include <shogun/lib/DataType.h>
20 
21 
22 namespace shogun
23 {
24  template <class T> class SGSparseVector;
25  template <class T> class SGMatrix;
26  class CFile;
27 
29 template<class T> class SGVector : public SGReferencedData
30 {
31  public:
33  SGVector();
34 
36  SGVector(T* v, index_t len, bool ref_counting=true);
37 
39  SGVector(index_t len, bool ref_counting=true);
40 
42  SGVector(const SGVector &orig);
43 
48  void set(SGVector<T> orig);
49 
51  virtual ~SGVector();
52 
54  inline int32_t size() const { return vlen; }
55 
57  operator T*() { return vector; };
58 
60  void zero();
61 
66  void set_const(T const_elem);
67 
72  void range_fill(T start=0);
73 
79  void random(T min_value, T max_value);
80 
82  void randperm();
83 
85  static SGVector<T> randperm_vec(int32_t n);
86 
93  static T* randperm(int32_t n);
94 
102  static SGVector<float64_t> linspace_vec(T start, T end, int32_t n);
103 
111  static float64_t* linspace(T start, T end, int32_t n);
112 
119  index_t find_position_to_insert(T element);
120 
124  void qsort();
125 
133 
138  bool is_sorted() const;
139 
141  SGVector<T> clone() const;
142 
144  static T* clone_vector(const T* vec, int32_t len);
145 
147  static void fill_vector(T* vec, int32_t len, T value);
148 
150  static void range_fill_vector(T* vec, int32_t len, T start=0);
151 
153  static void random_vector(T* vec, int32_t len, T min_value, T max_value);
154 
156  static void randperm(T* perm, int32_t n);
157 
159  static void permute(T* vec, int32_t n);
160 
162  static void permute(T* vec, int32_t n, CRandom * rand);
163 
170  {
171  return *this;
172  }
173 
179  const T& get_element(index_t index);
180 
187  void set_element(const T& p_element, index_t index);
188 
194  void resize_vector(int32_t n);
195 
201  inline const T& operator[](uint64_t index) const
202  {
203  return vector[index];
204  }
205 
211  inline const T& operator[](int64_t index) const
212  {
213  return vector[index];
214  }
215 
221  inline const T& operator[](uint32_t index) const
222  {
223  return vector[index];
224  }
225 
231  inline const T& operator[](int32_t index) const
232  {
233  return vector[index];
234  }
235 
241  inline T& operator[](uint64_t index)
242  {
243  return vector[index];
244  }
245 
251  inline T& operator[](int64_t index)
252  {
253  return vector[index];
254  }
255 
261  inline T& operator[](uint32_t index)
262  {
263  return vector[index];
264  }
265 
271  inline T& operator[](int32_t index)
272  {
273  return vector[index];
274  }
275 
280  void add(const SGVector<T> x);
281 
286  void add(const SGSparseVector<T>& x);
287 
292  void add(const T x);
293 
296 
299  {
300  add(x);
301  return *this;
302  }
303 
306  {
307  add(x);
308  return *this;
309  }
310 
316  bool equals(SGVector<T>& other);
317 
319  static void permute_vector(SGVector<T> vec);
320 
322  void permute();
323 
325  void permute(CRandom * rand);
326 
328  static T twonorm(const T* x, int32_t len);
329 
331  static float64_t onenorm(T* x, int32_t len);
332 
334  static T qsq(T* x, int32_t len, float64_t q);
335 
337  static T qnorm(T* x, int32_t len, float64_t q);
338 
340  static void vec1_plus_scalar_times_vec2(T* vec1,
341  const T scalar, const T* vec2, int32_t n);
342 
344  static inline float64_t dot(const bool* v1, const bool* v2, int32_t n)
345  {
346  float64_t r=0;
347  for (int32_t i=0; i<n; i++)
348  r+=((v1[i]) ? 1 : 0) * ((v2[i]) ? 1 : 0);
349  return r;
350  }
351 
353  static inline floatmax_t dot(const floatmax_t* v1, const floatmax_t* v2, int32_t n)
354  {
355  floatmax_t r=0;
356  for (int32_t i=0; i<n; i++)
357  r+=v1[i]*v2[i];
358  return r;
359  }
360 
361 
363  static float64_t dot(const float64_t* v1, const float64_t* v2, int32_t n);
364 
366  static float32_t dot(const float32_t* v1, const float32_t* v2, int32_t n);
367 
369  static inline float64_t dot(
370  const uint64_t* v1, const uint64_t* v2, int32_t n)
371  {
372  float64_t r=0;
373  for (int32_t i=0; i<n; i++)
374  r+=((float64_t) v1[i])*v2[i];
375 
376  return r;
377  }
379  static inline float64_t dot(
380  const int64_t* v1, const int64_t* v2, int32_t n)
381  {
382  float64_t r=0;
383  for (int32_t i=0; i<n; i++)
384  r+=((float64_t) v1[i])*v2[i];
385 
386  return r;
387  }
388 
390  static inline float64_t dot(
391  const int32_t* v1, const int32_t* v2, int32_t n)
392  {
393  float64_t r=0;
394  for (int32_t i=0; i<n; i++)
395  r+=((float64_t) v1[i])*v2[i];
396 
397  return r;
398  }
399 
401  static inline float64_t dot(
402  const uint32_t* v1, const uint32_t* v2, int32_t n)
403  {
404  float64_t r=0;
405  for (int32_t i=0; i<n; i++)
406  r+=((float64_t) v1[i])*v2[i];
407 
408  return r;
409  }
410 
412  static inline float64_t dot(
413  const uint16_t* v1, const uint16_t* v2, int32_t n)
414  {
415  float64_t r=0;
416  for (int32_t i=0; i<n; i++)
417  r+=((float64_t) v1[i])*v2[i];
418 
419  return r;
420  }
421 
423  static inline float64_t dot(
424  const int16_t* v1, const int16_t* v2, int32_t n)
425  {
426  float64_t r=0;
427  for (int32_t i=0; i<n; i++)
428  r+=((float64_t) v1[i])*v2[i];
429 
430  return r;
431  }
432 
434  static inline float64_t dot(
435  const char* v1, const char* v2, int32_t n)
436  {
437  float64_t r=0;
438  for (int32_t i=0; i<n; i++)
439  r+=((float64_t) v1[i])*v2[i];
440 
441  return r;
442  }
443 
445  static inline float64_t dot(
446  const uint8_t* v1, const uint8_t* v2, int32_t n)
447  {
448  float64_t r=0;
449  for (int32_t i=0; i<n; i++)
450  r+=((float64_t) v1[i])*v2[i];
451 
452  return r;
453  }
454 
456  static inline float64_t dot(
457  const int8_t* v1, const int8_t* v2, int32_t n)
458  {
459  float64_t r=0;
460  for (int32_t i=0; i<n; i++)
461  r+=((float64_t) v1[i])*v2[i];
462 
463  return r;
464  }
465 
467  static inline float64_t dot(
468  const float64_t* v1, const char* v2, int32_t n)
469  {
470  float64_t r=0;
471  for (int32_t i=0; i<n; i++)
472  r+=((float64_t) v1[i])*v2[i];
473 
474  return r;
475  }
476 
478  static inline void vector_multiply(
479  T* target, const T* v1, const T* v2,int32_t len)
480  {
481  for (int32_t i=0; i<len; i++)
482  target[i]=v1[i]*v2[i];
483  }
484 
485 
487  static inline void add(
488  T* target, T alpha, const T* v1, T beta, const T* v2,
489  int32_t len)
490  {
491  for (int32_t i=0; i<len; i++)
492  target[i]=alpha*v1[i]+beta*v2[i];
493  }
494 
496  static inline void add_scalar(T alpha, T* vec, int32_t len)
497  {
498  for (int32_t i=0; i<len; i++)
499  vec[i]+=alpha;
500  }
501 
503  static void scale_vector(T alpha, T* vec, int32_t len);
504 
506  static inline T sum(T* vec, int32_t len)
507  {
508  T result=0;
509  for (int32_t i=0; i<len; i++)
510  result+=vec[i];
511 
512  return result;
513  }
514 
516  static inline T sum(SGVector<T> vec)
517  {
518  return sum(vec.vector, vec.vlen);
519  }
520 
522  static inline T product(T* vec, int32_t len)
523  {
524  T result=1;
525  for (int32_t i=0; i<len; i++)
526  result*=vec[i];
527 
528  return result;
529  }
530 
532  inline T product()
533  {
534  return product(vector, vlen);
535  }
536 
538  static T min(T* vec, int32_t len);
539 
541  static T max_abs(T* vec, int32_t len);
542 
544  static T max(T* vec, int32_t len);
545 
547  static int32_t arg_max(T * vec, int32_t inc, int32_t len, T * maxv_ptr = NULL);
548 
550  static int32_t arg_max_abs(T * vec, int32_t inc, int32_t len, T * maxv_ptr = NULL);
551 
553  static int32_t arg_min(T * vec, int32_t inc, int32_t len, T * minv_ptr = NULL);
554 
556  static T sum_abs(T* vec, int32_t len);
557 
559  static bool fequal(T x, T y, float64_t precision=1e-6);
560 
564  static int32_t unique(T* output, int32_t size);
565 
567  void display_size() const;
568 
570  void display_vector(const char* name="vector",
571  const char* prefix="") const;
572 
574  static void display_vector(
575  const T* vector, int32_t n, const char* name="vector",
576  const char* prefix="");
577 
579  static void display_vector(
580  const SGVector<T>, const char* name="vector",
581  const char* prefix="");
582 
586  SGVector<index_t> find(T elem);
587 
591  template <typename Predicate>
593  {
594  SGVector<index_t> idx(vlen);
595  index_t k=0;
596 
597  for (index_t i=0; i < vlen; ++i)
598  if (p(vector[i]))
599  idx[k++] = i;
600 
601  idx.vlen = k;
602  return idx;
603  }
604 
606  void scale(T alpha);
607 
612  float64_t mean() const;
613 
618  void load(CFile* loader);
619 
624  void save(CFile* saver);
625 
627  void abs();
629  void acos();
631  void asin();
633  void atan();
635  void atan2(T x);
637  void cos();
639  void cosh();
641  void exp();
643  void log();
645  void log10();
647  void pow(T q);
649  void sin();
651  void sinh();
653  void sqrt();
655  void tan();
657  void tanh();
658 
661 
664 
675  static SGMatrix<T> convert_to_matrix(SGVector<T> vector, index_t nrows, index_t ncols, bool fortran_order);
676 
677 
690  static void convert_to_matrix(T*& matrix, index_t nrows, index_t ncols, const T* vector, int32_t vlen, bool fortran_order);
691 
692  protected:
694  virtual void copy_data(const SGReferencedData &orig);
695 
697  virtual void init_data();
698 
700  virtual void free_data();
701 
702  public:
704  T* vector;
707 };
708 
709 #ifndef DOXYGEN_SHOULD_SKIP_THIS
711  const float64_t scalar, const float64_t* vec2, int32_t n);
712 
714  const float32_t scalar, const float32_t* vec2, int32_t n);
715 #endif // DOXYGEN_SHOULD_SKIP_THIS
716 }
717 #endif // __SGVECTOR_H__
static SGVector< float64_t > linspace_vec(T start, T end, int32_t n)
Definition: SGVector.cpp:1330
void cosh()
hyperbolic cosine of vector elements
static T sum_abs(T *vec, int32_t len)
return sum(abs(vec))
Definition: SGVector.cpp:1109
static void range_fill_vector(T *vec, int32_t len, T start=0)
Definition: SGVector.cpp:278
bool equals(SGVector< T > &other)
Definition: SGVector.cpp:390
static float64_t dot(const bool *v1, const bool *v2, int32_t n)
compute dot product between v1 and v2 (blas optimized)
Definition: SGVector.h:344
SGVector< T > operator+(SGVector< T > x)
Definition: SGVector.cpp:318
T & operator[](uint32_t index)
Definition: SGVector.h:261
static float64_t dot(const uint32_t *v1, const uint32_t *v2, int32_t n)
compute dot product between v1 and v2 (for 32bit unsigned ints)
Definition: SGVector.h:401
static void random_vector(T *vec, int32_t len, T min_value, T max_value)
Definition: SGVector.cpp:650
static float64_t dot(const float64_t *v1, const char *v2, int32_t n)
compute dot product between v1 and v2
Definition: SGVector.h:467
index_t find_position_to_insert(T element)
Definition: SGVector.cpp:237
static T * clone_vector(const T *vec, int32_t len)
Definition: SGVector.cpp:263
T & operator[](int64_t index)
Definition: SGVector.h:251
static float64_t dot(const int8_t *v1, const int8_t *v2, int32_t n)
compute dot product between v1 and v2 (for 8bit (un)signed ints)
Definition: SGVector.h:456
int32_t index_t
Definition: common.h:60
const T & operator[](uint64_t index) const
Definition: SGVector.h:201
void atan2(T x)
atan2 of vector elements
Definition: SGVector.cpp:1314
static T max(T *vec, int32_t len)
Definition: SGVector.cpp:981
static float64_t dot(const uint8_t *v1, const uint8_t *v2, int32_t n)
compute dot product between v1 and v2 (for 8bit (un)signed ints)
Definition: SGVector.h:445
static T sum(T *vec, int32_t len)
return sum(vec)
Definition: SGVector.h:506
void log()
natural logarithm of vector elements
virtual void copy_data(const SGReferencedData &orig)
Definition: SGVector.cpp:368
static bool fequal(T x, T y, float64_t precision=1e-6)
return sum(abs(vec))
Definition: SGVector.cpp:1138
void set_element(const T &p_element, index_t index)
Definition: SGVector.cpp:300
void exp()
exponential of vector elements
void cos()
cosine of vector elements
static int32_t arg_max(T *vec, int32_t inc, int32_t len, T *maxv_ptr=NULL)
return arg_max(vec)
Definition: SGVector.cpp:1048
void tanh()
hyperbolic tangent of vector elements
void random(T min_value, T max_value)
Definition: SGVector.cpp:153
void load(CFile *loader)
Definition: SGVector.cpp:1222
void sqrt()
square root of vector elements
void tan()
tangent of vector elements
static void add(T *target, T alpha, const T *v1, T beta, const T *v2, int32_t len)
target=alpha*vec1 + beta*vec2
Definition: SGVector.h:487
static float64_t dot(const int16_t *v1, const int16_t *v2, int32_t n)
compute dot product between v1 and v2 (for 16bit unsigned ints)
Definition: SGVector.h:423
static float64_t onenorm(T *x, int32_t len)
|| x ||_1
Definition: SGVector.cpp:882
void acos()
arc cosine of vector elements
void add(const SGVector< T > x)
Definition: SGVector.cpp:329
virtual void init_data()
Definition: SGVector.cpp:375
static float64_t dot(const uint16_t *v1, const uint16_t *v2, int32_t n)
compute dot product between v1 and v2 (for 16bit unsigned ints)
Definition: SGVector.h:412
static float64_t dot(const uint64_t *v1, const uint64_t *v2, int32_t n)
compute dot product between v1 and v2 (for 64bit unsigned ints)
Definition: SGVector.h:369
static void vector_multiply(T *target, const T *v1, const T *v2, int32_t len)
compute vector multiplication
Definition: SGVector.h:478
static T sum(SGVector< T > vec)
return sum(vec)
Definition: SGVector.h:516
static int32_t arg_max_abs(T *vec, int32_t inc, int32_t len, T *maxv_ptr=NULL)
return arg_max_abs(vec)
Definition: SGVector.cpp:1019
template class SGSparseVector The assumtion is that the stored SGSparseVectorEntry* vector is orde...
static void permute_vector(SGVector< T > vec)
Definition: SGVector.cpp:736
void scale(T alpha)
scale vector inplace
Definition: SGVector.cpp:1200
static SGVector< T > randperm_vec(int32_t n)
Definition: SGVector.cpp:664
static float64_t dot(const char *v1, const char *v2, int32_t n)
compute dot product between v1 and v2 (for 8bit (un)signed ints)
Definition: SGVector.h:434
void atan()
arc tangent of vector elements
int32_t size() const
Definition: SGVector.h:54
virtual void free_data()
Definition: SGVector.cpp:382
static T min(T *vec, int32_t len)
Definition: SGVector.cpp:926
shogun reference count managed data
double float64_t
Definition: common.h:48
static float64_t dot(const int32_t *v1, const int32_t *v2, int32_t n)
compute dot product between v1 and v2 (for 32bit ints)
Definition: SGVector.h:390
static T product(T *vec, int32_t len)
return the product of the vectors elements
Definition: SGVector.h:522
static T max_abs(T *vec, int32_t len)
Definition: SGVector.cpp:961
void sinh()
hyperbolic sine of vector elements
long double floatmax_t
Definition: common.h:49
void save(CFile *saver)
Definition: SGVector.cpp:1242
A File access base class.
Definition: File.h:34
void range_fill(T start=0)
Definition: SGVector.cpp:145
void sin()
sine of vector elements
virtual ~SGVector()
Definition: SGVector.cpp:104
const T & operator[](int32_t index) const
Definition: SGVector.h:231
SGVector< T > clone() const
Definition: SGVector.cpp:257
static void fill_vector(T *vec, int32_t len, T value)
Definition: SGVector.cpp:271
static void scale_vector(T alpha, T *vec, int32_t len)
scale vector inplace
Definition: SGVector.cpp:1179
static float64_t dot(const int64_t *v1, const int64_t *v2, int32_t n)
compute dot product between v1 and v2 (for 64bit ints)
Definition: SGVector.h:379
SGVector< T > operator+=(SGVector< T > x)
Definition: SGVector.h:298
void set_const(T const_elem)
Definition: SGVector.cpp:124
float float32_t
Definition: common.h:47
: Pseudo random number geneartor
Definition: Random.h:32
void asin()
arc sine of vector elements
shogun matrix
Definition: DenseFeatures.h:27
const T & operator[](int64_t index) const
Definition: SGVector.h:211
bool is_sorted() const
Definition: SGVector.cpp:215
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:16
SGVector< float64_t > get_imag()
Definition: SGVector.cpp:1367
const T & operator[](uint32_t index) const
Definition: SGVector.h:221
SGVector< float64_t > get_real()
Definition: SGVector.cpp:1359
void log10()
common logarithm of vector elements
static floatmax_t dot(const floatmax_t *v1, const floatmax_t *v2, int32_t n)
compute dot product between v1 and v2 (blas optimized)
Definition: SGVector.h:353
void display_size() const
Definition: SGVector.cpp:362
SGVector< index_t > find(T elem)
Definition: SGVector.cpp:1166
static T qnorm(T *x, int32_t len, float64_t q)
|| x ||_q
Definition: SGVector.cpp:911
void pow(T q)
power of vector elements
Definition: SGVector.cpp:1323
float64_t mean() const
Definition: SGVector.cpp:1205
T product()
return product(vec)
Definition: SGVector.h:532
void resize_vector(int32_t n)
Definition: SGVector.cpp:307
SGVector< index_t > argsort()
Definition: SGVector.cpp:194
static void add_scalar(T alpha, T *vec, int32_t len)
add scalar to vector inplace
Definition: SGVector.h:496
static float64_t * linspace(T start, T end, int32_t n)
Definition: SGVector.cpp:1336
static SGMatrix< T > convert_to_matrix(SGVector< T > vector, index_t nrows, index_t ncols, bool fortran_order)
Definition: SGVector.cpp:1376
void display_vector(const char *name="vector", const char *prefix="") const
Definition: SGVector.cpp:405
T & operator[](int32_t index)
Definition: SGVector.h:271
void abs()
absolute value of vector elements
void set(SGVector< T > orig)
Definition: SGVector.cpp:98
const T & get_element(index_t index)
Definition: SGVector.cpp:293
T & operator[](uint64_t index)
Definition: SGVector.h:241
static int32_t unique(T *output, int32_t size)
Definition: SGVector.cpp:1144
static T qsq(T *x, int32_t len, float64_t q)
|| x ||_q^q
Definition: SGVector.cpp:893
SGVector< index_t > find_if(Predicate p)
Definition: SGVector.h:592
index_t vlen
Definition: SGVector.h:706
static int32_t arg_min(T *vec, int32_t inc, int32_t len, T *minv_ptr=NULL)
return arg_min(vec)
Definition: SGVector.cpp:1079
static void vec1_plus_scalar_times_vec2(T *vec1, const T scalar, const T *vec2, int32_t n)
x=x+alpha*y
Definition: SGVector.cpp:580
static T twonorm(const T *x, int32_t len)
|| x ||_2

SHOGUN Machine Learning Toolbox - Documentation