SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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/DataType.h>
19 
20 
21 namespace shogun
22 {
23  template <class T> class SGSparseVector;
24  template <class T> class SGMatrix;
25  class CFile;
26 
28 template<class T> class SGVector : public SGReferencedData
29 {
30  public:
32  SGVector();
33 
35  SGVector(T* v, index_t len, bool ref_counting=true);
36 
38  SGVector(index_t len, bool ref_counting=true);
39 
41  SGVector(const SGVector &orig);
42 
47  void set(SGVector<T> orig);
48 
50  virtual ~SGVector();
51 
53  inline int32_t size() const { return vlen; }
54 
56  operator T*() { return vector; };
57 
59  void zero();
60 
65  void set_const(T const_elem);
66 
71  void range_fill(T start=0);
72 
78  void random(T min_value, T max_value);
79 
81  void randperm();
82 
84  static SGVector<T> randperm_vec(int32_t n);
85 
92  static T* randperm(int32_t n);
93 
101  static SGVector<float64_t> linspace_vec(T start, T end, int32_t n);
102 
110  static float64_t* linspace(T start, T end, int32_t n);
111 
118  index_t find_position_to_insert(T element);
119 
123  void qsort();
124 
132 
137  bool is_sorted() const;
138 
140  SGVector<T> clone() const;
141 
143  static T* clone_vector(const T* vec, int32_t len);
144 
146  static void fill_vector(T* vec, int32_t len, T value);
147 
149  static void range_fill_vector(T* vec, int32_t len, T start=0);
150 
152  static void random_vector(T* vec, int32_t len, T min_value, T max_value);
153 
155  static void randperm(T* perm, int32_t n);
156 
158  static void permute(T* vec, int32_t n);
159 
161  static void permute(T* vec, int32_t n, CRandom * rand);
162 
169  {
170  return *this;
171  }
172 
178  const T& get_element(index_t index);
179 
186  void set_element(const T& p_element, index_t index);
187 
193  void resize_vector(int32_t n);
194 
200  inline const T& operator[](index_t index) const
201  {
202  return vector[index];
203  }
204 
210  inline T& operator[](index_t index)
211  {
212  return vector[index];
213  }
214 
219  void add(const SGVector<T> x);
220 
225  void add(const SGSparseVector<T>& x);
226 
231  void add(const T x);
232 
235 
238  {
239  add(x);
240  return *this;
241  }
242 
245  {
246  add(x);
247  return *this;
248  }
249 
255  bool equals(SGVector<T>& other);
256 
258  static void permute_vector(SGVector<T> vec);
259 
261  void permute();
262 
264  void permute(CRandom * rand);
265 
267  static T twonorm(const T* x, int32_t len);
268 
270  static float64_t onenorm(T* x, int32_t len);
271 
273  static T qsq(T* x, int32_t len, float64_t q);
274 
276  static T qnorm(T* x, int32_t len, float64_t q);
277 
279  static void vec1_plus_scalar_times_vec2(T* vec1,
280  const T scalar, const T* vec2, int32_t n);
281 
283  static inline float64_t dot(const bool* v1, const bool* v2, int32_t n)
284  {
285  float64_t r=0;
286  for (int32_t i=0; i<n; i++)
287  r+=((v1[i]) ? 1 : 0) * ((v2[i]) ? 1 : 0);
288  return r;
289  }
290 
292  static inline floatmax_t dot(const floatmax_t* v1, const floatmax_t* v2, int32_t n)
293  {
294  floatmax_t r=0;
295  for (int32_t i=0; i<n; i++)
296  r+=v1[i]*v2[i];
297  return r;
298  }
299 
300 
302  static float64_t dot(const float64_t* v1, const float64_t* v2, int32_t n);
303 
305  static float32_t dot(const float32_t* v1, const float32_t* v2, int32_t n);
306 
308  static inline float64_t dot(
309  const uint64_t* v1, const uint64_t* v2, int32_t n)
310  {
311  float64_t r=0;
312  for (int32_t i=0; i<n; i++)
313  r+=((float64_t) v1[i])*v2[i];
314 
315  return r;
316  }
318  static inline float64_t dot(
319  const int64_t* v1, const int64_t* v2, int32_t n)
320  {
321  float64_t r=0;
322  for (int32_t i=0; i<n; i++)
323  r+=((float64_t) v1[i])*v2[i];
324 
325  return r;
326  }
327 
329  static inline float64_t dot(
330  const int32_t* v1, const int32_t* v2, int32_t n)
331  {
332  float64_t r=0;
333  for (int32_t i=0; i<n; i++)
334  r+=((float64_t) v1[i])*v2[i];
335 
336  return r;
337  }
338 
340  static inline float64_t dot(
341  const uint32_t* v1, const uint32_t* v2, int32_t n)
342  {
343  float64_t r=0;
344  for (int32_t i=0; i<n; i++)
345  r+=((float64_t) v1[i])*v2[i];
346 
347  return r;
348  }
349 
351  static inline float64_t dot(
352  const uint16_t* v1, const uint16_t* v2, int32_t n)
353  {
354  float64_t r=0;
355  for (int32_t i=0; i<n; i++)
356  r+=((float64_t) v1[i])*v2[i];
357 
358  return r;
359  }
360 
362  static inline float64_t dot(
363  const int16_t* v1, const int16_t* v2, int32_t n)
364  {
365  float64_t r=0;
366  for (int32_t i=0; i<n; i++)
367  r+=((float64_t) v1[i])*v2[i];
368 
369  return r;
370  }
371 
373  static inline float64_t dot(
374  const char* v1, const char* v2, int32_t n)
375  {
376  float64_t r=0;
377  for (int32_t i=0; i<n; i++)
378  r+=((float64_t) v1[i])*v2[i];
379 
380  return r;
381  }
382 
384  static inline float64_t dot(
385  const uint8_t* v1, const uint8_t* v2, int32_t n)
386  {
387  float64_t r=0;
388  for (int32_t i=0; i<n; i++)
389  r+=((float64_t) v1[i])*v2[i];
390 
391  return r;
392  }
393 
395  static inline float64_t dot(
396  const int8_t* v1, const int8_t* v2, int32_t n)
397  {
398  float64_t r=0;
399  for (int32_t i=0; i<n; i++)
400  r+=((float64_t) v1[i])*v2[i];
401 
402  return r;
403  }
404 
406  static inline float64_t dot(
407  const float64_t* v1, const char* v2, int32_t n)
408  {
409  float64_t r=0;
410  for (int32_t i=0; i<n; i++)
411  r+=((float64_t) v1[i])*v2[i];
412 
413  return r;
414  }
415 
417  static inline void vector_multiply(
418  T* target, const T* v1, const T* v2,int32_t len)
419  {
420  for (int32_t i=0; i<len; i++)
421  target[i]=v1[i]*v2[i];
422  }
423 
424 
426  static inline void add(
427  T* target, T alpha, const T* v1, T beta, const T* v2,
428  int32_t len)
429  {
430  for (int32_t i=0; i<len; i++)
431  target[i]=alpha*v1[i]+beta*v2[i];
432  }
433 
435  static inline void add_scalar(T alpha, T* vec, int32_t len)
436  {
437  for (int32_t i=0; i<len; i++)
438  vec[i]+=alpha;
439  }
440 
442  static void scale_vector(T alpha, T* vec, int32_t len);
443 
445  static inline T sum(T* vec, int32_t len)
446  {
447  T result=0;
448  for (int32_t i=0; i<len; i++)
449  result+=vec[i];
450 
451  return result;
452  }
453 
455  static inline T sum(SGVector<T> vec)
456  {
457  return sum(vec.vector, vec.vlen);
458  }
459 
461  static inline T product(T* vec, int32_t len)
462  {
463  T result=1;
464  for (int32_t i=0; i<len; i++)
465  result*=vec[i];
466 
467  return result;
468  }
469 
471  inline T product()
472  {
473  return product(vector, vlen);
474  }
475 
477  static T min(T* vec, int32_t len);
478 
480  static T max_abs(T* vec, int32_t len);
481 
483  static T max(T* vec, int32_t len);
484 
486  static int32_t arg_max(T * vec, int32_t inc, int32_t len, T * maxv_ptr = NULL);
487 
489  static int32_t arg_max_abs(T * vec, int32_t inc, int32_t len, T * maxv_ptr = NULL);
490 
492  static int32_t arg_min(T * vec, int32_t inc, int32_t len, T * minv_ptr = NULL);
493 
495  static T sum_abs(T* vec, int32_t len);
496 
498  static bool fequal(T x, T y, float64_t precision=1e-6);
499 
503  static int32_t unique(T* output, int32_t size);
504 
506  void display_size() const;
507 
509  void display_vector(const char* name="vector",
510  const char* prefix="") const;
511 
513  static void display_vector(
514  const T* vector, int32_t n, const char* name="vector",
515  const char* prefix="");
516 
518  static void display_vector(
519  const SGVector<T>, const char* name="vector",
520  const char* prefix="");
521 
525  SGVector<index_t> find(T elem);
526 
530  template <typename Predicate>
532  {
533  SGVector<index_t> idx(vlen);
534  index_t k=0;
535 
536  for (index_t i=0; i < vlen; ++i)
537  if (p(vector[i]))
538  idx[k++] = i;
539 
540  idx.vlen = k;
541  return idx;
542  }
543 
545  void scale(T alpha);
546 
551  float64_t mean() const;
552 
557  void load(CFile* loader);
558 
563  void save(CFile* saver);
564 
566  void abs();
568  void acos();
570  void asin();
572  void atan();
574  void atan2(T x);
576  void cos();
578  void cosh();
580  void exp();
582  void log();
584  void log10();
586  void pow(T q);
588  void sin();
590  void sinh();
592  void sqrt();
594  void tan();
596  void tanh();
597 
600 
603 
614  static SGMatrix<T> convert_to_matrix(SGVector<T> vector, index_t nrows, index_t ncols, bool fortran_order);
615 
616 
629  static void convert_to_matrix(T*& matrix, index_t nrows, index_t ncols, const T* vector, int32_t vlen, bool fortran_order);
630 
631  protected:
633  virtual void copy_data(const SGReferencedData &orig);
634 
636  virtual void init_data();
637 
639  virtual void free_data();
640 
641  public:
643  T* vector;
646 };
647 
648 #ifndef DOXYGEN_SHOULD_SKIP_THIS
650  const float64_t scalar, const float64_t* vec2, int32_t n);
651 
653  const float32_t scalar, const float32_t* vec2, int32_t n);
654 #endif // DOXYGEN_SHOULD_SKIP_THIS
655 }
656 #endif // __SGVECTOR_H__

SHOGUN Machine Learning Toolbox - Documentation