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 <shogun/preprocessor/NormOne.h> 00012 #include <shogun/preprocessor/SimplePreprocessor.h> 00013 #include <shogun/mathematics/Math.h> 00014 #include <shogun/features/Features.h> 00015 #include <shogun/features/SimpleFeatures.h> 00016 00017 using namespace shogun; 00018 00019 CNormOne::CNormOne() 00020 : CSimplePreprocessor<float64_t>() 00021 { 00022 } 00023 00024 CNormOne::~CNormOne() 00025 { 00026 } 00027 00029 bool CNormOne::init(CFeatures* features) 00030 { 00031 ASSERT(features->get_feature_class()==C_SIMPLE); 00032 ASSERT(features->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 SGMatrix<float64_t> CNormOne::apply_to_feature_matrix(CFeatures* features) 00062 { 00063 SGMatrix<float64_t> feature_matrix=((CSimpleFeatures<float64_t>*)features)->get_feature_matrix(); 00064 00065 for (int32_t i=0; i<feature_matrix.num_cols; i++) 00066 { 00067 float64_t* vec= &(feature_matrix.matrix[i*feature_matrix.num_rows]); 00068 float64_t norm=CMath::sqrt(CMath::dot(vec, vec, feature_matrix.num_rows)); 00069 CMath::scale_vector(1.0/norm, vec, feature_matrix.num_rows); 00070 } 00071 return feature_matrix; 00072 } 00073 00076 SGVector<float64_t> CNormOne::apply_to_feature_vector(SGVector<float64_t> vector) 00077 { 00078 float64_t* normed_vec = SG_MALLOC(float64_t, vector.vlen); 00079 float64_t norm=CMath::sqrt(CMath::dot(vector.vector, vector.vector, vector.vlen)); 00080 00081 for (int32_t i=0; i<vector.vlen; i++) 00082 normed_vec[i]=vector.vector[i]/norm; 00083 00084 return SGVector<float64_t>(normed_vec,vector.vlen); 00085 }