CustomKernel.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/CustomKernel.h"
00013 #include "features/Features.h"
00014 #include "lib/io.h"
00015
00016 CCustomKernel::CCustomKernel()
00017 : CKernel(10), kmatrix(NULL), num_rows(0), num_cols(0), upper_diagonal(false)
00018 {
00019 }
00020
00021 CCustomKernel::CCustomKernel(CFeatures* l, CFeatures* r)
00022 : CKernel(10), kmatrix(NULL), num_rows(0), num_cols(0), upper_diagonal(false)
00023 {
00024 num_rows=l->get_num_vectors();
00025 num_cols=r->get_num_vectors();
00026 init(l, r);
00027 }
00028
00029 CCustomKernel::~CCustomKernel()
00030 {
00031 cleanup();
00032 }
00033
00034 SHORTREAL* CCustomKernel::get_kernel_matrix_shortreal(INT &num_vec1, INT &num_vec2, SHORTREAL* target)
00035 {
00036 if (target == NULL)
00037 return CKernel::get_kernel_matrix_shortreal(num_vec1, num_vec2, target);
00038 else
00039 {
00040 CFeatures* f1 = lhs;
00041 CFeatures* f2 = rhs;
00042 if (f1 && f2)
00043 {
00044 num_vec1=f1->get_num_vectors();
00045 num_vec2=f2->get_num_vectors();
00046 return kmatrix;
00047 }
00048 else
00049 {
00050 SG_ERROR( "no features assigned to kernel\n");
00051 return NULL;
00052 }
00053 }
00054 }
00055
00056 bool CCustomKernel::init(CFeatures* l, CFeatures* r)
00057 {
00058 CKernel::init(l, r);
00059
00060 SG_DEBUG( "num_vec_lhs: %d vs num_rows %d\n", l->get_num_vectors(), num_rows);
00061 SG_DEBUG( "num_vec_rhs: %d vs num_cols %d\n", r->get_num_vectors(), num_cols);
00062 ASSERT(l->get_num_vectors()==num_rows);
00063 ASSERT(r->get_num_vectors()==num_cols);
00064 return true;
00065 }
00066
00067 void CCustomKernel::cleanup_custom()
00068 {
00069 delete[] kmatrix;
00070 kmatrix=NULL;
00071 upper_diagonal=false;
00072 num_cols=0;
00073 num_rows=0;
00074 }
00075
00076 void CCustomKernel::cleanup()
00077 {
00078 cleanup_custom();
00079 CKernel::cleanup();
00080 }
00081
00082 bool CCustomKernel::load_init(FILE* src)
00083 {
00084 return false;
00085 }
00086
00087 bool CCustomKernel::save_init(FILE* dest)
00088 {
00089
00090 return false;
00091 }
00092
00093 bool CCustomKernel::set_triangle_kernel_matrix_from_triangle(const DREAL* km, int len)
00094 {
00095 ASSERT(km);
00096 ASSERT(len>0);
00097
00098 INT cols = (INT) floor(-0.5 + CMath::sqrt(0.25+2*len));
00099 if (cols*(cols+1)/2 != len)
00100 {
00101 SG_ERROR("km should be a vector containing a lower triangle matrix, with len=cols*(cols+1)/2 elements\n");
00102 return false;
00103 }
00104
00105
00106 cleanup_custom();
00107 SG_DEBUG( "using custom kernel of size %dx%d\n", cols,cols);
00108
00109 kmatrix= new SHORTREAL[len];
00110
00111 if (kmatrix)
00112 {
00113 upper_diagonal=true;
00114 num_rows=cols;
00115 num_cols=cols;
00116
00117 for (INT i=0; i<len; i++)
00118 kmatrix[i]=km[i];
00119
00120 return true;
00121 }
00122 else
00123 return false;
00124 }
00125
00126 bool CCustomKernel::set_triangle_kernel_matrix_from_full(const DREAL* km, INT rows, INT cols)
00127 {
00128 ASSERT(rows==cols);
00129
00130 cleanup_custom();
00131 SG_DEBUG( "using custom kernel of size %dx%d\n", cols,cols);
00132
00133 kmatrix= new SHORTREAL[cols*(cols+1)/2];
00134
00135 if (kmatrix)
00136 {
00137 upper_diagonal=true;
00138 num_rows=cols;
00139 num_cols=cols;
00140
00141 for (INT row=0; row<num_rows; row++)
00142 {
00143 for (INT col=row; col<num_cols; col++)
00144 {
00145 kmatrix[row * num_cols - row*(row+1)/2 + col]=km[col*num_rows+row];
00146 }
00147 }
00148 return true;
00149 }
00150 else
00151 return false;
00152 }
00153
00154 bool CCustomKernel::set_full_kernel_matrix_from_full(const DREAL* km, INT rows, INT cols)
00155 {
00156 cleanup_custom();
00157 SG_DEBUG( "using custom kernel of size %dx%d\n", rows,cols);
00158
00159 kmatrix= new SHORTREAL[rows*cols];
00160
00161 if (kmatrix)
00162 {
00163 upper_diagonal=false;
00164 num_rows=rows;
00165 num_cols=cols;
00166
00167 for (INT row=0; row<num_rows; row++)
00168 {
00169 for (INT col=0; col<num_cols; col++)
00170 {
00171 kmatrix[row * num_cols + col]=km[col*num_rows+row];
00172 }
00173 }
00174 return true;
00175 }
00176 else
00177 return false;
00178 }
00179
00180