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 #include <shogun/distance/BrayCurtisDistance.h>
00015 #include <shogun/features/Features.h>
00016
00017 using namespace shogun;
00018
00019 CBrayCurtisDistance::CBrayCurtisDistance()
00020 : CDenseDistance<float64_t>()
00021 {
00022 }
00023
00024 CBrayCurtisDistance::CBrayCurtisDistance(CDenseFeatures<float64_t>* l, CDenseFeatures<float64_t>* r)
00025 : CDenseDistance<float64_t>()
00026 {
00027 init(l, r);
00028 }
00029
00030 CBrayCurtisDistance::~CBrayCurtisDistance()
00031 {
00032 cleanup();
00033 }
00034
00035 bool CBrayCurtisDistance::init(CFeatures* l, CFeatures* r)
00036 {
00037 bool result=CDenseDistance<float64_t>::init(l,r);
00038
00039 return result;
00040 }
00041
00042 void CBrayCurtisDistance::cleanup()
00043 {
00044 }
00045
00046 float64_t CBrayCurtisDistance::compute(int32_t idx_a, int32_t idx_b)
00047 {
00048 int32_t alen, blen;
00049 bool afree, bfree;
00050
00051 float64_t* avec=
00052 ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
00053 float64_t* bvec=
00054 ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
00055
00056 ASSERT(alen==blen);
00057
00058 float64_t s1=0;
00059 float64_t s2=0;
00060 {
00061 for (int32_t i=0; i<alen; i++)
00062 {
00063 s1+=fabs(avec[i]-bvec[i]);
00064 s2+=fabs(avec[i]+bvec[i]);
00065 }
00066 }
00067
00068 ((CDenseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
00069 ((CDenseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00070
00071
00072 if(s2==0)
00073 return 0;
00074
00075 return (s1/s2);
00076 }