SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SGVector.h
Go to the documentation of this file.
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/config.h>
18 
19 #include <shogun/lib/common.h>
21 
22 namespace shogun
23 {
24  template <class T> class SGSparseVector;
25  template <class T> class SGMatrix;
26  class CFile;
27  class CRandom;
28 
30 template<class T> class SGVector : public SGReferencedData
31 {
32  public:
34  SGVector();
35 
37  SGVector(T* v, index_t len, bool ref_counting=true);
38 
40  SGVector(index_t len, bool ref_counting=true);
41 
43  SGVector(const SGVector &orig);
44 
49  void set(SGVector<T> orig);
50 
52  virtual ~SGVector();
53 
55  inline int32_t size() const { return vlen; }
56 
58  operator T*() { return vector; };
59 
61  void zero();
62 
67  void set_const(T const_elem);
68 
73  void range_fill(T start=0);
74 
80  void random(T min_value, T max_value);
81 
83  void randperm();
84 
86  static SGVector<T> randperm_vec(int32_t n);
87 
94  static T* randperm(int32_t n);
95 
103  static SGVector<float64_t> linspace_vec(T start, T end, int32_t n);
104 
112  static float64_t* linspace(T start, T end, int32_t n);
113 
120  index_t find_position_to_insert(T element);
121 
125  void qsort();
126 
134 
139  bool is_sorted() const;
140 
142  SGVector<T> clone() const;
143 
145  static T* clone_vector(const T* vec, int32_t len);
146 
148  static void fill_vector(T* vec, int32_t len, T value);
149 
151  static void range_fill_vector(T* vec, int32_t len, T start=0);
152 
154  static void random_vector(T* vec, int32_t len, T min_value, T max_value);
155 
157  static void randperm(T* perm, int32_t n);
158 
160  static void permute(T* vec, int32_t n);
161 
163  static void permute(T* vec, int32_t n, CRandom * rand);
164 
171  {
172  return *this;
173  }
174 
180  const T& get_element(index_t index);
181 
188  void set_element(const T& p_element, index_t index);
189 
195  void resize_vector(int32_t n);
196 
202  inline const T& operator[](uint64_t index) const
203  {
204  return vector[index];
205  }
206 
212  inline const T& operator[](int64_t index) const
213  {
214  return vector[index];
215  }
216 
222  inline const T& operator[](uint32_t index) const
223  {
224  return vector[index];
225  }
226 
232  inline const T& operator[](int32_t index) const
233  {
234  return vector[index];
235  }
236 
242  inline T& operator[](uint64_t index)
243  {
244  return vector[index];
245  }
246 
252  inline T& operator[](int64_t index)
253  {
254  return vector[index];
255  }
256 
262  inline T& operator[](uint32_t index)
263  {
264  return vector[index];
265  }
266 
272  inline T& operator[](int32_t index)
273  {
274  return vector[index];
275  }
276 
281  void add(const SGVector<T> x);
282 
287  void add(const SGSparseVector<T>& x);
288 
293  void add(const T x);
294 
297 
300  {
301  add(x);
302  return *this;
303  }
304 
307  {
308  add(x);
309  return *this;
310  }
311 
317  bool equals(SGVector<T>& other);
318 
320  static void permute_vector(SGVector<T> vec);
321 
323  void permute();
324 
326  void permute(CRandom * rand);
327 
329  static T twonorm(const T* x, int32_t len);
330 
332  static float64_t onenorm(T* x, int32_t len);
333 
335  static T qsq(T* x, int32_t len, float64_t q);
336 
338  static T qnorm(T* x, int32_t len, float64_t q);
339 
341  static void vec1_plus_scalar_times_vec2(T* vec1,
342  const T scalar, const T* vec2, int32_t n);
343 
345  static inline float64_t dot(const bool* v1, const bool* v2, int32_t n)
346  {
347  float64_t r=0;
348  for (int32_t i=0; i<n; i++)
349  r+=((v1[i]) ? 1 : 0) * ((v2[i]) ? 1 : 0);
350  return r;
351  }
352 
354  static inline floatmax_t dot(const floatmax_t* v1, const floatmax_t* v2, int32_t n)
355  {
356  floatmax_t r=0;
357  for (int32_t i=0; i<n; i++)
358  r+=v1[i]*v2[i];
359  return r;
360  }
361 
362 
364  static float64_t dot(const float64_t* v1, const float64_t* v2, int32_t n);
365 
367  static float32_t dot(const float32_t* v1, const float32_t* v2, int32_t n);
368 
370  static inline float64_t dot(
371  const uint64_t* v1, const uint64_t* v2, int32_t n)
372  {
373  float64_t r=0;
374  for (int32_t i=0; i<n; i++)
375  r+=((float64_t) v1[i])*v2[i];
376 
377  return r;
378  }
380  static inline float64_t dot(
381  const int64_t* v1, const int64_t* v2, int32_t n)
382  {
383  float64_t r=0;
384  for (int32_t i=0; i<n; i++)
385  r+=((float64_t) v1[i])*v2[i];
386 
387  return r;
388  }
389 
391  static inline float64_t dot(
392  const int32_t* v1, const int32_t* v2, int32_t n)
393  {
394  float64_t r=0;
395  for (int32_t i=0; i<n; i++)
396  r+=((float64_t) v1[i])*v2[i];
397 
398  return r;
399  }
400 
402  static inline float64_t dot(
403  const uint32_t* v1, const uint32_t* v2, int32_t n)
404  {
405  float64_t r=0;
406  for (int32_t i=0; i<n; i++)
407  r+=((float64_t) v1[i])*v2[i];
408 
409  return r;
410  }
411 
413  static inline float64_t dot(
414  const uint16_t* v1, const uint16_t* v2, int32_t n)
415  {
416  float64_t r=0;
417  for (int32_t i=0; i<n; i++)
418  r+=((float64_t) v1[i])*v2[i];
419 
420  return r;
421  }
422 
424  static inline float64_t dot(
425  const int16_t* v1, const int16_t* v2, int32_t n)
426  {
427  float64_t r=0;
428  for (int32_t i=0; i<n; i++)
429  r+=((float64_t) v1[i])*v2[i];
430 
431  return r;
432  }
433 
435  static inline float64_t dot(
436  const char* v1, const char* v2, int32_t n)
437  {
438  float64_t r=0;
439  for (int32_t i=0; i<n; i++)
440  r+=((float64_t) v1[i])*v2[i];
441 
442  return r;
443  }
444 
446  static inline float64_t dot(
447  const uint8_t* v1, const uint8_t* v2, int32_t n)
448  {
449  float64_t r=0;
450  for (int32_t i=0; i<n; i++)
451  r+=((float64_t) v1[i])*v2[i];
452 
453  return r;
454  }
455 
457  static inline float64_t dot(
458  const int8_t* v1, const int8_t* v2, int32_t n)
459  {
460  float64_t r=0;
461  for (int32_t i=0; i<n; i++)
462  r+=((float64_t) v1[i])*v2[i];
463 
464  return r;
465  }
466 
468  static inline float64_t dot(
469  const float64_t* v1, const char* v2, int32_t n)
470  {
471  float64_t r=0;
472  for (int32_t i=0; i<n; i++)
473  r+=((float64_t) v1[i])*v2[i];
474 
475  return r;
476  }
477 
479  static inline void vector_multiply(
480  T* target, const T* v1, const T* v2,int32_t len)
481  {
482  for (int32_t i=0; i<len; i++)
483  target[i]=v1[i]*v2[i];
484  }
485 
486 
488  static inline void add(
489  T* target, T alpha, const T* v1, T beta, const T* v2,
490  int32_t len)
491  {
492  for (int32_t i=0; i<len; i++)
493  target[i]=alpha*v1[i]+beta*v2[i];
494  }
495 
497  static inline void add_scalar(T alpha, T* vec, int32_t len)
498  {
499  for (int32_t i=0; i<len; i++)
500  vec[i]+=alpha;
501  }
502 
504  static void scale_vector(T alpha, T* vec, int32_t len);
505 
507  static inline T sum(T* vec, int32_t len)
508  {
509  T result=0;
510  for (int32_t i=0; i<len; i++)
511  result+=vec[i];
512 
513  return result;
514  }
515 
517  static inline T sum(SGVector<T> vec)
518  {
519  return sum(vec.vector, vec.vlen);
520  }
521 
523  static inline T product(T* vec, int32_t len)
524  {
525  T result=1;
526  for (int32_t i=0; i<len; i++)
527  result*=vec[i];
528 
529  return result;
530  }
531 
533  inline T product()
534  {
535  return product(vector, vlen);
536  }
537 
539  static T min(T* vec, int32_t len);
540 
542  static T max_abs(T* vec, int32_t len);
543 
545  static T max(T* vec, int32_t len);
546 
548  static int32_t arg_max(T * vec, int32_t inc, int32_t len, T * maxv_ptr = NULL);
549 
551  static int32_t arg_max_abs(T * vec, int32_t inc, int32_t len, T * maxv_ptr = NULL);
552 
554  static int32_t arg_min(T * vec, int32_t inc, int32_t len, T * minv_ptr = NULL);
555 
557  static T sum_abs(T* vec, int32_t len);
558 
560  static bool fequal(T x, T y, float64_t precision=1e-6);
561 
565  static int32_t unique(T* output, int32_t size);
566 
568  void display_size() const;
569 
571  void display_vector(const char* name="vector",
572  const char* prefix="") const;
573 
575  static void display_vector(
576  const T* vector, int32_t n, const char* name="vector",
577  const char* prefix="");
578 
580  static void display_vector(
581  const SGVector<T>, const char* name="vector",
582  const char* prefix="");
583 
587  SGVector<index_t> find(T elem);
588 
592  template <typename Predicate>
594  {
595  SGVector<index_t> idx(vlen);
596  index_t k=0;
597 
598  for (index_t i=0; i < vlen; ++i)
599  if (p(vector[i]))
600  idx[k++] = i;
601 
602  idx.vlen = k;
603  return idx;
604  }
605 
607  void scale(T alpha);
608 
613  float64_t mean() const;
614 
619  void load(CFile* loader);
620 
625  void save(CFile* saver);
626 
628  void abs();
630  void acos();
632  void asin();
634  void atan();
636  void atan2(T x);
638  void cos();
640  void cosh();
642  void exp();
644  void log();
646  void log10();
648  void pow(T q);
650  void sin();
652  void sinh();
654  void sqrt();
656  void tan();
658  void tanh();
659 
662 
665 
676  static SGMatrix<T> convert_to_matrix(SGVector<T> vector, index_t nrows, index_t ncols, bool fortran_order);
677 
678 
691  static void convert_to_matrix(T*& matrix, index_t nrows, index_t ncols, const T* vector, int32_t vlen, bool fortran_order);
692 
693  protected:
695  virtual void copy_data(const SGReferencedData &orig);
696 
698  virtual void init_data();
699 
701  virtual void free_data();
702 
703  public:
705  T* vector;
708 };
709 
710 #ifndef DOXYGEN_SHOULD_SKIP_THIS
712  const float64_t scalar, const float64_t* vec2, int32_t n);
713 
715  const float32_t scalar, const float32_t* vec2, int32_t n);
716 #endif // DOXYGEN_SHOULD_SKIP_THIS
717 }
718 #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:345
SGVector< T > operator+(SGVector< T > x)
Definition: SGVector.cpp:318
T & operator[](uint32_t index)
Definition: SGVector.h:262
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:402
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:468
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:252
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:457
int32_t index_t
Definition: common.h:62
const T & operator[](uint64_t index) const
Definition: SGVector.h:202
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:446
static T sum(T *vec, int32_t len)
return sum(vec)
Definition: SGVector.h:507
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:488
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:424
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:413
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:370
static void vector_multiply(T *target, const T *v1, const T *v2, int32_t len)
compute vector multiplication
Definition: SGVector.h:479
static T sum(SGVector< T > vec)
return sum(vec)
Definition: SGVector.h:517
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...
Definition: Parameter.h:29
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:435
void atan()
arc tangent of vector elements
int32_t size() const
Definition: SGVector.h:55
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:50
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:391
static T product(T *vec, int32_t len)
return the product of the vectors elements
Definition: SGVector.h:523
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:51
void save(CFile *saver)
Definition: SGVector.cpp:1242
A File access base class.
Definition: File.h:35
void range_fill(T start=0)
Definition: SGVector.cpp:145
void sin()
sine of vector elements
shogun vector
Definition: Parameter.h:28
virtual ~SGVector()
Definition: SGVector.cpp:104
const T & operator[](int32_t index) const
Definition: SGVector.h:232
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:380
SGVector< T > operator+=(SGVector< T > x)
Definition: SGVector.h:299
void set_const(T const_elem)
Definition: SGVector.cpp:124
float float32_t
Definition: common.h:49
: Pseudo random number geneartor
Definition: Random.h:34
void asin()
arc sine of vector elements
shogun matrix
Definition: Parameter.h:26
const T & operator[](int64_t index) const
Definition: SGVector.h:212
bool is_sorted() const
Definition: SGVector.cpp:215
SGVector< float64_t > get_imag()
Definition: SGVector.cpp:1367
const T & operator[](uint32_t index) const
Definition: SGVector.h:222
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:354
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:533
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:497
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:272
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:242
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:593
index_t vlen
Definition: SGVector.h:707
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