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-2008 Soeren Sonnenburg
00008  * Copyright (C) 1999-2008 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/RealFeatures.h"
00016 
00017 CNormOne::CNormOne()
00018 : CSimplePreProc<DREAL>("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 DREAL* CNormOne::apply_to_feature_matrix(CFeatures* f)
00056 {
00057     INT i,j;
00058     INT num_vec;
00059     INT num_feat;
00060     DREAL* matrix=((CRealFeatures*) f)->get_feature_matrix(num_feat, num_vec);
00061 
00062     for (i=0; i<num_vec; i++)
00063     {
00064         DREAL sqnorm=0;
00065         DREAL norm=0;
00066         DREAL* vec=&matrix[i*num_feat];
00067 
00068         for (j=0; j<num_feat; j++)
00069         {
00070             if (vec[j]>1e100)
00071                 vec[j]=0;
00072             sqnorm+=vec[j]*vec[j];
00073         }
00074 
00075         norm=sqrt(sqnorm);
00076 
00077         for (j=0; j<num_feat; j++)
00078             vec[j]/=norm;
00079     }
00080     return matrix;
00081 }
00082 
00085 DREAL* CNormOne::apply_to_feature_vector(DREAL* f, INT& len)
00086 {
00087     DREAL* vec=new DREAL[len];
00088     DREAL sqnorm=0;
00089     DREAL norm=0;
00090     INT i=0;
00091 
00092     for (i=0; i<len; i++)
00093         sqnorm+=f[i]*f[i];
00094 
00095     norm=sqrt(sqnorm);
00096 
00097     for (i=0; i<len; i++)
00098         vec[i]=f[i]/norm;
00099 
00100     return vec;
00101 }
00102 
00104 bool CNormOne::load_init_data(FILE* src)
00105 {
00106     return true;
00107 }
00108 
00110 bool CNormOne::save_init_data(FILE* dst)
00111 {
00112     return true;
00113 }

SHOGUN Machine Learning Toolbox - Documentation