SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MinkowskiMetric.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) 2006-2009 Christian Gehl
8  * Copyright (C) 2006-2009 Fraunhofer Institute FIRST
9  */
10 
11 #include <shogun/lib/config.h>
12 #include <shogun/lib/common.h>
13 #include <shogun/io/SGIO.h>
14 
15 #include <shogun/base/Parameter.h>
16 
19 
20 using namespace shogun;
21 
23 {
24  init();
25 }
26 
29 {
30  init();
31  k=k_;
32 }
33 
37 {
38  init();
39  k=k_;
40  init(l, r);
41 }
42 
44 {
45  cleanup();
46 }
47 
48 bool CMinkowskiMetric::init(CFeatures* l, CFeatures* r)
49 {
51 }
52 
54 {
55 }
56 
57 float64_t CMinkowskiMetric::compute(int32_t idx_a, int32_t idx_b)
58 {
59  int32_t alen, blen;
60  bool afree, bfree;
61 
62  float64_t* avec=
63  ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
64  float64_t* bvec=
65  ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
66 
67  ASSERT(avec)
68  ASSERT(bvec)
69  ASSERT(alen==blen)
70 
71  float64_t absTmp = 0;
72  float64_t result=0;
73  {
74  for (int32_t i=0; i<alen; i++)
75  {
76  absTmp=fabs(avec[i]-bvec[i]);
77  result+=pow(absTmp,k);
78  }
79 
80  }
81 
82  ((CDenseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
83  ((CDenseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
84 
85  return pow(result,1/k);
86 }
87 
88 void CMinkowskiMetric::init()
89 {
90  k = 2.0;
91  SG_ADD(&k, "k", "L_k norm.", MS_AVAILABLE);
92 }
virtual bool init(CFeatures *l, CFeatures *r)
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
CFeatures * lhs
feature vectors to occur on the left hand side
Definition: Distance.h:381
The class Features is the base class of all feature objects.
Definition: Features.h:68
CFeatures * rhs
feature vectors to occur on the right hand side
Definition: Distance.h:383
template class DenseDistance
Definition: DenseDistance.h:24
#define SG_ADD(...)
Definition: SGObject.h:84

SHOGUN Machine Learning Toolbox - Documentation