SHOGUN v0.9.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) 2010 Koen van de Sande 00008 * Copyright (C) 2010 Koen van de Sande / University of Amsterdam 00009 */ 00010 00011 #include "lib/common.h" 00012 #include "kernel/HistogramIntersectionKernel.h" 00013 #include "features/Features.h" 00014 #include "features/SimpleFeatures.h" 00015 #include "lib/io.h" 00016 00017 using namespace shogun; 00018 00019 CHistogramIntersectionKernel::CHistogramIntersectionKernel(void) 00020 : CDotKernel(0) 00021 { 00022 SG_UNSTABLE("CHistogramIntersectionKernel::" 00023 "CHistogramIntersectionKernel(void)", "\n"); 00024 } 00025 00026 CHistogramIntersectionKernel::CHistogramIntersectionKernel(int32_t size) 00027 : CDotKernel(size) 00028 { 00029 } 00030 00031 CHistogramIntersectionKernel::CHistogramIntersectionKernel( 00032 CSimpleFeatures<float64_t>* l, CSimpleFeatures<float64_t>* r, int32_t size) 00033 : CDotKernel(size) 00034 { 00035 init(l,r); 00036 } 00037 00038 CHistogramIntersectionKernel::~CHistogramIntersectionKernel() 00039 { 00040 cleanup(); 00041 } 00042 00043 bool CHistogramIntersectionKernel::init(CFeatures* l, CFeatures* r) 00044 { 00045 bool result=CDotKernel::init(l,r); 00046 init_normalizer(); 00047 return result; 00048 } 00049 00050 float64_t CHistogramIntersectionKernel::compute(int32_t idx_a, int32_t idx_b) 00051 { 00052 int32_t alen, blen; 00053 bool afree, bfree; 00054 00055 float64_t* avec= 00056 ((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree); 00057 float64_t* bvec= 00058 ((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 result += (avec[i] < bvec[i]) ? avec[i] : bvec[i]; 00064 00065 ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree); 00066 ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree); 00067 00068 return result; 00069 }