DistanceKernel.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 Christian Gehl
00008  * Copyright (C) 2007-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include "lib/config.h"
00012 
00013 #include "lib/common.h"
00014 #include "lib/io.h"
00015 #include "kernel/DistanceKernel.h"
00016 #include "features/SimpleFeatures.h"
00017 
00018 using namespace shogun;
00019 
00020 CDistanceKernel::CDistanceKernel(int32_t size, float64_t w, CDistance* d)
00021 : CKernel(size), distance(d), width(w)
00022 {
00023     ASSERT(distance);
00024     SG_REF(distance);
00025 }
00026 
00027 CDistanceKernel::CDistanceKernel(
00028     CFeatures *l, CFeatures *r, float64_t w , CDistance* d)
00029 : CKernel(10), distance(d), width(w)
00030 {
00031     ASSERT(distance);
00032     SG_REF(distance);
00033     init(l, r);
00034 }
00035 
00036 CDistanceKernel::~CDistanceKernel()
00037 {
00038     // important to have the cleanup of CKernel first, it calls get_name which
00039     // uses the distance
00040     cleanup();
00041     SG_UNREF(distance);
00042 }
00043 
00044 bool CDistanceKernel::init(CFeatures* l, CFeatures* r)
00045 {
00046     ASSERT(distance);
00047     CKernel::init(l,r);
00048     distance->init(l,r);
00049     return init_normalizer();
00050 }
00051 
00052 float64_t CDistanceKernel::compute(int32_t idx_a, int32_t idx_b)
00053 {
00054     float64_t result=distance->distance(idx_a, idx_b);
00055     return exp(-result/width);
00056 }

SHOGUN Machine Learning Toolbox - Documentation