CombinedDotFeatures.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "features/CombinedDotFeatures.h"
00012 #include "lib/io.h"
00013 #include "lib/Mathematics.h"
00014
00015 CCombinedDotFeatures::CCombinedDotFeatures() : CDotFeatures()
00016 {
00017 feature_list=new CList<CDotFeatures*>(true);
00018 update_dim_feature_space_and_num_vec();
00019 }
00020
00021 CCombinedDotFeatures::CCombinedDotFeatures(const CCombinedDotFeatures & orig)
00022 : CDotFeatures(orig), num_vectors(orig.num_vectors),
00023 num_dimensions(orig.num_dimensions)
00024 {
00025 }
00026
00027 CFeatures* CCombinedDotFeatures::duplicate() const
00028 {
00029 return new CCombinedDotFeatures(*this);
00030 }
00031
00032 CCombinedDotFeatures::~CCombinedDotFeatures()
00033 {
00034 delete feature_list;
00035 }
00036
00037 void CCombinedDotFeatures::list_feature_objs()
00038 {
00039 SG_INFO( "BEGIN COMBINED DOTFEATURES LIST (%d, %d) - ", num_vectors, num_dimensions);
00040 this->list_feature_obj();
00041
00042 CListElement<CDotFeatures*> * current = NULL ;
00043 CDotFeatures* f=get_first_feature_obj(current);
00044
00045 while (f)
00046 {
00047 f->list_feature_obj();
00048 f=get_next_feature_obj(current);
00049 }
00050
00051 SG_INFO( "END COMBINED DOTFEATURES LIST (%d, %d) - ", num_vectors, num_dimensions);
00052 this->list_feature_obj();
00053 }
00054
00055 void CCombinedDotFeatures::update_dim_feature_space_and_num_vec()
00056 {
00057 CListElement<CDotFeatures*> * current = NULL ;
00058 CDotFeatures* f=get_first_feature_obj(current);
00059
00060 int32_t dim=0;
00061 int32_t vec=-1;
00062
00063 while (f)
00064 {
00065 dim+= f->get_dim_feature_space();
00066 if (vec==-1)
00067 vec=f->get_num_vectors();
00068 else if (vec != f->get_num_vectors())
00069 {
00070 f->list_feature_obj();
00071 SG_ERROR("Number of vectors (%d) mismatches in above feature obj (%d)\n", vec, f->get_num_vectors());
00072 }
00073
00074 f=get_next_feature_obj(current);
00075 }
00076
00077 num_dimensions=dim;
00078 num_vectors=vec;
00079 SG_DEBUG("vecs=%d, dims=%d\n", num_vectors, num_dimensions);
00080 }
00081
00082 float64_t CCombinedDotFeatures::dot(int32_t vec_idx1, int32_t vec_idx2)
00083 {
00084 float64_t result=0;
00085
00086 CListElement<CDotFeatures*> * current = NULL ;
00087 CDotFeatures* f=get_first_feature_obj(current);
00088
00089 while (f)
00090 {
00091 result += f->dot(vec_idx1, vec_idx2)*CMath::sq(f->get_combined_feature_weight());
00092 f=get_next_feature_obj(current);
00093 }
00094
00095 return result;
00096 }
00097
00098 float64_t CCombinedDotFeatures::dense_dot(int32_t vec_idx1, const float64_t* vec2, int32_t vec2_len)
00099 {
00100 float64_t result=0;
00101
00102 CListElement<CDotFeatures*> * current = NULL ;
00103 CDotFeatures* f=get_first_feature_obj(current);
00104 uint32_t offs=0;
00105
00106 while (f)
00107 {
00108 int32_t dim = f->get_dim_feature_space();
00109 result += f->dense_dot(vec_idx1, vec2+offs, dim)*f->get_combined_feature_weight();
00110 offs += dim;
00111 f=get_next_feature_obj(current);
00112 }
00113
00114 return result;
00115 }
00116
00117 void CCombinedDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val)
00118 {
00119 CListElement<CDotFeatures*> * current = NULL ;
00120 CDotFeatures* f=get_first_feature_obj(current);
00121 uint32_t offs=0;
00122
00123 while (f)
00124 {
00125 int32_t dim = f->get_dim_feature_space();
00126 f->add_to_dense_vec(alpha*f->get_combined_feature_weight(), vec_idx1, vec2+offs, dim, abs_val);
00127 offs += dim;
00128 f=get_next_feature_obj(current);
00129 }
00130 }
00131
00132
00133 int32_t CCombinedDotFeatures::get_nnz_features_for_vector(int32_t num)
00134 {
00135 CListElement<CDotFeatures*> * current = NULL ;
00136 CDotFeatures* f=get_first_feature_obj(current);
00137 int32_t result=0;
00138
00139 while (f)
00140 {
00141 result+=f->get_nnz_features_for_vector(num);
00142 f=get_next_feature_obj(current);
00143 }
00144
00145 return result;
00146 }
00147
00148 void CCombinedDotFeatures::get_subfeature_weights(float64_t** weights, int32_t* num_weights)
00149 {
00150 *num_weights = get_num_feature_obj();
00151 ASSERT(*num_weights > 0);
00152
00153 *weights=new float64_t[*num_weights];
00154 float64_t* w = *weights;
00155
00156 CListElement<CDotFeatures*> * current = NULL;
00157 CDotFeatures* f = get_first_feature_obj(current);
00158
00159 while (f)
00160 {
00161 *w++=f->get_combined_feature_weight();
00162 f = get_next_feature_obj(current);
00163 }
00164 }
00165
00166 void CCombinedDotFeatures::set_subfeature_weights(
00167 float64_t* weights, int32_t num_weights)
00168 {
00169 int32_t i=0 ;
00170 CListElement<CDotFeatures*> * current = NULL ;
00171 CDotFeatures* f = get_first_feature_obj(current);
00172
00173 ASSERT(num_weights==get_num_feature_obj());
00174
00175 while(f)
00176 {
00177 f->set_combined_feature_weight(weights[i]);
00178 f = get_next_feature_obj(current);
00179 i++;
00180 }
00181 }