SHOGUN  4.2.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 template <class T>
111 : SGReferencedData(false), vector(vec.data()), vlen(vec.size())
112 {
113 
114 }
115 
116 template <class T>
118 : SGReferencedData(false), vector(vec.data()), vlen(vec.size())
119 {
120 
121 }
122 
123 template <class T>
125 {
126  return EigenVectorXtMap(vector, vlen);
127 }
128 
129 template <class T>
131 {
132  return EigenRowVectorXtMap(vector, vlen);
133 }
134 
135 template<class T>
137 {
138  if (vector && vlen)
139  set_const(0);
140 }
141 
142 template <>
144 {
145  if (vector && vlen)
146  set_const(complex128_t(0.0));
147 }
148 
149 template<class T>
150 void SGVector<T>::set_const(T const_elem)
151 {
152  for (index_t i=0; i<vlen; i++)
153  vector[i]=const_elem ;
154 }
155 
156 #if HAVE_CATLAS
157 template<>
159 {
160  catlas_dset(vlen, const_elem, vector, 1);
161 }
162 
163 template<>
165 {
166  catlas_sset(vlen, const_elem, vector, 1);
167 }
168 #endif // HAVE_CATLAS
169 
170 template<class T>
172 {
173  range_fill_vector(vector, vlen, start);
174 }
175 
177 
178 template<class T>
179 void SGVector<T>::random(T min_value, T max_value)
180 {
181  random_vector(vector, vlen, min_value, max_value);
182 }
183 
185 
186 template <class T>
187 index_t SGVector<T>::find_position_to_insert(T element)
188 {
189  index_t i;
190  for (i=0; i<vlen; ++i)
191  {
192  if (vector[i]>element)
193  break;
194  }
195  return i;
196 }
197 
198 template <>
200 {
201  SG_SERROR("SGVector::find_position_to_insert():: \
202  Not supported for complex128_t\n");
203  return index_t(-1);
204 }
205 
206 template<class T>
208 {
209  return SGVector<T>(clone_vector(vector, vlen), vlen);
210 }
211 
212 template<class T>
213 T* SGVector<T>::clone_vector(const T* vec, int32_t len)
214 {
215  T* result = SG_MALLOC(T, len);
216  memcpy(result, vec, sizeof(T)*len);
217  return result;
218 }
219 
220 template<class T>
221 void SGVector<T>::fill_vector(T* vec, int32_t len, T value)
222 {
223  for (int32_t i=0; i<len; i++)
224  vec[i]=value;
225 }
226 
227 template<class T>
228 void SGVector<T>::range_fill_vector(T* vec, int32_t len, T start)
229 {
230  for (int32_t i=0; i<len; i++)
231  vec[i]=i+start;
232 }
233 
234 template <>
236  int32_t len, complex128_t start)
237 {
238  SG_SERROR("SGVector::range_fill_vector():: \
239  Not supported for complex128_t\n");
240 }
241 
242 template<class T>
244 {
245  REQUIRE(vector && (index>=0) && (index<vlen), "Provided index (%d) must be between 0 and %d.\n", index, vlen);
246  return vector[index];
247 }
248 
249 template<class T>
250 void SGVector<T>::set_element(const T& p_element, index_t index)
251 {
252  REQUIRE(vector && (index>=0) && (index<vlen), "Provided index (%d) must be between 0 and %d.\n", index, vlen);
253  vector[index]=p_element;
254 }
255 
256 template<class T>
258 {
259  vector=SG_REALLOC(T, vector, vlen, n);
260 
261  if (n > vlen)
262  memset(&vector[vlen], 0, (n-vlen)*sizeof(T));
263  vlen=n;
264 }
265 
267 template<class T>
269 {
270  REQUIRE(x.vector && vector, "Addition possible for only non-null vectors.\n");
271  REQUIRE(x.vlen == vlen, "Length of the two vectors to be added should be same. [V(%d) + V(%d)]\n", vlen, x.vlen);
272 
273  SGVector<T> result=clone();
274  result.add(x);
275  return result;
276 }
277 
278 template<class T>
280 {
281  REQUIRE(x.vector && vector, "Addition possible for only non-null vectors.\n");
282  REQUIRE(x.vlen == vlen, "Length of the two vectors to be added should be same. [V(%d) + V(%d)]\n", vlen, x.vlen);
283 
284  for (int32_t i=0; i<vlen; i++)
285  vector[i]+=x.vector[i];
286 }
287 
288 template<class T>
289 void SGVector<T>::add(const T x)
290 {
291  REQUIRE(vector, "Addition possible for only non-null vectors.\n");
292  for (int32_t i=0; i<vlen; i++)
293  vector[i]+=x;
294 }
295 
296 template<class T>
298 {
299  if (x.features)
300  {
301  for (int32_t i=0; i < x.num_feat_entries; i++)
302  {
303  index_t idx = x.features[i].feat_index;
304  REQUIRE(idx < vlen, "Feature index should be less than %d.\n", vlen);
305  vector[idx] += x.features[i].entry;
306  }
307  }
308 }
309 
310 template<class T>
312 {
313  SG_SPRINT("SGVector '%p' of size: %d\n", vector, vlen)
314 }
315 
316 template<class T>
318 {
319  vector=((SGVector*)(&orig))->vector;
320  vlen=((SGVector*)(&orig))->vlen;
321 }
322 
323 template<class T>
325 {
326  vector=NULL;
327  vlen=0;
328 }
329 
330 template<class T>
332 {
333  SG_FREE(vector);
334  vector=NULL;
335  vlen=0;
336 }
337 
338 template<class T>
340 {
341  if (other.vlen!=vlen)
342  return false;
343 
344  for (index_t i=0; i<vlen; ++i)
345  {
346  if (other.vector[i]!=vector[i])
347  return false;
348  }
349 
350  return true;
351 }
352 
353 template<class T>
354 void SGVector<T>::display_vector(const char* name,
355  const char* prefix) const
356 {
357  display_vector(vector, vlen, name, prefix);
358 }
359 
360 template <class T>
361 void SGVector<T>::display_vector(const SGVector<T> vector, const char* name,
362  const char* prefix)
363 {
364  vector.display_vector(prefix);
365 }
366 
367 template <>
368 void SGVector<bool>::display_vector(const bool* vector, int32_t n, const char* name,
369  const char* prefix)
370 {
371  REQUIRE(n>=0, "Vector size can not be negative.\n");
372  SG_SPRINT("%s%s=[", prefix, name)
373  for (int32_t i=0; i<n; i++)
374  SG_SPRINT("%s%d%s", prefix, vector[i] ? 1 : 0, i==n-1? "" : ",")
375  SG_SPRINT("%s]\n", prefix)
376 }
377 
378 template <>
379 void SGVector<char>::display_vector(const char* vector, int32_t n, const char* name,
380  const char* prefix)
381 {
382  REQUIRE(n>=0, "Vector size can not be negative.\n");
383  SG_SPRINT("%s%s=[", prefix, name)
384  for (int32_t i=0; i<n; i++)
385  SG_SPRINT("%s%c%s", prefix, vector[i], i==n-1? "" : ",")
386  SG_SPRINT("%s]\n", prefix)
387 }
388 
389 template <>
390 void SGVector<uint8_t>::display_vector(const uint8_t* vector, int32_t n, const char* name,
391  const char* prefix)
392 {
393  REQUIRE(n>=0, "Vector size can not be negative.\n");
394  SG_SPRINT("%s%s=[", prefix, name)
395  for (int32_t i=0; i<n; i++)
396  SG_SPRINT("%s%u%s", prefix, vector[i], i==n-1? "" : ",")
397  SG_SPRINT("%s]\n", prefix)
398 }
399 
400 template <>
401 void SGVector<int8_t>::display_vector(const int8_t* vector, int32_t n, const char* name,
402  const char* prefix)
403 {
404  REQUIRE(n>=0, "Vector size can not be negative.\n");
405  SG_SPRINT("%s%s=[", prefix, name)
406  for (int32_t i=0; i<n; i++)
407  SG_SPRINT("%s%d%s", prefix, vector[i], i==n-1? "" : ",")
408  SG_SPRINT("%s]\n", prefix)
409 }
410 
411 template <>
412 void SGVector<uint16_t>::display_vector(const uint16_t* vector, int32_t n, const char* name,
413  const char* prefix)
414 {
415  REQUIRE(n>=0, "Vector size can not be negative.\n");
416  SG_SPRINT("%s%s=[", prefix, name)
417  for (int32_t i=0; i<n; i++)
418  SG_SPRINT("%s%u%s", prefix, vector[i], i==n-1? "" : ",")
419  SG_SPRINT("%s]\n", prefix)
420 }
421 
422 template <>
423 void SGVector<int16_t>::display_vector(const int16_t* vector, int32_t n, const char* name,
424  const char* prefix)
425 {
426  REQUIRE(n>=0, "Vector size can not be negative.\n");
427  SG_SPRINT("%s%s=[", prefix, name)
428  for (int32_t i=0; i<n; i++)
429  SG_SPRINT("%s%d%s", prefix, vector[i], i==n-1? "" : ",")
430  SG_SPRINT("%s]\n", prefix)
431 }
432 
433 template <>
434 void SGVector<int32_t>::display_vector(const int32_t* vector, int32_t n, const char* name,
435  const char* prefix)
436 {
437  REQUIRE(n>=0, "Vector size can not be negative.\n");
438  SG_SPRINT("%s%s=[", prefix, name)
439  for (int32_t i=0; i<n; i++)
440  SG_SPRINT("%s%d%s", prefix, vector[i], i==n-1? "" : ",")
441  SG_SPRINT("%s]\n", prefix)
442 }
443 
444 template <>
445 void SGVector<uint32_t>::display_vector(const uint32_t* vector, int32_t n, const char* name,
446  const char* prefix)
447 {
448  REQUIRE(n>=0, "Vector size can not be negative.\n");
449  SG_SPRINT("%s%s=[", prefix, name)
450  for (int32_t i=0; i<n; i++)
451  SG_SPRINT("%s%u%s", prefix, vector[i], i==n-1? "" : ",")
452  SG_SPRINT("%s]\n", prefix)
453 }
454 
455 
456 template <>
457 void SGVector<int64_t>::display_vector(const int64_t* vector, int32_t n, const char* name,
458  const char* prefix)
459 {
460  REQUIRE(n>=0, "Vector size can not be negative.\n");
461  SG_SPRINT("%s%s=[", prefix, name)
462  for (int32_t i=0; i<n; i++)
463  SG_SPRINT("%s%lld%s", prefix, vector[i], i==n-1? "" : ",")
464  SG_SPRINT("%s]\n", prefix)
465 }
466 
467 template <>
468 void SGVector<uint64_t>::display_vector(const uint64_t* vector, int32_t n, const char* name,
469  const char* prefix)
470 {
471  REQUIRE(n>=0, "Vector size can not be negative.\n");
472  SG_SPRINT("%s%s=[", prefix, name)
473  for (int32_t i=0; i<n; i++)
474  SG_SPRINT("%s%llu%s", prefix, vector[i], i==n-1? "" : ",")
475  SG_SPRINT("%s]\n", prefix)
476 }
477 
478 template <>
479 void SGVector<float32_t>::display_vector(const float32_t* vector, int32_t n, const char* name,
480  const char* prefix)
481 {
482  REQUIRE(n>=0, "Vector size can not be negative.\n");
483  SG_SPRINT("%s%s=[", prefix, name)
484  for (int32_t i=0; i<n; i++)
485  SG_SPRINT("%s%g%s", prefix, vector[i], i==n-1? "" : ",")
486  SG_SPRINT("%s]\n", prefix)
487 }
488 
489 template <>
490 void SGVector<float64_t>::display_vector(const float64_t* vector, int32_t n, const char* name,
491  const char* prefix)
492 {
493  REQUIRE(n>=0, "Vector size can not be negative.\n");
494  SG_SPRINT("%s%s=[", prefix, name)
495  for (int32_t i=0; i<n; i++)
496  SG_SPRINT("%s%.18g%s", prefix, vector[i], i==n-1? "" : ",")
497  SG_SPRINT("%s]\n", prefix)
498 }
499 
500 template <>
501 void SGVector<floatmax_t>::display_vector(const floatmax_t* vector, int32_t n,
502  const char* name, const char* prefix)
503 {
504  REQUIRE(n>=0, "Vector size can not be negative.\n");
505  SG_SPRINT("%s%s=[", prefix, name)
506  for (int32_t i=0; i<n; i++)
507  {
508  SG_SPRINT("%s%.36Lg%s", prefix, (long double) vector[i],
509  i==n-1? "" : ",");
510  }
511  SG_SPRINT("%s]\n", prefix)
512 }
513 
514 template <>
516  const char* name, const char* prefix)
517 {
518  REQUIRE(n>=0, "Vector size can not be negative.\n");
519  SG_SPRINT("%s%s=[", prefix, name)
520  for (int32_t i=0; i<n; i++)
521  {
522  SG_SPRINT("%s(%.36lg+i%.36lg)%s", prefix, vector[i].real(),
523  vector[i].imag(), i==n-1? "" : ",");
524  }
525  SG_SPRINT("%s]\n", prefix)
526 }
527 
528 template <class T>
530  const T scalar, const T* vec2, int32_t n)
531 {
532  for (int32_t i=0; i<n; i++)
533  vec1[i]+=scalar*vec2[i];
534 }
535 
536 template <>
538  float64_t scalar, const float64_t* vec2, int32_t n)
539 {
540 #ifdef HAVE_LAPACK
541  int32_t skip=1;
542  cblas_daxpy(n, scalar, vec2, skip, vec1, skip);
543 #else
544  for (int32_t i=0; i<n; i++)
545  vec1[i]+=scalar*vec2[i];
546 #endif
547 }
548 
549 template <>
551  float32_t scalar, const float32_t* vec2, int32_t n)
552 {
553 #ifdef HAVE_LAPACK
554  int32_t skip=1;
555  cblas_saxpy(n, scalar, vec2, skip, vec1, skip);
556 #else
557  for (int32_t i=0; i<n; i++)
558  vec1[i]+=scalar*vec2[i];
559 #endif
560 }
561 
562 template <class T>
563  void SGVector<T>::random_vector(T* vec, int32_t len, T min_value, T max_value)
564  {
565  for (int32_t i=0; i<len; i++)
566  vec[i]=CMath::random(min_value, max_value);
567  }
568 
569 template <>
571  complex128_t min_value, complex128_t max_value)
572 {
574 }
575 
576 template <>
577 bool SGVector<bool>::twonorm(const bool* x, int32_t len)
578 {
580  return false;
581 }
582 
583 template <>
584 char SGVector<char>::twonorm(const char* x, int32_t len)
585 {
587  return '\0';
588 }
589 
590 template <>
591 int8_t SGVector<int8_t>::twonorm(const int8_t* x, int32_t len)
592 {
593  float64_t result=0;
594  for (int32_t i=0; i<len; i++)
595  result+=x[i]*x[i];
596 
597  return CMath::sqrt(result);
598 }
599 
600 template <>
601 uint8_t SGVector<uint8_t>::twonorm(const uint8_t* x, int32_t len)
602 {
603  float64_t result=0;
604  for (int32_t i=0; i<len; i++)
605  result+=x[i]*x[i];
606 
607  return CMath::sqrt(result);
608 }
609 
610 template <>
611 int16_t SGVector<int16_t>::twonorm(const int16_t* x, int32_t len)
612 {
613  float64_t result=0;
614  for (int32_t i=0; i<len; i++)
615  result+=x[i]*x[i];
616 
617  return CMath::sqrt(result);
618 }
619 
620 template <>
621 uint16_t SGVector<uint16_t>::twonorm(const uint16_t* x, int32_t len)
622 {
623  float64_t result=0;
624  for (int32_t i=0; i<len; i++)
625  result+=x[i]*x[i];
626 
627  return CMath::sqrt(result);
628 }
629 
630 template <>
631 int32_t SGVector<int32_t>::twonorm(const int32_t* x, int32_t len)
632 {
633  float64_t result=0;
634  for (int32_t i=0; i<len; i++)
635  result+=x[i]*x[i];
636 
637  return CMath::sqrt(result);
638 }
639 
640 template <>
641 uint32_t SGVector<uint32_t>::twonorm(const uint32_t* x, int32_t len)
642 {
643  float64_t result=0;
644  for (int32_t i=0; i<len; i++)
645  result+=x[i]*x[i];
646 
647  return CMath::sqrt(result);
648 }
649 
650 template <>
651 int64_t SGVector<int64_t>::twonorm(const int64_t* x, int32_t len)
652 {
653  float64_t result=0;
654  for (int32_t i=0; i<len; i++)
655  result+=x[i]*x[i];
656 
657  return CMath::sqrt(result);
658 }
659 
660 template <>
661 uint64_t SGVector<uint64_t>::twonorm(const uint64_t* x, int32_t len)
662 {
663  float64_t result=0;
664  for (int32_t i=0; i<len; i++)
665  result+=x[i]*x[i];
666 
667  return CMath::sqrt(result);
668 }
669 
670 template <>
672 {
673  float64_t result=0;
674  for (int32_t i=0; i<len; i++)
675  result+=x[i]*x[i];
676 
677  return CMath::sqrt(result);
678 }
679 
680 template <>
682 {
683  float64_t norm = 0.0;
684 #ifdef HAVE_LAPACK
685  norm = cblas_dnrm2(n, v, 1);
686 #else
687  norm = CMath::sqrt(CMath::dot(v, v, n));
688 #endif
689  return norm;
690 }
691 
692 template <>
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 <>
704 {
705  complex128_t result(0.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 <class T>
713 float64_t SGVector<T>::onenorm(T* x, int32_t len)
714 {
715  float64_t result=0;
716  for (int32_t i=0;i<len; ++i)
717  result+=CMath::abs(x[i]);
718 
719  return result;
720 }
721 
723 template <class T>
724 T SGVector<T>::qsq(T* x, int32_t len, float64_t q)
725 {
726  float64_t result=0;
727  for (int32_t i=0; i<len; i++)
728  result+=CMath::pow(fabs(x[i]), q);
729 
730  return result;
731 }
732 
733 template <>
735 {
737  return complex128_t(0.0);
738 }
739 
741 template <class T>
742 T SGVector<T>::qnorm(T* x, int32_t len, float64_t q)
743 {
744  REQUIRE(q!=0, "Q should be non-zero for calculating qnorm\n");
745  return CMath::pow((float64_t) qsq(x, len, q), 1.0/q);
746 }
747 
748 template <>
750 {
752  return complex128_t(0.0);
753 }
754 
756 template <class T>
757 T SGVector<T>::sum_abs(T* vec, int32_t len)
758 {
759  T result=0;
760  for (int32_t i=0; i<len; i++)
761  result+=CMath::abs(vec[i]);
762 
763  return result;
764 }
765 
766 #if HAVE_LAPACK
767 template <>
769 {
770  float64_t result=0;
771  result = cblas_dasum(len, vec, 1);
772  return result;
773 }
774 
775 template <>
777 {
778  float32_t result=0;
779  result = cblas_sasum(len, vec, 1);
780  return result;
781 }
782 #endif
783 
784 template <class T>
785 int32_t SGVector<T>::unique(T* output, int32_t size)
786 {
787  CMath::qsort<T>(output, size);
788  int32_t j=0;
789 
790  for (int32_t i=0; i<size; i++)
791  {
792  if (i==0 || output[i]!=output[i-1])
793  output[j++]=output[i];
794  }
795  return j;
796 }
797 
798 template <>
799 int32_t SGVector<complex128_t>::unique(complex128_t* output, int32_t size)
800 {
801  int32_t j=0;
802  SG_SERROR("SGVector::unique():: Not supported for complex128_t\n");
803  return j;
804 }
805 
806 template <class T>
808 {
809  SGVector<index_t> idx(vlen);
810  index_t k=0;
811 
812  for (index_t i=0; i < vlen; ++i)
813  if (vector[i] == elem)
814  idx[k++] = i;
815  idx.vlen = k;
816  return idx;
817 }
818 
819 template<class T>
820 void SGVector<T>::scale_vector(T alpha, T* vec, int32_t len)
821 {
822  for (int32_t i=0; i<len; i++)
823  vec[i]*=alpha;
824 }
825 
826 #ifdef HAVE_LAPACK
827 template<>
829 {
830  cblas_dscal(len, alpha, vec, 1);
831 }
832 
833 template<>
835 {
836  cblas_sscal(len, alpha, vec, 1);
837 }
838 #endif
839 
840 template<class T>
841 void SGVector<T>::scale(T alpha)
842 {
843  scale_vector(alpha, vector, vlen);
844 }
845 
846 template<class T> void SGVector<T>::load(CFile* loader)
847 {
848  REQUIRE(loader, "Require a valid 'c FILE pointer'\n");
849  unref();
850 
852  SGVector<T> vec;
853  loader->get_vector(vec.vector, vec.vlen);
854  copy_data(vec);
855  copy_refcount(vec);
856  ref();
858 }
859 
860 template<>
862 {
863  SG_SERROR("SGVector::load():: Not supported for complex128_t\n");
864 }
865 
866 template<class T> void SGVector<T>::save(CFile* saver)
867 {
868  REQUIRE(saver, "Requires a valid 'c FILE pointer'\n");
869 
871  saver->set_vector(vector, vlen);
873 }
874 
875 template<>
877 {
878  SG_SERROR("SGVector::save():: Not supported for complex128_t\n");
879 }
880 
882 {
883  SGVector<float64_t> real(vlen);
884  for (int32_t i=0; i<vlen; i++)
885  real[i]=CMath::real(vector[i]);
886  return real;
887 }
888 
890 {
891  SGVector<float64_t> imag(vlen);
892  for (int32_t i=0; i<vlen; i++)
893  imag[i]=CMath::imag(vector[i]);
894  return imag;
895 }
896 
897 template <class T>
899  index_t nrows, index_t ncols, bool fortran_order)
900 {
901  if (nrows*ncols>vector.size())
902  SG_SERROR("SGVector::convert_to_matrix():: Dimensions mismatch\n");
903 
904  T* data=NULL;
905  SGVector<T>::convert_to_matrix(data, nrows, ncols, vector.vector, vector.vlen, fortran_order);
906 
907  SGMatrix<T> matrix=SGMatrix<T>(data, nrows, ncols);
908  return matrix;
909 }
910 
911 template <class T>
912 void SGVector<T>::convert_to_matrix(T*& matrix, index_t nrows, index_t ncols, const T* vector, int32_t vlen, bool fortran_order)
913 {
914  if (nrows*ncols>vlen)
915  SG_SERROR("SGVector::convert_to_matrix():: Dimensions mismatch\n");
916 
917  if (matrix!=NULL)
918  SG_FREE(matrix);
919  matrix=SG_MALLOC(T, nrows*ncols);
920 
921  if (fortran_order)
922  {
923  for (index_t i=0; i<ncols*nrows; i++)
924  matrix[i]=vector[i];
925  }
926  else
927  {
928  for (index_t i=0; i<nrows; i++)
929  {
930  for (index_t j=0; j<ncols; j++)
931  matrix[i+j*nrows]=vector[j+i*ncols];
932  }
933  }
934 }
935 
936 #define UNDEFINED(function, type) \
937 template <> \
938 SGVector<float64_t> SGVector<type>::function() \
939 { \
940  SG_SERROR("SGVector::%s():: Not supported for %s\n", \
941  #function, #type); \
942  SGVector<float64_t> ret(vlen); \
943  return ret; \
944 }
945 
946 UNDEFINED(get_real, bool)
947 UNDEFINED(get_real, char)
948 UNDEFINED(get_real, int8_t)
949 UNDEFINED(get_real, uint8_t)
950 UNDEFINED(get_real, int16_t)
951 UNDEFINED(get_real, uint16_t)
952 UNDEFINED(get_real, int32_t)
953 UNDEFINED(get_real, uint32_t)
954 UNDEFINED(get_real, int64_t)
955 UNDEFINED(get_real, uint64_t)
956 UNDEFINED(get_real, float32_t)
957 UNDEFINED(get_real, float64_t)
958 UNDEFINED(get_real, floatmax_t)
959 UNDEFINED(get_imag, bool)
960 UNDEFINED(get_imag, char)
961 UNDEFINED(get_imag, int8_t)
962 UNDEFINED(get_imag, uint8_t)
963 UNDEFINED(get_imag, int16_t)
964 UNDEFINED(get_imag, uint16_t)
965 UNDEFINED(get_imag, int32_t)
966 UNDEFINED(get_imag, uint32_t)
967 UNDEFINED(get_imag, int64_t)
968 UNDEFINED(get_imag, uint64_t)
969 UNDEFINED(get_imag, float32_t)
970 UNDEFINED(get_imag, float64_t)
971 UNDEFINED(get_imag, floatmax_t)
972 #undef UNDEFINED
973 
974 template class SGVector<bool>;
975 template class SGVector<char>;
976 template class SGVector<int8_t>;
977 template class SGVector<uint8_t>;
978 template class SGVector<int16_t>;
979 template class SGVector<uint16_t>;
980 template class SGVector<int32_t>;
981 template class SGVector<uint32_t>;
982 template class SGVector<int64_t>;
983 template class SGVector<uint64_t>;
984 template class SGVector<float32_t>;
985 template class SGVector<float64_t>;
986 template class SGVector<floatmax_t>;
987 template class SGVector<complex128_t>;
988 }
989 
990 #undef COMPLEX128_ERROR_NOARG
991 #undef COMPLEX128_ERROR_ONEARG
992 #undef COMPLEX128_ERROR_TWOARGS
993 #undef COMPLEX128_ERROR_THREEARGS
#define SG_RESET_LOCALE
Definition: SGIO.h:86
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:58
#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:324
#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 range_fill(Matrix A, typename Matrix::Scalar start=0.0)
Definition: Core.h:111
void display_vector(const char *name="vector", const char *prefix="") const
Definition: SGVector.cpp:354
int32_t size() const
Definition: SGVector.h:113
index_t vlen
Definition: SGVector.h:494
#define UNDEFINED(function, type)
Definition: SGVector.cpp:936
#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:66
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:94
template class SGSparseVector The assumtion is that the stored SGSparseVectorEntry* vector is orde...
virtual void copy_data(const SGReferencedData &orig)
Definition: SGVector.cpp:317
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:279

SHOGUN Machine Learning Toolbox - Documentation