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

SHOGUN Machine Learning Toolbox - Documentation