SHOGUN  v2.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) 2012 Fernando José Iglesias García
8  * Written (W) 2010,2012 Soeren Sonnenburg
9  * Copyright (C) 2010 Berlin Institute of Technology
10  * Copyright (C) 2012 Soeren Sonnenburg
11  */
12 #ifndef __SGVECTOR_H__
13 #define __SGVECTOR_H__
14 
15 #include <algorithm>
16 
17 #include <shogun/io/SGIO.h>
18 #include <shogun/lib/DataType.h>
20 
21 namespace shogun
22 {
24 template<class T> class SGVector : public SGReferencedData
25 {
26  public:
28  SGVector();
29 
31  SGVector(T* v, index_t len, bool ref_counting=true);
32 
34  SGVector(index_t len, bool ref_counting=true);
35 
37  SGVector(const SGVector &orig);
38 
40  virtual ~SGVector();
41 
43  inline int32_t size() const { return vlen; }
44 
46  operator T*() { return vector; };
47 
49  void zero();
50 
55  void set_const(T const_elem);
56 
61  void range_fill(T start=0);
62 
68  void random(T min_value, T max_value);
69 
71  void randperm();
72 
74  SGVector<T> clone() const;
75 
77  static T* clone_vector(const T* vec, int32_t len)
78  {
79  T* result = SG_MALLOC(T, len);
80  memcpy(result, vec, sizeof(T)*len);
81  return result;
82  }
83 
85  static void fill_vector(T* vec, int32_t len, T value)
86  {
87  for (int32_t i=0; i<len; i++)
88  vec[i]=value;
89  }
90 
92  static void range_fill_vector(T* vec, int32_t len, T start=0)
93  {
94  for (int32_t i=0; i<len; i++)
95  vec[i]=i+start;
96  }
97 
99  static void random_vector(T* vec, int32_t len, T min_value, T max_value);
100 
102  static void randperm(T* perm, int32_t n);
103 
105  static void permute(T* vec, int32_t n);
106 
112  const T& get_element(index_t index);
113 
120  void set_element(const T& p_element, index_t index);
121 
127  void resize_vector(int32_t n);
128 
134  inline const T& operator[](index_t index) const
135  {
136  return vector[index];
137  }
138 
144  inline T& operator[](index_t index)
145  {
146  return vector[index];
147  }
148 
153  void add(const SGVector<T> x);
154 
159  void add(const T x);
160 
163  {
164  ASSERT(x.vector && vector);
165  ASSERT(x.vlen == vlen);
166 
167  SGVector<T> result=clone();
168  result.add(x);
169  return result;
170  }
171 
174  {
175  add(x);
176  return *this;
177  }
178 
180  static void permute_vector(SGVector<T> vec);
181 
183  void permute();
184 
188  static inline void resize(T* &data, int64_t old_size, int64_t new_size)
189  {
190  if (old_size==new_size)
191  return;
192 
193  data = SG_REALLOC(T, data, new_size);
194  }
195 
197  static T twonorm(const T* x, int32_t len);
198 
200  static float64_t onenorm(T* x, int32_t len);
201 
203  static T qsq(T* x, int32_t len, float64_t q);
204 
206  static T qnorm(T* x, int32_t len, float64_t q);
207 
208 // /// x=x+alpha*y
209 // static inline void vec1_plus_scalar_times_vec2(T* vec1,
210 // T scalar, const T* vec2, int32_t n)
211 // {
212 // for (int32_t i=0; i<n; i++)
213 // vec1[i]+=scalar*vec2[i];
214 // }
215 
217  static void vec1_plus_scalar_times_vec2(float64_t* vec1,
218  const float64_t scalar, const float64_t* vec2, int32_t n);
219 
221  static void vec1_plus_scalar_times_vec2(float32_t* vec1,
222  const float32_t scalar, const float32_t* vec2, int32_t n);
223 
225  static inline float64_t dot(const bool* v1, const bool* v2, int32_t n)
226  {
227  float64_t r=0;
228  for (int32_t i=0; i<n; i++)
229  r+=((v1[i]) ? 1 : 0) * ((v2[i]) ? 1 : 0);
230  return r;
231  }
232 
234  static inline floatmax_t dot(const floatmax_t* v1, const floatmax_t* v2, int32_t n)
235  {
236  floatmax_t r=0;
237  for (int32_t i=0; i<n; i++)
238  r+=v1[i]*v2[i];
239  return r;
240  }
241 
242 
244  static float64_t dot(const float64_t* v1, const float64_t* v2, int32_t n);
245 
247  static float32_t dot(const float32_t* v1, const float32_t* v2, int32_t n);
248 
250  static inline float64_t dot(
251  const uint64_t* v1, const uint64_t* v2, int32_t n)
252  {
253  float64_t r=0;
254  for (int32_t i=0; i<n; i++)
255  r+=((float64_t) v1[i])*v2[i];
256 
257  return r;
258  }
260  static inline float64_t dot(
261  const int64_t* v1, const int64_t* v2, int32_t n)
262  {
263  float64_t r=0;
264  for (int32_t i=0; i<n; i++)
265  r+=((float64_t) v1[i])*v2[i];
266 
267  return r;
268  }
269 
271  static inline float64_t dot(
272  const int32_t* v1, const int32_t* v2, int32_t n)
273  {
274  float64_t r=0;
275  for (int32_t i=0; i<n; i++)
276  r+=((float64_t) v1[i])*v2[i];
277 
278  return r;
279  }
280 
282  static inline float64_t dot(
283  const uint32_t* v1, const uint32_t* v2, int32_t n)
284  {
285  float64_t r=0;
286  for (int32_t i=0; i<n; i++)
287  r+=((float64_t) v1[i])*v2[i];
288 
289  return r;
290  }
291 
293  static inline float64_t dot(
294  const uint16_t* v1, const uint16_t* v2, int32_t n)
295  {
296  float64_t r=0;
297  for (int32_t i=0; i<n; i++)
298  r+=((float64_t) v1[i])*v2[i];
299 
300  return r;
301  }
302 
304  static inline float64_t dot(
305  const int16_t* v1, const int16_t* v2, int32_t n)
306  {
307  float64_t r=0;
308  for (int32_t i=0; i<n; i++)
309  r+=((float64_t) v1[i])*v2[i];
310 
311  return r;
312  }
313 
315  static inline float64_t dot(
316  const char* v1, const char* v2, int32_t n)
317  {
318  float64_t r=0;
319  for (int32_t i=0; i<n; i++)
320  r+=((float64_t) v1[i])*v2[i];
321 
322  return r;
323  }
324 
326  static inline float64_t dot(
327  const uint8_t* v1, const uint8_t* v2, int32_t n)
328  {
329  float64_t r=0;
330  for (int32_t i=0; i<n; i++)
331  r+=((float64_t) v1[i])*v2[i];
332 
333  return r;
334  }
335 
337  static inline float64_t dot(
338  const int8_t* v1, const int8_t* v2, int32_t n)
339  {
340  float64_t r=0;
341  for (int32_t i=0; i<n; i++)
342  r+=((float64_t) v1[i])*v2[i];
343 
344  return r;
345  }
346 
348  static inline float64_t dot(
349  const float64_t* v1, const char* v2, int32_t n)
350  {
351  float64_t r=0;
352  for (int32_t i=0; i<n; i++)
353  r+=((float64_t) v1[i])*v2[i];
354 
355  return r;
356  }
357 
359  static inline void vector_multiply(
360  T* target, const T* v1, const T* v2,int32_t len)
361  {
362  for (int32_t i=0; i<len; i++)
363  target[i]=v1[i]*v2[i];
364  }
365 
366 
368  static inline void add(
369  T* target, T alpha, const T* v1, T beta, const T* v2,
370  int32_t len)
371  {
372  for (int32_t i=0; i<len; i++)
373  target[i]=alpha*v1[i]+beta*v2[i];
374  }
375 
377  static inline void add_scalar(T alpha, T* vec, int32_t len)
378  {
379  for (int32_t i=0; i<len; i++)
380  vec[i]+=alpha;
381  }
382 
384  static inline void scale_vector(T alpha, T* vec, int32_t len)
385  {
386  for (int32_t i=0; i<len; i++)
387  vec[i]*=alpha;
388  }
389 
391  static inline T sum(T* vec, int32_t len)
392  {
393  T result=0;
394  for (int32_t i=0; i<len; i++)
395  result+=vec[i];
396 
397  return result;
398  }
399 
401  static inline T sum(SGVector<T> vec)
402  {
403  return sum(vec.vector, vec.vlen);
404  }
405 
406 
408  static T min(T* vec, int32_t len);
409 
411  static T max(T* vec, int32_t len);
412 
414  static inline int32_t arg_max(T * vec, int32_t inc, int32_t len, T * maxv_ptr = NULL)
415  {
416  ASSERT(len > 0 || inc > 0);
417 
418  T maxv = vec[0];
419  int32_t maxIdx = 0;
420 
421  for (int32_t i = 1, j = inc ; i < len ; i++, j += inc)
422  {
423  if (vec[j] > maxv)
424  maxv = vec[j], maxIdx = i;
425  }
426 
427  if (maxv_ptr != NULL)
428  *maxv_ptr = maxv;
429 
430  return maxIdx;
431  }
432 
434  static inline int32_t arg_min(T * vec, int32_t inc, int32_t len, T * minv_ptr = NULL)
435  {
436  ASSERT(len > 0 || inc > 0);
437 
438  T minv = vec[0];
439  int32_t minIdx = 0;
440 
441  for (int32_t i = 1, j = inc ; i < len ; i++, j += inc)
442  {
443  if (vec[j] < minv)
444  minv = vec[j], minIdx = i;
445  }
446 
447  if (minv_ptr != NULL)
448  *minv_ptr = minv;
449 
450  return minIdx;
451  }
452 
454  static T sum_abs(T* vec, int32_t len);
455 
457  static bool fequal(T x, T y, float64_t precision=1e-6);
458 
462  static int32_t unique(T* output, int32_t size);
463 
465  void display_size() const;
466 
468  void display_vector(const char* name="vector",
469  const char* prefix="") const;
470 
472  static void display_vector(
473  const T* vector, int32_t n, const char* name="vector",
474  const char* prefix="");
475 
477  static void display_vector(
478  const SGVector<T>, const char* name="vector",
479  const char* prefix="");
480 
484  SGVector<index_t> find(T elem);
485 
489  template <typename Predicate>
491  {
492  SGVector<index_t> idx(vlen);
493  index_t k=0;
494 
495  for (index_t i=0; i < vlen; ++i)
496  if (p(vector[i]))
497  idx[k++] = i;
498 
499  idx.vlen = k;
500  return idx;
501  }
502 
504  struct IndexSorter
505  {
507  IndexSorter(const SGVector<T> *vec) { data = vec->vector; }
508 
510  bool operator() (index_t i, index_t j) const
511  {
512  return data[i] < data[j];
513  }
514 
516  const T* data;
517  };
525  {
526  IndexSorter cmp(this);
527  SGVector<index_t> idx(vlen);
528  for (index_t i=0; i < vlen; ++i)
529  idx[i] = i;
530 
531  std::sort(idx.vector, idx.vector+vlen, cmp);
532 
533  return idx;
534  }
535 
537  void scale(T alpha);
538 
543  float64_t mean() const;
544 
545  protected:
547  virtual void copy_data(const SGReferencedData &orig);
548 
550  virtual void init_data();
551 
553  virtual void free_data();
554 
555  public:
557  T* vector;
560 };
561 }
562 #endif // __SGVECTOR_H__

SHOGUN Machine Learning Toolbox - Documentation