SphericalKernel.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  * Based on GaussianKernel, Written (W) 1999-2010 Soeren Sonnenburg
00008  * Written (W) 2011 Shashwat Lal Das
00009  * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
00010  */
00011 
00012 #include <shogun/kernel/SphericalKernel.h>
00013 #include <shogun/mathematics/Math.h>
00014 
00015 using namespace shogun;
00016 
00017 CSphericalKernel::CSphericalKernel(): CKernel(0), distance(NULL)
00018 {
00019     init();
00020     set_sigma(1.0);
00021 }
00022 
00023 CSphericalKernel::CSphericalKernel(int32_t size, float64_t sig, CDistance* dist)
00024 : CKernel(size), distance(dist)
00025 {
00026     ASSERT(distance);
00027     SG_REF(distance);
00028     init();
00029     set_sigma(sig);
00030 }
00031 
00032 CSphericalKernel::CSphericalKernel(
00033     CFeatures *l, CFeatures *r, float64_t sig, CDistance* dist)
00034 : CKernel(10), distance(dist)
00035 {
00036     ASSERT(distance);
00037     SG_REF(distance);
00038     init();
00039     set_sigma(sig);
00040     init(l, r);
00041 }
00042 
00043 CSphericalKernel::~CSphericalKernel()
00044 {
00045     cleanup();
00046     SG_UNREF(distance);
00047 }
00048 
00049 bool CSphericalKernel::init(CFeatures* l, CFeatures* r)
00050 {
00051     ASSERT(distance);
00052     CKernel::init(l,r);
00053     distance->init(l,r);
00054     return init_normalizer();
00055 }
00056 
00057 void CSphericalKernel::init()
00058 {
00059     SG_ADD((CSGObject**) &distance, "distance", "Distance to be used.",
00060         MS_AVAILABLE);
00061     SG_ADD(&sigma, "sigma", "Sigma kernel parameter.", MS_AVAILABLE);
00062 }
00063 
00064 float64_t CSphericalKernel::compute(int32_t idx_a, int32_t idx_b)
00065 {
00066     float64_t dist=distance->distance(idx_a, idx_b);
00067     float64_t ds_ratio=dist/sigma;
00068 
00069     if (dist < sigma)
00070         return 1.0-1.5*(ds_ratio)+0.5*(ds_ratio*ds_ratio*ds_ratio);
00071     else
00072         return 0;
00073 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation