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-2009 Soeren Sonnenburg
00008  * Copyright (C) 2007-2009 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/SimpleFeatures.h"
00016 
00017 using namespace shogun;
00018 
00019 CEuclidianDistance::CEuclidianDistance()
00020 : CRealDistance()
00021 {
00022     disable_sqrt=false;
00023 }
00024 
00025 CEuclidianDistance::CEuclidianDistance(CSimpleFeatures<float64_t>* l, CSimpleFeatures<float64_t>* r)
00026 : CRealDistance()
00027 {
00028     disable_sqrt=false;
00029     init(l, r);
00030 }
00031 
00032 CEuclidianDistance::~CEuclidianDistance()
00033 {
00034     cleanup();
00035 }
00036 
00037 bool CEuclidianDistance::init(CFeatures* l, CFeatures* r)
00038 {
00039     CRealDistance::init(l, r);
00040 
00041     return true;
00042 }
00043 
00044 void CEuclidianDistance::cleanup()
00045 {
00046 }
00047 
00048 float64_t CEuclidianDistance::compute(int32_t idx_a, int32_t idx_b)
00049 {
00050     int32_t alen, blen;
00051     bool afree, bfree;
00052     float64_t result=0;
00053 
00054     float64_t* avec=((CSimpleFeatures<float64_t>*) lhs)->
00055         get_feature_vector(idx_a, alen, afree);
00056     float64_t* bvec=((CSimpleFeatures<float64_t>*) rhs)->
00057         get_feature_vector(idx_b, blen, bfree);
00058     ASSERT(alen==blen);
00059 
00060     for (int32_t i=0; i<alen; i++)
00061         result+=CMath::sq(avec[i] - bvec[i]);
00062 
00063     ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
00064     ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00065 
00066     if (disable_sqrt) {
00067         return result;
00068     } else {
00069         return CMath::sqrt(result);
00070     }
00071 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation