NormOne.cpp

Go to the documentation of this file.
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/DensePreprocessor.h>
00013 #include <shogun/mathematics/Math.h>
00014 #include <shogun/features/Features.h>
00015 
00016 using namespace shogun;
00017 
00018 CNormOne::CNormOne()
00019 : CDensePreprocessor<float64_t>()
00020 {
00021 }
00022 
00023 CNormOne::~CNormOne()
00024 {
00025 }
00026 
00028 bool CNormOne::init(CFeatures* features)
00029 {
00030     ASSERT(features->get_feature_class()==C_DENSE);
00031     ASSERT(features->get_feature_type()==F_DREAL);
00032 
00033     return true;
00034 }
00035 
00037 void CNormOne::cleanup()
00038 {
00039 }
00040 
00042 bool CNormOne::load(FILE* f)
00043 {
00044     SG_SET_LOCALE_C;
00045     SG_RESET_LOCALE;
00046     return false;
00047 }
00048 
00050 bool CNormOne::save(FILE* f)
00051 {
00052     SG_SET_LOCALE_C;
00053     SG_RESET_LOCALE;
00054     return false;
00055 }
00056 
00060 SGMatrix<float64_t> CNormOne::apply_to_feature_matrix(CFeatures* features)
00061 {
00062     SGMatrix<float64_t> feature_matrix=((CDenseFeatures<float64_t>*)features)->get_feature_matrix();
00063 
00064     for (int32_t i=0; i<feature_matrix.num_cols; i++)
00065     {
00066         float64_t* vec= &(feature_matrix.matrix[i*feature_matrix.num_rows]);
00067         float64_t norm=CMath::sqrt(SGVector<float64_t>::dot(vec, vec, feature_matrix.num_rows));
00068         SGVector<float64_t>::scale_vector(1.0/norm, vec, feature_matrix.num_rows);
00069     }
00070     return feature_matrix;
00071 }
00072 
00075 SGVector<float64_t> CNormOne::apply_to_feature_vector(SGVector<float64_t> vector)
00076 {
00077     float64_t* normed_vec = SG_MALLOC(float64_t, vector.vlen);
00078     float64_t norm=CMath::sqrt(SGVector<float64_t>::dot(vector.vector, vector.vector, vector.vlen));
00079 
00080     for (int32_t i=0; i<vector.vlen; i++)
00081         normed_vec[i]=vector.vector[i]/norm;
00082 
00083     return SGVector<float64_t>(normed_vec,vector.vlen);
00084 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation