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 "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     return false;
00046 }
00047 
00049 bool CNormOne::save(FILE* f)
00050 {
00051     return false;
00052 }
00053 
00057 float64_t* CNormOne::apply_to_feature_matrix(CFeatures* f)
00058 {
00059     int32_t num_vec;
00060     int32_t num_feat;
00061     float64_t* matrix=((CSimpleFeatures<float64_t>*) f)->get_feature_matrix(num_feat, num_vec);
00062 
00063     for (int32_t i=0; i<num_vec; i++)
00064     {
00065         float64_t* vec=&matrix[i*num_feat];
00066         float64_t norm=CMath::sqrt(CMath::dot(vec, vec, num_feat));
00067         CMath::scale_vector(1.0/norm, vec, num_feat);
00068     }
00069     return matrix;
00070 }
00071 
00074 float64_t* CNormOne::apply_to_feature_vector(float64_t* f, int32_t& len)
00075 {
00076     float64_t* vec=new float64_t[len];
00077     float64_t norm=CMath::sqrt(CMath::dot(f, f, len));
00078 
00079     for (int32_t i=0; i<len; i++)
00080         vec[i]=f[i]/norm;
00081 
00082     return vec;
00083 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation