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/common.h>
18 #include <shogun/lib/DataType.h>
20 
21 
22 namespace shogun
23 {
24  template <class T> class SGSparseVector;
25  template <class T> class SGMatrix;
26  class CFile;
27 
29 template<class T> class SGVector : public SGReferencedData
30 {
31  public:
33  SGVector();
34 
36  SGVector(T* v, index_t len, bool ref_counting=true);
37 
39  SGVector(index_t len, bool ref_counting=true);
40 
42  SGVector(const SGVector &orig);
43 
48  void set(SGVector<T> orig);
49 
51  virtual ~SGVector();
52 
54  inline int32_t size() const { return vlen; }
55 
57  operator T*() { return vector; };
58 
60  void zero();
61 
66  void set_const(T const_elem);
67 
72  void range_fill(T start=0);
73 
79  void random(T min_value, T max_value);
80 
82  void randperm();
83 
85  static SGVector<T> randperm_vec(int32_t n);
86 
93  static T* randperm(int32_t n);
94 
102  static SGVector<float64_t> linspace_vec(T start, T end, int32_t n);
103 
111  static float64_t* linspace(T start, T end, int32_t n);
112 
119  index_t find_position_to_insert(T element);
120 
124  void qsort();
125 
133 
138  bool is_sorted() const;
139 
141  SGVector<T> clone() const;
142 
144  static T* clone_vector(const T* vec, int32_t len);
145 
147  static void fill_vector(T* vec, int32_t len, T value);
148 
150  static void range_fill_vector(T* vec, int32_t len, T start=0);
151 
153  static void random_vector(T* vec, int32_t len, T min_value, T max_value);
154 
156  static void randperm(T* perm, int32_t n);
157 
159  static void permute(T* vec, int32_t n);
160 
162  static void permute(T* vec, int32_t n, CRandom * rand);
163 
170  {
171  return *this;
172  }
173 
179  const T& get_element(index_t index);
180 
187  void set_element(const T& p_element, index_t index);
188 
194  void resize_vector(int32_t n);
195 
201  inline const T& operator[](uint64_t index) const
202  {
203  return vector[index];
204  }
205 
211  inline const T& operator[](int64_t index) const
212  {
213  return vector[index];
214  }
215 
221  inline const T& operator[](uint32_t index) const
222  {
223  return vector[index];
224  }
225 
231  inline const T& operator[](int32_t index) const
232  {
233  return vector[index];
234  }
235 
241  inline T& operator[](uint64_t index)
242  {
243  return vector[index];
244  }
245 
251  inline T& operator[](int64_t index)
252  {
253  return vector[index];
254  }
255 
261  inline T& operator[](uint32_t index)
262  {
263  return vector[index];
264  }
265 
271  inline T& operator[](int32_t index)
272  {
273  return vector[index];
274  }
275 
280  void add(const SGVector<T> x);
281 
286  void add(const SGSparseVector<T>& x);
287 
292  void add(const T x);
293 
296 
299  {
300  add(x);
301  return *this;
302  }
303 
306  {
307  add(x);
308  return *this;
309  }
310 
316  bool equals(SGVector<T>& other);
317 
319  static void permute_vector(SGVector<T> vec);
320 
322  void permute();
323 
325  void permute(CRandom * rand);
326 
328  static T twonorm(const T* x, int32_t len);
329 
331  static float64_t onenorm(T* x, int32_t len);
332 
334  static T qsq(T* x, int32_t len, float64_t q);
335 
337  static T qnorm(T* x, int32_t len, float64_t q);
338 
340  static void vec1_plus_scalar_times_vec2(T* vec1,
341  const T scalar, const T* vec2, int32_t n);
342 
344  static inline float64_t dot(const bool* v1, const bool* v2, int32_t n)
345  {
346  float64_t r=0;
347  for (int32_t i=0; i<n; i++)
348  r+=((v1[i]) ? 1 : 0) * ((v2[i]) ? 1 : 0);
349  return r;
350  }
351 
353  static inline floatmax_t dot(const floatmax_t* v1, const floatmax_t* v2, int32_t n)
354  {
355  floatmax_t r=0;
356  for (int32_t i=0; i<n; i++)
357  r+=v1[i]*v2[i];
358  return r;
359  }
360 
361 
363  static float64_t dot(const float64_t* v1, const float64_t* v2, int32_t n);
364 
366  static float32_t dot(const float32_t* v1, const float32_t* v2, int32_t n);
367 
369  static inline float64_t dot(
370  const uint64_t* v1, const uint64_t* v2, int32_t n)
371  {
372  float64_t r=0;
373  for (int32_t i=0; i<n; i++)
374  r+=((float64_t) v1[i])*v2[i];
375 
376  return r;
377  }
379  static inline float64_t dot(
380  const int64_t* v1, const int64_t* v2, int32_t n)
381  {
382  float64_t r=0;
383  for (int32_t i=0; i<n; i++)
384  r+=((float64_t) v1[i])*v2[i];
385 
386  return r;
387  }
388 
390  static inline float64_t dot(
391  const int32_t* v1, const int32_t* v2, int32_t n)
392  {
393  float64_t r=0;
394  for (int32_t i=0; i<n; i++)
395  r+=((float64_t) v1[i])*v2[i];
396 
397  return r;
398  }
399 
401  static inline float64_t dot(
402  const uint32_t* v1, const uint32_t* v2, int32_t n)
403  {
404  float64_t r=0;
405  for (int32_t i=0; i<n; i++)
406  r+=((float64_t) v1[i])*v2[i];
407 
408  return r;
409  }
410 
412  static inline float64_t dot(
413  const uint16_t* v1, const uint16_t* v2, int32_t n)
414  {
415  float64_t r=0;
416  for (int32_t i=0; i<n; i++)
417  r+=((float64_t) v1[i])*v2[i];
418 
419  return r;
420  }
421 
423  static inline float64_t dot(
424  const int16_t* v1, const int16_t* v2, int32_t n)
425  {
426  float64_t r=0;
427  for (int32_t i=0; i<n; i++)
428  r+=((float64_t) v1[i])*v2[i];
429 
430  return r;
431  }
432 
434  static inline float64_t dot(
435  const char* v1, const char* v2, int32_t n)
436  {
437  float64_t r=0;
438  for (int32_t i=0; i<n; i++)
439  r+=((float64_t) v1[i])*v2[i];
440 
441  return r;
442  }
443 
445  static inline float64_t dot(
446  const uint8_t* v1, const uint8_t* v2, int32_t n)
447  {
448  float64_t r=0;
449  for (int32_t i=0; i<n; i++)
450  r+=((float64_t) v1[i])*v2[i];
451 
452  return r;
453  }
454 
456  static inline float64_t dot(
457  const int8_t* v1, const int8_t* v2, int32_t n)
458  {
459  float64_t r=0;
460  for (int32_t i=0; i<n; i++)
461  r+=((float64_t) v1[i])*v2[i];
462 
463  return r;
464  }
465 
467  static inline float64_t dot(
468  const float64_t* v1, const char* v2, int32_t n)
469  {
470  float64_t r=0;
471  for (int32_t i=0; i<n; i++)
472  r+=((float64_t) v1[i])*v2[i];
473 
474  return r;
475  }
476 
478  static inline void vector_multiply(
479  T* target, const T* v1, const T* v2,int32_t len)
480  {
481  for (int32_t i=0; i<len; i++)
482  target[i]=v1[i]*v2[i];
483  }
484 
485 
487  static inline void add(
488  T* target, T alpha, const T* v1, T beta, const T* v2,
489  int32_t len)
490  {
491  for (int32_t i=0; i<len; i++)
492  target[i]=alpha*v1[i]+beta*v2[i];
493  }
494 
496  static inline void add_scalar(T alpha, T* vec, int32_t len)
497  {
498  for (int32_t i=0; i<len; i++)
499  vec[i]+=alpha;
500  }
501 
503  static void scale_vector(T alpha, T* vec, int32_t len);
504 
506  static inline T sum(T* vec, int32_t len)
507  {
508  T result=0;
509  for (int32_t i=0; i<len; i++)
510  result+=vec[i];
511 
512  return result;
513  }
514 
516  static inline T sum(SGVector<T> vec)
517  {
518  return sum(vec.vector, vec.vlen);
519  }
520 
522  static inline T product(T* vec, int32_t len)
523  {
524  T result=1;
525  for (int32_t i=0; i<len; i++)
526  result*=vec[i];
527 
528  return result;
529  }
530 
532  inline T product()
533  {
534  return product(vector, vlen);
535  }
536 
538  static T min(T* vec, int32_t len);
539 
541  static T max_abs(T* vec, int32_t len);
542 
544  static T max(T* vec, int32_t len);
545 
547  static int32_t arg_max(T * vec, int32_t inc, int32_t len, T * maxv_ptr = NULL);
548 
550  static int32_t arg_max_abs(T * vec, int32_t inc, int32_t len, T * maxv_ptr = NULL);
551 
553  static int32_t arg_min(T * vec, int32_t inc, int32_t len, T * minv_ptr = NULL);
554 
556  static T sum_abs(T* vec, int32_t len);
557 
559  static bool fequal(T x, T y, float64_t precision=1e-6);
560 
564  static int32_t unique(T* output, int32_t size);
565 
567  void display_size() const;
568 
570  void display_vector(const char* name="vector",
571  const char* prefix="") const;
572 
574  static void display_vector(
575  const T* vector, int32_t n, const char* name="vector",
576  const char* prefix="");
577 
579  static void display_vector(
580  const SGVector<T>, const char* name="vector",
581  const char* prefix="");
582 
586  SGVector<index_t> find(T elem);
587 
591  template <typename Predicate>
593  {
594  SGVector<index_t> idx(vlen);
595  index_t k=0;
596 
597  for (index_t i=0; i < vlen; ++i)
598  if (p(vector[i]))
599  idx[k++] = i;
600 
601  idx.vlen = k;
602  return idx;
603  }
604 
606  void scale(T alpha);
607 
612  float64_t mean() const;
613 
618  void load(CFile* loader);
619 
624  void save(CFile* saver);
625 
627  void abs();
629  void acos();
631  void asin();
633  void atan();
635  void atan2(T x);
637  void cos();
639  void cosh();
641  void exp();
643  void log();
645  void log10();
647  void pow(T q);
649  void sin();
651  void sinh();
653  void sqrt();
655  void tan();
657  void tanh();
658 
661 
664 
675  static SGMatrix<T> convert_to_matrix(SGVector<T> vector, index_t nrows, index_t ncols, bool fortran_order);
676 
677 
690  static void convert_to_matrix(T*& matrix, index_t nrows, index_t ncols, const T* vector, int32_t vlen, bool fortran_order);
691 
692  protected:
694  virtual void copy_data(const SGReferencedData &orig);
695 
697  virtual void init_data();
698 
700  virtual void free_data();
701 
702  public:
704  T* vector;
707 };
708 
709 #ifndef DOXYGEN_SHOULD_SKIP_THIS
711  const float64_t scalar, const float64_t* vec2, int32_t n);
712 
714  const float32_t scalar, const float32_t* vec2, int32_t n);
715 #endif // DOXYGEN_SHOULD_SKIP_THIS
716 }
717 #endif // __SGVECTOR_H__

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