Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
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) {}
00030
00032 CDenseSubsetFeatures(CDenseFeatures<ST> *fea, SGVector<int32_t> idx)
00033 :m_fea(fea), m_idx(idx) { SG_REF(m_fea); }
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
00099
00106 virtual int32_t get_size() const
00107 {
00108 return m_fea->get_size();
00109 }
00110
00111
00119 virtual int32_t get_dim_feature_space() const
00120 {
00121 return m_idx.vlen;
00122 }
00123
00131 virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2)
00132 {
00133 CDenseSubsetFeatures<ST> *dsf = dynamic_cast<CDenseSubsetFeatures<ST> *>(df);
00134 if (dsf == NULL)
00135 SG_ERROR("Require DenseSubsetFeatures of the same kind to perform dot\n");
00136
00137 if (m_idx.vlen != dsf->m_idx.vlen)
00138 SG_ERROR("Cannot dot vectors of different length\n");
00139
00140 SGVector<ST> vec1 = m_fea->get_feature_vector(vec_idx1);
00141 SGVector<ST> vec2 = dsf->m_fea->get_feature_vector(vec_idx2);
00142
00143 float64_t sum = 0;
00144 for (int32_t i=0; i < m_idx.vlen; ++i)
00145 sum += vec1[m_idx[i]] * vec2[dsf->m_idx[i]];
00146
00147 return sum;
00148 }
00149
00156 virtual float64_t dense_dot(int32_t vec_idx1, const float64_t* vec2, int32_t vec2_len)
00157 {
00158 if (m_idx.vlen != vec2_len)
00159 SG_ERROR("Cannot dot vectors of different length\n");
00160 SGVector<ST> vec1 = m_fea->get_feature_vector(vec_idx1);
00161
00162 float64_t sum=0;
00163 for (int32_t i=0; i < vec2_len; ++i)
00164 sum += vec1[m_idx[i]] * vec2[i];
00165
00166 return sum;
00167 }
00168
00177 virtual void add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val=false)
00178 {
00179 if (m_idx.vlen != vec2_len)
00180 SG_ERROR("Cannot add_to_dense_vec vectors of different length\n");
00181
00182 SGVector<ST> vec1 = m_fea->get_feature_vector(vec_idx1);
00183 if (abs_val)
00184 {
00185 for (int32_t i=0; i < vec2_len; ++i)
00186 vec2[i] += alpha * CMath::abs(vec1[m_idx[i]]);
00187 }
00188 else
00189 {
00190 for (int32_t i=0; i < vec2_len; ++i)
00191 vec2[i] += alpha * vec1[m_idx[i]];
00192 }
00193 }
00194
00202 virtual int32_t get_nnz_features_for_vector(int32_t num)
00203 {
00204 return m_idx.vlen;
00205 }
00206
00216 virtual void* get_feature_iterator(int32_t vector_index)
00217 {
00218 SG_NOTIMPLEMENTED;
00219 return NULL;
00220 }
00221
00232 virtual bool get_next_feature(int32_t& index, float64_t& value, void* iterator)
00233 {
00234 SG_NOTIMPLEMENTED;
00235 return false;
00236 }
00237
00243 virtual void free_feature_iterator(void* iterator)
00244 {
00245 SG_NOTIMPLEMENTED;
00246 }
00247 private:
00248 CDenseFeatures<ST> *m_fea;
00249 SGVector<int32_t> m_idx;
00250 };
00251 }
00252
00253 #endif
00254