AttenuatedEuclidianDistance.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) 2011 Miguel Angel Bautista
00008  * Copyright (C) 2011 Berlin Institute of Technology and Max Planck Society
00009  */
00010 
00011 #include <shogun/lib/common.h>
00012 #include <shogun/io/SGIO.h>
00013 #include <shogun/distance/AttenuatedEuclidianDistance.h>
00014 #include <shogun/features/Features.h>
00015 #include <shogun/features/SimpleFeatures.h>
00016 
00017 using namespace shogun;
00018 
00019 CAttenuatedEuclidianDistance::CAttenuatedEuclidianDistance() : CRealDistance()
00020 {
00021     init();
00022 }
00023 
00024 CAttenuatedEuclidianDistance::CAttenuatedEuclidianDistance(CSimpleFeatures<float64_t>* l, CSimpleFeatures<float64_t>* r)
00025 : CRealDistance()
00026 {
00027     init();
00028     init(l, r);
00029 }
00030 
00031 CAttenuatedEuclidianDistance::~CAttenuatedEuclidianDistance()
00032 {
00033     cleanup();
00034 }
00035 
00036 bool CAttenuatedEuclidianDistance::init(CFeatures* l, CFeatures* r)
00037 {
00038     CRealDistance::init(l, r);
00039     return true;
00040 }
00041 
00042 void CAttenuatedEuclidianDistance::cleanup()
00043 {
00044 }
00045 
00046 float64_t CAttenuatedEuclidianDistance::compute(int32_t idx_a, int32_t idx_b)
00047 {
00048     int32_t alen, blen;
00049     bool afree, bfree;
00050     float64_t result=0;
00051 
00052     float64_t* avec=((CSimpleFeatures<float64_t>*) lhs)->
00053         get_feature_vector(idx_a, alen, afree);
00054     float64_t* bvec=((CSimpleFeatures<float64_t>*) rhs)->
00055         get_feature_vector(idx_b, blen, bfree);
00056     ASSERT(alen==blen);
00057 
00058     for (int32_t i=0; i<alen; i++)
00059         result+=(CMath::abs(avec[i])*CMath::abs(bvec[i]))*CMath::pow(avec[i] - bvec[i],2);
00060 
00061     ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
00062     ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00063 
00064     if (disable_sqrt)
00065         return result;
00066 
00067     return CMath::sqrt(result);
00068 }
00069 
00070 void CAttenuatedEuclidianDistance::init()
00071 {
00072     disable_sqrt=false;
00073 
00074     m_parameters->add(&disable_sqrt, "disable_sqrt", "If sqrt shall not be applied.");
00075 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation