SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 }
virtual float64_t compute(int32_t row, int32_t col)
Class Distance, a base class for all the distances used in the Shogun toolbox.
Definition: Distance.h:87
virtual int32_t get_num_vec_lhs()
Definition: Distance.h:312
virtual bool dummy_init(int32_t rows, int32_t cols)
virtual int32_t get_num_vectors() const =0
Parameter * m_parameters
Definition: SGObject.h:546
index_t num_cols
Definition: SGMatrix.h:376
index_t num_rows
Definition: SGMatrix.h:374
bool lhs_equals_rhs()
Definition: Distance.h:339
void add(bool *param, const char *name, const char *description="")
Definition: Parameter.cpp:37
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
bool set_full_distance_matrix_from_full(const float64_t *dm, int32_t rows, int32_t cols)
virtual int32_t get_num_vec_rhs()
Definition: Distance.h:321
float float32_t
Definition: common.h:49
The class DummyFeatures implements features that only know the number of feature objects (but don't a...
Definition: DummyFeatures.h:25
virtual float64_t distance(int32_t idx_a, int32_t idx_b)
Definition: Distance.cpp:206
#define SG_DEBUG(...)
Definition: SGIO.h:107
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
The class Features is the base class of all feature objects.
Definition: Features.h:68
virtual bool init(CFeatures *l, CFeatures *r)
The Custom Distance allows for custom user provided distance matrices.
void add_matrix(bool **param, index_t *length_y, index_t *length_x, const char *name, const char *description="")
Definition: Parameter.cpp:943

SHOGUN Machine Learning Toolbox - Documentation