00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 1999-2009 Soeren Sonnenburg 00008 * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society 00009 */ 00010 00011 #include "preproc/NormOne.h" 00012 #include "preproc/SimplePreProc.h" 00013 #include "lib/Mathematics.h" 00014 #include "features/Features.h" 00015 #include "features/SimpleFeatures.h" 00016 00017 using namespace shogun; 00018 00019 CNormOne::CNormOne() 00020 : CSimplePreProc<float64_t>("NormOne", "NRM1") 00021 { 00022 } 00023 00024 CNormOne::~CNormOne() 00025 { 00026 } 00027 00029 bool CNormOne::init(CFeatures* f) 00030 { 00031 ASSERT(f->get_feature_class()==C_SIMPLE); 00032 ASSERT(f->get_feature_type()==F_DREAL); 00033 00034 return true; 00035 } 00036 00038 void CNormOne::cleanup() 00039 { 00040 } 00041 00043 bool CNormOne::load(FILE* f) 00044 { 00045 SG_SET_LOCALE_C; 00046 SG_RESET_LOCALE; 00047 return false; 00048 } 00049 00051 bool CNormOne::save(FILE* f) 00052 { 00053 SG_SET_LOCALE_C; 00054 SG_RESET_LOCALE; 00055 return false; 00056 } 00057 00061 float64_t* CNormOne::apply_to_feature_matrix(CFeatures* f) 00062 { 00063 int32_t num_vec; 00064 int32_t num_feat; 00065 float64_t* matrix=((CSimpleFeatures<float64_t>*) f)->get_feature_matrix(num_feat, num_vec); 00066 00067 for (int32_t i=0; i<num_vec; i++) 00068 { 00069 float64_t* vec=&matrix[i*num_feat]; 00070 float64_t norm=CMath::sqrt(CMath::dot(vec, vec, num_feat)); 00071 CMath::scale_vector(1.0/norm, vec, num_feat); 00072 } 00073 return matrix; 00074 } 00075 00078 float64_t* CNormOne::apply_to_feature_vector(float64_t* f, int32_t& len) 00079 { 00080 float64_t* vec=new float64_t[len]; 00081 float64_t norm=CMath::sqrt(CMath::dot(f, f, len)); 00082 00083 for (int32_t i=0; i<len; i++) 00084 vec[i]=f[i]/norm; 00085 00086 return vec; 00087 }