SHOGUN  v3.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 #include <shogun/lib/memory.h>
13 
14 namespace shogun {
15 
16 template <class T>
18 {
19  init_data();
20 }
21 
22 template <class T>
23 SGMatrixList<T>::SGMatrixList(SGMatrix<T>* ml, int32_t nmats, bool ref_counting)
24 : SGReferencedData(ref_counting), matrix_list(ml), num_matrices(nmats)
25 {
26 }
27 
28 template <class T>
29 SGMatrixList<T>::SGMatrixList(int32_t nmats, bool ref_counting)
30 : SGReferencedData(ref_counting), num_matrices(nmats)
31 {
32  matrix_list = SG_MALLOC(SGMatrix<T>, nmats);
33 }
34 
35 template <class T>
37 {
38  copy_data(orig);
39 }
40 
41 template <class T>
43 {
44  unref();
45 }
46 
47 template <class T>
49 {
50  return matrix_list[index];
51 }
52 
53 template <class T>
55 {
56  return matrix_list[index];
57 }
58 
59 template <class T>
61 {
62  matrix_list[index] = matrix;
63 }
64 
65 template <class T>
67 {
68  matrix_list = ((SGMatrixList*) (&orig))->matrix_list;
69  num_matrices = ((SGMatrixList*) (&orig))->num_matrices;
70 }
71 
72 template <class T>
74 {
75  matrix_list = NULL;
76  num_matrices = 0;
77 }
78 
79 template <class T>
81 {
82  SG_FREE(matrix_list);
83  num_matrices = 0;
84  matrix_list = NULL;
85 }
86 
87 template <class T>
88 SGMatrixList<T> SGMatrixList<T>::split(SGMatrix<T> matrix, int32_t num_components)
89 {
90  REQUIRE((matrix.num_cols % num_components) == 0,
91  "The number of columns (%d) must be multiple of the number "
92  "of components (%d).\n",
93  matrix.num_cols, num_components);
94 
95  int32_t new_num_cols = matrix.num_cols / num_components;
96  SGMatrixList<T> out(num_components);
97 
98  for ( int32_t i = 0 ; i < num_components ; ++i )
99  {
100  SGMatrix<T> new_matrix = SGMatrix<T>(matrix.num_rows, new_num_cols);
101 
102  for ( int32_t row = 0 ; row < matrix.num_rows ; ++row )
103  {
104  for ( int32_t col = 0 ; col < new_num_cols ; ++col )
105  new_matrix(row, col) = matrix(row, i*new_num_cols + col);
106  }
107 
108  out.set_matrix(i, new_matrix);
109  }
110 
111  return out;
112 }
113 
114 template class SGMatrixList<bool>;
115 template class SGMatrixList<char>;
116 template class SGMatrixList<int8_t>;
117 template class SGMatrixList<uint8_t>;
118 template class SGMatrixList<int16_t>;
119 template class SGMatrixList<uint16_t>;
120 template class SGMatrixList<int32_t>;
121 template class SGMatrixList<uint32_t>;
122 template class SGMatrixList<int64_t>;
123 template class SGMatrixList<uint64_t>;
124 template class SGMatrixList<float32_t>;
125 template class SGMatrixList<float64_t>;
126 template class SGMatrixList<floatmax_t>;
127 
128 } /* namespace shogun */

SHOGUN Machine Learning Toolbox - Documentation