SHOGUN  6.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
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/io/SGIO.h>
20 #include <shogun/lib/common.h>
23 
24 #include <memory>
25 #include <atomic>
26 
27 namespace Eigen
28 {
29  template <class, int, int, int, int, int> class Matrix;
30  template<int, int> class Stride;
31  template <class, int, class> class Map;
32 }
33 
34 namespace shogun
35 {
36  template <class T> class SGSparseVector;
37  template <class T> class SGMatrix;
38  class CFile;
39  class CRandom;
40 
42 template<class T> class SGVector : public SGReferencedData
43 {
44  friend class LinalgBackendEigen;
45 
46  public:
47  typedef Eigen::Matrix<T,-1,1,0,-1,1> EigenVectorXt;
48  typedef Eigen::Matrix<T,1,-1,0x1,1,-1> EigenRowVectorXt;
49 
52 
54  typedef T Scalar;
55 
57  SGVector();
58 
60  SGVector(T* v, index_t len, bool ref_counting=true);
61 
63  SGVector(T* m, index_t len, index_t offset);
64 
66  SGVector(index_t len, bool ref_counting=true);
67 
75 
77  SGVector(const SGVector &orig);
78 
83  bool on_gpu() const
84  {
85  return gpu_ptr != NULL;
86  }
87 
88 #ifndef SWIG // SWIG should skip this part
89 #if defined(HAVE_CXX0X) || defined(HAVE_CXX11)
90 
92  template <typename ST> using container_type = SGVector<ST>;
93 
94 #endif // define (HAVE_CXX0X) || defined(HAVE_CXX11)
95 
97  SGVector(EigenVectorXt& vec);
98 
100  SGVector(EigenRowVectorXt& vec);
101 
103  operator EigenVectorXtMap() const;
104 
106  operator EigenRowVectorXtMap() const;
107 #endif // SWIG
108 
113  void set_const(T const_elem);
114 
121  {
122  return *this;
123  }
124 
125 #ifndef SWIG // SWIG should skip this part
126 
130  void set(SGVector<T> orig);
131 
133  virtual ~SGVector();
134 
136  inline int32_t size() const { return vlen; }
137 
139  inline T* data() const
140  {
141  assert_on_cpu();
142  return vector;
143  }
144 
146 
148  operator T*() { return vector; }
149 
151  void zero();
152 
157  void range_fill(T start=0);
158 
164  void random(T min_value, T max_value);
165 
172  index_t find_position_to_insert(T element);
173 
175  SGVector<T> clone() const;
176 
178  static T* clone_vector(const T* vec, int32_t len);
179 
181  static void fill_vector(T* vec, int32_t len, T value);
182 
184  static void range_fill_vector(T* vec, int32_t len, T start=0);
185 
187  static void random_vector(T* vec, int32_t len, T min_value, T max_value);
188 #endif // SWIG // SWIG should skip this part
189 
195  const T& get_element(index_t index)
196  {
197  return (*this)[index];
198  }
199 
205  void set_element(const T& el, index_t index)
206  {
207  (*this)[index]=el;
208  }
209 
210 #ifndef SWIG // SWIG should skip this part
211 
215  void resize_vector(int32_t n);
216 
222  inline const T& operator[](uint64_t index) const
223  {
224  assert_on_cpu();
225  return vector[index];
226  }
227 
233  inline const T& operator[](int64_t index) const
234  {
235  assert_on_cpu();
236  return vector[index];
237  }
238 
244  inline const T& operator[](uint32_t index) const
245  {
246  assert_on_cpu();
247  return vector[index];
248  }
249 
255  inline const T& operator[](int32_t index) const
256  {
257  assert_on_cpu();
258  return vector[index];
259  }
260 
266  inline T& operator[](uint64_t index)
267  {
268  assert_on_cpu();
269  return vector[index];
270  }
271 
277  inline T& operator[](int64_t index)
278  {
279  assert_on_cpu();
280  return vector[index];
281  }
282 
288  inline T& operator[](uint32_t index)
289  {
290  assert_on_cpu();
291  return vector[index];
292  }
293 
299  inline T& operator[](int32_t index)
300  {
301  assert_on_cpu();
302  return vector[index];
303  }
304 
309  void add(const SGVector<T> x);
310 
315  void add(const SGSparseVector<T>& x);
316 
321  void add(const T x);
322 
325 
328  {
329  add(x);
330  return *this;
331  }
332 
335  {
336  add(x);
337  return *this;
338  }
339 
345  bool equals(SGVector<T>& other);
346 
348  static T twonorm(const T* x, int32_t len);
349 
351  static float64_t onenorm(T* x, int32_t len);
352 
354  static T qsq(T* x, int32_t len, float64_t q);
355 
357  static T qnorm(T* x, int32_t len, float64_t q);
358 
360  static void vec1_plus_scalar_times_vec2(T* vec1,
361  const T scalar, const T* vec2, int32_t n);
362 
364  static inline void vector_multiply(
365  T* target, const T* v1, const T* v2,int32_t len)
366  {
367  for (int32_t i=0; i<len; i++)
368  target[i]=v1[i]*v2[i];
369  }
370 
371 
373  static inline void add(
374  T* target, T alpha, const T* v1, T beta, const T* v2,
375  int32_t len)
376  {
377  for (int32_t i=0; i<len; i++)
378  target[i]=alpha*v1[i]+beta*v2[i];
379  }
380 
382  static inline void add_scalar(T alpha, T* vec, int32_t len)
383  {
384  for (int32_t i=0; i<len; i++)
385  vec[i]+=alpha;
386  }
387 
389  static void scale_vector(T alpha, T* vec, int32_t len);
390 
392  static inline T sum(T* vec, int32_t len)
393  {
394  T result=0;
395  for (int32_t i=0; i<len; i++)
396  result+=vec[i];
397 
398  return result;
399  }
400 
402  static inline T sum(SGVector<T> vec)
403  {
404  return sum(vec.vector, vec.vlen);
405  }
406 
408  static inline T product(T* vec, int32_t len)
409  {
410  T result=1;
411  for (int32_t i=0; i<len; i++)
412  result*=vec[i];
413 
414  return result;
415  }
416 
418  inline T product()
419  {
420  return product(vector, vlen);
421  }
422 
424  static T sum_abs(T* vec, int32_t len);
425 
429  static int32_t unique(T* output, int32_t size);
430 
432  void display_size() const;
433 
435  void display_vector(const char* name="vector",
436  const char* prefix="") const;
437 
439  static void display_vector(
440  const T* vector, int32_t n, const char* name="vector",
441  const char* prefix="");
442 
444  static void display_vector(
445  const SGVector<T>, const char* name="vector",
446  const char* prefix="");
447 
451  SGVector<index_t> find(T elem);
452 
456  template <typename Predicate>
458  {
459  SGVector<index_t> idx(vlen);
460  index_t k=0;
461 
462  for (index_t i=0; i < vlen; ++i)
463  if (p(vector[i]))
464  idx[k++] = i;
465 
466  idx.vlen = k;
467  return idx;
468  }
469 
471  void scale(T alpha);
472 
477  void load(CFile* loader);
478 
483  void save(CFile* saver);
484 
487 
490 
501  static SGMatrix<T> convert_to_matrix(SGVector<T> vector, index_t nrows, index_t ncols, bool fortran_order);
502 
503 
516  static void convert_to_matrix(T*& matrix, index_t nrows, index_t ncols, const T* vector, int32_t vlen, bool fortran_order);
517 #endif // #ifndef SWIG // SWIG should skip this part
518  protected:
520  virtual void copy_data(const SGReferencedData &orig);
521 
523  virtual void init_data();
524 
526  virtual void free_data();
527 
528  private:
530  std::atomic<bool> m_on_gpu;
531 
535  void assert_on_cpu() const
536  {
537  if (on_gpu())
538  SG_SERROR("Direct memory access not possible when data is in GPU memory.\n");
539  }
540 
541  public:
543  T* vector;
547  std::shared_ptr<GPUMemoryBase<T>> gpu_ptr;
548 };
549 
550 #ifndef DOXYGEN_SHOULD_SKIP_THIS
552  const float64_t scalar, const float64_t* vec2, int32_t n);
553 
555  const float32_t scalar, const float32_t* vec2, int32_t n);
556 #endif // DOXYGEN_SHOULD_SKIP_THIS
557 }
558 #endif // __SGVECTOR_H__
static T twonorm(const T *x, int32_t len)
|| x ||_2
void range_fill(T start=0)
Definition: SGVector.cpp:208
static void fill_vector(T *vec, int32_t len, T value)
Definition: SGVector.cpp:264
SGVector< float64_t > get_real()
Definition: SGVector.cpp:926
Eigen::Matrix< T, 1,-1, 0x1, 1,-1 > EigenRowVectorXt
Definition: SGVector.h:48
T & operator[](uint64_t index)
Definition: SGVector.h:266
int32_t index_t
Definition: common.h:72
Eigen::Map< EigenVectorXt, 0, Eigen::Stride< 0, 0 > > EigenVectorXtMap
Definition: SGVector.h:50
void set(SGVector< T > orig)
Definition: SGVector.cpp:128
bool equals(SGVector< T > &other)
Definition: SGVector.cpp:380
SGVector< float64_t > get_imag()
Definition: SGVector.cpp:935
Definition: SGMatrix.h:24
virtual void init_data()
Definition: SGVector.cpp:362
void random(T min_value, T max_value)
Definition: SGVector.cpp:217
void display_size() const
Definition: SGVector.cpp:345
const T & operator[](int32_t index) const
Definition: SGVector.h:255
void scale(T alpha)
Scale vector inplace.
Definition: SGVector.cpp:884
shogun matrix
static SGMatrix< T > convert_to_matrix(SGVector< T > vector, index_t nrows, index_t ncols, bool fortran_order)
Definition: SGVector.cpp:945
static T qnorm(T *x, int32_t len, float64_t q)
|| x ||_q
Definition: SGVector.cpp:784
static void scale_vector(T alpha, T *vec, int32_t len)
Scale vector inplace.
Definition: SGVector.cpp:863
void display_vector(const char *name="vector", const char *prefix="") const
Definition: SGVector.cpp:396
static T * clone_vector(const T *vec, int32_t len)
Definition: SGVector.cpp:256
int32_t size() const
Definition: SGVector.h:136
index_t vlen
Definition: SGVector.h:545
SGVector< index_t > find_if(Predicate p)
Definition: SGVector.h:457
static T product(T *vec, int32_t len)
Return the product of the vectors elements.
Definition: SGVector.h:408
void set_element(const T &el, index_t index)
Definition: SGVector.h:205
static T sum_abs(T *vec, int32_t len)
return sum(abs(vec))
Definition: SGVector.cpp:799
shogun vector
const T & operator[](uint64_t index) const
Definition: SGVector.h:222
static void vector_multiply(T *target, const T *v1, const T *v2, int32_t len)
Compute vector multiplication.
Definition: SGVector.h:364
static void add_scalar(T alpha, T *vec, int32_t len)
Add scalar to vector inplace.
Definition: SGVector.h:382
shogun reference count managed data
double float64_t
Definition: common.h:60
static void range_fill_vector(T *vec, int32_t len, T start=0)
Definition: SGVector.cpp:271
SGVector< T > & operator=(const SGVector< T > &)
Definition: SGVector.cpp:115
A File access base class.
Definition: File.h:34
void save(CFile *saver)
Definition: SGVector.cpp:910
virtual ~SGVector()
Definition: SGVector.cpp:134
static T sum(T *vec, int32_t len)
Return sum(vec)
Definition: SGVector.h:392
bool on_gpu() const
Definition: SGVector.h:83
T & operator[](int32_t index)
Definition: SGVector.h:299
SGVector< T > operator+=(SGVector< T > x)
Definition: SGVector.h:327
T & operator[](uint32_t index)
Definition: SGVector.h:288
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:571
static T sum(SGVector< T > vec)
Return sum(vec)
Definition: SGVector.h:402
virtual void free_data()
Definition: SGVector.cpp:371
float float32_t
Definition: common.h:59
index_t find_position_to_insert(T element)
Definition: SGVector.cpp:226
const T & get_element(index_t index)
Definition: SGVector.h:195
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
void load(CFile *loader)
Definition: SGVector.cpp:889
SGVector< T > operator+(SGVector< T > x)
Definition: SGVector.cpp:298
Interface for GPU memory libraries.
Definition: SGMatrix.h:34
std::shared_ptr< GPUMemoryBase< T > > gpu_ptr
Definition: SGVector.h:547
#define SG_SERROR(...)
Definition: SGIO.h:178
T product()
Return product(vec)
Definition: SGVector.h:418
SGVector< T > clone() const
Definition: SGVector.cpp:247
template class SGSparseVector The assumtion is that the stored SGSparseVectorEntry* vector is orde...
T & operator[](int64_t index)
Definition: SGVector.h:277
virtual void copy_data(const SGReferencedData &orig)
Definition: SGVector.cpp:352
Eigen::Matrix< T,-1, 1, 0,-1, 1 > EigenVectorXt
Definition: SGVector.h:47
static float64_t onenorm(T *x, int32_t len)
|| x ||_1
Definition: SGVector.cpp:755
Linalg methods with Eigen3 backend.
static T qsq(T *x, int32_t len, float64_t q)
|| x ||_q^q
Definition: SGVector.cpp:766
void resize_vector(int32_t n)
Definition: SGVector.cpp:286
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:373
const T & operator[](uint32_t index) const
Definition: SGVector.h:244
const T & operator[](int64_t index) const
Definition: SGVector.h:233
Eigen::Map< EigenRowVectorXt, 0, Eigen::Stride< 0, 0 > > EigenRowVectorXtMap
Definition: SGVector.h:51
T * data() const
Definition: SGVector.h:139
SGVector< index_t > find(T elem)
Definition: SGVector.cpp:849
static int32_t unique(T *output, int32_t size)
Definition: SGVector.cpp:827
void set_const(T const_elem)
Definition: SGVector.cpp:184
void add(const SGVector< T > x)
Definition: SGVector.cpp:310
static void random_vector(T *vec, int32_t len, T min_value, T max_value)
Definition: SGVector.cpp:605

SHOGUN Machine Learning Toolbox - Documentation