SHOGUN  4.1.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  typedef Eigen::Matrix<T,-1,1,0,-1,1> EigenVectorXt;
40  typedef Eigen::Matrix<T,1,-1,0x1,1,-1> EigenRowVectorXt;
41 
42  typedef Eigen::Map<EigenVectorXt,0,Eigen::Stride<0,0> > EigenVectorXtMap;
43  typedef Eigen::Map<EigenRowVectorXt,0,Eigen::Stride<0,0> > EigenRowVectorXtMap;
44 
45  public:
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 
73 #ifdef HAVE_EIGEN3
74 
75  SGVector(EigenVectorXt& vec);
76 
78  SGVector(EigenRowVectorXt& vec);
79 
81  operator EigenVectorXtMap() const;
82 
84  operator EigenRowVectorXtMap() const;
85 #endif // HAVE_EIGEN3
86 #endif // SWIG
87 
92  void set_const(T const_elem);
93 
100  {
101  return *this;
102  }
103 
104 #ifndef SWIG // SWIG should skip this part
105 
109  void set(SGVector<T> orig);
110 
112  virtual ~SGVector();
113 
115  inline int32_t size() const { return vlen; }
116 
118  inline T* data() const
119  {
120  return vector;
121  }
122 
124  operator T*() { return vector; }
125 
127  void zero();
128 
133  void range_fill(T start=0);
134 
140  void random(T min_value, T max_value);
141 
148  index_t find_position_to_insert(T element);
149 
151  SGVector<T> clone() const;
152 
154  static T* clone_vector(const T* vec, int32_t len);
155 
157  static void fill_vector(T* vec, int32_t len, T value);
158 
160  static void range_fill_vector(T* vec, int32_t len, T start=0);
161 
163  static void random_vector(T* vec, int32_t len, T min_value, T max_value);
164 
170  const T& get_element(index_t index);
171 
178  void set_element(const T& p_element, index_t index);
179 
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:173
static void fill_vector(T *vec, int32_t len, T value)
Definition: SGVector.cpp:223
SGVector< float64_t > get_real()
Definition: SGVector.cpp:883
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
void set(SGVector< T > orig)
Definition: SGVector.cpp:98
bool equals(SGVector< T > &other)
Definition: SGVector.cpp:341
SGVector< float64_t > get_imag()
Definition: SGVector.cpp:891
Definition: SGMatrix.h:20
virtual void init_data()
Definition: SGVector.cpp:326
void random(T min_value, T max_value)
Definition: SGVector.cpp:181
void display_size() const
Definition: SGVector.cpp:313
const T & operator[](int32_t index) const
Definition: SGVector.h:222
void scale(T alpha)
Scale vector inplace.
Definition: SGVector.cpp:843
shogun matrix
const T & get_element(index_t index)
Definition: SGVector.cpp:245
static SGMatrix< T > convert_to_matrix(SGVector< T > vector, index_t nrows, index_t ncols, bool fortran_order)
Definition: SGVector.cpp:900
static T qnorm(T *x, int32_t len, float64_t q)
|| x ||_q
Definition: SGVector.cpp:744
static void scale_vector(T alpha, T *vec, int32_t len)
Scale vector inplace.
Definition: SGVector.cpp:822
void display_vector(const char *name="vector", const char *prefix="") const
Definition: SGVector.cpp:356
static T * clone_vector(const T *vec, int32_t len)
Definition: SGVector.cpp:215
int32_t size() const
Definition: SGVector.h:115
void set_element(const T &p_element, index_t index)
Definition: SGVector.cpp:252
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:759
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:230
A File access base class.
Definition: File.h:34
void save(CFile *saver)
Definition: SGVector.cpp:868
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:531
static T sum(SGVector< T > vec)
Return sum(vec)
Definition: SGVector.h:364
virtual void free_data()
Definition: SGVector.cpp:333
float float32_t
Definition: common.h:49
index_t find_position_to_insert(T element)
Definition: SGVector.cpp:189
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
void load(CFile *loader)
Definition: SGVector.cpp:848
SGVector< T > operator+(SGVector< T > x)
Definition: SGVector.cpp:270
T product()
Return product(vec)
Definition: SGVector.h:380
SGVector< T > clone() const
Definition: SGVector.cpp:209
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:319
static float64_t onenorm(T *x, int32_t len)
|| x ||_1
Definition: SGVector.cpp:715
static T qsq(T *x, int32_t len, float64_t q)
|| x ||_q^q
Definition: SGVector.cpp:726
void resize_vector(int32_t n)
Definition: SGVector.cpp:259
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
T * data() const
Definition: SGVector.h:118
SGVector< index_t > find(T elem)
Definition: SGVector.cpp:809
static int32_t unique(T *output, int32_t size)
Definition: SGVector.cpp:787
void set_const(T const_elem)
Definition: SGVector.cpp:152
void add(const SGVector< T > x)
Definition: SGVector.cpp:281
static void random_vector(T *vec, int32_t len, T min_value, T max_value)
Definition: SGVector.cpp:565

SHOGUN Machine Learning Toolbox - Documentation