MinkowskiMetric.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2006-2009 Christian Gehl
00008  * Copyright (C) 2006-2009 Fraunhofer Institute FIRST
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 
00020 using namespace shogun;
00021 
00022 CMinkowskiMetric::CMinkowskiMetric() : CDenseDistance<float64_t>()
00023 {
00024     init();
00025 }
00026 
00027 CMinkowskiMetric::CMinkowskiMetric(float64_t k_)
00028 : CDenseDistance<float64_t>()
00029 {
00030     init();
00031     k=k_;
00032 }
00033 
00034 CMinkowskiMetric::CMinkowskiMetric(
00035     CDenseFeatures<float64_t>* l, CDenseFeatures<float64_t>* r, float64_t k_)
00036 : CDenseDistance<float64_t>()
00037 {
00038     init();
00039     k=k_;
00040     init(l, r);
00041 }
00042 
00043 CMinkowskiMetric::~CMinkowskiMetric()
00044 {
00045     cleanup();
00046 }
00047 
00048 bool CMinkowskiMetric::init(CFeatures* l, CFeatures* r)
00049 {
00050     return CDenseDistance<float64_t>::init(l,r);
00051 }
00052 
00053 void CMinkowskiMetric::cleanup()
00054 {
00055 }
00056 
00057 float64_t CMinkowskiMetric::compute(int32_t idx_a, int32_t idx_b)
00058 {
00059     int32_t alen, blen;
00060     bool afree, bfree;
00061 
00062     float64_t* avec=
00063         ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
00064     float64_t* bvec=
00065         ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
00066 
00067     ASSERT(avec);
00068     ASSERT(bvec);
00069     ASSERT(alen==blen);
00070 
00071     float64_t absTmp = 0;
00072     float64_t result=0;
00073     {
00074         for (int32_t i=0; i<alen; i++)
00075         {
00076             absTmp=fabs(avec[i]-bvec[i]);
00077             result+=pow(absTmp,k);
00078         }
00079 
00080     }
00081 
00082     ((CDenseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
00083     ((CDenseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00084 
00085     return pow(result,1/k);
00086 }
00087 
00088 void CMinkowskiMetric::init()
00089 {
00090     k = 2.0;
00091     SG_ADD(&k, "k", "L_k norm.", MS_AVAILABLE);
00092 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation