SHOGUN  6.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
SGVector.cpp
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) 2013 Thoralf Klein
8  * Written (W) 2011-2013 Heiko Strathmann
9  * Written (W) 2013 Soumyajit De
10  * Written (W) 2012 Fernando José Iglesias García
11  * Written (W) 2010,2012 Soeren Sonnenburg
12  * Copyright (C) 2010 Berlin Institute of Technology
13  * Copyright (C) 2012 Soeren Sonnenburg
14  */
15 
16 #include <shogun/lib/config.h>
17 #include <shogun/lib/SGVector.h>
18 #include <shogun/lib/SGMatrix.h>
21 #include <shogun/io/File.h>
22 
25 #include <algorithm>
26 
28 
29 #define COMPLEX128_ERROR_NOARG(function) \
30 template <> \
31 void SGVector<complex128_t>::function() \
32 { \
33  SG_SERROR("SGVector::%s():: Not supported for complex128_t\n",\
34  #function);\
35 }
36 
37 #define BOOL_ERROR_ONEARG(function) \
38 template <> \
39 void SGVector<bool>::function(bool a) \
40 { \
41  SG_SERROR("SGVector::%s():: Not supported for bool\n",\
42  #function);\
43 }
44 
45 #define COMPLEX128_ERROR_ONEARG(function) \
46 template <> \
47 void SGVector<complex128_t>::function(complex128_t a) \
48 { \
49  SG_SERROR("SGVector::%s():: Not supported for complex128_t\n",\
50  #function);\
51 }
52 
53 #define COMPLEX128_ERROR_TWOARGS(function) \
54 template <> \
55 void SGVector<complex128_t>::function(complex128_t a, complex128_t b) \
56 { \
57  SG_SERROR("SGVector::%s():: Not supported for complex128_t\n",\
58  #function);\
59 }
60 
61 #define COMPLEX128_ERROR_THREEARGS(function) \
62 template <> \
63 void SGVector<complex128_t>::function(complex128_t a, complex128_t b,\
64  complex128_t c) \
65 { \
66  SG_SERROR("SGVector::%s():: Not supported for complex128_t\n",\
67  #function);\
68 }
69 
70 namespace shogun {
71 
72 template<class T>
74 {
75  init_data();
76 }
77 
78 template<class T>
79 SGVector<T>::SGVector(T* v, index_t len, bool ref_counting)
80 : SGReferencedData(ref_counting), vector(v), vlen(len), gpu_ptr(NULL)
81 {
82  m_on_gpu.store(false, std::memory_order_release);
83 }
84 
85 template<class T>
87 : SGReferencedData(false), vector(m+offset), vlen(len)
88 {
89  m_on_gpu.store(false, std::memory_order_release);
90 }
91 
92 template<class T>
93 SGVector<T>::SGVector(index_t len, bool ref_counting)
94 : SGReferencedData(ref_counting), vlen(len), gpu_ptr(NULL)
95 {
96  vector=SG_MALLOC(T, len);
97  m_on_gpu.store(false, std::memory_order_release);
98 }
99 
100 template<class T>
102  : SGReferencedData(true), vector(NULL), vlen(len),
103  gpu_ptr(std::shared_ptr<GPUMemoryBase<T>>(gpu_vector))
104 {
105  m_on_gpu.store(true, std::memory_order_release);
106 }
107 
108 template<class T>
110 {
111  copy_data(orig);
112 }
113 
114 template<class T>
116 {
117  if(&other == this)
118  return *this;
119 
120  unref();
121  copy_data(other);
122  copy_refcount(other);
123  ref();
124  return *this;
125 }
126 
127 template<class T>
129 {
130  *this = SGVector<T>(orig);
131 }
132 
133 template<class T>
135 {
136  unref();
137 }
138 
139 template <class T>
141 : SGReferencedData(false), vector(vec.data()), vlen(vec.size()), gpu_ptr(NULL)
142 {
143  m_on_gpu.store(false, std::memory_order_release);
144 }
145 
146 template <class T>
148 : SGReferencedData(false), vector(vec.data()), vlen(vec.size()), gpu_ptr(NULL)
149 {
150  m_on_gpu.store(false, std::memory_order_release);
151 }
152 
153 template <class T>
155 {
156  assert_on_cpu();
157  return EigenVectorXtMap(vector, vlen);
158 }
159 
160 template <class T>
162 {
163  assert_on_cpu();
164  return EigenRowVectorXtMap(vector, vlen);
165 }
166 
167 template<class T>
169 {
170  assert_on_cpu();
171  if (vector && vlen)
172  set_const(0);
173 }
174 
175 template <>
177 {
178  assert_on_cpu();
179  if (vector && vlen)
180  set_const(complex128_t(0.0));
181 }
182 
183 template<class T>
184 void SGVector<T>::set_const(T const_elem)
185 {
186  assert_on_cpu();
187  for (index_t i=0; i<vlen; i++)
188  vector[i]=const_elem ;
189 }
190 
191 #if HAVE_CATLAS
192 template<>
194 {
195  assert_on_cpu();
196  catlas_dset(vlen, const_elem, vector, 1);
197 }
198 
199 template<>
201 {
202  assert_on_cpu();
203  catlas_sset(vlen, const_elem, vector, 1);
204 }
205 #endif // HAVE_CATLAS
206 
207 template<class T>
209 {
210  assert_on_cpu();
211  range_fill_vector(vector, vlen, start);
212 }
213 
215 
216 template<class T>
217 void SGVector<T>::random(T min_value, T max_value)
218 {
219  assert_on_cpu();
220  random_vector(vector, vlen, min_value, max_value);
221 }
222 
224 
225 template <class T>
226 index_t SGVector<T>::find_position_to_insert(T element)
227 {
228  assert_on_cpu();
229  index_t i;
230  for (i=0; i<vlen; ++i)
231  {
232  if (vector[i]>element)
233  break;
234  }
235  return i;
236 }
237 
238 template <>
240 {
241  SG_SERROR("SGVector::find_position_to_insert():: \
242  Not supported for complex128_t\n");
243  return index_t(-1);
244 }
245 
246 template<class T>
248 {
249  if (on_gpu())
250  return SGVector<T>(gpu_ptr->clone_vector(gpu_ptr.get(), vlen), vlen);
251  else
252  return SGVector<T>(clone_vector(vector, vlen), vlen);
253 }
254 
255 template<class T>
256 T* SGVector<T>::clone_vector(const T* vec, int32_t len)
257 {
258  T* result = SG_MALLOC(T, len);
259  sg_memcpy(result, vec, sizeof(T)*len);
260  return result;
261 }
262 
263 template<class T>
264 void SGVector<T>::fill_vector(T* vec, int32_t len, T value)
265 {
266  for (int32_t i=0; i<len; i++)
267  vec[i]=value;
268 }
269 
270 template<class T>
271 void SGVector<T>::range_fill_vector(T* vec, int32_t len, T start)
272 {
273  for (int32_t i=0; i<len; i++)
274  vec[i]=i+start;
275 }
276 
277 template <>
279  int32_t len, complex128_t start)
280 {
281  SG_SERROR("SGVector::range_fill_vector():: \
282  Not supported for complex128_t\n");
283 }
284 
285 template<class T>
287 {
288  assert_on_cpu();
289  vector=SG_REALLOC(T, vector, vlen, n);
290 
291  if (n > vlen)
292  memset(&vector[vlen], 0, (n-vlen)*sizeof(T));
293  vlen=n;
294 }
295 
297 template<class T>
299 {
300  assert_on_cpu();
301  REQUIRE(x.vector && vector, "Addition possible for only non-null vectors.\n");
302  REQUIRE(x.vlen == vlen, "Length of the two vectors to be added should be same. [V(%d) + V(%d)]\n", vlen, x.vlen);
303 
304  SGVector<T> result=clone();
305  result.add(x);
306  return result;
307 }
308 
309 template<class T>
311 {
312  assert_on_cpu();
313  REQUIRE(x.vector && vector, "Addition possible for only non-null vectors.\n");
314  REQUIRE(x.vlen == vlen, "Length of the two vectors to be added should be same. [V(%d) + V(%d)]\n", vlen, x.vlen);
315 
316  for (int32_t i=0; i<vlen; i++)
317  vector[i]+=x.vector[i];
318 }
319 
320 template<class T>
321 void SGVector<T>::add(const T x)
322 {
323  assert_on_cpu();
324  REQUIRE(vector, "Addition possible for only non-null vectors.\n");
325  for (int32_t i=0; i<vlen; i++)
326  vector[i]+=x;
327 }
328 
329 template<class T>
331 {
332  assert_on_cpu();
333  if (x.features)
334  {
335  for (int32_t i=0; i < x.num_feat_entries; i++)
336  {
337  index_t idx = x.features[i].feat_index;
338  REQUIRE(idx < vlen, "Feature index should be less than %d.\n", vlen);
339  vector[idx] += x.features[i].entry;
340  }
341  }
342 }
343 
344 template<class T>
346 {
347  assert_on_cpu();
348  SG_SPRINT("SGVector '%p' of size: %d\n", vector, vlen)
349 }
350 
351 template<class T>
353 {
354  gpu_ptr=std::shared_ptr<GPUMemoryBase<T>>(((SGVector*)(&orig))->gpu_ptr);
355  vector=((SGVector*)(&orig))->vector;
356  vlen=((SGVector*)(&orig))->vlen;
357  m_on_gpu.store(((SGVector*)(&orig))->m_on_gpu.load(
358  std::memory_order_acquire), std::memory_order_release);
359 }
360 
361 template<class T>
363 {
364  vector=NULL;
365  vlen=0;
366  gpu_ptr=NULL;
367  m_on_gpu.store(false, std::memory_order_release);
368 }
369 
370 template<class T>
372 {
373  SG_FREE(vector);
374  vector=NULL;
375  vlen=0;
376  gpu_ptr=NULL;
377 }
378 
379 template<class T>
381 {
382  assert_on_cpu();
383  if (other.vlen!=vlen)
384  return false;
385 
386  for (index_t i=0; i<vlen; ++i)
387  {
388  if (other.vector[i]!=vector[i])
389  return false;
390  }
391 
392  return true;
393 }
394 
395 template<class T>
396 void SGVector<T>::display_vector(const char* name,
397  const char* prefix) const
398 {
399  display_vector(vector, vlen, name, prefix);
400 }
401 
402 template <class T>
403 void SGVector<T>::display_vector(const SGVector<T> vector, const char* name,
404  const char* prefix)
405 {
406  vector.display_vector(prefix);
407 }
408 
409 template <>
410 void SGVector<bool>::display_vector(const bool* vector, int32_t n, const char* name,
411  const char* prefix)
412 {
413  REQUIRE(n>=0, "Vector size can not be negative.\n");
414  SG_SPRINT("%s%s=[", prefix, name)
415  for (int32_t i=0; i<n; i++)
416  SG_SPRINT("%s%d%s", prefix, vector[i] ? 1 : 0, i==n-1? "" : ",")
417  SG_SPRINT("%s]\n", prefix)
418 }
419 
420 template <>
421 void SGVector<char>::display_vector(const char* vector, int32_t n, const char* name,
422  const char* prefix)
423 {
424  REQUIRE(n>=0, "Vector size can not be negative.\n");
425  SG_SPRINT("%s%s=[", prefix, name)
426  for (int32_t i=0; i<n; i++)
427  SG_SPRINT("%s%c%s", prefix, vector[i], i==n-1? "" : ",")
428  SG_SPRINT("%s]\n", prefix)
429 }
430 
431 template <>
432 void SGVector<uint8_t>::display_vector(const uint8_t* vector, int32_t n, const char* name,
433  const char* prefix)
434 {
435  REQUIRE(n>=0, "Vector size can not be negative.\n");
436  SG_SPRINT("%s%s=[", prefix, name)
437  for (int32_t i=0; i<n; i++)
438  SG_SPRINT("%s%u%s", prefix, vector[i], i==n-1? "" : ",")
439  SG_SPRINT("%s]\n", prefix)
440 }
441 
442 template <>
443 void SGVector<int8_t>::display_vector(const int8_t* vector, int32_t n, const char* name,
444  const char* prefix)
445 {
446  REQUIRE(n>=0, "Vector size can not be negative.\n");
447  SG_SPRINT("%s%s=[", prefix, name)
448  for (int32_t i=0; i<n; i++)
449  SG_SPRINT("%s%d%s", prefix, vector[i], i==n-1? "" : ",")
450  SG_SPRINT("%s]\n", prefix)
451 }
452 
453 template <>
454 void SGVector<uint16_t>::display_vector(const uint16_t* vector, int32_t n, const char* name,
455  const char* prefix)
456 {
457  REQUIRE(n>=0, "Vector size can not be negative.\n");
458  SG_SPRINT("%s%s=[", prefix, name)
459  for (int32_t i=0; i<n; i++)
460  SG_SPRINT("%s%u%s", prefix, vector[i], i==n-1? "" : ",")
461  SG_SPRINT("%s]\n", prefix)
462 }
463 
464 template <>
465 void SGVector<int16_t>::display_vector(const int16_t* vector, int32_t n, const char* name,
466  const char* prefix)
467 {
468  REQUIRE(n>=0, "Vector size can not be negative.\n");
469  SG_SPRINT("%s%s=[", prefix, name)
470  for (int32_t i=0; i<n; i++)
471  SG_SPRINT("%s%d%s", prefix, vector[i], i==n-1? "" : ",")
472  SG_SPRINT("%s]\n", prefix)
473 }
474 
475 template <>
476 void SGVector<int32_t>::display_vector(const int32_t* vector, int32_t n, const char* name,
477  const char* prefix)
478 {
479  REQUIRE(n>=0, "Vector size can not be negative.\n");
480  SG_SPRINT("%s%s=[", prefix, name)
481  for (int32_t i=0; i<n; i++)
482  SG_SPRINT("%s%d%s", prefix, vector[i], i==n-1? "" : ",")
483  SG_SPRINT("%s]\n", prefix)
484 }
485 
486 template <>
487 void SGVector<uint32_t>::display_vector(const uint32_t* vector, int32_t n, const char* name,
488  const char* prefix)
489 {
490  REQUIRE(n>=0, "Vector size can not be negative.\n");
491  SG_SPRINT("%s%s=[", prefix, name)
492  for (int32_t i=0; i<n; i++)
493  SG_SPRINT("%s%u%s", prefix, vector[i], i==n-1? "" : ",")
494  SG_SPRINT("%s]\n", prefix)
495 }
496 
497 
498 template <>
499 void SGVector<int64_t>::display_vector(const int64_t* vector, int32_t n, const char* name,
500  const char* prefix)
501 {
502  REQUIRE(n>=0, "Vector size can not be negative.\n");
503  SG_SPRINT("%s%s=[", prefix, name)
504  for (int32_t i=0; i<n; i++)
505  SG_SPRINT("%s%lld%s", prefix, vector[i], i==n-1? "" : ",")
506  SG_SPRINT("%s]\n", prefix)
507 }
508 
509 template <>
510 void SGVector<uint64_t>::display_vector(const uint64_t* vector, int32_t n, const char* name,
511  const char* prefix)
512 {
513  REQUIRE(n>=0, "Vector size can not be negative.\n");
514  SG_SPRINT("%s%s=[", prefix, name)
515  for (int32_t i=0; i<n; i++)
516  SG_SPRINT("%s%llu%s", prefix, vector[i], i==n-1? "" : ",")
517  SG_SPRINT("%s]\n", prefix)
518 }
519 
520 template <>
521 void SGVector<float32_t>::display_vector(const float32_t* vector, int32_t n, const char* name,
522  const char* prefix)
523 {
524  REQUIRE(n>=0, "Vector size can not be negative.\n");
525  SG_SPRINT("%s%s=[", prefix, name)
526  for (int32_t i=0; i<n; i++)
527  SG_SPRINT("%s%g%s", prefix, vector[i], i==n-1? "" : ",")
528  SG_SPRINT("%s]\n", prefix)
529 }
530 
531 template <>
532 void SGVector<float64_t>::display_vector(const float64_t* vector, int32_t n, const char* name,
533  const char* prefix)
534 {
535  REQUIRE(n>=0, "Vector size can not be negative.\n");
536  SG_SPRINT("%s%s=[", prefix, name)
537  for (int32_t i=0; i<n; i++)
538  SG_SPRINT("%s%.18g%s", prefix, vector[i], i==n-1? "" : ",")
539  SG_SPRINT("%s]\n", prefix)
540 }
541 
542 template <>
543 void SGVector<floatmax_t>::display_vector(const floatmax_t* vector, int32_t n,
544  const char* name, const char* prefix)
545 {
546  REQUIRE(n>=0, "Vector size can not be negative.\n");
547  SG_SPRINT("%s%s=[", prefix, name)
548  for (int32_t i=0; i<n; i++)
549  {
550  SG_SPRINT("%s%.36Lg%s", prefix, (long double) vector[i],
551  i==n-1? "" : ",");
552  }
553  SG_SPRINT("%s]\n", prefix)
554 }
555 
556 template <>
558  const char* name, const char* prefix)
559 {
560  REQUIRE(n>=0, "Vector size can not be negative.\n");
561  SG_SPRINT("%s%s=[", prefix, name)
562  for (int32_t i=0; i<n; i++)
563  {
564  SG_SPRINT("%s(%.36lg+i%.36lg)%s", prefix, vector[i].real(),
565  vector[i].imag(), i==n-1? "" : ",");
566  }
567  SG_SPRINT("%s]\n", prefix)
568 }
569 
570 template <class T>
572  const T scalar, const T* vec2, int32_t n)
573 {
574  for (int32_t i=0; i<n; i++)
575  vec1[i]+=scalar*vec2[i];
576 }
577 
578 template <>
580  float64_t scalar, const float64_t* vec2, int32_t n)
581 {
582 #ifdef HAVE_LAPACK
583  int32_t skip=1;
584  cblas_daxpy(n, scalar, vec2, skip, vec1, skip);
585 #else
586  for (int32_t i=0; i<n; i++)
587  vec1[i]+=scalar*vec2[i];
588 #endif
589 }
590 
591 template <>
593  float32_t scalar, const float32_t* vec2, int32_t n)
594 {
595 #ifdef HAVE_LAPACK
596  int32_t skip=1;
597  cblas_saxpy(n, scalar, vec2, skip, vec1, skip);
598 #else
599  for (int32_t i=0; i<n; i++)
600  vec1[i]+=scalar*vec2[i];
601 #endif
602 }
603 
604 template <class T>
605  void SGVector<T>::random_vector(T* vec, int32_t len, T min_value, T max_value)
606  {
607  for (int32_t i=0; i<len; i++)
608  vec[i]=CMath::random(min_value, max_value);
609  }
610 
611 template <>
613  complex128_t min_value, complex128_t max_value)
614 {
616 }
617 
618 template <>
619 bool SGVector<bool>::twonorm(const bool* x, int32_t len)
620 {
622  return false;
623 }
624 
625 template <>
626 char SGVector<char>::twonorm(const char* x, int32_t len)
627 {
629  return '\0';
630 }
631 
632 template <>
633 int8_t SGVector<int8_t>::twonorm(const int8_t* x, int32_t len)
634 {
635  float64_t result=0;
636  for (int32_t i=0; i<len; i++)
637  result+=x[i]*x[i];
638 
639  return CMath::sqrt(result);
640 }
641 
642 template <>
643 uint8_t SGVector<uint8_t>::twonorm(const uint8_t* x, int32_t len)
644 {
645  float64_t result=0;
646  for (int32_t i=0; i<len; i++)
647  result+=x[i]*x[i];
648 
649  return CMath::sqrt(result);
650 }
651 
652 template <>
653 int16_t SGVector<int16_t>::twonorm(const int16_t* x, int32_t len)
654 {
655  float64_t result=0;
656  for (int32_t i=0; i<len; i++)
657  result+=x[i]*x[i];
658 
659  return CMath::sqrt(result);
660 }
661 
662 template <>
663 uint16_t SGVector<uint16_t>::twonorm(const uint16_t* x, int32_t len)
664 {
665  float64_t result=0;
666  for (int32_t i=0; i<len; i++)
667  result+=x[i]*x[i];
668 
669  return CMath::sqrt(result);
670 }
671 
672 template <>
673 int32_t SGVector<int32_t>::twonorm(const int32_t* x, int32_t len)
674 {
675  float64_t result=0;
676  for (int32_t i=0; i<len; i++)
677  result+=x[i]*x[i];
678 
679  return CMath::sqrt(result);
680 }
681 
682 template <>
683 uint32_t SGVector<uint32_t>::twonorm(const uint32_t* x, int32_t len)
684 {
685  float64_t result=0;
686  for (int32_t i=0; i<len; i++)
687  result+=x[i]*x[i];
688 
689  return CMath::sqrt(result);
690 }
691 
692 template <>
693 int64_t SGVector<int64_t>::twonorm(const int64_t* x, int32_t len)
694 {
695  float64_t result=0;
696  for (int32_t i=0; i<len; i++)
697  result+=x[i]*x[i];
698 
699  return CMath::sqrt(result);
700 }
701 
702 template <>
703 uint64_t SGVector<uint64_t>::twonorm(const uint64_t* x, int32_t len)
704 {
705  float64_t result=0;
706  for (int32_t i=0; i<len; i++)
707  result+=x[i]*x[i];
708 
709  return CMath::sqrt(result);
710 }
711 
712 template <>
714 {
715  float64_t result=0;
716  for (int32_t i=0; i<len; i++)
717  result+=x[i]*x[i];
718 
719  return CMath::sqrt(result);
720 }
721 
722 template <>
724 {
725  float64_t norm = 0.0;
726 #ifdef HAVE_LAPACK
727  norm = cblas_dnrm2(n, v, 1);
728 #else
729  norm = CMath::sqrt(CMath::dot(v, v, n));
730 #endif
731  return norm;
732 }
733 
734 template <>
736 {
737  float64_t result=0;
738  for (int32_t i=0; i<len; i++)
739  result+=x[i]*x[i];
740 
741  return CMath::sqrt(result);
742 }
743 
744 template <>
746 {
747  complex128_t result(0.0);
748  for (int32_t i=0; i<len; i++)
749  result+=x[i]*x[i];
750 
751  return CMath::sqrt(result);
752 }
753 
754 template <class T>
755 float64_t SGVector<T>::onenorm(T* x, int32_t len)
756 {
757  float64_t result=0;
758  for (int32_t i=0;i<len; ++i)
759  result+=CMath::abs(x[i]);
760 
761  return result;
762 }
763 
765 template <class T>
766 T SGVector<T>::qsq(T* x, int32_t len, float64_t q)
767 {
768  float64_t result=0;
769  for (int32_t i=0; i<len; i++)
770  result+=CMath::pow(fabs(x[i]), q);
771 
772  return result;
773 }
774 
775 template <>
777 {
779  return complex128_t(0.0);
780 }
781 
783 template <class T>
784 T SGVector<T>::qnorm(T* x, int32_t len, float64_t q)
785 {
786  REQUIRE(q!=0, "Q should be non-zero for calculating qnorm\n");
787  return CMath::pow((float64_t) qsq(x, len, q), 1.0/q);
788 }
789 
790 template <>
792 {
794  return complex128_t(0.0);
795 }
796 
798 template <class T>
799 T SGVector<T>::sum_abs(T* vec, int32_t len)
800 {
801  T result=0;
802  for (int32_t i=0; i<len; i++)
803  result+=CMath::abs(vec[i]);
804 
805  return result;
806 }
807 
808 #if HAVE_LAPACK
809 template <>
811 {
812  float64_t result=0;
813  result = cblas_dasum(len, vec, 1);
814  return result;
815 }
816 
817 template <>
819 {
820  float32_t result=0;
821  result = cblas_sasum(len, vec, 1);
822  return result;
823 }
824 #endif
825 
826 template <class T>
827 int32_t SGVector<T>::unique(T* output, int32_t size)
828 {
829  CMath::qsort<T>(output, size);
830  int32_t j=0;
831 
832  for (int32_t i=0; i<size; i++)
833  {
834  if (i==0 || output[i]!=output[i-1])
835  output[j++]=output[i];
836  }
837  return j;
838 }
839 
840 template <>
841 int32_t SGVector<complex128_t>::unique(complex128_t* output, int32_t size)
842 {
843  int32_t j=0;
844  SG_SERROR("SGVector::unique():: Not supported for complex128_t\n");
845  return j;
846 }
847 
848 template <class T>
850 {
851  assert_on_cpu();
852  SGVector<index_t> idx(vlen);
853  index_t k=0;
854 
855  for (index_t i=0; i < vlen; ++i)
856  if (vector[i] == elem)
857  idx[k++] = i;
858  idx.vlen = k;
859  return idx;
860 }
861 
862 template<class T>
863 void SGVector<T>::scale_vector(T alpha, T* vec, int32_t len)
864 {
865  for (int32_t i=0; i<len; i++)
866  vec[i]*=alpha;
867 }
868 
869 #ifdef HAVE_LAPACK
870 template<>
872 {
873  cblas_dscal(len, alpha, vec, 1);
874 }
875 
876 template<>
878 {
879  cblas_sscal(len, alpha, vec, 1);
880 }
881 #endif
882 
883 template<class T>
884 void SGVector<T>::scale(T alpha)
885 {
886  scale_vector(alpha, vector, vlen);
887 }
888 
889 template<class T> void SGVector<T>::load(CFile* loader)
890 {
891  REQUIRE(loader, "Require a valid 'c FILE pointer'\n");
892  unref();
893 
895  SGVector<T> vec;
896  loader->get_vector(vec.vector, vec.vlen);
897  vec.gpu_ptr = nullptr;
898  copy_data(vec);
899  copy_refcount(vec);
900  ref();
902 }
903 
904 template<>
906 {
907  SG_SERROR("SGVector::load():: Not supported for complex128_t\n");
908 }
909 
910 template<class T> void SGVector<T>::save(CFile* saver)
911 {
912  REQUIRE(saver, "Requires a valid 'c FILE pointer'\n");
913 
914  assert_on_cpu();
916  saver->set_vector(vector, vlen);
918 }
919 
920 template<>
922 {
923  SG_SERROR("SGVector::save():: Not supported for complex128_t\n");
924 }
925 
927 {
928  assert_on_cpu();
929  SGVector<float64_t> real(vlen);
930  for (int32_t i=0; i<vlen; i++)
931  real[i]=CMath::real(vector[i]);
932  return real;
933 }
934 
936 {
937  assert_on_cpu();
938  SGVector<float64_t> imag(vlen);
939  for (int32_t i=0; i<vlen; i++)
940  imag[i]=CMath::imag(vector[i]);
941  return imag;
942 }
943 
944 template <class T>
946  index_t nrows, index_t ncols, bool fortran_order)
947 {
948  if (nrows*ncols>vector.size())
949  SG_SERROR("SGVector::convert_to_matrix():: Dimensions mismatch\n");
950 
951  T* data=NULL;
952  SGVector<T>::convert_to_matrix(data, nrows, ncols, vector.vector, vector.vlen, fortran_order);
953 
954  SGMatrix<T> matrix=SGMatrix<T>(data, nrows, ncols);
955  return matrix;
956 }
957 
958 template <class T>
959 void SGVector<T>::convert_to_matrix(T*& matrix, index_t nrows, index_t ncols, const T* vector, int32_t vlen, bool fortran_order)
960 {
961  if (nrows*ncols>vlen)
962  SG_SERROR("SGVector::convert_to_matrix():: Dimensions mismatch\n");
963 
964  if (matrix!=NULL)
965  SG_FREE(matrix);
966  matrix=SG_MALLOC(T, nrows*ncols);
967 
968  if (fortran_order)
969  {
970  for (index_t i=0; i<ncols*nrows; i++)
971  matrix[i]=vector[i];
972  }
973  else
974  {
975  for (index_t i=0; i<nrows; i++)
976  {
977  for (index_t j=0; j<ncols; j++)
978  matrix[i+j*nrows]=vector[j+i*ncols];
979  }
980  }
981 }
982 
983 #define UNDEFINED(function, type) \
984 template <> \
985 SGVector<float64_t> SGVector<type>::function() \
986 { \
987  SG_SERROR("SGVector::%s():: Not supported for %s\n", \
988  #function, #type); \
989  SGVector<float64_t> ret(vlen); \
990  return ret; \
991 }
992 
993 UNDEFINED(get_real, bool)
994 UNDEFINED(get_real, char)
995 UNDEFINED(get_real, int8_t)
996 UNDEFINED(get_real, uint8_t)
997 UNDEFINED(get_real, int16_t)
998 UNDEFINED(get_real, uint16_t)
999 UNDEFINED(get_real, int32_t)
1000 UNDEFINED(get_real, uint32_t)
1001 UNDEFINED(get_real, int64_t)
1002 UNDEFINED(get_real, uint64_t)
1003 UNDEFINED(get_real, float32_t)
1004 UNDEFINED(get_real, float64_t)
1005 UNDEFINED(get_real, floatmax_t)
1006 UNDEFINED(get_imag, bool)
1007 UNDEFINED(get_imag, char)
1008 UNDEFINED(get_imag, int8_t)
1009 UNDEFINED(get_imag, uint8_t)
1010 UNDEFINED(get_imag, int16_t)
1011 UNDEFINED(get_imag, uint16_t)
1012 UNDEFINED(get_imag, int32_t)
1013 UNDEFINED(get_imag, uint32_t)
1014 UNDEFINED(get_imag, int64_t)
1015 UNDEFINED(get_imag, uint64_t)
1016 UNDEFINED(get_imag, float32_t)
1017 UNDEFINED(get_imag, float64_t)
1018 UNDEFINED(get_imag, floatmax_t)
1019 #undef UNDEFINED
1020 
1021 template class SGVector<bool>;
1022 template class SGVector<char>;
1023 template class SGVector<int8_t>;
1024 template class SGVector<uint8_t>;
1025 template class SGVector<int16_t>;
1026 template class SGVector<uint16_t>;
1027 template class SGVector<int32_t>;
1028 template class SGVector<uint32_t>;
1029 template class SGVector<int64_t>;
1030 template class SGVector<uint64_t>;
1031 template class SGVector<float32_t>;
1032 template class SGVector<float64_t>;
1033 template class SGVector<floatmax_t>;
1034 template class SGVector<complex128_t>;
1035 }
1036 
1037 #undef COMPLEX128_ERROR_NOARG
1038 #undef COMPLEX128_ERROR_ONEARG
1039 #undef COMPLEX128_ERROR_TWOARGS
1040 #undef COMPLEX128_ERROR_THREEARGS
#define SG_RESET_LOCALE
Definition: SGIO.h:85
std::complex< float64_t > complex128_t
Definition: common.h:77
int32_t index_t
Definition: common.h:72
#define COMPLEX128_ERROR_TWOARGS(function)
Definition: SGVector.cpp:53
void set(SGVector< T > orig)
Definition: SGVector.cpp:128
void scale(SGVector< T > &a, SGVector< T > &result, T alpha=1)
virtual void init_data()
Definition: SGVector.cpp:362
Definition: basetag.h:132
void add(SGVector< T > &a, SGVector< T > &b, SGVector< T > &result, T alpha=1, T beta=1)
#define REQUIRE(x,...)
Definition: SGIO.h:205
T dot(const SGVector< T > &a, const SGVector< T > &b)
#define SG_SNOTIMPLEMENTED
Definition: SGIO.h:197
void set_const(Container< T > &a, T value)
#define SG_SET_LOCALE_C
Definition: SGIO.h:84
shogun matrix
void display_vector(const char *name="vector", const char *prefix="") const
Definition: SGVector.cpp:396
int32_t size() const
Definition: SGVector.h:136
index_t vlen
Definition: SGVector.h:545
#define UNDEFINED(function, type)
Definition: SGVector.cpp:983
#define SG_SPRINT(...)
Definition: SGIO.h:179
shogun vector
virtual void get_vector(bool *&vector, int32_t &len)
Definition: File.cpp:81
shogun reference count managed data
double float64_t
Definition: common.h:60
long double floatmax_t
Definition: common.h:61
SGVector< T > & operator=(const SGVector< T > &)
Definition: SGVector.cpp:115
void range_fill(Container< T > &a, const T start=0)
A File access base class.
Definition: File.h:34
virtual ~SGVector()
Definition: SGVector.cpp:134
SGSparseVectorEntry< T > * features
float float32_t
Definition: common.h:59
#define COMPLEX128_ERROR_ONEARG(function)
Definition: SGVector.cpp:45
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
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
template class SGSparseVector The assumtion is that the stored SGSparseVectorEntry* vector is orde...
virtual void copy_data(const SGReferencedData &orig)
Definition: SGVector.cpp:352
virtual void set_vector(const bool *vector, int32_t len)
Definition: File.cpp:95
void add(const SGVector< T > x)
Definition: SGVector.cpp:310

SHOGUN Machine Learning Toolbox - Documentation