Chi2Kernel.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "lib/common.h"
00012 #include "kernel/Chi2Kernel.h"
00013 #include "features/Features.h"
00014 #include "features/RealFeatures.h"
00015 #include "lib/io.h"
00016
00017 CChi2Kernel::CChi2Kernel(INT size, DREAL w)
00018 : CSimpleKernel<DREAL>(size), width(w)
00019 {
00020 }
00021
00022 CChi2Kernel::CChi2Kernel(CRealFeatures* l, CRealFeatures* r, DREAL w, INT size)
00023 : CSimpleKernel<DREAL>(size), width(w)
00024 {
00025 init(l,r);
00026 }
00027
00028 CChi2Kernel::~CChi2Kernel()
00029 {
00030 cleanup();
00031 }
00032
00033 bool CChi2Kernel::init(CFeatures* l, CFeatures* r)
00034 {
00035 bool result=CSimpleKernel<DREAL>::init(l,r);
00036 return result;
00037 }
00038
00039 bool CChi2Kernel::load_init(FILE* src)
00040 {
00041 return false;
00042 }
00043
00044 bool CChi2Kernel::save_init(FILE* dest)
00045 {
00046 return false;
00047 }
00048
00049 DREAL CChi2Kernel::compute(INT idx_a, INT idx_b)
00050 {
00051 INT alen, blen;
00052 bool afree, bfree;
00053
00054 double* avec=((CRealFeatures*) lhs)->get_feature_vector(idx_a, alen, afree);
00055 double* bvec=((CRealFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree);
00056 ASSERT(alen==blen);
00057
00058 DREAL result=0;
00059 for (INT i=0; i<alen; i++)
00060 {
00061 DREAL n=avec[i]-bvec[i];
00062 DREAL d=avec[i]+bvec[i];
00063 if (d!=0)
00064 result+=n*n/d;
00065 }
00066
00067 result=exp(-result/width);
00068
00069 ((CRealFeatures*) lhs)->free_feature_vector(avec, idx_a, afree);
00070 ((CRealFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00071
00072 return result;
00073 }