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

SHOGUN Machine Learning Toolbox - Documentation