SHOGUN
v1.1.0
|
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 RafaĆ Surowiecki 00008 * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society 00009 */ 00010 00011 #include <shogun/kernel/RationalQuadraticKernel.h> 00012 #include <shogun/mathematics/Math.h> 00013 00014 using namespace shogun; 00015 00016 CRationalQuadraticKernel::CRationalQuadraticKernel(): CKernel(0), m_distance(NULL), m_coef(0.001) 00017 { 00018 init(); 00019 } 00020 00021 CRationalQuadraticKernel::CRationalQuadraticKernel(int32_t cache, float64_t coef, CDistance* distance) 00022 : CKernel(cache), m_distance(distance), m_coef(coef) 00023 { 00024 ASSERT(m_distance); 00025 SG_REF(m_distance); 00026 init(); 00027 } 00028 00029 CRationalQuadraticKernel::CRationalQuadraticKernel(CFeatures *l, CFeatures *r, float64_t coef, CDistance* dist) 00030 : CKernel(10), m_distance(dist), m_coef(coef) 00031 { 00032 ASSERT(m_distance); 00033 SG_REF(m_distance); 00034 init(); 00035 init(l, r); 00036 } 00037 00038 CRationalQuadraticKernel::~CRationalQuadraticKernel() 00039 { 00040 cleanup(); 00041 SG_UNREF(m_distance); 00042 } 00043 00044 bool CRationalQuadraticKernel::init(CFeatures* l, CFeatures* r) 00045 { 00046 ASSERT(m_distance); 00047 CKernel::init(l,r); 00048 m_distance->init(l,r); 00049 return init_normalizer(); 00050 } 00051 00052 float64_t CRationalQuadraticKernel::compute(int32_t idx_a, int32_t idx_b) 00053 { 00054 float64_t dist = m_distance->distance(idx_a, idx_b); 00055 float64_t pDist = dist * dist; 00056 return 1-pDist/(pDist+m_coef); 00057 } 00058 00059 void CRationalQuadraticKernel::init() 00060 { 00061 m_parameters->add(&m_coef, "coef", "Kernel coefficient."); 00062 m_parameters->add((CSGObject**) &m_distance, "distance", "Distance to be used."); 00063 } 00064