Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <shogun/lib/common.h>
00012 #include <shogun/distance/CustomDistance.h>
00013 #include <shogun/features/Features.h>
00014 #include <shogun/features/DummyFeatures.h>
00015 #include <shogun/io/SGIO.h>
00016
00017 using namespace shogun;
00018
00019 CCustomDistance::CCustomDistance() : CDistance()
00020 {
00021 init();
00022 }
00023
00024 CCustomDistance::CCustomDistance(CDistance* d) : CDistance()
00025 {
00026 init();
00027
00028 if (d->lhs_equals_rhs())
00029 {
00030 int32_t cols=d->get_num_vec_lhs();
00031 SG_DEBUG( "using custom distance of size %dx%d\n", cols,cols);
00032
00033 dmatrix= SG_MALLOC(float32_t, int64_t(cols)*(cols+1)/2);
00034
00035 upper_diagonal=true;
00036 num_rows=cols;
00037 num_cols=cols;
00038
00039 for (int32_t row=0; row<num_rows; row++)
00040 {
00041 for (int32_t col=row; col<num_cols; col++)
00042 dmatrix[int64_t(row) * num_cols - int64_t(row)*(row+1)/2 + col]=d->distance(row,col);
00043 }
00044 }
00045 else
00046 {
00047 int32_t rows=d->get_num_vec_lhs();
00048 int32_t cols=d->get_num_vec_rhs();
00049 dmatrix= SG_MALLOC(float32_t, int64_t(rows)*cols);
00050
00051 upper_diagonal=false;
00052 num_rows=rows;
00053 num_cols=cols;
00054
00055 for (int32_t row=0; row<num_rows; row++)
00056 {
00057 for (int32_t col=0; col<num_cols; col++)
00058 dmatrix[int64_t(row) * num_cols + col]=d->distance(row,col);
00059 }
00060 }
00061
00062 dummy_init(num_rows, num_cols);
00063 }
00064
00065 CCustomDistance::CCustomDistance(const float64_t* dm, int32_t rows, int32_t cols)
00066 : CDistance()
00067 {
00068 init();
00069 set_full_distance_matrix_from_full(dm, rows, cols);
00070 }
00071
00072 CCustomDistance::CCustomDistance(const float32_t* dm, int32_t rows, int32_t cols)
00073 : CDistance()
00074 {
00075 init();
00076 set_full_distance_matrix_from_full(dm, rows, cols);
00077 }
00078
00079 CCustomDistance::~CCustomDistance()
00080 {
00081 cleanup();
00082 }
00083
00084 bool CCustomDistance::dummy_init(int32_t rows, int32_t cols)
00085 {
00086 return init(new CDummyFeatures(rows), new CDummyFeatures(cols));
00087 }
00088
00089 bool CCustomDistance::init(CFeatures* l, CFeatures* r)
00090 {
00091 CDistance::init(l, r);
00092
00093 SG_DEBUG( "num_vec_lhs: %d vs num_rows %d\n", l->get_num_vectors(), num_rows);
00094 SG_DEBUG( "num_vec_rhs: %d vs num_cols %d\n", r->get_num_vectors(), num_cols);
00095 ASSERT(l->get_num_vectors()==num_rows);
00096 ASSERT(r->get_num_vectors()==num_cols);
00097 return true;
00098 }
00099
00100 void CCustomDistance::cleanup_custom()
00101 {
00102 SG_DEBUG("cleanup up custom distance\n");
00103 SG_FREE(dmatrix);
00104 dmatrix=NULL;
00105 upper_diagonal=false;
00106 num_cols=0;
00107 num_rows=0;
00108 }
00109
00110 void CCustomDistance::init()
00111 {
00112 dmatrix=NULL;
00113 num_rows=0;
00114 num_cols=0;
00115 upper_diagonal=false;
00116
00117 m_parameters->add_matrix(&dmatrix, &num_rows, &num_cols, "dmatrix", "Distance Matrix");
00118 m_parameters->add(&upper_diagonal, "upper_diagonal", "Upper diagonal");
00119 }
00120
00121 void CCustomDistance::cleanup()
00122 {
00123 cleanup_custom();
00124 }