SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CustomDistance.h
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 #ifndef _CUSTOMDISTANCE_H___
12 #define _CUSTOMDISTANCE_H___
13 
15 #include <shogun/lib/common.h>
18 
19 namespace shogun
20 {
30 {
31  public:
34 
41 
45  CCustomDistance(const SGMatrix<float64_t> distance_matrix);
46 
58  const float64_t* dm, int32_t rows, int32_t cols);
59 
71  const float32_t* dm, int32_t rows, int32_t cols);
72 
73  virtual ~CCustomDistance();
74 
85  virtual bool dummy_init(int32_t rows, int32_t cols);
86 
93  virtual bool init(CFeatures* l, CFeatures* r);
94 
96  virtual void cleanup();
97 
102  inline virtual EDistanceType get_distance_type() { return D_CUSTOM; }
103 
108  inline virtual EFeatureType get_feature_type() { return F_ANY; }
109 
114  inline virtual EFeatureClass get_feature_class() { return C_ANY; }
115 
120  virtual const char* get_name() const { return "CustomDistance"; }
121 
133  const float64_t* dm, int32_t len)
134  {
136  }
137 
149  const float32_t* dm, int32_t len)
150  {
152  }
153 
164  template <class T>
166  const T* dm, int64_t len)
167  {
168  ASSERT(dm);
169  ASSERT(len>0);
170 
171  int64_t cols = (int64_t) floor(-0.5 + CMath::sqrt(0.25+2*len));
172 
173  int64_t int32_max=2147483647;
174 
175  if (cols> int32_max)
176  SG_ERROR("Matrix larger than %d x %d\n", int32_max);
177 
178  if (cols*(cols+1)/2 != len)
179  {
180  SG_ERROR("dm should be a vector containing a lower triangle matrix, with len=cols*(cols+1)/2 elements\n");
181  return false;
182  }
183 
184  cleanup_custom();
185  SG_DEBUG( "using custom distance of size %dx%d\n", cols,cols);
186 
187  dmatrix= SG_MALLOC(float32_t, len);
188 
189  upper_diagonal=true;
190  num_rows=cols;
191  num_cols=cols;
192 
193  for (int64_t i=0; i<len; i++)
194  dmatrix[i]=dm[i];
195 
197  return true;
198  }
199 
211  const float64_t* dm, int32_t rows, int32_t cols)
212  {
213  return set_triangle_distance_matrix_from_full_generic(dm, rows, cols);
214  }
215 
227  const float32_t* dm, int32_t rows, int32_t cols)
228  {
229  return set_triangle_distance_matrix_from_full_generic(dm, rows, cols);
230  }
231 
240  template <class T>
242  const T* dm, int32_t rows, int32_t cols)
243  {
244  ASSERT(rows==cols);
245 
246  cleanup_custom();
247  SG_DEBUG( "using custom distance of size %dx%d\n", cols,cols);
248 
249  dmatrix= SG_MALLOC(float32_t, int64_t(cols)*(cols+1)/2);
250 
251  upper_diagonal=true;
252  num_rows=cols;
253  num_cols=cols;
254 
255  for (int64_t row=0; row<num_rows; row++)
256  {
257  for (int64_t col=row; col<num_cols; col++)
258  {
259  int64_t idx=row * num_cols - row*(row+1)/2 + col;
260  dmatrix[idx]= (float32_t) dm[col*num_rows+row];
261  }
262  }
263  dummy_init(rows, cols);
264  return true;
265  }
266 
277  const float64_t* dm, int32_t rows, int32_t cols)
278  {
279  return set_full_distance_matrix_from_full_generic(dm, rows, cols);
280  }
281 
292  const float32_t* dm, int32_t rows, int32_t cols)
293  {
294  return set_full_distance_matrix_from_full_generic(dm, rows, cols);
295  }
296 
304  template <class T>
306  const T* dm, int32_t rows, int32_t cols)
307  {
308  cleanup_custom();
309  SG_DEBUG( "using custom distance of size %dx%d\n", rows,cols);
310 
311  dmatrix= SG_MALLOC(float32_t, rows*cols);
312 
313  upper_diagonal=false;
314  num_rows=rows;
315  num_cols=cols;
316 
317  for (int32_t row=0; row<num_rows; row++)
318  {
319  for (int32_t col=0; col<num_cols; col++)
320  {
321  dmatrix[row * num_cols + col]=dm[col*num_rows+row];
322  }
323  }
324 
325  dummy_init(rows, cols);
326  return true;
327  }
328 
333  virtual inline int32_t get_num_vec_lhs()
334  {
335  return num_rows;
336  }
337 
342  virtual inline int32_t get_num_vec_rhs()
343  {
344  return num_cols;
345  }
346 
351  virtual inline bool has_features()
352  {
353  return (num_rows>0) && (num_cols>0);
354  }
355 
356  protected:
363  inline virtual float64_t compute(int32_t row, int32_t col)
364  {
365  ASSERT(dmatrix);
366 
367  if (upper_diagonal)
368  {
369  if (row <= col)
370  {
371  int64_t r=row;
372  return dmatrix[r*num_cols - r*(r+1)/2 + col];
373  }
374  else
375  {
376  int64_t c=col;
377  return dmatrix[c*num_cols - c*(c+1)/2 + row];
378  }
379  }
380  else
381  {
382  int64_t r=row;
383  return dmatrix[r*num_cols+col];
384  }
385  }
386 
387  private:
388  void init();
389 
391  void cleanup_custom();
392 
393  protected:
397  int32_t num_rows;
399  int32_t num_cols;
402 };
403 
404 }
405 #endif /* _CUSTOMKERNEL_H__ */

SHOGUN Machine Learning Toolbox - Documentation