SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DenseSubsetFeatures.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2012 Chiyuan Zhang
8  * Copyright (C) 2012 Chiyuan Zhang
9  */
10 
11 #ifndef DENSESUBSETFEATURES_H__
12 #define DENSESUBSETFEATURES_H__
13 
16 
17 namespace shogun
18 {
19 
20 template<class ST> class CDenseFeatures;
21 template<class ST> class SGVector;
22 class CDotFeatures;
23 
25 template<class ST> class CDenseSubsetFeatures: public CDotFeatures
26 {
27 public:
29  CDenseSubsetFeatures():m_fea(NULL) {}
30 
33  :m_fea(fea), m_idx(idx) { SG_REF(m_fea); }
34 
36  virtual ~CDenseSubsetFeatures() { SG_UNREF(m_fea); }
37 
39  virtual const char* get_name() const { return "DenseSubsetFeatures"; }
40 
43  {
44  SG_REF(fea);
45  SG_UNREF(m_fea);
46  m_fea = fea;
47  }
48 
51  {
52  m_idx = idx;
53  }
54 
61  virtual CFeatures* duplicate() const
62  {
63  return new CDenseSubsetFeatures(m_fea, m_idx);
64  }
65 
73  {
74  return m_fea->get_feature_type();
75  }
76 
84  {
85  return m_fea->get_feature_class();
86  }
87 
94  virtual int32_t get_num_vectors() const
95  {
96  return m_fea->get_num_vectors();
97  }
98 
99 
106  virtual int32_t get_size() const
107  {
108  return m_fea->get_size();
109  }
110 
111 
119  virtual int32_t get_dim_feature_space() const
120  {
121  return m_idx.vlen;
122  }
123 
131  virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2)
132  {
133  CDenseSubsetFeatures<ST> *dsf = dynamic_cast<CDenseSubsetFeatures<ST> *>(df);
134  if (dsf == NULL)
135  SG_ERROR("Require DenseSubsetFeatures of the same kind to perform dot\n");
136 
137  if (m_idx.vlen != dsf->m_idx.vlen)
138  SG_ERROR("Cannot dot vectors of different length\n");
139 
140  SGVector<ST> vec1 = m_fea->get_feature_vector(vec_idx1);
141  SGVector<ST> vec2 = dsf->m_fea->get_feature_vector(vec_idx2);
142 
143  float64_t sum = 0;
144  for (int32_t i=0; i < m_idx.vlen; ++i)
145  sum += vec1[m_idx[i]] * vec2[dsf->m_idx[i]];
146 
147  return sum;
148  }
149 
156  virtual float64_t dense_dot(int32_t vec_idx1, const float64_t* vec2, int32_t vec2_len)
157  {
158  if (m_idx.vlen != vec2_len)
159  SG_ERROR("Cannot dot vectors of different length\n");
160  SGVector<ST> vec1 = m_fea->get_feature_vector(vec_idx1);
161 
162  float64_t sum=0;
163  for (int32_t i=0; i < vec2_len; ++i)
164  sum += vec1[m_idx[i]] * vec2[i];
165 
166  return sum;
167  }
168 
177  virtual void add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val=false)
178  {
179  if (m_idx.vlen != vec2_len)
180  SG_ERROR("Cannot add_to_dense_vec vectors of different length\n");
181 
182  SGVector<ST> vec1 = m_fea->get_feature_vector(vec_idx1);
183  if (abs_val)
184  {
185  for (int32_t i=0; i < vec2_len; ++i)
186  vec2[i] += alpha * CMath::abs(vec1[m_idx[i]]);
187  }
188  else
189  {
190  for (int32_t i=0; i < vec2_len; ++i)
191  vec2[i] += alpha * vec1[m_idx[i]];
192  }
193  }
194 
202  virtual int32_t get_nnz_features_for_vector(int32_t num)
203  {
204  return m_idx.vlen;
205  }
206 
216  virtual void* get_feature_iterator(int32_t vector_index)
217  {
219  return NULL;
220  }
221 
232  virtual bool get_next_feature(int32_t& index, float64_t& value, void* iterator)
233  {
235  return false;
236  }
237 
243  virtual void free_feature_iterator(void* iterator)
244  {
246  }
247 private:
248  CDenseFeatures<ST> *m_fea;
249  SGVector<int32_t> m_idx;
250 };
251 } /* shogun */
252 
253 #endif /* end of include guard: DENSESUBSETFEATURES_H__ */
254 

SHOGUN Machine Learning Toolbox - Documentation