CustomDistance.cpp

Go to the documentation of this file.
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) 1999-2009 Soeren Sonnenburg
00008  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include "lib/common.h"
00012 #include "distance/CustomDistance.h"
00013 #include "features/Features.h"
00014 #include "features/DummyFeatures.h"
00015 #include "lib/io.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= new 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= new 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     delete[] 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 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation