SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 
20 
21 using namespace shogun;
22 
24 {
25  init();
26 }
27 
30 {
31  init();
32  k=k_;
33 }
34 
38 {
39  init();
40  k=k_;
41  init(l, r);
42 }
43 
45 {
46  cleanup();
47 }
48 
49 bool CMinkowskiMetric::init(CFeatures* l, CFeatures* r)
50 {
52 }
53 
55 {
56 }
57 
58 float64_t CMinkowskiMetric::compute(int32_t idx_a, int32_t idx_b)
59 {
60  int32_t alen, blen;
61  bool afree, bfree;
62 
63  float64_t* avec=
64  ((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
65  float64_t* bvec=
66  ((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
67 
68  ASSERT(avec);
69  ASSERT(bvec);
70  ASSERT(alen==blen);
71 
72  float64_t absTmp = 0;
73  float64_t result=0;
74  {
75  for (int32_t i=0; i<alen; i++)
76  {
77  absTmp=fabs(avec[i]-bvec[i]);
78  result+=pow(absTmp,k);
79  }
80 
81  }
82 
83  ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
84  ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
85 
86  return pow(result,1/k);
87 }
88 
89 void CMinkowskiMetric::init()
90 {
91  k = 2.0;
92  SG_ADD(&k, "k", "L_k norm.", MS_AVAILABLE);
93 }

SHOGUN Machine Learning Toolbox - Documentation