SHOGUN  3.2.1
 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 
14 #include <shogun/lib/config.h>
15 
18 
19 namespace shogun
20 {
21 
22 template<class ST> class CDenseFeatures;
23 template<class ST> class SGVector;
24 class CDotFeatures;
25 
27 template<class ST> class CDenseSubsetFeatures: public CDotFeatures
28 {
29 public:
31  CDenseSubsetFeatures():m_fea(NULL) { set_generic<ST>(); }
32 
35  :m_fea(fea), m_idx(idx) { SG_REF(m_fea); set_generic<ST>(); }
36 
38  virtual ~CDenseSubsetFeatures() { SG_UNREF(m_fea); }
39 
41  virtual const char* get_name() const { return "DenseSubsetFeatures"; }
42 
45  {
46  SG_REF(fea);
47  SG_UNREF(m_fea);
48  m_fea = fea;
49  }
50 
53  {
54  m_idx = idx;
55  }
56 
63  virtual CFeatures* duplicate() const
64  {
65  return new CDenseSubsetFeatures(m_fea, m_idx);
66  }
67 
75  {
76  return m_fea->get_feature_type();
77  }
78 
86  {
87  return m_fea->get_feature_class();
88  }
89 
96  virtual int32_t get_num_vectors() const
97  {
98  return m_fea->get_num_vectors();
99  }
100 
108  virtual int32_t get_dim_feature_space() const
109  {
110  return m_idx.vlen;
111  }
112 
120  virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2)
121  {
122  CDenseSubsetFeatures<ST> *dsf = dynamic_cast<CDenseSubsetFeatures<ST> *>(df);
123  if (dsf == NULL)
124  SG_ERROR("Require DenseSubsetFeatures of the same kind to perform dot\n")
125 
126  if (m_idx.vlen != dsf->m_idx.vlen)
127  SG_ERROR("Cannot dot vectors of different length\n")
128 
129  SGVector<ST> vec1 = m_fea->get_feature_vector(vec_idx1);
130  SGVector<ST> vec2 = dsf->m_fea->get_feature_vector(vec_idx2);
131 
132  float64_t sum = 0;
133  for (int32_t i=0; i < m_idx.vlen; ++i)
134  sum += vec1[m_idx[i]] * vec2[dsf->m_idx[i]];
135 
136  return sum;
137  }
138 
145  virtual float64_t dense_dot(int32_t vec_idx1, const float64_t* vec2, int32_t vec2_len)
146  {
147  if (m_idx.vlen != vec2_len)
148  SG_ERROR("Cannot dot vectors of different length\n")
149  SGVector<ST> vec1 = m_fea->get_feature_vector(vec_idx1);
150 
151  float64_t sum=0;
152  for (int32_t i=0; i < vec2_len; ++i)
153  sum += vec1[m_idx[i]] * vec2[i];
154 
155  return sum;
156  }
157 
166  virtual void add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val=false)
167  {
168  if (m_idx.vlen != vec2_len)
169  SG_ERROR("Cannot add_to_dense_vec vectors of different length\n")
170 
171  SGVector<ST> vec1 = m_fea->get_feature_vector(vec_idx1);
172  if (abs_val)
173  {
174  for (int32_t i=0; i < vec2_len; ++i)
175  vec2[i] += alpha * CMath::abs(vec1[m_idx[i]]);
176  }
177  else
178  {
179  for (int32_t i=0; i < vec2_len; ++i)
180  vec2[i] += alpha * vec1[m_idx[i]];
181  }
182  }
183 
191  virtual int32_t get_nnz_features_for_vector(int32_t num)
192  {
193  return m_idx.vlen;
194  }
195 
205  virtual void* get_feature_iterator(int32_t vector_index)
206  {
208  return NULL;
209  }
210 
221  virtual bool get_next_feature(int32_t& index, float64_t& value, void* iterator)
222  {
224  return false;
225  }
226 
232  virtual void free_feature_iterator(void* iterator)
233  {
235  }
236 private:
237  CDenseFeatures<ST> *m_fea;
238  SGVector<int32_t> m_idx;
239 };
240 } /* shogun */
241 
242 #endif /* end of include guard: DENSESUBSETFEATURES_H__ */
243 

SHOGUN Machine Learning Toolbox - Documentation