SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SGMatrixList.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) 2012 Fernando José Iglesias García
8  * Copyright (C) 2012 Fernando José Iglesias García
9  */
10 
12 
13 namespace shogun {
14 
15 template <class T>
17 {
18  init_data();
19 }
20 
21 template <class T>
22 SGMatrixList<T>::SGMatrixList(SGMatrix<T>* ml, int32_t nmats, bool ref_counting)
23 : SGReferencedData(ref_counting), matrix_list(ml), num_matrices(nmats)
24 {
25 }
26 
27 template <class T>
28 SGMatrixList<T>::SGMatrixList(int32_t nmats, bool ref_counting)
29 : SGReferencedData(ref_counting), num_matrices(nmats)
30 {
32  // Call to SGMatrix default constructor in-place
33  for ( int32_t i = 0 ; i < nmats ; ++i )
34  new (&matrix_list[i]) SGMatrix<T>();
35 }
36 
37 template <class T>
39 {
40  copy_data(orig);
41 }
42 
43 template <class T>
45 {
46  unref();
47 }
48 
49 template <class T>
51 {
52  matrix_list = ((SGMatrixList*) (&orig))->matrix_list;
53  num_matrices = ((SGMatrixList*) (&orig))->num_matrices;
54 }
55 
56 template <class T>
58 {
59  matrix_list = NULL;
60  num_matrices = 0;
61 }
62 
63 template <class T>
65 {
66  cleanup_matrices();
67  SG_FREE(matrix_list);
68  num_matrices = 0;
69  matrix_list = NULL;
70 }
71 
72 template <class T>
74 {
75  if ( matrix_list && num_matrices )
76  {
77  for ( int32_t i = 0 ; i < num_matrices ; ++i )
78  {
79  // Explicit call to the destructor required
80  // due to the use of in-place constructors
81  matrix_list[i].~SGMatrix();
82  }
83  }
84 }
85 
86 template <class T>
87 SGMatrixList<T> SGMatrixList<T>::split(SGMatrix<T> matrix, int32_t num_components)
88 {
89  REQUIRE((matrix.num_cols % num_components) == 0,
90  "The number of columns (%d) must be multiple of the number "
91  "of components (%d).\n",
92  matrix.num_cols, num_components);
93 
94  int32_t new_num_cols = matrix.num_cols / num_components;
95  int32_t start;
96  SGMatrixList<T> out(num_components);
97 
98  for ( int32_t i = 0 ; i < num_components ; ++i )
99  {
100  out[i] = SGMatrix<T>(matrix.num_rows, new_num_cols);
101  start = i*matrix.num_rows*new_num_cols;
102 
103  for ( int32_t row = 0 ; row < matrix.num_rows ; ++row )
104  {
105  for ( int32_t col = 0 ; col < new_num_cols ; ++col )
106  {
107  out[i][col*matrix.num_rows + row] =
108  matrix[start + col*matrix.num_rows + row];
109  }
110  }
111  }
112 
113  return out;
114 }
115 
116 template class SGMatrixList<bool>;
117 template class SGMatrixList<char>;
118 template class SGMatrixList<int8_t>;
119 template class SGMatrixList<uint8_t>;
120 template class SGMatrixList<int16_t>;
121 template class SGMatrixList<uint16_t>;
122 template class SGMatrixList<int32_t>;
123 template class SGMatrixList<uint32_t>;
124 template class SGMatrixList<int64_t>;
125 template class SGMatrixList<uint64_t>;
126 template class SGMatrixList<float32_t>;
127 template class SGMatrixList<float64_t>;
128 template class SGMatrixList<floatmax_t>;
129 
130 } /* namespace shogun */

SHOGUN Machine Learning Toolbox - Documentation