|
SHOGUN v0.9.3
|
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) 2009-2010 Soeren Sonnenburg 00008 * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society 00009 * Copyright (C) 2010 Berlin Institute of Technology 00010 * 00011 */ 00012 00013 #ifndef _COMBINEDDOTFEATURES_H___ 00014 #define _COMBINEDDOTFEATURES_H___ 00015 00016 #include "lib/common.h" 00017 #include "lib/List.h" 00018 #include "features/DotFeatures.h" 00019 #include "features/Features.h" 00020 00021 namespace shogun 00022 { 00023 class CFeatures; 00024 template <class T> class CList; 00025 template <class T> class CListElement; 00045 class CCombinedDotFeatures : public CDotFeatures 00046 { 00047 public: 00049 CCombinedDotFeatures(); 00050 00052 CCombinedDotFeatures(const CCombinedDotFeatures & orig); 00053 00055 virtual ~CCombinedDotFeatures(); 00056 00061 inline virtual int32_t get_num_vectors() 00062 { 00063 return num_vectors; 00064 } 00065 00070 inline virtual int32_t get_dim_feature_space() 00071 { 00072 return num_dimensions; 00073 } 00074 00081 virtual float64_t dot(int32_t vec_idx1, int32_t vec_idx2); 00082 00089 virtual float64_t dense_dot(int32_t vec_idx1, float64_t* vec2, int32_t vec2_len); 00090 00102 virtual void dense_dot_range(float64_t* output, int32_t start, int32_t stop, float64_t* alphas, float64_t* vec, int32_t dim, float64_t b); 00103 00115 virtual void dense_dot_range_subset(int32_t* sub_index, int32_t num, float64_t* output, float64_t* alphas, float64_t* vec, int32_t dim, float64_t b); 00116 00125 virtual void add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val=false); 00126 00132 virtual int32_t get_nnz_features_for_vector(int32_t num); 00133 00138 inline virtual EFeatureType get_feature_type() 00139 { 00140 return F_DREAL; 00141 } 00142 00147 inline virtual EFeatureClass get_feature_class() 00148 { 00149 return C_COMBINED_DOT; 00150 } 00151 00156 inline virtual int32_t get_size() 00157 { 00158 return sizeof(float64_t); 00159 } 00160 00162 struct combined_feature_iterator 00163 { 00165 CDotFeatures* f; 00167 CListElement<CDotFeatures*>* current; 00169 void* iterator; 00170 /* the index of the vector over whose components to iterate over */ 00171 int32_t vector_index; 00172 }; 00173 00183 virtual void* get_feature_iterator(int32_t vector_index) 00184 { 00185 combined_feature_iterator* it=new combined_feature_iterator[1]; 00186 00187 it->current=NULL; 00188 it->f=get_first_feature_obj(it->current); 00189 it->iterator=it->f->get_feature_iterator(vector_index); 00190 it->vector_index=vector_index; 00191 return it; 00192 } 00193 00204 virtual bool get_next_feature(int32_t& index, float64_t& value, void* iterator) 00205 { 00206 ASSERT(iterator); 00207 combined_feature_iterator* it = (combined_feature_iterator*) iterator; 00208 00209 while (it->f) 00210 { 00211 if (it->f->get_next_feature(index, value, it->iterator)) 00212 { 00213 value*=get_combined_feature_weight(); 00214 return true; 00215 } 00216 00217 it->f->free_feature_iterator(it->iterator); 00218 it->f=get_next_feature_obj(it->current); 00219 if (it->f) 00220 it->iterator=it->f->get_feature_iterator(it->vector_index); 00221 else 00222 it->iterator=NULL; 00223 } 00224 return false; 00225 } 00226 00232 virtual void free_feature_iterator(void* iterator) 00233 { 00234 if (iterator) 00235 { 00236 combined_feature_iterator* it = (combined_feature_iterator*) iterator; 00237 if (it->iterator && it->f) 00238 it->f->free_feature_iterator(it->iterator); 00239 delete[] it; 00240 } 00241 } 00242 00247 virtual CFeatures* duplicate() const; 00248 00250 void list_feature_objs(); 00251 00256 inline CDotFeatures* get_first_feature_obj() 00257 { 00258 CDotFeatures* f=feature_list->get_first_element(); 00259 SG_REF(f); 00260 return f; 00261 } 00262 00268 inline CDotFeatures* get_first_feature_obj(CListElement<CDotFeatures*>*¤t) 00269 { 00270 CDotFeatures* f=feature_list->get_first_element(current); 00271 SG_REF(f); 00272 return f; 00273 } 00274 00279 inline CDotFeatures* get_next_feature_obj() 00280 { 00281 CDotFeatures* f=feature_list->get_next_element(); 00282 SG_REF(f); 00283 return f; 00284 } 00285 00291 inline CDotFeatures* get_next_feature_obj(CListElement<CDotFeatures*>*¤t) 00292 { 00293 CDotFeatures* f=feature_list->get_next_element(current); 00294 SG_REF(f); 00295 return f; 00296 } 00297 00302 inline CDotFeatures* get_last_feature_obj() 00303 { 00304 CDotFeatures* f=feature_list->get_last_element(); 00305 SG_REF(f); 00306 return f; 00307 } 00308 00314 inline bool insert_feature_obj(CDotFeatures* obj) 00315 { 00316 ASSERT(obj); 00317 SG_REF(obj); 00318 bool result=feature_list->insert_element(obj); 00319 update_dim_feature_space_and_num_vec(); 00320 return result; 00321 } 00322 00328 inline bool append_feature_obj(CDotFeatures* obj) 00329 { 00330 ASSERT(obj); 00331 SG_REF(obj); 00332 bool result=feature_list->append_element(obj); 00333 update_dim_feature_space_and_num_vec(); 00334 return result; 00335 } 00336 00341 inline bool delete_feature_obj() 00342 { 00343 CDotFeatures* f=feature_list->delete_element(); 00344 if (f) 00345 { 00346 SG_UNREF(f); 00347 update_dim_feature_space_and_num_vec(); 00348 return true; 00349 } 00350 else 00351 return false; 00352 } 00353 00358 inline int32_t get_num_feature_obj() 00359 { 00360 return feature_list->get_num_elements(); 00361 } 00362 00368 virtual void get_subfeature_weights(float64_t** weights, int32_t* num_weights); 00369 00375 virtual void set_subfeature_weights( 00376 float64_t* weights, int32_t num_weights); 00377 00379 inline virtual const char* get_name() const { return "CombinedDotFeatures"; } 00380 00381 protected: 00383 void update_dim_feature_space_and_num_vec(); 00384 00385 protected: 00387 CList<CDotFeatures*>* feature_list; 00388 00390 int32_t num_vectors; 00392 int32_t num_dimensions; 00393 }; 00394 } 00395 #endif // _DOTFEATURES_H___