EuclidianDistance.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) 2007-2008 Soeren Sonnenburg
00008  * Copyright (C) 2007-2008 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include "lib/common.h"
00012 #include "lib/io.h"
00013 #include "distance/EuclidianDistance.h"
00014 #include "features/Features.h"
00015 #include "features/RealFeatures.h"
00016 
00017 CEuclidianDistance::CEuclidianDistance()
00018 : CRealDistance()
00019 {
00020     disable_sqrt=false;
00021 }
00022 
00023 CEuclidianDistance::CEuclidianDistance(CRealFeatures* l, CRealFeatures* r)
00024 : CRealDistance()
00025 {
00026     disable_sqrt=false;
00027     init(l, r);
00028 }
00029 
00030 CEuclidianDistance::~CEuclidianDistance()
00031 {
00032     cleanup();
00033 }
00034 
00035 bool CEuclidianDistance::init(CFeatures* l, CFeatures* r)
00036 {
00037     CRealDistance::init(l, r);
00038 
00039     return true;
00040 }
00041 
00042 void CEuclidianDistance::cleanup()
00043 {
00044 }
00045 
00046 bool CEuclidianDistance::load_init(FILE* src)
00047 {
00048     return false;
00049 }
00050 
00051 bool CEuclidianDistance::save_init(FILE* dest)
00052 {
00053     return false;
00054 }
00055 
00056 DREAL CEuclidianDistance::compute(INT idx_a, INT idx_b)
00057 {
00058     INT alen, blen;
00059     bool afree, bfree;
00060     DREAL result=0;
00061 
00062     DREAL* avec=((CRealFeatures*) lhs)->get_feature_vector(idx_a, alen, afree);
00063     DREAL* bvec=((CRealFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree);
00064     ASSERT(alen==blen);
00065 
00066     for (INT i=0; i<alen; i++)
00067         result+=CMath::sq(avec[i] - bvec[i]);
00068 
00069     ((CRealFeatures*) lhs)->free_feature_vector(avec, idx_a, afree);
00070     ((CRealFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00071 
00072     if (disable_sqrt) {
00073         return result;
00074     } else {
00075         return CMath::sqrt(result);
00076     }
00077 }

SHOGUN Machine Learning Toolbox - Documentation