TanimotoKernelNormalizer.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef _TANIMOTOKERNELNORMALIZER_H___
00012 #define _TANIMOTOKERNELNORMALIZER_H___
00013
00014 #include "kernel/KernelNormalizer.h"
00015 #include "kernel/CommWordStringKernel.h"
00016
00025 class CTanimotoKernelNormalizer : public CKernelNormalizer
00026 {
00027 public:
00032 CTanimotoKernelNormalizer(bool use_opt_diag=false) : diag_lhs(NULL),
00033 diag_rhs(NULL), use_optimized_diagonal_computation(use_opt_diag)
00034 {
00035 }
00036
00038 virtual ~CTanimotoKernelNormalizer()
00039 {
00040 delete[] diag_lhs;
00041 delete[] diag_rhs;
00042 }
00043
00046 virtual bool init(CKernel* k)
00047 {
00048 ASSERT(k);
00049 int32_t num_lhs=k->get_num_vec_lhs();
00050 int32_t num_rhs=k->get_num_vec_rhs();
00051 ASSERT(num_lhs>0);
00052 ASSERT(num_rhs>0);
00053
00054 CFeatures* old_lhs=k->lhs;
00055 CFeatures* old_rhs=k->rhs;
00056
00057 k->lhs=old_lhs;
00058 k->rhs=old_lhs;
00059 bool r1=alloc_and_compute_diag(k, diag_lhs, num_lhs);
00060
00061 k->lhs=old_rhs;
00062 k->rhs=old_rhs;
00063 bool r2=alloc_and_compute_diag(k, diag_rhs, num_rhs);
00064
00065 k->lhs=old_lhs;
00066 k->rhs=old_rhs;
00067
00068 return r1 && r2;
00069 }
00070
00076 inline virtual float64_t normalize(
00077 float64_t value, int32_t idx_lhs, int32_t idx_rhs)
00078 {
00079 float64_t diag_sum=diag_lhs[idx_lhs]*diag_rhs[idx_rhs];
00080 return value/(diag_sum-value);
00081 }
00082
00087 inline virtual float64_t normalize_lhs(float64_t value, int32_t idx_lhs)
00088 {
00089 SG_ERROR("linadd not supported with Tanimoto normalization.\n");
00090 return 0;
00091 }
00092
00097 inline virtual float64_t normalize_rhs(float64_t value, int32_t idx_rhs)
00098 {
00099 SG_ERROR("linadd not supported with Tanimoto normalization.\n");
00100 return 0;
00101 }
00102
00103 public:
00108 bool alloc_and_compute_diag(CKernel* k, float64_t* &v, int32_t num)
00109 {
00110 delete[] v;
00111 v=new float64_t[num];
00112
00113 for (int32_t i=0; i<num; i++)
00114 {
00115 if (k->get_kernel_type() == K_COMMWORDSTRING)
00116 {
00117 if (use_optimized_diagonal_computation)
00118 v[i]=((CCommWordStringKernel*) k)->compute_diag(i);
00119 else
00120 v[i]=((CCommWordStringKernel*) k)->compute_helper(i,i, true);
00121 }
00122 else
00123 v[i]=k->compute(i,i);
00124
00125 if (v[i]==0.0)
00126 v[i]=1e-16;
00127 }
00128
00129 return (v!=NULL);
00130 }
00131
00132 protected:
00134 float64_t* diag_lhs;
00136 float64_t* diag_rhs;
00138 bool use_optimized_diagonal_computation;
00139 };
00140
00141 #endif