SHOGUN  v3.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) { set_generic<ST>(); }
30 
33  :m_fea(fea), m_idx(idx) { SG_REF(m_fea); set_generic<ST>(); }
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 
106  virtual int32_t get_dim_feature_space() const
107  {
108  return m_idx.vlen;
109  }
110 
118  virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2)
119  {
120  CDenseSubsetFeatures<ST> *dsf = dynamic_cast<CDenseSubsetFeatures<ST> *>(df);
121  if (dsf == NULL)
122  SG_ERROR("Require DenseSubsetFeatures of the same kind to perform dot\n")
123 
124  if (m_idx.vlen != dsf->m_idx.vlen)
125  SG_ERROR("Cannot dot vectors of different length\n")
126 
127  SGVector<ST> vec1 = m_fea->get_feature_vector(vec_idx1);
128  SGVector<ST> vec2 = dsf->m_fea->get_feature_vector(vec_idx2);
129 
130  float64_t sum = 0;
131  for (int32_t i=0; i < m_idx.vlen; ++i)
132  sum += vec1[m_idx[i]] * vec2[dsf->m_idx[i]];
133 
134  return sum;
135  }
136 
143  virtual float64_t dense_dot(int32_t vec_idx1, const float64_t* vec2, int32_t vec2_len)
144  {
145  if (m_idx.vlen != vec2_len)
146  SG_ERROR("Cannot dot vectors of different length\n")
147  SGVector<ST> vec1 = m_fea->get_feature_vector(vec_idx1);
148 
149  float64_t sum=0;
150  for (int32_t i=0; i < vec2_len; ++i)
151  sum += vec1[m_idx[i]] * vec2[i];
152 
153  return sum;
154  }
155 
164  virtual void add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val=false)
165  {
166  if (m_idx.vlen != vec2_len)
167  SG_ERROR("Cannot add_to_dense_vec vectors of different length\n")
168 
169  SGVector<ST> vec1 = m_fea->get_feature_vector(vec_idx1);
170  if (abs_val)
171  {
172  for (int32_t i=0; i < vec2_len; ++i)
173  vec2[i] += alpha * CMath::abs(vec1[m_idx[i]]);
174  }
175  else
176  {
177  for (int32_t i=0; i < vec2_len; ++i)
178  vec2[i] += alpha * vec1[m_idx[i]];
179  }
180  }
181 
189  virtual int32_t get_nnz_features_for_vector(int32_t num)
190  {
191  return m_idx.vlen;
192  }
193 
203  virtual void* get_feature_iterator(int32_t vector_index)
204  {
206  return NULL;
207  }
208 
219  virtual bool get_next_feature(int32_t& index, float64_t& value, void* iterator)
220  {
222  return false;
223  }
224 
230  virtual void free_feature_iterator(void* iterator)
231  {
233  }
234 private:
235  CDenseFeatures<ST> *m_fea;
236  SGVector<int32_t> m_idx;
237 };
238 } /* shogun */
239 
240 #endif /* end of include guard: DENSESUBSETFEATURES_H__ */
241 

SHOGUN Machine Learning Toolbox - Documentation