MinkowskiMetric.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) 2006 Christian Gehl
00008  * Copyright (C) 1999-2008 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/RealFeatures.h"
00017 
00018 CMinkowskiMetric::CMinkowskiMetric(float64_t k_)
00019 : CSimpleDistance<float64_t>(), k(k_)
00020 {
00021 }
00022 
00023 CMinkowskiMetric::CMinkowskiMetric(
00024     CRealFeatures* l, CRealFeatures* r, float64_t k_)
00025 : CSimpleDistance<float64_t>(), k(k_)
00026 {
00027     init(l, r);
00028 }
00029 
00030 CMinkowskiMetric::~CMinkowskiMetric()
00031 {
00032     cleanup();
00033 }
00034 
00035 bool CMinkowskiMetric::init(CFeatures* l, CFeatures* r)
00036 {
00037     bool result=CSimpleDistance<float64_t>::init(l,r);
00038 
00039     return result;
00040 }
00041 
00042 void CMinkowskiMetric::cleanup()
00043 {
00044 }
00045 
00046 bool CMinkowskiMetric::load_init(FILE* src)
00047 {
00048     return false;
00049 }
00050 
00051 bool CMinkowskiMetric::save_init(FILE* dest)
00052 {
00053     return false;
00054 }
00055 
00056 float64_t CMinkowskiMetric::compute(int32_t idx_a, int32_t idx_b)
00057 {
00058     int32_t alen, blen;
00059     bool afree, bfree;
00060 
00061     float64_t* avec=
00062         ((CRealFeatures*) lhs)->get_feature_vector(idx_a, alen, afree);
00063     float64_t* bvec=
00064         ((CRealFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree);
00065 
00066     ASSERT(alen==blen);
00067 
00068     float64_t absTmp = 0;
00069     float64_t result=0;
00070     {
00071         for (int32_t i=0; i<alen; i++)
00072         {
00073             absTmp=fabs(avec[i]-bvec[i]);
00074             result+=pow(absTmp,k);
00075         }
00076 
00077     }
00078 
00079     ((CRealFeatures*) lhs)->free_feature_vector(avec, idx_a, afree);
00080     ((CRealFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00081 
00082     return pow(result,1/k);
00083 }

SHOGUN Machine Learning Toolbox - Documentation