SHOGUN
v2.0.0
|
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) 2006-2009 Christian Gehl 00008 * Copyright (C) 2006-2009 Fraunhofer Institute FIRST 00009 */ 00010 00011 #include <shogun/lib/config.h> 00012 #include <shogun/lib/common.h> 00013 #include <shogun/io/SGIO.h> 00014 #include <shogun/distance/JensenMetric.h> 00015 #include <shogun/features/Features.h> 00016 00017 using namespace shogun; 00018 00019 CJensenMetric::CJensenMetric() : CDenseDistance<float64_t>() 00020 { 00021 } 00022 00023 CJensenMetric::CJensenMetric(CDenseFeatures<float64_t>* l, CDenseFeatures<float64_t>* r) 00024 : CDenseDistance<float64_t>() 00025 { 00026 init(l, r); 00027 } 00028 00029 CJensenMetric::~CJensenMetric() 00030 { 00031 cleanup(); 00032 } 00033 00034 bool CJensenMetric::init(CFeatures* l, CFeatures* r) 00035 { 00036 return CDenseDistance<float64_t>::init(l,r); 00037 } 00038 00039 void CJensenMetric::cleanup() 00040 { 00041 } 00042 00043 float64_t CJensenMetric::compute(int32_t idx_a, int32_t idx_b) 00044 { 00045 int32_t alen, blen; 00046 bool afree, bfree; 00047 00048 float64_t* avec= 00049 ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree); 00050 float64_t* bvec= 00051 ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree); 00052 00053 ASSERT(alen==blen); 00054 00055 float64_t absTmp = 0; 00056 float64_t result=0; 00057 { 00058 for (int32_t i=0; i<alen; i++) 00059 { 00060 absTmp=0.5*(avec[i]+bvec[i]); 00061 if (absTmp>0) 00062 { 00063 if(avec[i]>0) 00064 result+=avec[i]*log(avec[i]/absTmp); 00065 if(bvec[i]>0) 00066 result+=bvec[i]*log(bvec[i]/absTmp); 00067 } 00068 } 00069 00070 } 00071 00072 ((CDenseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree); 00073 ((CDenseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree); 00074 00075 00076 return result; 00077 }