SHOGUN  3.2.1
 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 
111 void CCustomDistance::cleanup_custom()
112 {
113  SG_DEBUG("cleanup up custom distance\n")
114  SG_FREE(dmatrix);
115  dmatrix=NULL;
116  upper_diagonal=false;
117  num_cols=0;
118  num_rows=0;
119 }
120 
121 void CCustomDistance::init()
122 {
123  dmatrix=NULL;
124  num_rows=0;
125  num_cols=0;
126  upper_diagonal=false;
127 
128  m_parameters->add_matrix(&dmatrix, &num_rows, &num_cols, "dmatrix", "Distance Matrix");
129  m_parameters->add(&upper_diagonal, "upper_diagonal", "Upper diagonal");
130 }
131 
133 {
134  cleanup_custom();
135 }
136 
137 float64_t CCustomDistance::compute(int32_t row, int32_t col)
138 {
139  ASSERT(dmatrix)
140 
141  if (upper_diagonal)
142  {
143  if (row <= col)
144  {
145  int64_t r=row;
146  return dmatrix[r*num_cols - r*(r+1)/2 + col];
147  }
148  else
149  {
150  int64_t c=col;
151  return dmatrix[c*num_cols - c*(c+1)/2 + row];
152  }
153  }
154  else
155  {
156  int64_t r=row;
157  return dmatrix[r*num_cols+col];
158  }
159 }

SHOGUN Machine Learning Toolbox - Documentation