SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
DenseSubsetFeatures.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) 2012 Chiyuan Zhang
00008  * Copyright (C) 2012 Chiyuan Zhang
00009  */
00010 
00011 #ifndef DENSESUBSETFEATURES_H__
00012 #define DENSESUBSETFEATURES_H__
00013 
00014 #include <shogun/features/DenseFeatures.h>
00015 #include <shogun/features/DotFeatures.h>
00016 
00017 namespace shogun
00018 {
00019 
00020 template<class ST> class CDenseFeatures;
00021 template<class ST> class SGVector;
00022 class CDotFeatures;
00023 
00025 template<class ST> class CDenseSubsetFeatures: public CDotFeatures
00026 {
00027 public:
00029     CDenseSubsetFeatures():m_fea(NULL) { set_generic<ST>(); }
00030 
00032     CDenseSubsetFeatures(CDenseFeatures<ST> *fea, SGVector<int32_t> idx)
00033         :m_fea(fea), m_idx(idx) { SG_REF(m_fea); set_generic<ST>(); }
00034 
00036     virtual ~CDenseSubsetFeatures() { SG_UNREF(m_fea); }
00037 
00039     virtual const char* get_name() const { return "DenseSubsetFeatures"; }
00040 
00042     void set_features(CDenseFeatures<ST> *fea)
00043     {
00044         SG_REF(fea);
00045         SG_UNREF(m_fea);
00046         m_fea = fea;
00047     }
00048 
00050     void set_subset_idx(SGVector<int32_t> idx)
00051     {
00052         m_idx = idx;
00053     }
00054 
00061     virtual CFeatures* duplicate() const
00062     {
00063         return new CDenseSubsetFeatures(m_fea, m_idx);
00064     }
00065 
00072     virtual EFeatureType get_feature_type() const
00073     {
00074         return m_fea->get_feature_type();
00075     }
00076 
00083     virtual EFeatureClass get_feature_class() const
00084     {
00085         return m_fea->get_feature_class();
00086     }
00087 
00094     virtual int32_t get_num_vectors() const
00095     {
00096         return m_fea->get_num_vectors();
00097     }
00098 
00106     virtual int32_t get_dim_feature_space() const
00107     {
00108         return m_idx.vlen;
00109     }
00110 
00118     virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2)
00119     {
00120         CDenseSubsetFeatures<ST> *dsf = dynamic_cast<CDenseSubsetFeatures<ST> *>(df);
00121         if (dsf == NULL)
00122             SG_ERROR("Require DenseSubsetFeatures of the same kind to perform dot\n")
00123 
00124         if (m_idx.vlen != dsf->m_idx.vlen)
00125             SG_ERROR("Cannot dot vectors of different length\n")
00126 
00127         SGVector<ST> vec1 = m_fea->get_feature_vector(vec_idx1);
00128         SGVector<ST> vec2 = dsf->m_fea->get_feature_vector(vec_idx2);
00129 
00130         float64_t sum = 0;
00131         for (int32_t i=0; i < m_idx.vlen; ++i)
00132             sum += vec1[m_idx[i]] * vec2[dsf->m_idx[i]];
00133 
00134         return sum;
00135     }
00136 
00143     virtual float64_t dense_dot(int32_t vec_idx1, float64_t* vec2, int32_t vec2_len)
00144     {
00145         if (m_idx.vlen != vec2_len)
00146             SG_ERROR("Cannot dot vectors of different length\n")
00147         SGVector<ST> vec1 = m_fea->get_feature_vector(vec_idx1);
00148 
00149         float64_t sum=0;
00150         for (int32_t i=0; i < vec2_len; ++i)
00151             sum += vec1[m_idx[i]] * vec2[i];
00152 
00153         return sum;
00154     }
00155 
00164     virtual void add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val=false)
00165     {
00166         if (m_idx.vlen != vec2_len)
00167             SG_ERROR("Cannot add_to_dense_vec vectors of different length\n")
00168 
00169         SGVector<ST> vec1 = m_fea->get_feature_vector(vec_idx1);
00170         if (abs_val)
00171         {
00172             for (int32_t i=0; i < vec2_len; ++i)
00173                 vec2[i] += alpha * CMath::abs(vec1[m_idx[i]]);
00174         }
00175         else
00176         {
00177             for (int32_t i=0; i < vec2_len; ++i)
00178                 vec2[i] += alpha * vec1[m_idx[i]];
00179         }
00180     }
00181 
00189     virtual int32_t get_nnz_features_for_vector(int32_t num)
00190     {
00191         return m_idx.vlen;
00192     }
00193 
00203     virtual void* get_feature_iterator(int32_t vector_index)
00204     {
00205         SG_NOTIMPLEMENTED
00206         return NULL;
00207     }
00208 
00219     virtual bool get_next_feature(int32_t& index, float64_t& value, void* iterator)
00220     {
00221         SG_NOTIMPLEMENTED
00222         return false;
00223     }
00224 
00230     virtual void free_feature_iterator(void* iterator)
00231     {
00232         SG_NOTIMPLEMENTED
00233     }
00234 private:
00235     CDenseFeatures<ST> *m_fea;
00236     SGVector<int32_t> m_idx;
00237 };
00238 } /*  shogun */
00239 
00240 #endif /* end of include guard: DENSESUBSETFEATURES_H__ */
00241 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation