SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 Eigen
23 {
24  template <class, int, int, int, int, int> class Matrix;
25  template<int, int> class Stride;
26  template <class, int, class> class Map;
27 }
28 
29 namespace shogun
30 {
31  template <class T> class SGSparseVector;
32  template <class T> class SGMatrix;
33  class CFile;
34  class CRandom;
35 
37 template<class T> class SGVector : public SGReferencedData
38 {
39  public:
40  typedef Eigen::Matrix<T,-1,1,0,-1,1> EigenVectorXt;
41  typedef Eigen::Matrix<T,1,-1,0x1,1,-1> EigenRowVectorXt;
42 
45 
47  typedef T Scalar;
48 
50  SGVector();
51 
53  SGVector(T* v, index_t len, bool ref_counting=true);
54 
56  SGVector(T* m, index_t len, index_t offset)
57  : SGReferencedData(false), vector(m+offset), vlen(len) { }
58 
60  SGVector(index_t len, bool ref_counting=true);
61 
63  SGVector(const SGVector &orig);
64 
65 #ifndef SWIG // SWIG should skip this part
66 #if defined(HAVE_CXX0X) || defined(HAVE_CXX11)
67 
69  template <typename ST> using container_type = SGVector<ST>;
70 
71 #endif // define (HAVE_CXX0X) || defined(HAVE_CXX11)
72 
74  SGVector(EigenVectorXt& vec);
75 
77  SGVector(EigenRowVectorXt& vec);
78 
80  operator EigenVectorXtMap() const;
81 
83  operator EigenRowVectorXtMap() const;
84 #endif // SWIG
85 
90  void set_const(T const_elem);
91 
98  {
99  return *this;
100  }
101 
102 #ifndef SWIG // SWIG should skip this part
103 
107  void set(SGVector<T> orig);
108 
110  virtual ~SGVector();
111 
113  inline int32_t size() const { return vlen; }
114 
116  inline T* data() const
117  {
118  return vector;
119  }
120 
122  operator T*() { return vector; }
123 
125  void zero();
126 
131  void range_fill(T start=0);
132 
138  void random(T min_value, T max_value);
139 
146  index_t find_position_to_insert(T element);
147 
149  SGVector<T> clone() const;
150 
152  static T* clone_vector(const T* vec, int32_t len);
153 
155  static void fill_vector(T* vec, int32_t len, T value);
156 
158  static void range_fill_vector(T* vec, int32_t len, T start=0);
159 
161  static void random_vector(T* vec, int32_t len, T min_value, T max_value);
162 #endif // SWIG // SWIG should skip this part
163 
169  const T& get_element(index_t index);
170 
177  void set_element(const T& p_element, index_t index);
178 
179 #ifndef SWIG // SWIG should skip this part
180 
185  void resize_vector(int32_t n);
186 
192  inline const T& operator[](uint64_t index) const
193  {
194  return vector[index];
195  }
196 
202  inline const T& operator[](int64_t index) const
203  {
204  return vector[index];
205  }
206 
212  inline const T& operator[](uint32_t index) const
213  {
214  return vector[index];
215  }
216 
222  inline const T& operator[](int32_t index) const
223  {
224  return vector[index];
225  }
226 
232  inline T& operator[](uint64_t index)
233  {
234  return vector[index];
235  }
236 
242  inline T& operator[](int64_t index)
243  {
244  return vector[index];
245  }
246 
252  inline T& operator[](uint32_t index)
253  {
254  return vector[index];
255  }
256 
262  inline T& operator[](int32_t index)
263  {
264  return vector[index];
265  }
266 
271  void add(const SGVector<T> x);
272 
277  void add(const SGSparseVector<T>& x);
278 
283  void add(const T x);
284 
287 
290  {
291  add(x);
292  return *this;
293  }
294 
297  {
298  add(x);
299  return *this;
300  }
301 
307  bool equals(SGVector<T>& other);
308 
310  static T twonorm(const T* x, int32_t len);
311 
313  static float64_t onenorm(T* x, int32_t len);
314 
316  static T qsq(T* x, int32_t len, float64_t q);
317 
319  static T qnorm(T* x, int32_t len, float64_t q);
320 
322  static void vec1_plus_scalar_times_vec2(T* vec1,
323  const T scalar, const T* vec2, int32_t n);
324 
326  static inline void vector_multiply(
327  T* target, const T* v1, const T* v2,int32_t len)
328  {
329  for (int32_t i=0; i<len; i++)
330  target[i]=v1[i]*v2[i];
331  }
332 
333 
335  static inline void add(
336  T* target, T alpha, const T* v1, T beta, const T* v2,
337  int32_t len)
338  {
339  for (int32_t i=0; i<len; i++)
340  target[i]=alpha*v1[i]+beta*v2[i];
341  }
342 
344  static inline void add_scalar(T alpha, T* vec, int32_t len)
345  {
346  for (int32_t i=0; i<len; i++)
347  vec[i]+=alpha;
348  }
349 
351  static void scale_vector(T alpha, T* vec, int32_t len);
352 
354  static inline T sum(T* vec, int32_t len)
355  {
356  T result=0;
357  for (int32_t i=0; i<len; i++)
358  result+=vec[i];
359 
360  return result;
361  }
362 
364  static inline T sum(SGVector<T> vec)
365  {
366  return sum(vec.vector, vec.vlen);
367  }
368 
370  static inline T product(T* vec, int32_t len)
371  {
372  T result=1;
373  for (int32_t i=0; i<len; i++)
374  result*=vec[i];
375 
376  return result;
377  }
378 
380  inline T product()
381  {
382  return product(vector, vlen);
383  }
384 
386  static T sum_abs(T* vec, int32_t len);
387 
391  static int32_t unique(T* output, int32_t size);
392 
394  void display_size() const;
395 
397  void display_vector(const char* name="vector",
398  const char* prefix="") const;
399 
401  static void display_vector(
402  const T* vector, int32_t n, const char* name="vector",
403  const char* prefix="");
404 
406  static void display_vector(
407  const SGVector<T>, const char* name="vector",
408  const char* prefix="");
409 
413  SGVector<index_t> find(T elem);
414 
418  template <typename Predicate>
420  {
421  SGVector<index_t> idx(vlen);
422  index_t k=0;
423 
424  for (index_t i=0; i < vlen; ++i)
425  if (p(vector[i]))
426  idx[k++] = i;
427 
428  idx.vlen = k;
429  return idx;
430  }
431 
433  void scale(T alpha);
434 
439  void load(CFile* loader);
440 
445  void save(CFile* saver);
446 
449 
452 
463  static SGMatrix<T> convert_to_matrix(SGVector<T> vector, index_t nrows, index_t ncols, bool fortran_order);
464 
465 
478  static void convert_to_matrix(T*& matrix, index_t nrows, index_t ncols, const T* vector, int32_t vlen, bool fortran_order);
479 #endif // #ifndef SWIG // SWIG should skip this part
480  protected:
482  virtual void copy_data(const SGReferencedData &orig);
483 
485  virtual void init_data();
486 
488  virtual void free_data();
489 
490  public:
492  T* vector;
495 };
496 
497 #ifndef DOXYGEN_SHOULD_SKIP_THIS
499  const float64_t scalar, const float64_t* vec2, int32_t n);
500 
502  const float32_t scalar, const float32_t* vec2, int32_t n);
503 #endif // DOXYGEN_SHOULD_SKIP_THIS
504 }
505 #endif // __SGVECTOR_H__
static T twonorm(const T *x, int32_t len)
|| x ||_2
void range_fill(T start=0)
Definition: SGVector.cpp:171
static void fill_vector(T *vec, int32_t len, T value)
Definition: SGVector.cpp:221
SGVector< float64_t > get_real()
Definition: SGVector.cpp:881
Eigen::Matrix< T, 1,-1, 0x1, 1,-1 > EigenRowVectorXt
Definition: SGVector.h:41
T & operator[](uint64_t index)
Definition: SGVector.h:232
int32_t index_t
Definition: common.h:62
SGVector(T *m, index_t len, index_t offset)
Definition: SGVector.h:56
Eigen::Map< EigenVectorXt, 0, Eigen::Stride< 0, 0 > > EigenVectorXtMap
Definition: SGVector.h:43
void set(SGVector< T > orig)
Definition: SGVector.cpp:98
bool equals(SGVector< T > &other)
Definition: SGVector.cpp:339
SGVector< float64_t > get_imag()
Definition: SGVector.cpp:889
Definition: SGMatrix.h:20
virtual void init_data()
Definition: SGVector.cpp:324
void random(T min_value, T max_value)
Definition: SGVector.cpp:179
void display_size() const
Definition: SGVector.cpp:311
const T & operator[](int32_t index) const
Definition: SGVector.h:222
void scale(T alpha)
Scale vector inplace.
Definition: SGVector.cpp:841
shogun matrix
const T & get_element(index_t index)
Definition: SGVector.cpp:243
static SGMatrix< T > convert_to_matrix(SGVector< T > vector, index_t nrows, index_t ncols, bool fortran_order)
Definition: SGVector.cpp:898
static T qnorm(T *x, int32_t len, float64_t q)
|| x ||_q
Definition: SGVector.cpp:742
static void scale_vector(T alpha, T *vec, int32_t len)
Scale vector inplace.
Definition: SGVector.cpp:820
void display_vector(const char *name="vector", const char *prefix="") const
Definition: SGVector.cpp:354
static T * clone_vector(const T *vec, int32_t len)
Definition: SGVector.cpp:213
int32_t size() const
Definition: SGVector.h:113
void set_element(const T &p_element, index_t index)
Definition: SGVector.cpp:250
index_t vlen
Definition: SGVector.h:494
SGVector< index_t > find_if(Predicate p)
Definition: SGVector.h:419
static T product(T *vec, int32_t len)
Return the product of the vectors elements.
Definition: SGVector.h:370
static T sum_abs(T *vec, int32_t len)
return sum(abs(vec))
Definition: SGVector.cpp:757
shogun vector
const T & operator[](uint64_t index) const
Definition: SGVector.h:192
static void vector_multiply(T *target, const T *v1, const T *v2, int32_t len)
Compute vector multiplication.
Definition: SGVector.h:326
static void add_scalar(T alpha, T *vec, int32_t len)
Add scalar to vector inplace.
Definition: SGVector.h:344
shogun reference count managed data
double float64_t
Definition: common.h:50
static void range_fill_vector(T *vec, int32_t len, T start=0)
Definition: SGVector.cpp:228
A File access base class.
Definition: File.h:34
void save(CFile *saver)
Definition: SGVector.cpp:866
virtual ~SGVector()
Definition: SGVector.cpp:104
static T sum(T *vec, int32_t len)
Return sum(vec)
Definition: SGVector.h:354
T & operator[](int32_t index)
Definition: SGVector.h:262
SGVector< T > operator+=(SGVector< T > x)
Definition: SGVector.h:289
T & operator[](uint32_t index)
Definition: SGVector.h:252
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:529
static T sum(SGVector< T > vec)
Return sum(vec)
Definition: SGVector.h:364
virtual void free_data()
Definition: SGVector.cpp:331
float float32_t
Definition: common.h:49
index_t find_position_to_insert(T element)
Definition: SGVector.cpp:187
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
void load(CFile *loader)
Definition: SGVector.cpp:846
SGVector< T > operator+(SGVector< T > x)
Definition: SGVector.cpp:268
T product()
Return product(vec)
Definition: SGVector.h:380
SGVector< T > clone() const
Definition: SGVector.cpp:207
template class SGSparseVector The assumtion is that the stored SGSparseVectorEntry* vector is orde...
T & operator[](int64_t index)
Definition: SGVector.h:242
virtual void copy_data(const SGReferencedData &orig)
Definition: SGVector.cpp:317
Eigen::Matrix< T,-1, 1, 0,-1, 1 > EigenVectorXt
Definition: SGVector.h:40
static float64_t onenorm(T *x, int32_t len)
|| x ||_1
Definition: SGVector.cpp:713
static T qsq(T *x, int32_t len, float64_t q)
|| x ||_q^q
Definition: SGVector.cpp:724
void resize_vector(int32_t n)
Definition: SGVector.cpp:257
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:335
const T & operator[](uint32_t index) const
Definition: SGVector.h:212
const T & operator[](int64_t index) const
Definition: SGVector.h:202
Eigen::Map< EigenRowVectorXt, 0, Eigen::Stride< 0, 0 > > EigenRowVectorXtMap
Definition: SGVector.h:44
T * data() const
Definition: SGVector.h:116
SGVector< index_t > find(T elem)
Definition: SGVector.cpp:807
static int32_t unique(T *output, int32_t size)
Definition: SGVector.cpp:785
void set_const(T const_elem)
Definition: SGVector.cpp:150
void add(const SGVector< T > x)
Definition: SGVector.cpp:279
static void random_vector(T *vec, int32_t len, T min_value, T max_value)
Definition: SGVector.cpp:563

SHOGUN Machine Learning Toolbox - Documentation