|
SHOGUN v0.9.3
|
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 "lib/config.h" 00012 #include "lib/common.h" 00013 #include "lib/io.h" 00014 #include "distance/MinkowskiMetric.h" 00015 #include "features/Features.h" 00016 #include "features/SimpleFeatures.h" 00017 00018 using namespace shogun; 00019 00020 CMinkowskiMetric::CMinkowskiMetric(float64_t k_) 00021 : CSimpleDistance<float64_t>(), k(k_) 00022 { 00023 } 00024 00025 CMinkowskiMetric::CMinkowskiMetric( 00026 CSimpleFeatures<float64_t>* l, CSimpleFeatures<float64_t>* r, float64_t k_) 00027 : CSimpleDistance<float64_t>(), k(k_) 00028 { 00029 init(l, r); 00030 } 00031 00032 CMinkowskiMetric::~CMinkowskiMetric() 00033 { 00034 cleanup(); 00035 } 00036 00037 bool CMinkowskiMetric::init(CFeatures* l, CFeatures* r) 00038 { 00039 bool result=CSimpleDistance<float64_t>::init(l,r); 00040 00041 return result; 00042 } 00043 00044 void CMinkowskiMetric::cleanup() 00045 { 00046 } 00047 00048 float64_t CMinkowskiMetric::compute(int32_t idx_a, int32_t idx_b) 00049 { 00050 int32_t alen, blen; 00051 bool afree, bfree; 00052 00053 float64_t* avec= 00054 ((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree); 00055 float64_t* bvec= 00056 ((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree); 00057 00058 ASSERT(avec); 00059 ASSERT(bvec); 00060 ASSERT(alen==blen); 00061 00062 float64_t absTmp = 0; 00063 float64_t result=0; 00064 { 00065 for (int32_t i=0; i<alen; i++) 00066 { 00067 absTmp=fabs(avec[i]-bvec[i]); 00068 result+=pow(absTmp,k); 00069 } 00070 00071 } 00072 00073 ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree); 00074 ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree); 00075 00076 return pow(result,1/k); 00077 }