Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <shogun/lib/config.h>
00012 #include <shogun/lib/common.h>
00013 #include <shogun/io/SGIO.h>
00014
00015 #include <shogun/base/Parameter.h>
00016
00017 #include <shogun/distance/MinkowskiMetric.h>
00018 #include <shogun/features/Features.h>
00019 #include <shogun/features/SimpleFeatures.h>
00020
00021 using namespace shogun;
00022
00023 CMinkowskiMetric::CMinkowskiMetric() : CSimpleDistance<float64_t>()
00024 {
00025 init();
00026 }
00027
00028 CMinkowskiMetric::CMinkowskiMetric(float64_t k_)
00029 : CSimpleDistance<float64_t>()
00030 {
00031 init();
00032 k=k_;
00033 }
00034
00035 CMinkowskiMetric::CMinkowskiMetric(
00036 CSimpleFeatures<float64_t>* l, CSimpleFeatures<float64_t>* r, float64_t k_)
00037 : CSimpleDistance<float64_t>()
00038 {
00039 init();
00040 k=k_;
00041 init(l, r);
00042 }
00043
00044 CMinkowskiMetric::~CMinkowskiMetric()
00045 {
00046 cleanup();
00047 }
00048
00049 bool CMinkowskiMetric::init(CFeatures* l, CFeatures* r)
00050 {
00051 return CSimpleDistance<float64_t>::init(l,r);
00052 }
00053
00054 void CMinkowskiMetric::cleanup()
00055 {
00056 }
00057
00058 float64_t CMinkowskiMetric::compute(int32_t idx_a, int32_t idx_b)
00059 {
00060 int32_t alen, blen;
00061 bool afree, bfree;
00062
00063 float64_t* avec=
00064 ((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
00065 float64_t* bvec=
00066 ((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
00067
00068 ASSERT(avec);
00069 ASSERT(bvec);
00070 ASSERT(alen==blen);
00071
00072 float64_t absTmp = 0;
00073 float64_t result=0;
00074 {
00075 for (int32_t i=0; i<alen; i++)
00076 {
00077 absTmp=fabs(avec[i]-bvec[i]);
00078 result+=pow(absTmp,k);
00079 }
00080
00081 }
00082
00083 ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
00084 ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00085
00086 return pow(result,1/k);
00087 }
00088
00089 void CMinkowskiMetric::init()
00090 {
00091 k = 2.0;
00092 SG_ADD(&k, "k", "L_k norm.", MS_AVAILABLE);
00093 }