SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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)
81 {
82 }
83 
84 template<class T>
85 SGVector<T>::SGVector(index_t len, bool ref_counting)
86 : SGReferencedData(ref_counting), vlen(len)
87 {
88  vector=SG_MALLOC(T, len);
89 }
90 
91 template<class T>
93 {
94  copy_data(orig);
95 }
96 
97 template<class T>
99 {
100  *this = SGVector<T>(orig);
101 }
102 
103 template<class T>
105 {
106  unref();
107 }
108 
109 #ifdef HAVE_EIGEN3
110 template <class T>
112 : SGReferencedData(false), vector(vec.data()), vlen(vec.size())
113 {
114 
115 }
116 
117 template <class T>
119 : SGReferencedData(false), vector(vec.data()), vlen(vec.size())
120 {
121 
122 }
123 
124 template <class T>
126 {
127  return EigenVectorXtMap(vector, vlen);
128 }
129 
130 template <class T>
132 {
133  return EigenRowVectorXtMap(vector, vlen);
134 }
135 #endif
136 
137 template<class T>
139 {
140  if (vector && vlen)
141  set_const(0);
142 }
143 
144 template <>
146 {
147  if (vector && vlen)
148  set_const(complex128_t(0.0));
149 }
150 
151 template<class T>
152 void SGVector<T>::set_const(T const_elem)
153 {
154  for (index_t i=0; i<vlen; i++)
155  vector[i]=const_elem ;
156 }
157 
158 #if HAVE_CATLAS
159 template<>
161 {
162  catlas_dset(vlen, const_elem, vector, 1);
163 }
164 
165 template<>
167 {
168  catlas_sset(vlen, const_elem, vector, 1);
169 }
170 #endif // HAVE_CATLAS
171 
172 template<class T>
174 {
175  range_fill_vector(vector, vlen, start);
176 }
177 
178 COMPLEX128_ERROR_ONEARG(range_fill)
179 
180 template<class T>
181 void SGVector<T>::random(T min_value, T max_value)
182 {
183  random_vector(vector, vlen, min_value, max_value);
184 }
185 
187 
188 template <class T>
189 index_t SGVector<T>::find_position_to_insert(T element)
190 {
191  index_t i;
192  for (i=0; i<vlen; ++i)
193  {
194  if (vector[i]>element)
195  break;
196  }
197  return i;
198 }
199 
200 template <>
202 {
203  SG_SERROR("SGVector::find_position_to_insert():: \
204  Not supported for complex128_t\n");
205  return index_t(-1);
206 }
207 
208 template<class T>
210 {
211  return SGVector<T>(clone_vector(vector, vlen), vlen);
212 }
213 
214 template<class T>
215 T* SGVector<T>::clone_vector(const T* vec, int32_t len)
216 {
217  T* result = SG_MALLOC(T, len);
218  memcpy(result, vec, sizeof(T)*len);
219  return result;
220 }
221 
222 template<class T>
223 void SGVector<T>::fill_vector(T* vec, int32_t len, T value)
224 {
225  for (int32_t i=0; i<len; i++)
226  vec[i]=value;
227 }
228 
229 template<class T>
230 void SGVector<T>::range_fill_vector(T* vec, int32_t len, T start)
231 {
232  for (int32_t i=0; i<len; i++)
233  vec[i]=i+start;
234 }
235 
236 template <>
238  int32_t len, complex128_t start)
239 {
240  SG_SERROR("SGVector::range_fill_vector():: \
241  Not supported for complex128_t\n");
242 }
243 
244 template<class T>
246 {
247  REQUIRE(vector && (index>=0) && (index<vlen), "Provided index (%d) must be between 0 and %d.\n", index, vlen);
248  return vector[index];
249 }
250 
251 template<class T>
252 void SGVector<T>::set_element(const T& p_element, index_t index)
253 {
254  REQUIRE(vector && (index>=0) && (index<vlen), "Provided index (%d) must be between 0 and %d.\n", index, vlen);
255  vector[index]=p_element;
256 }
257 
258 template<class T>
260 {
261  vector=SG_REALLOC(T, vector, vlen, n);
262 
263  if (n > vlen)
264  memset(&vector[vlen], 0, (n-vlen)*sizeof(T));
265  vlen=n;
266 }
267 
269 template<class T>
271 {
272  REQUIRE(x.vector && vector, "Addition possible for only non-null vectors.\n");
273  REQUIRE(x.vlen == vlen, "Length of the two vectors to be added should be same. [V(%d) + V(%d)]\n", vlen, x.vlen);
274 
275  SGVector<T> result=clone();
276  result.add(x);
277  return result;
278 }
279 
280 template<class T>
282 {
283  REQUIRE(x.vector && vector, "Addition possible for only non-null vectors.\n");
284  REQUIRE(x.vlen == vlen, "Length of the two vectors to be added should be same. [V(%d) + V(%d)]\n", vlen, x.vlen);
285 
286  for (int32_t i=0; i<vlen; i++)
287  vector[i]+=x.vector[i];
288 }
289 
290 template<class T>
291 void SGVector<T>::add(const T x)
292 {
293  REQUIRE(vector, "Addition possible for only non-null vectors.\n");
294  for (int32_t i=0; i<vlen; i++)
295  vector[i]+=x;
296 }
297 
298 template<class T>
300 {
301  if (x.features)
302  {
303  for (int32_t i=0; i < x.num_feat_entries; i++)
304  {
305  index_t idx = x.features[i].feat_index;
306  REQUIRE(idx < vlen, "Feature index should be less than %d.\n", vlen);
307  vector[idx] += x.features[i].entry;
308  }
309  }
310 }
311 
312 template<class T>
314 {
315  SG_SPRINT("SGVector '%p' of size: %d\n", vector, vlen)
316 }
317 
318 template<class T>
320 {
321  vector=((SGVector*)(&orig))->vector;
322  vlen=((SGVector*)(&orig))->vlen;
323 }
324 
325 template<class T>
327 {
328  vector=NULL;
329  vlen=0;
330 }
331 
332 template<class T>
334 {
335  SG_FREE(vector);
336  vector=NULL;
337  vlen=0;
338 }
339 
340 template<class T>
342 {
343  if (other.vlen!=vlen)
344  return false;
345 
346  for (index_t i=0; i<vlen; ++i)
347  {
348  if (other.vector[i]!=vector[i])
349  return false;
350  }
351 
352  return true;
353 }
354 
355 template<class T>
356 void SGVector<T>::display_vector(const char* name,
357  const char* prefix) const
358 {
359  display_vector(vector, vlen, name, prefix);
360 }
361 
362 template <class T>
363 void SGVector<T>::display_vector(const SGVector<T> vector, const char* name,
364  const char* prefix)
365 {
366  vector.display_vector(prefix);
367 }
368 
369 template <>
370 void SGVector<bool>::display_vector(const bool* vector, int32_t n, const char* name,
371  const char* prefix)
372 {
373  REQUIRE(n>=0, "Vector size can not be negative.\n");
374  SG_SPRINT("%s%s=[", prefix, name)
375  for (int32_t i=0; i<n; i++)
376  SG_SPRINT("%s%d%s", prefix, vector[i] ? 1 : 0, i==n-1? "" : ",")
377  SG_SPRINT("%s]\n", prefix)
378 }
379 
380 template <>
381 void SGVector<char>::display_vector(const char* vector, int32_t n, const char* name,
382  const char* prefix)
383 {
384  REQUIRE(n>=0, "Vector size can not be negative.\n");
385  SG_SPRINT("%s%s=[", prefix, name)
386  for (int32_t i=0; i<n; i++)
387  SG_SPRINT("%s%c%s", prefix, vector[i], i==n-1? "" : ",")
388  SG_SPRINT("%s]\n", prefix)
389 }
390 
391 template <>
392 void SGVector<uint8_t>::display_vector(const uint8_t* vector, int32_t n, const char* name,
393  const char* prefix)
394 {
395  REQUIRE(n>=0, "Vector size can not be negative.\n");
396  SG_SPRINT("%s%s=[", prefix, name)
397  for (int32_t i=0; i<n; i++)
398  SG_SPRINT("%s%u%s", prefix, vector[i], i==n-1? "" : ",")
399  SG_SPRINT("%s]\n", prefix)
400 }
401 
402 template <>
403 void SGVector<int8_t>::display_vector(const int8_t* vector, int32_t n, const char* name,
404  const char* prefix)
405 {
406  REQUIRE(n>=0, "Vector size can not be negative.\n");
407  SG_SPRINT("%s%s=[", prefix, name)
408  for (int32_t i=0; i<n; i++)
409  SG_SPRINT("%s%d%s", prefix, vector[i], i==n-1? "" : ",")
410  SG_SPRINT("%s]\n", prefix)
411 }
412 
413 template <>
414 void SGVector<uint16_t>::display_vector(const uint16_t* vector, int32_t n, const char* name,
415  const char* prefix)
416 {
417  REQUIRE(n>=0, "Vector size can not be negative.\n");
418  SG_SPRINT("%s%s=[", prefix, name)
419  for (int32_t i=0; i<n; i++)
420  SG_SPRINT("%s%u%s", prefix, vector[i], i==n-1? "" : ",")
421  SG_SPRINT("%s]\n", prefix)
422 }
423 
424 template <>
425 void SGVector<int16_t>::display_vector(const int16_t* 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], i==n-1? "" : ",")
432  SG_SPRINT("%s]\n", prefix)
433 }
434 
435 template <>
436 void SGVector<int32_t>::display_vector(const int32_t* 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%d%s", prefix, vector[i], i==n-1? "" : ",")
443  SG_SPRINT("%s]\n", prefix)
444 }
445 
446 template <>
447 void SGVector<uint32_t>::display_vector(const uint32_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 
458 template <>
459 void SGVector<int64_t>::display_vector(const int64_t* vector, int32_t n, const char* name,
460  const char* prefix)
461 {
462  REQUIRE(n>=0, "Vector size can not be negative.\n");
463  SG_SPRINT("%s%s=[", prefix, name)
464  for (int32_t i=0; i<n; i++)
465  SG_SPRINT("%s%lld%s", prefix, vector[i], i==n-1? "" : ",")
466  SG_SPRINT("%s]\n", prefix)
467 }
468 
469 template <>
470 void SGVector<uint64_t>::display_vector(const uint64_t* vector, int32_t n, const char* name,
471  const char* prefix)
472 {
473  REQUIRE(n>=0, "Vector size can not be negative.\n");
474  SG_SPRINT("%s%s=[", prefix, name)
475  for (int32_t i=0; i<n; i++)
476  SG_SPRINT("%s%llu%s", prefix, vector[i], i==n-1? "" : ",")
477  SG_SPRINT("%s]\n", prefix)
478 }
479 
480 template <>
481 void SGVector<float32_t>::display_vector(const float32_t* vector, int32_t n, const char* name,
482  const char* prefix)
483 {
484  REQUIRE(n>=0, "Vector size can not be negative.\n");
485  SG_SPRINT("%s%s=[", prefix, name)
486  for (int32_t i=0; i<n; i++)
487  SG_SPRINT("%s%g%s", prefix, vector[i], i==n-1? "" : ",")
488  SG_SPRINT("%s]\n", prefix)
489 }
490 
491 template <>
492 void SGVector<float64_t>::display_vector(const float64_t* vector, int32_t n, const char* name,
493  const char* prefix)
494 {
495  REQUIRE(n>=0, "Vector size can not be negative.\n");
496  SG_SPRINT("%s%s=[", prefix, name)
497  for (int32_t i=0; i<n; i++)
498  SG_SPRINT("%s%.18g%s", prefix, vector[i], i==n-1? "" : ",")
499  SG_SPRINT("%s]\n", prefix)
500 }
501 
502 template <>
503 void SGVector<floatmax_t>::display_vector(const floatmax_t* vector, int32_t n,
504  const char* name, const char* prefix)
505 {
506  REQUIRE(n>=0, "Vector size can not be negative.\n");
507  SG_SPRINT("%s%s=[", prefix, name)
508  for (int32_t i=0; i<n; i++)
509  {
510  SG_SPRINT("%s%.36Lg%s", prefix, (long double) vector[i],
511  i==n-1? "" : ",");
512  }
513  SG_SPRINT("%s]\n", prefix)
514 }
515 
516 template <>
518  const char* name, const char* prefix)
519 {
520  REQUIRE(n>=0, "Vector size can not be negative.\n");
521  SG_SPRINT("%s%s=[", prefix, name)
522  for (int32_t i=0; i<n; i++)
523  {
524  SG_SPRINT("%s(%.36lg+i%.36lg)%s", prefix, vector[i].real(),
525  vector[i].imag(), i==n-1? "" : ",");
526  }
527  SG_SPRINT("%s]\n", prefix)
528 }
529 
530 template <class T>
532  const T scalar, const T* vec2, int32_t n)
533 {
534  for (int32_t i=0; i<n; i++)
535  vec1[i]+=scalar*vec2[i];
536 }
537 
538 template <>
540  float64_t scalar, const float64_t* vec2, int32_t n)
541 {
542 #ifdef HAVE_LAPACK
543  int32_t skip=1;
544  cblas_daxpy(n, scalar, vec2, skip, vec1, skip);
545 #else
546  for (int32_t i=0; i<n; i++)
547  vec1[i]+=scalar*vec2[i];
548 #endif
549 }
550 
551 template <>
553  float32_t scalar, const float32_t* vec2, int32_t n)
554 {
555 #ifdef HAVE_LAPACK
556  int32_t skip=1;
557  cblas_saxpy(n, scalar, vec2, skip, vec1, skip);
558 #else
559  for (int32_t i=0; i<n; i++)
560  vec1[i]+=scalar*vec2[i];
561 #endif
562 }
563 
564 template <class T>
565  void SGVector<T>::random_vector(T* vec, int32_t len, T min_value, T max_value)
566  {
567  for (int32_t i=0; i<len; i++)
568  vec[i]=CMath::random(min_value, max_value);
569  }
570 
571 template <>
573  complex128_t min_value, complex128_t max_value)
574 {
576 }
577 
578 template <>
579 bool SGVector<bool>::twonorm(const bool* x, int32_t len)
580 {
582  return false;
583 }
584 
585 template <>
586 char SGVector<char>::twonorm(const char* x, int32_t len)
587 {
589  return '\0';
590 }
591 
592 template <>
593 int8_t SGVector<int8_t>::twonorm(const int8_t* x, int32_t len)
594 {
595  float64_t result=0;
596  for (int32_t i=0; i<len; i++)
597  result+=x[i]*x[i];
598 
599  return CMath::sqrt(result);
600 }
601 
602 template <>
603 uint8_t SGVector<uint8_t>::twonorm(const uint8_t* x, int32_t len)
604 {
605  float64_t result=0;
606  for (int32_t i=0; i<len; i++)
607  result+=x[i]*x[i];
608 
609  return CMath::sqrt(result);
610 }
611 
612 template <>
613 int16_t SGVector<int16_t>::twonorm(const int16_t* x, int32_t len)
614 {
615  float64_t result=0;
616  for (int32_t i=0; i<len; i++)
617  result+=x[i]*x[i];
618 
619  return CMath::sqrt(result);
620 }
621 
622 template <>
623 uint16_t SGVector<uint16_t>::twonorm(const uint16_t* x, int32_t len)
624 {
625  float64_t result=0;
626  for (int32_t i=0; i<len; i++)
627  result+=x[i]*x[i];
628 
629  return CMath::sqrt(result);
630 }
631 
632 template <>
633 int32_t SGVector<int32_t>::twonorm(const int32_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 uint32_t SGVector<uint32_t>::twonorm(const uint32_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 int64_t SGVector<int64_t>::twonorm(const int64_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 uint64_t SGVector<uint64_t>::twonorm(const uint64_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 <>
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 <>
684 {
685  float64_t norm = 0.0;
686 #ifdef HAVE_LAPACK
687  norm = cblas_dnrm2(n, v, 1);
688 #else
689  norm = CMath::sqrt(CMath::dot(v, v, n));
690 #endif
691  return norm;
692 }
693 
694 template <>
696 {
697  float64_t result=0;
698  for (int32_t i=0; i<len; i++)
699  result+=x[i]*x[i];
700 
701  return CMath::sqrt(result);
702 }
703 
704 template <>
706 {
707  complex128_t result(0.0);
708  for (int32_t i=0; i<len; i++)
709  result+=x[i]*x[i];
710 
711  return CMath::sqrt(result);
712 }
713 
714 template <class T>
715 float64_t SGVector<T>::onenorm(T* x, int32_t len)
716 {
717  float64_t result=0;
718  for (int32_t i=0;i<len; ++i)
719  result+=CMath::abs(x[i]);
720 
721  return result;
722 }
723 
725 template <class T>
726 T SGVector<T>::qsq(T* x, int32_t len, float64_t q)
727 {
728  float64_t result=0;
729  for (int32_t i=0; i<len; i++)
730  result+=CMath::pow(fabs(x[i]), q);
731 
732  return result;
733 }
734 
735 template <>
737 {
739  return complex128_t(0.0);
740 }
741 
743 template <class T>
744 T SGVector<T>::qnorm(T* x, int32_t len, float64_t q)
745 {
746  REQUIRE(q!=0, "Q should be non-zero for calculating qnorm\n");
747  return CMath::pow((float64_t) qsq(x, len, q), 1.0/q);
748 }
749 
750 template <>
752 {
754  return complex128_t(0.0);
755 }
756 
758 template <class T>
759 T SGVector<T>::sum_abs(T* vec, int32_t len)
760 {
761  T result=0;
762  for (int32_t i=0; i<len; i++)
763  result+=CMath::abs(vec[i]);
764 
765  return result;
766 }
767 
768 #if HAVE_LAPACK
769 template <>
771 {
772  float64_t result=0;
773  result = cblas_dasum(len, vec, 1);
774  return result;
775 }
776 
777 template <>
779 {
780  float32_t result=0;
781  result = cblas_sasum(len, vec, 1);
782  return result;
783 }
784 #endif
785 
786 template <class T>
787 int32_t SGVector<T>::unique(T* output, int32_t size)
788 {
789  CMath::qsort<T>(output, size);
790  int32_t j=0;
791 
792  for (int32_t i=0; i<size; i++)
793  {
794  if (i==0 || output[i]!=output[i-1])
795  output[j++]=output[i];
796  }
797  return j;
798 }
799 
800 template <>
801 int32_t SGVector<complex128_t>::unique(complex128_t* output, int32_t size)
802 {
803  int32_t j=0;
804  SG_SERROR("SGVector::unique():: Not supported for complex128_t\n");
805  return j;
806 }
807 
808 template <class T>
810 {
811  SGVector<index_t> idx(vlen);
812  index_t k=0;
813 
814  for (index_t i=0; i < vlen; ++i)
815  if (vector[i] == elem)
816  idx[k++] = i;
817  idx.vlen = k;
818  return idx;
819 }
820 
821 template<class T>
822 void SGVector<T>::scale_vector(T alpha, T* vec, int32_t len)
823 {
824  for (int32_t i=0; i<len; i++)
825  vec[i]*=alpha;
826 }
827 
828 #ifdef HAVE_LAPACK
829 template<>
831 {
832  cblas_dscal(len, alpha, vec, 1);
833 }
834 
835 template<>
837 {
838  cblas_sscal(len, alpha, vec, 1);
839 }
840 #endif
841 
842 template<class T>
843 void SGVector<T>::scale(T alpha)
844 {
845  scale_vector(alpha, vector, vlen);
846 }
847 
848 template<class T> void SGVector<T>::load(CFile* loader)
849 {
850  REQUIRE(loader, "Require a valid 'c FILE pointer'\n");
851  unref();
852 
854  SGVector<T> vec;
855  loader->get_vector(vec.vector, vec.vlen);
856  copy_data(vec);
857  copy_refcount(vec);
858  ref();
860 }
861 
862 template<>
864 {
865  SG_SERROR("SGVector::load():: Not supported for complex128_t\n");
866 }
867 
868 template<class T> void SGVector<T>::save(CFile* saver)
869 {
870  REQUIRE(saver, "Requires a valid 'c FILE pointer'\n");
871 
873  saver->set_vector(vector, vlen);
875 }
876 
877 template<>
879 {
880  SG_SERROR("SGVector::save():: Not supported for complex128_t\n");
881 }
882 
884 {
885  SGVector<float64_t> real(vlen);
886  for (int32_t i=0; i<vlen; i++)
887  real[i]=CMath::real(vector[i]);
888  return real;
889 }
890 
892 {
893  SGVector<float64_t> imag(vlen);
894  for (int32_t i=0; i<vlen; i++)
895  imag[i]=CMath::imag(vector[i]);
896  return imag;
897 }
898 
899 template <class T>
901  index_t nrows, index_t ncols, bool fortran_order)
902 {
903  if (nrows*ncols>vector.size())
904  SG_SERROR("SGVector::convert_to_matrix():: Dimensions mismatch\n");
905 
906  T* data=NULL;
907  SGVector<T>::convert_to_matrix(data, nrows, ncols, vector.vector, vector.vlen, fortran_order);
908 
909  SGMatrix<T> matrix=SGMatrix<T>(data, nrows, ncols);
910  return matrix;
911 }
912 
913 template <class T>
914 void SGVector<T>::convert_to_matrix(T*& matrix, index_t nrows, index_t ncols, const T* vector, int32_t vlen, bool fortran_order)
915 {
916  if (nrows*ncols>vlen)
917  SG_SERROR("SGVector::convert_to_matrix():: Dimensions mismatch\n");
918 
919  if (matrix!=NULL)
920  SG_FREE(matrix);
921  matrix=SG_MALLOC(T, nrows*ncols);
922 
923  if (fortran_order)
924  {
925  for (index_t i=0; i<ncols*nrows; i++)
926  matrix[i]=vector[i];
927  }
928  else
929  {
930  for (index_t i=0; i<nrows; i++)
931  {
932  for (index_t j=0; j<ncols; j++)
933  matrix[i+j*nrows]=vector[j+i*ncols];
934  }
935  }
936 }
937 
938 #define UNDEFINED(function, type) \
939 template <> \
940 SGVector<float64_t> SGVector<type>::function() \
941 { \
942  SG_SERROR("SGVector::%s():: Not supported for %s\n", \
943  #function, #type); \
944  SGVector<float64_t> ret(vlen); \
945  return ret; \
946 }
947 
948 UNDEFINED(get_real, bool)
949 UNDEFINED(get_real, char)
950 UNDEFINED(get_real, int8_t)
951 UNDEFINED(get_real, uint8_t)
952 UNDEFINED(get_real, int16_t)
953 UNDEFINED(get_real, uint16_t)
954 UNDEFINED(get_real, int32_t)
955 UNDEFINED(get_real, uint32_t)
956 UNDEFINED(get_real, int64_t)
957 UNDEFINED(get_real, uint64_t)
958 UNDEFINED(get_real, float32_t)
959 UNDEFINED(get_real, float64_t)
960 UNDEFINED(get_real, floatmax_t)
961 UNDEFINED(get_imag, bool)
962 UNDEFINED(get_imag, char)
963 UNDEFINED(get_imag, int8_t)
964 UNDEFINED(get_imag, uint8_t)
965 UNDEFINED(get_imag, int16_t)
966 UNDEFINED(get_imag, uint16_t)
967 UNDEFINED(get_imag, int32_t)
968 UNDEFINED(get_imag, uint32_t)
969 UNDEFINED(get_imag, int64_t)
970 UNDEFINED(get_imag, uint64_t)
971 UNDEFINED(get_imag, float32_t)
972 UNDEFINED(get_imag, float64_t)
973 UNDEFINED(get_imag, floatmax_t)
974 #undef UNDEFINED
975 
976 template class SGVector<bool>;
977 template class SGVector<char>;
978 template class SGVector<int8_t>;
979 template class SGVector<uint8_t>;
980 template class SGVector<int16_t>;
981 template class SGVector<uint16_t>;
982 template class SGVector<int32_t>;
983 template class SGVector<uint32_t>;
984 template class SGVector<int64_t>;
985 template class SGVector<uint64_t>;
986 template class SGVector<float32_t>;
987 template class SGVector<float64_t>;
988 template class SGVector<floatmax_t>;
989 template class SGVector<complex128_t>;
990 }
991 
992 #undef COMPLEX128_ERROR_NOARG
993 #undef COMPLEX128_ERROR_ONEARG
994 #undef COMPLEX128_ERROR_TWOARGS
995 #undef COMPLEX128_ERROR_THREEARGS
#define SG_RESET_LOCALE
Definition: SGIO.h:86
double norm(double *v, double p, int n)
Definition: epph.cpp:452
std::complex< float64_t > complex128_t
Definition: common.h:67
int32_t index_t
Definition: common.h:62
Vector::Scalar dot(Vector a, Vector b)
Definition: Redux.h:56
#define COMPLEX128_ERROR_TWOARGS(function)
Definition: SGVector.cpp:53
void set(SGVector< T > orig)
Definition: SGVector.cpp:98
virtual void init_data()
Definition: SGVector.cpp:326
#define REQUIRE(x,...)
Definition: SGIO.h:206
#define SG_SNOTIMPLEMENTED
Definition: SGIO.h:198
#define SG_SET_LOCALE_C
Definition: SGIO.h:85
shogun matrix
void display_vector(const char *name="vector", const char *prefix="") const
Definition: SGVector.cpp:356
int32_t size() const
Definition: SGVector.h:115
index_t vlen
Definition: SGVector.h:494
#define UNDEFINED(function, type)
Definition: SGVector.cpp:938
#define SG_SPRINT(...)
Definition: SGIO.h:180
shogun vector
virtual void get_vector(bool *&vector, int32_t &len)
Definition: File.cpp:81
void add(Matrix A, Matrix B, Matrix C, typename Matrix::Scalar alpha=1.0, typename Matrix::Scalar beta=1.0)
Definition: Core.h:65
shogun reference count managed data
double float64_t
Definition: common.h:50
long double floatmax_t
Definition: common.h:51
A File access base class.
Definition: File.h:34
virtual ~SGVector()
Definition: SGVector.cpp:104
SGSparseVectorEntry< T > * features
float float32_t
Definition: common.h:49
#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
#define SG_SERROR(...)
Definition: SGIO.h:179
void scale(Matrix A, Matrix B, typename Matrix::Scalar alpha)
Definition: Core.h:93
template class SGSparseVector The assumtion is that the stored SGSparseVectorEntry* vector is orde...
virtual void copy_data(const SGReferencedData &orig)
Definition: SGVector.cpp:319
virtual void set_vector(const bool *vector, int32_t len)
Definition: File.cpp:95
void set_const(float32_tconst_elem)
void add(const SGVector< T > x)
Definition: SGVector.cpp:281

SHOGUN Machine Learning Toolbox - Documentation