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  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 
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 
168  const T& get_element(index_t index);
169 
176  void set_element(const T& p_element, index_t index);
177 
183  void resize_vector(int32_t n);
184 
190  inline const T& operator[](uint64_t index) const
191  {
192  return vector[index];
193  }
194 
200  inline const T& operator[](int64_t index) const
201  {
202  return vector[index];
203  }
204 
210  inline const T& operator[](uint32_t index) const
211  {
212  return vector[index];
213  }
214 
220  inline const T& operator[](int32_t index) const
221  {
222  return vector[index];
223  }
224 
230  inline T& operator[](uint64_t index)
231  {
232  return vector[index];
233  }
234 
240  inline T& operator[](int64_t index)
241  {
242  return vector[index];
243  }
244 
250  inline T& operator[](uint32_t index)
251  {
252  return vector[index];
253  }
254 
260  inline T& operator[](int32_t index)
261  {
262  return vector[index];
263  }
264 
269  void add(const SGVector<T> x);
270 
275  void add(const SGSparseVector<T>& x);
276 
281  void add(const T x);
282 
285 
288  {
289  add(x);
290  return *this;
291  }
292 
295  {
296  add(x);
297  return *this;
298  }
299 
305  bool equals(SGVector<T>& other);
306 
308  static T twonorm(const T* x, int32_t len);
309 
311  static float64_t onenorm(T* x, int32_t len);
312 
314  static T qsq(T* x, int32_t len, float64_t q);
315 
317  static T qnorm(T* x, int32_t len, float64_t q);
318 
320  static void vec1_plus_scalar_times_vec2(T* vec1,
321  const T scalar, const T* vec2, int32_t n);
322 
324  static inline void vector_multiply(
325  T* target, const T* v1, const T* v2,int32_t len)
326  {
327  for (int32_t i=0; i<len; i++)
328  target[i]=v1[i]*v2[i];
329  }
330 
331 
333  static inline void add(
334  T* target, T alpha, const T* v1, T beta, const T* v2,
335  int32_t len)
336  {
337  for (int32_t i=0; i<len; i++)
338  target[i]=alpha*v1[i]+beta*v2[i];
339  }
340 
342  static inline void add_scalar(T alpha, T* vec, int32_t len)
343  {
344  for (int32_t i=0; i<len; i++)
345  vec[i]+=alpha;
346  }
347 
349  static void scale_vector(T alpha, T* vec, int32_t len);
350 
352  static inline T sum(T* vec, int32_t len)
353  {
354  T result=0;
355  for (int32_t i=0; i<len; i++)
356  result+=vec[i];
357 
358  return result;
359  }
360 
362  static inline T sum(SGVector<T> vec)
363  {
364  return sum(vec.vector, vec.vlen);
365  }
366 
368  static inline T product(T* vec, int32_t len)
369  {
370  T result=1;
371  for (int32_t i=0; i<len; i++)
372  result*=vec[i];
373 
374  return result;
375  }
376 
378  inline T product()
379  {
380  return product(vector, vlen);
381  }
382 
384  static T sum_abs(T* vec, int32_t len);
385 
389  static int32_t unique(T* output, int32_t size);
390 
392  void display_size() const;
393 
395  void display_vector(const char* name="vector",
396  const char* prefix="") const;
397 
399  static void display_vector(
400  const T* vector, int32_t n, const char* name="vector",
401  const char* prefix="");
402 
404  static void display_vector(
405  const SGVector<T>, const char* name="vector",
406  const char* prefix="");
407 
411  SGVector<index_t> find(T elem);
412 
416  template <typename Predicate>
418  {
419  SGVector<index_t> idx(vlen);
420  index_t k=0;
421 
422  for (index_t i=0; i < vlen; ++i)
423  if (p(vector[i]))
424  idx[k++] = i;
425 
426  idx.vlen = k;
427  return idx;
428  }
429 
431  void scale(T alpha);
432 
437  void load(CFile* loader);
438 
443  void save(CFile* saver);
444 
447 
450 
461  static SGMatrix<T> convert_to_matrix(SGVector<T> vector, index_t nrows, index_t ncols, bool fortran_order);
462 
463 
476  static void convert_to_matrix(T*& matrix, index_t nrows, index_t ncols, const T* vector, int32_t vlen, bool fortran_order);
477 #endif // #ifndef SWIG // SWIG should skip this part
478  protected:
480  virtual void copy_data(const SGReferencedData &orig);
481 
483  virtual void init_data();
484 
486  virtual void free_data();
487 
488  public:
490  T* vector;
493 };
494 
495 #ifndef DOXYGEN_SHOULD_SKIP_THIS
497  const float64_t scalar, const float64_t* vec2, int32_t n);
498 
500  const float32_t scalar, const float32_t* vec2, int32_t n);
501 #endif // DOXYGEN_SHOULD_SKIP_THIS
502 }
503 #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
T & operator[](uint64_t index)
Definition: SGVector.h:230
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: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:220
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:492
SGVector< index_t > find_if(Predicate p)
Definition: SGVector.h:417
static T product(T *vec, int32_t len)
Return the product of the vectors elements.
Definition: SGVector.h:368
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:190
static void vector_multiply(T *target, const T *v1, const T *v2, int32_t len)
Compute vector multiplication.
Definition: SGVector.h:324
static void add_scalar(T alpha, T *vec, int32_t len)
Add scalar to vector inplace.
Definition: SGVector.h:342
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:352
T & operator[](int32_t index)
Definition: SGVector.h:260
SGVector< T > operator+=(SGVector< T > x)
Definition: SGVector.h:287
T & operator[](uint32_t index)
Definition: SGVector.h:250
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:362
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:378
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:240
virtual void copy_data(const SGReferencedData &orig)
Definition: SGVector.cpp:317
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:333
const T & operator[](uint32_t index) const
Definition: SGVector.h:210
const T & operator[](int64_t index) const
Definition: SGVector.h:200
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