-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
MatrixFeatures.cpp
189 lines (161 loc) · 5.07 KB
/
MatrixFeatures.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2012 Fernando José Iglesias García
* Copyright (C) 2012 Fernando José Iglesias García
*/
#include <shogun/features/MatrixFeatures.h>
namespace shogun {
template< class ST > CMatrixFeatures< ST >::CMatrixFeatures()
: CFeatures(0)
{
init();
}
template< class ST > CMatrixFeatures< ST >::CMatrixFeatures(
int32_t num_vecs,
int32_t num_feats)
: CFeatures(0)
{
init();
m_features = SGMatrixList< ST >(num_vecs);
m_num_vectors = num_vecs;
m_num_features = num_feats;
}
template< class ST > CMatrixFeatures< ST >::CMatrixFeatures(
SGMatrixList< ST > feats, int32_t num_feats)
: CFeatures(0)
{
init();
set_features(feats, num_feats);
}
template< class ST > CMatrixFeatures< ST >::CMatrixFeatures(
SGMatrix< ST > feats, int32_t feat_length, int32_t num_vecs)
: CFeatures(0)
{
REQUIRE(feats.num_cols == feat_length*num_vecs, "The number of columns of feats "
"must be equal to feat_length times num_vecs\n");
init();
SGMatrixList< ST > feats_list = SGMatrixList< ST >::split(feats, num_vecs);
set_features(feats_list, feats.num_rows);
}
/* TODO */
template< class ST > CFeatures* CMatrixFeatures< ST >::duplicate() const
{
return NULL;
}
template< class ST > CMatrixFeatures< ST >::~CMatrixFeatures()
{
cleanup();
}
/* TODO */
template< class ST > EFeatureType CMatrixFeatures< ST >::get_feature_type() const
{
return F_UNKNOWN;
}
template< class ST > EFeatureClass CMatrixFeatures< ST >::get_feature_class() const
{
return C_MATRIX;
}
template< class ST > SGMatrix< ST > CMatrixFeatures< ST >::get_feature_vector(
int32_t num) const
{
if ( num < 0 || num >= get_num_vectors() )
{
SG_ERROR("The index of the feature vector to get must be between "
"0 and %d (get_num_vectors()-1)\n", get_num_vectors()-1);
}
return m_features[num];
}
template< class ST > void CMatrixFeatures< ST >::get_feature_vector_col(
SGVector< ST > out,
int32_t num,
int32_t col) const
{
if ( num < 0 || num >= get_num_vectors() )
{
SG_ERROR("The index of the feature vector to get must be between "
"0 and %d (get_num_vectors()-1)\n", get_num_vectors()-1);
}
// Shorthands for the dimensions of the feature vector to get
int32_t num_cols = m_features[num].num_cols;
int32_t num_rows = m_features[num].num_rows;
if ( col < 0 || col >= num_cols )
{
SG_ERROR("The index of the column to get must be between "
"0 and %d (#columns of the feature vector)\n", num_cols);
}
if ( out.vlen < get_num_features() )
{
SG_ERROR("The vector out must have space to hold at least "
"%d (get_num_features()) elements\n", get_num_features());
}
int32_t start = col*num_rows;
for ( int32_t i = 0 ; i < get_num_features(); ++i )
{
out[i] = m_features[num][start + i];
}
}
template< class ST > void CMatrixFeatures< ST >::set_feature_vector(
SGMatrix< ST > const vec,
int32_t num)
{
if ( num < 0 || num >= get_num_vectors() )
{
SG_ERROR("The index of the feature vector to set must be between "
"0 and %d (get_num_vectors()-1)\n", get_num_vectors()-1);
}
if ( get_num_features() != 0 && vec.num_rows != get_num_features() )
{
SG_ERROR("The feature vector to set must have the same features "
"as the rest of the MatrixFeatures, %d "
"(get_num_features())\n", get_num_features());
}
m_features[num] = vec;
}
template< class ST > void CMatrixFeatures< ST >::set_features(
SGMatrixList< ST > features, int32_t num_feats)
{
m_features = features;
m_num_vectors = features.num_matrices;
m_num_features = num_feats;
}
template< class ST > void CMatrixFeatures< ST >::init()
{
SG_ADD(&m_num_vectors, "m_num_vectors", "Number of feature vectors",
MS_NOT_AVAILABLE);
SG_ADD(&m_num_features, "m_num_features",
"Number of features per vector (optional)", MS_NOT_AVAILABLE);
//TODO add SG_ADD for SGMatrixList
//SG_ADD(&m_features, "m_features", "Matrix features", MS_NOT_AVAILABLE);
m_num_vectors = 0;
m_num_features = 0;
}
template< class ST > void CMatrixFeatures< ST >::cleanup()
{
m_features = SGMatrixList< ST >();
m_num_vectors = 0;
m_num_features = 0;
}
template< class ST > CMatrixFeatures< ST >* CMatrixFeatures< ST >::obtain_from_generic(CFeatures* const base_features)
{
REQUIRE(base_features->get_feature_class() == C_MATRIX,
"base_features must be of dynamic type CMatrixFeatures\n")
return (CMatrixFeatures< ST >*) base_features;
}
template class CMatrixFeatures<bool>;
template class CMatrixFeatures<char>;
template class CMatrixFeatures<int8_t>;
template class CMatrixFeatures<uint8_t>;
template class CMatrixFeatures<int16_t>;
template class CMatrixFeatures<uint16_t>;
template class CMatrixFeatures<int32_t>;
template class CMatrixFeatures<uint32_t>;
template class CMatrixFeatures<int64_t>;
template class CMatrixFeatures<uint64_t>;
template class CMatrixFeatures<float32_t>;
template class CMatrixFeatures<float64_t>;
template class CMatrixFeatures<floatmax_t>;
} /* namespace shogun */