CombinedDotFeatures.h

Go to the documentation of this file.
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 <shogun/lib/common.h>
00017 #include <shogun/lib/List.h>
00018 #include <shogun/features/DotFeatures.h>
00019 #include <shogun/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() const
00064         {
00065             return num_vectors;
00066         }
00067 
00072         inline virtual int32_t get_dim_feature_space() const
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 
00164         #ifndef DOXYGEN_SHOULD_SKIP_THIS
00165 
00166         struct combined_feature_iterator
00167         {
00169             CDotFeatures* f;
00171             CListElement* current;
00173             void* iterator;
00175             int32_t vector_index;
00176         };
00177         #endif
00178 
00188         virtual void* get_feature_iterator(int32_t vector_index)
00189         {
00190             combined_feature_iterator* it=SG_MALLOC(combined_feature_iterator, 1);
00191 
00192             it->current=NULL;
00193             it->f=get_first_feature_obj(it->current);
00194             it->iterator=it->f->get_feature_iterator(vector_index);
00195             it->vector_index=vector_index;
00196             return it;
00197         }
00198 
00209         virtual bool get_next_feature(int32_t& index, float64_t& value, void* iterator)
00210         {
00211             ASSERT(iterator);
00212             combined_feature_iterator* it = (combined_feature_iterator*) iterator;
00213 
00214             while (it->f)
00215             {
00216                 if (it->f->get_next_feature(index, value, it->iterator))
00217                 {
00218                     value*=get_combined_feature_weight();
00219                     return true;
00220                 }
00221 
00222                 it->f->free_feature_iterator(it->iterator);
00223                 it->f=get_next_feature_obj(it->current);
00224                 if (it->f)
00225                     it->iterator=it->f->get_feature_iterator(it->vector_index);
00226                 else
00227                     it->iterator=NULL;
00228             }
00229             return false;
00230         }
00231 
00237         virtual void free_feature_iterator(void* iterator)
00238         {
00239             if (iterator)
00240             {
00241                 combined_feature_iterator* it = (combined_feature_iterator*) iterator;
00242                 if (it->iterator && it->f)
00243                     it->f->free_feature_iterator(it->iterator);
00244                 SG_FREE(it);
00245             }
00246         }
00247 
00252         virtual CFeatures* duplicate() const;
00253 
00255         void list_feature_objs();
00256 
00261         inline CDotFeatures* get_first_feature_obj()
00262         {
00263             return (CDotFeatures*) feature_list->get_first_element();
00264         }
00265 
00271         inline CDotFeatures* get_first_feature_obj(CListElement*& current)
00272         {
00273             return (CDotFeatures*) feature_list->get_first_element(current);
00274         }
00275 
00280         inline CDotFeatures* get_next_feature_obj()
00281         {
00282             return (CDotFeatures*) feature_list->get_next_element();
00283         }
00284 
00290         inline CDotFeatures* get_next_feature_obj(CListElement*& current)
00291         {
00292             return (CDotFeatures*) feature_list->get_next_element(current);
00293         }
00294 
00299         inline CDotFeatures* get_last_feature_obj()
00300         {
00301             return (CDotFeatures*) feature_list->get_last_element();
00302         }
00303 
00309         inline bool insert_feature_obj(CDotFeatures* obj)
00310         {
00311             ASSERT(obj);
00312             bool result=feature_list->insert_element(obj);
00313             update_dim_feature_space_and_num_vec();
00314             return result;
00315         }
00316 
00322         inline bool append_feature_obj(CDotFeatures* obj)
00323         {
00324             ASSERT(obj);
00325             bool result=feature_list->append_element(obj);
00326             update_dim_feature_space_and_num_vec();
00327             return result;
00328         }
00329 
00334         inline bool delete_feature_obj()
00335         {
00336             CDotFeatures* f=(CDotFeatures*) feature_list->delete_element();
00337             if (f)
00338             {
00339                 SG_UNREF(f);
00340                 update_dim_feature_space_and_num_vec();
00341                 return true;
00342             }
00343             else
00344                 return false;
00345         }
00346 
00351         inline int32_t get_num_feature_obj()
00352         {
00353             return feature_list->get_num_elements();
00354         }
00355 
00361         virtual void get_subfeature_weights(float64_t** weights, int32_t* num_weights);
00362 
00368         virtual void set_subfeature_weights(
00369             float64_t* weights, int32_t num_weights);
00370 
00372         inline virtual const char* get_name() const { return "CombinedDotFeatures"; }
00373 
00374     protected:
00376         void update_dim_feature_space_and_num_vec();
00377 
00378     protected:
00380         CList* feature_list;
00381 
00383         int32_t num_vectors;
00385         int32_t num_dimensions;
00386 };
00387 }
00388 #endif // _DOTFEATURES_H___
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation