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 CNormOne::CNormOne()
00018 : CSimplePreProc<float64_t>("NormOne", "NRM1")
00019 {
00020 }
00021 
00022 CNormOne::~CNormOne()
00023 {
00024 }
00025 
00027 bool CNormOne::init(CFeatures* f)
00028 {
00029     ASSERT(f->get_feature_class()==C_SIMPLE);
00030     ASSERT(f->get_feature_type()==F_DREAL);
00031 
00032     return true;
00033 }
00034 
00036 void CNormOne::cleanup()
00037 {
00038 }
00039 
00041 bool CNormOne::load(FILE* f)
00042 {
00043     return false;
00044 }
00045 
00047 bool CNormOne::save(FILE* f)
00048 {
00049     return false;
00050 }
00051 
00055 float64_t* CNormOne::apply_to_feature_matrix(CFeatures* f)
00056 {
00057     int32_t num_vec;
00058     int32_t num_feat;
00059     float64_t* matrix=((CSimpleFeatures<float64_t>*) f)->get_feature_matrix(num_feat, num_vec);
00060 
00061     for (int32_t i=0; i<num_vec; i++)
00062     {
00063         float64_t* vec=&matrix[i*num_feat];
00064         float64_t norm=CMath::sqrt(CMath::dot(vec, vec, num_feat));
00065         CMath::scale_vector(1.0/norm, vec, num_feat);
00066     }
00067     return matrix;
00068 }
00069 
00072 float64_t* CNormOne::apply_to_feature_vector(float64_t* f, int32_t& len)
00073 {
00074     float64_t* vec=new float64_t[len];
00075     float64_t norm=CMath::sqrt(CMath::dot(f, f, len));
00076 
00077     for (int32_t i=0; i<len; i++)
00078         vec[i]=f[i]/norm;
00079 
00080     return vec;
00081 }
00082 
00084 bool CNormOne::load_init_data(FILE* src)
00085 {
00086     return true;
00087 }
00088 
00090 bool CNormOne::save_init_data(FILE* dst)
00091 {
00092     return true;
00093 }

SHOGUN Machine Learning Toolbox - Documentation