SplineKernel.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 Sergey Bartunov
00008  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  * Copyright (C) 2010 Berlin Institute of Technology
00010  */
00011 
00012 #include <shogun/lib/config.h>
00013 #include <shogun/lib/common.h>
00014 #include <shogun/io/SGIO.h>
00015 #include <shogun/kernel/SplineKernel.h>
00016 #include <shogun/features/DotFeatures.h>
00017 #include <shogun/features/SimpleFeatures.h>
00018 
00019 using namespace shogun;
00020 
00021 CSplineKernel::CSplineKernel() : CDotKernel()
00022 {
00023 }
00024 
00025 CSplineKernel::CSplineKernel(CDotFeatures* l, CDotFeatures* r) : CDotKernel()
00026 {
00027     init(l,r);
00028 }
00029 
00030 CSplineKernel::~CSplineKernel()
00031 {
00032     cleanup();
00033 }
00034 
00035 bool CSplineKernel::init(CFeatures* l, CFeatures* r)
00036 {
00037     ASSERT(l->get_feature_type()==F_DREAL);
00038     ASSERT(l->get_feature_type()==r->get_feature_type());
00039 
00040     ASSERT(l->get_feature_class()==C_SIMPLE);
00041     ASSERT(l->get_feature_class()==r->get_feature_class());
00042 
00043     CDotKernel::init(l,r);
00044     return init_normalizer();
00045 }
00046 
00047 void CSplineKernel::cleanup()
00048 {
00049     CKernel::cleanup();
00050 }
00051 
00052 float64_t CSplineKernel::compute(int32_t idx_a, int32_t idx_b)
00053 {
00054     int32_t alen, blen;
00055     bool afree, bfree;
00056 
00057     float64_t* avec = ((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
00058     float64_t* bvec = ((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
00059     ASSERT(alen == blen);
00060 
00061     float64_t result = 0;
00062     for (int32_t i = 0; i < alen; i++) {
00063         const float64_t x = avec[i], y = bvec[i];
00064         const float64_t min = CMath::min(avec[i], bvec[i]);
00065         result += 1 + x*y + x*y*min - ((x+y)/2)*min*min + min*min*min/3;
00066     }
00067 
00068     ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
00069     ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00070 
00071     return result;
00072 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation