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) 2009 Soeren Sonnenburg 00008 * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society 00009 */ 00010 00011 #ifndef _AVGDIAGKERNELNORMALIZER_H___ 00012 #define _AVGDIAGKERNELNORMALIZER_H___ 00013 00014 #include "kernel/KernelNormalizer.h" 00015 namespace shogun 00016 { 00031 class CAvgDiagKernelNormalizer : public CKernelNormalizer 00032 { 00033 public: 00039 CAvgDiagKernelNormalizer(float64_t c=0.0) 00040 { 00041 scale=c; 00042 } 00043 00045 virtual ~CAvgDiagKernelNormalizer() 00046 { 00047 } 00048 00051 virtual bool init(CKernel* k) 00052 { 00053 if (scale<=0) 00054 { 00055 ASSERT(k); 00056 int32_t num=k->get_num_vec_lhs(); 00057 ASSERT(num>0); 00058 00059 CFeatures* old_lhs=k->lhs; 00060 CFeatures* old_rhs=k->rhs; 00061 k->lhs=old_lhs; 00062 k->rhs=old_lhs; 00063 00064 float64_t sum=0; 00065 for (int32_t i=0; i<num; i++) 00066 sum+=k->compute(i, i); 00067 00068 scale=sum/num; 00069 k->lhs=old_lhs; 00070 k->rhs=old_rhs; 00071 } 00072 00073 return true; 00074 } 00075 00081 inline virtual float64_t normalize( 00082 float64_t value, int32_t idx_lhs, int32_t idx_rhs) 00083 { 00084 return value/scale; 00085 } 00086 00091 inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs) 00092 { 00093 return value/sqrt(scale); 00094 } 00095 00100 inline virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs) 00101 { 00102 return value/sqrt(scale); 00103 } 00104 00106 inline virtual const char* get_name() const { return "AvgDiagKernelNormalizer"; } 00107 00108 protected: 00110 float64_t scale; 00111 }; 00112 } 00113 #endif