SHOGUN  3.2.1
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 
SGVector.h
浏览该文件的文档.
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 #ifdef HAVE_EIGEN3
67 
68  SGVector(EigenVectorXt& vec);
69 
71  SGVector(EigenRowVectorXt& vec);
72 
74  operator EigenVectorXtMap() const;
75 
77  operator EigenRowVectorXtMap() const;
78 #endif
79 #endif
80 
85  void set_const(T const_elem);
86 
93  {
94  return *this;
95  }
96 
97 #ifndef SWIG // SWIG should skip this part
98 
102  void set(SGVector<T> orig);
103 
105  virtual ~SGVector();
106 
108  inline int32_t size() const { return vlen; }
109 
111  operator T*() { return vector; };
112 
114  void zero();
115 
120  void range_fill(T start=0);
121 
127  void random(T min_value, T max_value);
128 
135  index_t find_position_to_insert(T element);
136 
140  void qsort();
141 
149 
154  bool is_sorted() const;
155 
157  SGVector<T> clone() const;
158 
160  static T* clone_vector(const T* vec, int32_t len);
161 
163  static void fill_vector(T* vec, int32_t len, T value);
164 
166  static void range_fill_vector(T* vec, int32_t len, T start=0);
167 
169  static void random_vector(T* vec, int32_t len, T min_value, T max_value);
170 
176  const T& get_element(index_t index);
177 
184  void set_element(const T& p_element, index_t index);
185 
191  void resize_vector(int32_t n);
192 
198  inline const T& operator[](uint64_t index) const
199  {
200  return vector[index];
201  }
202 
208  inline const T& operator[](int64_t index) const
209  {
210  return vector[index];
211  }
212 
218  inline const T& operator[](uint32_t index) const
219  {
220  return vector[index];
221  }
222 
228  inline const T& operator[](int32_t index) const
229  {
230  return vector[index];
231  }
232 
238  inline T& operator[](uint64_t index)
239  {
240  return vector[index];
241  }
242 
248  inline T& operator[](int64_t index)
249  {
250  return vector[index];
251  }
252 
258  inline T& operator[](uint32_t index)
259  {
260  return vector[index];
261  }
262 
268  inline T& operator[](int32_t index)
269  {
270  return vector[index];
271  }
272 
277  void add(const SGVector<T> x);
278 
283  void add(const SGSparseVector<T>& x);
284 
289  void add(const T x);
290 
293 
296  {
297  add(x);
298  return *this;
299  }
300 
303  {
304  add(x);
305  return *this;
306  }
307 
313  bool equals(SGVector<T>& other);
314 
316  static T twonorm(const T* x, int32_t len);
317 
319  static float64_t onenorm(T* x, int32_t len);
320 
322  static T qsq(T* x, int32_t len, float64_t q);
323 
325  static T qnorm(T* x, int32_t len, float64_t q);
326 
328  static void vec1_plus_scalar_times_vec2(T* vec1,
329  const T scalar, const T* vec2, int32_t n);
330 
332  static inline float64_t dot(const bool* v1, const bool* v2, int32_t n)
333  {
334  float64_t r=0;
335  for (int32_t i=0; i<n; i++)
336  r+=((v1[i]) ? 1 : 0) * ((v2[i]) ? 1 : 0);
337  return r;
338  }
339 
341  static inline floatmax_t dot(const floatmax_t* v1, const floatmax_t* v2, int32_t n)
342  {
343  floatmax_t r=0;
344  for (int32_t i=0; i<n; i++)
345  r+=v1[i]*v2[i];
346  return r;
347  }
348 
349 
351  static float64_t dot(const float64_t* v1, const float64_t* v2, int32_t n);
352 
354  static float32_t dot(const float32_t* v1, const float32_t* v2, int32_t n);
355 
357  static inline float64_t dot(
358  const uint64_t* v1, const uint64_t* v2, int32_t n)
359  {
360  float64_t r=0;
361  for (int32_t i=0; i<n; i++)
362  r+=((float64_t) v1[i])*v2[i];
363 
364  return r;
365  }
367  static inline float64_t dot(
368  const int64_t* v1, const int64_t* v2, int32_t n)
369  {
370  float64_t r=0;
371  for (int32_t i=0; i<n; i++)
372  r+=((float64_t) v1[i])*v2[i];
373 
374  return r;
375  }
376 
378  static inline float64_t dot(
379  const int32_t* v1, const int32_t* v2, int32_t n)
380  {
381  float64_t r=0;
382  for (int32_t i=0; i<n; i++)
383  r+=((float64_t) v1[i])*v2[i];
384 
385  return r;
386  }
387 
389  static inline float64_t dot(
390  const uint32_t* v1, const uint32_t* v2, int32_t n)
391  {
392  float64_t r=0;
393  for (int32_t i=0; i<n; i++)
394  r+=((float64_t) v1[i])*v2[i];
395 
396  return r;
397  }
398 
400  static inline float64_t dot(
401  const uint16_t* v1, const uint16_t* v2, int32_t n)
402  {
403  float64_t r=0;
404  for (int32_t i=0; i<n; i++)
405  r+=((float64_t) v1[i])*v2[i];
406 
407  return r;
408  }
409 
411  static inline float64_t dot(
412  const int16_t* v1, const int16_t* v2, int32_t n)
413  {
414  float64_t r=0;
415  for (int32_t i=0; i<n; i++)
416  r+=((float64_t) v1[i])*v2[i];
417 
418  return r;
419  }
420 
422  static inline float64_t dot(
423  const char* v1, const char* v2, int32_t n)
424  {
425  float64_t r=0;
426  for (int32_t i=0; i<n; i++)
427  r+=((float64_t) v1[i])*v2[i];
428 
429  return r;
430  }
431 
433  static inline float64_t dot(
434  const uint8_t* v1, const uint8_t* v2, int32_t n)
435  {
436  float64_t r=0;
437  for (int32_t i=0; i<n; i++)
438  r+=((float64_t) v1[i])*v2[i];
439 
440  return r;
441  }
442 
444  static inline float64_t dot(
445  const int8_t* v1, const int8_t* v2, int32_t n)
446  {
447  float64_t r=0;
448  for (int32_t i=0; i<n; i++)
449  r+=((float64_t) v1[i])*v2[i];
450 
451  return r;
452  }
453 
455  static inline float64_t dot(
456  const float64_t* v1, const char* v2, int32_t n)
457  {
458  float64_t r=0;
459  for (int32_t i=0; i<n; i++)
460  r+=((float64_t) v1[i])*v2[i];
461 
462  return r;
463  }
464 
466  static inline void vector_multiply(
467  T* target, const T* v1, const T* v2,int32_t len)
468  {
469  for (int32_t i=0; i<len; i++)
470  target[i]=v1[i]*v2[i];
471  }
472 
473 
475  static inline void add(
476  T* target, T alpha, const T* v1, T beta, const T* v2,
477  int32_t len)
478  {
479  for (int32_t i=0; i<len; i++)
480  target[i]=alpha*v1[i]+beta*v2[i];
481  }
482 
484  static inline void add_scalar(T alpha, T* vec, int32_t len)
485  {
486  for (int32_t i=0; i<len; i++)
487  vec[i]+=alpha;
488  }
489 
491  static void scale_vector(T alpha, T* vec, int32_t len);
492 
494  static inline T sum(T* vec, int32_t len)
495  {
496  T result=0;
497  for (int32_t i=0; i<len; i++)
498  result+=vec[i];
499 
500  return result;
501  }
502 
504  static inline T sum(SGVector<T> vec)
505  {
506  return sum(vec.vector, vec.vlen);
507  }
508 
510  static inline T product(T* vec, int32_t len)
511  {
512  T result=1;
513  for (int32_t i=0; i<len; i++)
514  result*=vec[i];
515 
516  return result;
517  }
518 
520  inline T product()
521  {
522  return product(vector, vlen);
523  }
524 
526  static T sum_abs(T* vec, int32_t len);
527 
529  static bool fequal(T x, T y, float64_t precision=1e-6);
530 
534  static int32_t unique(T* output, int32_t size);
535 
537  void display_size() const;
538 
540  void display_vector(const char* name="vector",
541  const char* prefix="") const;
542 
544  static void display_vector(
545  const T* vector, int32_t n, const char* name="vector",
546  const char* prefix="");
547 
549  static void display_vector(
550  const SGVector<T>, const char* name="vector",
551  const char* prefix="");
552 
556  SGVector<index_t> find(T elem);
557 
561  template <typename Predicate>
563  {
564  SGVector<index_t> idx(vlen);
565  index_t k=0;
566 
567  for (index_t i=0; i < vlen; ++i)
568  if (p(vector[i]))
569  idx[k++] = i;
570 
571  idx.vlen = k;
572  return idx;
573  }
574 
576  void scale(T alpha);
577 
582  void load(CFile* loader);
583 
588  void save(CFile* saver);
589 
592 
595 
606  static SGMatrix<T> convert_to_matrix(SGVector<T> vector, index_t nrows, index_t ncols, bool fortran_order);
607 
608 
621  static void convert_to_matrix(T*& matrix, index_t nrows, index_t ncols, const T* vector, int32_t vlen, bool fortran_order);
622 #endif // #ifndef SWIG // SWIG should skip this part
623  protected:
625  virtual void copy_data(const SGReferencedData &orig);
626 
628  virtual void init_data();
629 
631  virtual void free_data();
632 
633  public:
635  T* vector;
638 };
639 
640 #ifndef DOXYGEN_SHOULD_SKIP_THIS
642  const float64_t scalar, const float64_t* vec2, int32_t n);
643 
645  const float32_t scalar, const float32_t* vec2, int32_t n);
646 #endif // DOXYGEN_SHOULD_SKIP_THIS
647 }
648 #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:292
SGVector< float64_t > get_real()
Definition: SGVector.cpp:996
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:341
T & operator[](uint64_t index)
Definition: SGVector.h:238
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:411
SGVector< float64_t > get_imag()
Definition: SGVector.cpp:1004
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:411
Definition: SGMatrix.h:20
virtual void init_data()
Definition: SGVector.cpp:396
void random(T min_value, T max_value)
Definition: SGVector.cpp:181
void display_size() const
Definition: SGVector.cpp:383
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:378
const T & operator[](int32_t index) const
Definition: SGVector.h:228
void scale(T alpha)
Scale vector inplace.
Definition: SGVector.cpp:956
shogun matrix
Definition: Parameter.h:26
const T & get_element(index_t index)
Definition: SGVector.cpp:314
static SGMatrix< T > convert_to_matrix(SGVector< T > vector, index_t nrows, index_t ncols, bool fortran_order)
Definition: SGVector.cpp:1013
static T qnorm(T *x, int32_t len, float64_t q)
|| x ||_q
Definition: SGVector.cpp:850
static void scale_vector(T alpha, T *vec, int32_t len)
Scale vector inplace.
Definition: SGVector.cpp:935
static bool fequal(T x, T y, float64_t precision=1e-6)
return sum(abs(vec))
Definition: SGVector.cpp:894
void display_vector(const char *name="vector", const char *prefix="") const
Definition: SGVector.cpp:426
static T * clone_vector(const T *vec, int32_t len)
Definition: SGVector.cpp:284
SGVector< index_t > argsort()
Definition: SGVector.cpp:215
int32_t size() const
Definition: SGVector.h:108
void set_element(const T &p_element, index_t index)
Definition: SGVector.cpp:321
index_t vlen
Definition: SGVector.h:637
SGVector< index_t > find_if(Predicate p)
Definition: SGVector.h:562
static T product(T *vec, int32_t len)
Return the product of the vectors elements.
Definition: SGVector.h:510
static T sum_abs(T *vec, int32_t len)
return sum(abs(vec))
Definition: SGVector.cpp:865
shogun vector
Definition: Parameter.h:28
const T & operator[](uint64_t index) const
Definition: SGVector.h:198
static void vector_multiply(T *target, const T *v1, const T *v2, int32_t len)
Compute vector multiplication.
Definition: SGVector.h:466
static void add_scalar(T alpha, T *vec, int32_t len)
Add scalar to vector inplace.
Definition: SGVector.h:484
shogun reference count managed data
double float64_t
Definition: common.h:50
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:433
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:389
long double floatmax_t
Definition: common.h:51
static void range_fill_vector(T *vec, int32_t len, T start=0)
Definition: SGVector.cpp:299
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:367
A File access base class.
Definition: File.h:34
void save(CFile *saver)
Definition: SGVector.cpp:981
virtual ~SGVector()
Definition: SGVector.cpp:104
static T sum(T *vec, int32_t len)
Return sum(vec)
Definition: SGVector.h:494
T & operator[](int32_t index)
Definition: SGVector.h:268
SGVector< T > operator+=(SGVector< T > x)
Definition: SGVector.h:295
T & operator[](uint32_t index)
Definition: SGVector.h:258
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:601
static T sum(SGVector< T > vec)
Return sum(vec)
Definition: SGVector.h:504
virtual void free_data()
Definition: SGVector.cpp:403
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:332
float float32_t
Definition: common.h:49
index_t find_position_to_insert(T element)
Definition: SGVector.cpp:258
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:444
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:400
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
void load(CFile *loader)
Definition: SGVector.cpp:961
SGVector< T > operator+(SGVector< T > x)
Definition: SGVector.cpp:339
bool is_sorted() const
Definition: SGVector.cpp:236
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:357
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:422
T product()
Return product(vec)
Definition: SGVector.h:520
static float64_t dot(const float64_t *v1, const char *v2, int32_t n)
Compute dot product between v1 and v2.
Definition: SGVector.h:455
SGVector< T > clone() const
Definition: SGVector.cpp:278
template class SGSparseVector The assumtion is that the stored SGSparseVectorEntry* vector is orde...
Definition: Parameter.h:29
T & operator[](int64_t index)
Definition: SGVector.h:248
virtual void copy_data(const SGReferencedData &orig)
Definition: SGVector.cpp:389
static float64_t onenorm(T *x, int32_t len)
|| x ||_1
Definition: SGVector.cpp:821
static T qsq(T *x, int32_t len, float64_t q)
|| x ||_q^q
Definition: SGVector.cpp:832
void resize_vector(int32_t n)
Definition: SGVector.cpp:328
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:475
const T & operator[](uint32_t index) const
Definition: SGVector.h:218
const T & operator[](int64_t index) const
Definition: SGVector.h:208
SGVector< index_t > find(T elem)
Definition: SGVector.cpp:922
static int32_t unique(T *output, int32_t size)
Definition: SGVector.cpp:900
void set_const(T const_elem)
Definition: SGVector.cpp:152
void add(const SGVector< T > x)
Definition: SGVector.cpp:350
static void random_vector(T *vec, int32_t len, T min_value, T max_value)
Definition: SGVector.cpp:671

SHOGUN 机器学习工具包 - 项目文档