SparseGaussianKernel.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) 1999-2008 Soeren Sonnenburg
00008  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include "lib/common.h"
00012 #include "lib/io.h"
00013 #include "kernel/SparseGaussianKernel.h"
00014 #include "features/Features.h"
00015 #include "features/SparseFeatures.h"
00016 
00017 CSparseGaussianKernel::CSparseGaussianKernel(int32_t size, float64_t w)
00018 : CSparseKernel<float64_t>(size), width(w), sq_lhs(NULL), sq_rhs(NULL)
00019 {
00020 }
00021 
00022 CSparseGaussianKernel::CSparseGaussianKernel(
00023     CSparseFeatures<float64_t>* l, CSparseFeatures<float64_t>* r, float64_t w)
00024 : CSparseKernel<float64_t>(10), width(w), sq_lhs(NULL), sq_rhs(NULL)
00025 {
00026     init(l, r);
00027 }
00028 
00029 CSparseGaussianKernel::~CSparseGaussianKernel()
00030 {
00031     cleanup();
00032 }
00033 
00034 bool CSparseGaussianKernel::init(CFeatures* l, CFeatures* r)
00035 {
00037     cleanup();
00038 
00039     CSparseKernel<float64_t>::init(l, r);
00040 
00041     sq_lhs=new float64_t[lhs->get_num_vectors()];
00042     sq_lhs=((CSparseFeatures<float64_t>*) lhs)->compute_squared(sq_lhs);
00043     if (lhs==rhs)
00044         sq_rhs=sq_lhs;
00045     else
00046     {
00047         sq_rhs=new float64_t[rhs->get_num_vectors()];
00048         sq_rhs=((CSparseFeatures<float64_t>*) rhs)->compute_squared(sq_rhs);
00049     }
00050 
00051     return init_normalizer();
00052 }
00053 
00054 void CSparseGaussianKernel::cleanup()
00055 {
00056     if (sq_lhs != sq_rhs)
00057         delete[] sq_rhs;
00058     sq_rhs = NULL;
00059 
00060     delete[] sq_lhs;
00061     sq_lhs = NULL;
00062 
00063     CKernel::cleanup();
00064 }
00065 
00066 bool CSparseGaussianKernel::load_init(FILE* src)
00067 {
00068     return false;
00069 }
00070 
00071 bool CSparseGaussianKernel::save_init(FILE* dest)
00072 {
00073     return false;
00074 }
00075 
00076 float64_t CSparseGaussianKernel::compute(int32_t idx_a, int32_t idx_b)
00077 {
00078     //float64_t result = sq_lhs[idx_a] + sq_rhs[idx_b];
00079     float64_t result=((CSparseFeatures<float64_t>*) lhs)->compute_squared_norm(
00080         (CSparseFeatures<float64_t>*) lhs, sq_lhs, idx_a,
00081         (CSparseFeatures<float64_t>*) rhs, sq_rhs, idx_b);
00082     return exp(-result/width);
00083 }

SHOGUN Machine Learning Toolbox - Documentation