SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CustomDistance.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2009 Soeren Sonnenburg
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #include <shogun/base/Parameter.h>
12 #include <shogun/lib/common.h>
16 #include <shogun/io/SGIO.h>
17 
18 using namespace shogun;
19 
21 {
22  init();
23 }
24 
26 {
27  init();
28 
29  if (d->lhs_equals_rhs())
30  {
31  int32_t cols=d->get_num_vec_lhs();
32  SG_DEBUG( "using custom distance of size %dx%d\n", cols,cols);
33 
34  dmatrix= SG_MALLOC(float32_t, int64_t(cols)*(cols+1)/2);
35 
36  upper_diagonal=true;
37  num_rows=cols;
38  num_cols=cols;
39 
40  for (int32_t row=0; row<num_rows; row++)
41  {
42  for (int32_t col=row; col<num_cols; col++)
43  dmatrix[int64_t(row) * num_cols - int64_t(row)*(row+1)/2 + col]=d->distance(row,col);
44  }
45  }
46  else
47  {
48  int32_t rows=d->get_num_vec_lhs();
49  int32_t cols=d->get_num_vec_rhs();
50  dmatrix= SG_MALLOC(float32_t, int64_t(rows)*cols);
51 
52  upper_diagonal=false;
53  num_rows=rows;
54  num_cols=cols;
55 
56  for (int32_t row=0; row<num_rows; row++)
57  {
58  for (int32_t col=0; col<num_cols; col++)
59  dmatrix[int64_t(row) * num_cols + col]=d->distance(row,col);
60  }
61  }
62 
64 }
65 
67 : CDistance()
68 {
69  init();
71  distance_matrix.num_rows,
72  distance_matrix.num_cols);
73 }
74 
75 CCustomDistance::CCustomDistance(const float64_t* dm, int32_t rows, int32_t cols)
76 : CDistance()
77 {
78  init();
79  set_full_distance_matrix_from_full(dm, rows, cols);
80 }
81 
82 CCustomDistance::CCustomDistance(const float32_t* dm, int32_t rows, int32_t cols)
83 : CDistance()
84 {
85  init();
86  set_full_distance_matrix_from_full(dm, rows, cols);
87 }
88 
90 {
91  cleanup();
92 }
93 
94 bool CCustomDistance::dummy_init(int32_t rows, int32_t cols)
95 {
96  return init(new CDummyFeatures(rows), new CDummyFeatures(cols));
97 }
98 
99 bool CCustomDistance::init(CFeatures* l, CFeatures* r)
100 {
101  CDistance::init(l, r);
102 
103  SG_DEBUG( "num_vec_lhs: %d vs num_rows %d\n", l->get_num_vectors(), num_rows);
104  SG_DEBUG( "num_vec_rhs: %d vs num_cols %d\n", r->get_num_vectors(), num_cols);
107  return true;
108 }
109 
110 void CCustomDistance::cleanup_custom()
111 {
112  SG_DEBUG("cleanup up custom distance\n");
113  SG_FREE(dmatrix);
114  dmatrix=NULL;
115  upper_diagonal=false;
116  num_cols=0;
117  num_rows=0;
118 }
119 
120 void CCustomDistance::init()
121 {
122  dmatrix=NULL;
123  num_rows=0;
124  num_cols=0;
125  upper_diagonal=false;
126 
127  m_parameters->add_matrix(&dmatrix, &num_rows, &num_cols, "dmatrix", "Distance Matrix");
128  m_parameters->add(&upper_diagonal, "upper_diagonal", "Upper diagonal");
129 }
130 
132 {
133  cleanup_custom();
134 }

SHOGUN Machine Learning Toolbox - Documentation