SHOGUN v0.9.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) 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 class CList; 00025 class CListElement; 00045 class CCombinedDotFeatures : public CDotFeatures 00046 { 00047 void init(void); 00048 00049 public: 00051 CCombinedDotFeatures(); 00052 00054 CCombinedDotFeatures(const CCombinedDotFeatures & orig); 00055 00057 virtual ~CCombinedDotFeatures(); 00058 00063 inline virtual int32_t get_num_vectors() 00064 { 00065 return num_vectors; 00066 } 00067 00072 inline virtual int32_t get_dim_feature_space() 00073 { 00074 return num_dimensions; 00075 } 00076 00084 virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2); 00085 00092 virtual float64_t dense_dot(int32_t vec_idx1, const float64_t* vec2, int32_t vec2_len); 00093 00105 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); 00106 00118 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); 00119 00128 virtual void add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val=false); 00129 00135 virtual int32_t get_nnz_features_for_vector(int32_t num); 00136 00141 inline virtual EFeatureType get_feature_type() 00142 { 00143 return F_DREAL; 00144 } 00145 00150 inline virtual EFeatureClass get_feature_class() 00151 { 00152 return C_COMBINED_DOT; 00153 } 00154 00159 inline virtual int32_t get_size() 00160 { 00161 return sizeof(float64_t); 00162 } 00163 00165 struct combined_feature_iterator 00166 { 00168 CDotFeatures* f; 00170 CListElement* current; 00172 void* iterator; 00173 /* the index of the vector over whose components to iterate over */ 00174 int32_t vector_index; 00175 }; 00176 00186 virtual void* get_feature_iterator(int32_t vector_index) 00187 { 00188 combined_feature_iterator* it=new combined_feature_iterator[1]; 00189 00190 it->current=NULL; 00191 it->f=get_first_feature_obj(it->current); 00192 it->iterator=it->f->get_feature_iterator(vector_index); 00193 it->vector_index=vector_index; 00194 return it; 00195 } 00196 00207 virtual bool get_next_feature(int32_t& index, float64_t& value, void* iterator) 00208 { 00209 ASSERT(iterator); 00210 combined_feature_iterator* it = (combined_feature_iterator*) iterator; 00211 00212 while (it->f) 00213 { 00214 if (it->f->get_next_feature(index, value, it->iterator)) 00215 { 00216 value*=get_combined_feature_weight(); 00217 return true; 00218 } 00219 00220 it->f->free_feature_iterator(it->iterator); 00221 it->f=get_next_feature_obj(it->current); 00222 if (it->f) 00223 it->iterator=it->f->get_feature_iterator(it->vector_index); 00224 else 00225 it->iterator=NULL; 00226 } 00227 return false; 00228 } 00229 00235 virtual void free_feature_iterator(void* iterator) 00236 { 00237 if (iterator) 00238 { 00239 combined_feature_iterator* it = (combined_feature_iterator*) iterator; 00240 if (it->iterator && it->f) 00241 it->f->free_feature_iterator(it->iterator); 00242 delete[] it; 00243 } 00244 } 00245 00250 virtual CFeatures* duplicate() const; 00251 00253 void list_feature_objs(); 00254 00259 inline CDotFeatures* get_first_feature_obj() 00260 { 00261 return (CDotFeatures*) feature_list->get_first_element(); 00262 } 00263 00269 inline CDotFeatures* get_first_feature_obj(CListElement*& current) 00270 { 00271 return (CDotFeatures*) feature_list->get_first_element(current); 00272 } 00273 00278 inline CDotFeatures* get_next_feature_obj() 00279 { 00280 return (CDotFeatures*) feature_list->get_next_element(); 00281 } 00282 00288 inline CDotFeatures* get_next_feature_obj(CListElement*& current) 00289 { 00290 return (CDotFeatures*) feature_list->get_next_element(current); 00291 } 00292 00297 inline CDotFeatures* get_last_feature_obj() 00298 { 00299 return (CDotFeatures*) feature_list->get_last_element(); 00300 } 00301 00307 inline bool insert_feature_obj(CDotFeatures* obj) 00308 { 00309 ASSERT(obj); 00310 bool result=feature_list->insert_element(obj); 00311 update_dim_feature_space_and_num_vec(); 00312 return result; 00313 } 00314 00320 inline bool append_feature_obj(CDotFeatures* obj) 00321 { 00322 ASSERT(obj); 00323 bool result=feature_list->append_element(obj); 00324 update_dim_feature_space_and_num_vec(); 00325 return result; 00326 } 00327 00332 inline bool delete_feature_obj() 00333 { 00334 CDotFeatures* f=(CDotFeatures*) feature_list->delete_element(); 00335 if (f) 00336 { 00337 SG_UNREF(f); 00338 update_dim_feature_space_and_num_vec(); 00339 return true; 00340 } 00341 else 00342 return false; 00343 } 00344 00349 inline int32_t get_num_feature_obj() 00350 { 00351 return feature_list->get_num_elements(); 00352 } 00353 00359 virtual void get_subfeature_weights(float64_t** weights, int32_t* num_weights); 00360 00366 virtual void set_subfeature_weights( 00367 float64_t* weights, int32_t num_weights); 00368 00370 inline virtual const char* get_name() const { return "CombinedDotFeatures"; } 00371 00372 protected: 00374 void update_dim_feature_space_and_num_vec(); 00375 00376 protected: 00378 CList* feature_list; 00379 00381 int32_t num_vectors; 00383 int32_t num_dimensions; 00384 }; 00385 } 00386 #endif // _DOTFEATURES_H___