SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MatrixFeatures.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 
16 : CFeatures(0)
17 {
18  init();
19 }
20 
22  int32_t num_vecs,
23  int32_t num_feats)
24 : CFeatures(0)
25 {
26  init();
27  m_features = SGMatrixList< ST >(num_vecs);
28  m_num_vectors = num_vecs;
29  m_num_features = num_feats;
30 }
31 
33  SGMatrixList< ST > feats, int32_t num_feats)
34 : CFeatures(0)
35 {
36  init();
37  set_features(feats, num_feats);
38 }
39 
41  SGMatrix< ST > feats, int32_t feat_length, int32_t num_vecs)
42 : CFeatures(0)
43 {
44  REQUIRE(feats.num_cols == feat_length*num_vecs, "The number of columns of feats "
45  "must be equal to feat_length times num_vecs\n");
46  init();
47  SGMatrixList< ST > feats_list = SGMatrixList< ST >::split(feats, num_vecs);
48  set_features(feats_list, feats.num_rows);
49 }
50 
51 /* TODO */
52 template< class ST > CFeatures* CMatrixFeatures< ST >::duplicate() const
53 {
54  return NULL;
55 }
56 
58 {
59  cleanup();
60 }
61 
62 /* TODO */
64 {
65  return F_UNKNOWN;
66 }
67 
68 /* TODO */
70 {
71  return C_UNKNOWN;
72 }
73 
74 /* TODO */
75 template< class ST > int32_t CMatrixFeatures< ST >::get_size() const
76 {
77  return 0;
78 }
79 
81  int32_t num) const
82 {
83  if ( num < 0 || num >= get_num_vectors() )
84  {
85  SG_ERROR("The index of the feature vector to get must be between "
86  "0 and %d (get_num_vectors()-1)\n", get_num_vectors()-1);
87  }
88 
89  return m_features[num];
90 }
91 
93  SGVector< ST > out,
94  int32_t num,
95  int32_t col) const
96 {
97  if ( num < 0 || num >= get_num_vectors() )
98  {
99  SG_ERROR("The index of the feature vector to get must be between "
100  "0 and %d (get_num_vectors()-1)\n", get_num_vectors()-1);
101  }
102 
103  // Shorthands for the dimensions of the feature vector to get
104  int32_t num_cols = m_features[num].num_cols;
105  int32_t num_rows = m_features[num].num_rows;
106 
107  if ( col < 0 || col >= num_cols )
108  {
109  SG_ERROR("The index of the column to get must be between "
110  "0 and %d (#columns of the feature vector)\n", num_cols);
111  }
112 
113  if ( out.vlen < get_num_features() )
114  {
115  SG_ERROR("The vector out must have space to hold at least "
116  "%d (get_num_features()) elements\n", get_num_features());
117  }
118 
119  int32_t start = col*num_rows;
120  for ( int32_t i = 0 ; i < get_num_features(); ++i )
121  {
122  out[i] = m_features[num][start + i];
123  }
124 }
125 
127  SGMatrix< ST > const & vec,
128  int32_t num)
129 {
130  if ( num < 0 || num >= get_num_vectors() )
131  {
132  SG_ERROR("The index of the feature vector to set must be between "
133  "0 and %d (get_num_vectors()-1)\n", get_num_vectors()-1);
134  }
135 
136  if ( get_num_features() != 0 && vec.num_rows != get_num_features() )
137  {
138  SG_ERROR("The feature vector to set must have the same features "
139  "as the rest of the MatrixFeatures, %d "
140  "(get_num_features())\n", get_num_features());
141  }
142 
143  m_features[num] = vec;
144 }
145 
146 template< class ST > void CMatrixFeatures< ST >::set_features(
147  SGMatrixList< ST > features, int32_t num_feats)
148 {
149  m_features = features;
150  m_num_vectors = features.num_matrices;
151  m_num_features = num_feats;
152 }
153 
154 template< class ST > void CMatrixFeatures< ST >::init()
155 {
156  SG_ADD(&m_num_vectors, "m_num_vectors", "Number of feature vectors",
158  SG_ADD(&m_num_features, "m_num_features",
159  "Number of features per vector (optional)", MS_NOT_AVAILABLE);
160  //TODO add SG_ADD for SGMatrixList
161  //SG_ADD(&m_features, "m_features", "Matrix features", MS_NOT_AVAILABLE);
162 
163  m_num_vectors = 0;
164  m_num_features = 0;
165 }
166 
167 template< class ST > void CMatrixFeatures< ST >::cleanup()
168 {
169  m_features = SGMatrixList< ST >();
170  m_num_vectors = 0;
171  m_num_features = 0;
172 }
173 
174 template class CMatrixFeatures<bool>;
175 template class CMatrixFeatures<char>;
176 template class CMatrixFeatures<int8_t>;
177 template class CMatrixFeatures<uint8_t>;
178 template class CMatrixFeatures<int16_t>;
179 template class CMatrixFeatures<uint16_t>;
180 template class CMatrixFeatures<int32_t>;
181 template class CMatrixFeatures<uint32_t>;
182 template class CMatrixFeatures<int64_t>;
183 template class CMatrixFeatures<uint64_t>;
184 template class CMatrixFeatures<float32_t>;
185 template class CMatrixFeatures<float64_t>;
186 template class CMatrixFeatures<floatmax_t>;
187 
188 } /* namespace shogun */

SHOGUN Machine Learning Toolbox - Documentation