SHOGUN
v3.2.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2012 Fernando Jose Iglesias Garcia 00008 * Copyright (C) 2012 Fernando Jose Iglesias Garcia 00009 */ 00010 00011 #include <shogun/features/MatrixFeatures.h> 00012 00013 namespace shogun { 00014 00015 template< class ST > CMatrixFeatures< ST >::CMatrixFeatures() 00016 : CFeatures(0) 00017 { 00018 init(); 00019 } 00020 00021 template< class ST > CMatrixFeatures< ST >::CMatrixFeatures( 00022 int32_t num_vecs, 00023 int32_t num_feats) 00024 : CFeatures(0) 00025 { 00026 init(); 00027 m_features = SGMatrixList< ST >(num_vecs); 00028 m_num_vectors = num_vecs; 00029 m_num_features = num_feats; 00030 } 00031 00032 template< class ST > CMatrixFeatures< ST >::CMatrixFeatures( 00033 SGMatrixList< ST > feats, int32_t num_feats) 00034 : CFeatures(0) 00035 { 00036 init(); 00037 set_features(feats, num_feats); 00038 } 00039 00040 template< class ST > CMatrixFeatures< ST >::CMatrixFeatures( 00041 SGMatrix< ST > feats, int32_t feat_length, int32_t num_vecs) 00042 : CFeatures(0) 00043 { 00044 REQUIRE(feats.num_cols == feat_length*num_vecs, "The number of columns of feats " 00045 "must be equal to feat_length times num_vecs\n"); 00046 init(); 00047 SGMatrixList< ST > feats_list = SGMatrixList< ST >::split(feats, num_vecs); 00048 set_features(feats_list, feats.num_rows); 00049 } 00050 00051 /* TODO */ 00052 template< class ST > CFeatures* CMatrixFeatures< ST >::duplicate() const 00053 { 00054 return NULL; 00055 } 00056 00057 template< class ST > CMatrixFeatures< ST >::~CMatrixFeatures() 00058 { 00059 cleanup(); 00060 } 00061 00062 /* TODO */ 00063 template< class ST > EFeatureType CMatrixFeatures< ST >::get_feature_type() const 00064 { 00065 return F_UNKNOWN; 00066 } 00067 00068 template< class ST > EFeatureClass CMatrixFeatures< ST >::get_feature_class() const 00069 { 00070 return C_MATRIX; 00071 } 00072 00073 template< class ST > SGMatrix< ST > CMatrixFeatures< ST >::get_feature_vector( 00074 int32_t num) const 00075 { 00076 if ( num < 0 || num >= get_num_vectors() ) 00077 { 00078 SG_ERROR("The index of the feature vector to get must be between " 00079 "0 and %d (get_num_vectors()-1)\n", get_num_vectors()-1); 00080 } 00081 00082 return m_features[num]; 00083 } 00084 00085 template< class ST > void CMatrixFeatures< ST >::get_feature_vector_col( 00086 SGVector< ST > out, 00087 int32_t num, 00088 int32_t col) const 00089 { 00090 if ( num < 0 || num >= get_num_vectors() ) 00091 { 00092 SG_ERROR("The index of the feature vector to get must be between " 00093 "0 and %d (get_num_vectors()-1)\n", get_num_vectors()-1); 00094 } 00095 00096 // Shorthands for the dimensions of the feature vector to get 00097 int32_t num_cols = m_features[num].num_cols; 00098 int32_t num_rows = m_features[num].num_rows; 00099 00100 if ( col < 0 || col >= num_cols ) 00101 { 00102 SG_ERROR("The index of the column to get must be between " 00103 "0 and %d (#columns of the feature vector)\n", num_cols); 00104 } 00105 00106 if ( out.vlen < get_num_features() ) 00107 { 00108 SG_ERROR("The vector out must have space to hold at least " 00109 "%d (get_num_features()) elements\n", get_num_features()); 00110 } 00111 00112 int32_t start = col*num_rows; 00113 for ( int32_t i = 0 ; i < get_num_features(); ++i ) 00114 { 00115 out[i] = m_features[num][start + i]; 00116 } 00117 } 00118 00119 template< class ST > void CMatrixFeatures< ST >::set_feature_vector( 00120 SGMatrix< ST > const vec, 00121 int32_t num) 00122 { 00123 if ( num < 0 || num >= get_num_vectors() ) 00124 { 00125 SG_ERROR("The index of the feature vector to set must be between " 00126 "0 and %d (get_num_vectors()-1)\n", get_num_vectors()-1); 00127 } 00128 00129 if ( get_num_features() != 0 && vec.num_rows != get_num_features() ) 00130 { 00131 SG_ERROR("The feature vector to set must have the same features " 00132 "as the rest of the MatrixFeatures, %d " 00133 "(get_num_features())\n", get_num_features()); 00134 } 00135 00136 m_features.set_matrix(num, vec); 00137 } 00138 00139 template< class ST > void CMatrixFeatures< ST >::set_features( 00140 SGMatrixList< ST > features, int32_t num_feats) 00141 { 00142 m_features = features; 00143 m_num_vectors = features.num_matrices; 00144 m_num_features = num_feats; 00145 } 00146 00147 template< class ST > void CMatrixFeatures< ST >::init() 00148 { 00149 SG_ADD(&m_num_vectors, "m_num_vectors", "Number of feature vectors", 00150 MS_NOT_AVAILABLE); 00151 SG_ADD(&m_num_features, "m_num_features", 00152 "Number of features per vector (optional)", MS_NOT_AVAILABLE); 00153 //TODO add SG_ADD for SGMatrixList 00154 //SG_ADD(&m_features, "m_features", "Matrix features", MS_NOT_AVAILABLE); 00155 00156 m_num_vectors = 0; 00157 m_num_features = 0; 00158 00159 set_generic<ST>(); 00160 } 00161 00162 template< class ST > void CMatrixFeatures< ST >::cleanup() 00163 { 00164 m_features = SGMatrixList< ST >(); 00165 m_num_vectors = 0; 00166 m_num_features = 0; 00167 } 00168 00169 template< class ST > CMatrixFeatures< ST >* CMatrixFeatures< ST >::obtain_from_generic(CFeatures* const base_features) 00170 { 00171 REQUIRE(base_features->get_feature_class() == C_MATRIX, 00172 "base_features must be of dynamic type CMatrixFeatures\n") 00173 00174 return (CMatrixFeatures< ST >*) base_features; 00175 } 00176 00177 template class CMatrixFeatures<bool>; 00178 template class CMatrixFeatures<char>; 00179 template class CMatrixFeatures<int8_t>; 00180 template class CMatrixFeatures<uint8_t>; 00181 template class CMatrixFeatures<int16_t>; 00182 template class CMatrixFeatures<uint16_t>; 00183 template class CMatrixFeatures<int32_t>; 00184 template class CMatrixFeatures<uint32_t>; 00185 template class CMatrixFeatures<int64_t>; 00186 template class CMatrixFeatures<uint64_t>; 00187 template class CMatrixFeatures<float32_t>; 00188 template class CMatrixFeatures<float64_t>; 00189 template class CMatrixFeatures<floatmax_t>; 00190 00191 } /* namespace shogun */