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

SHOGUN Machine Learning Toolbox - Documentation