SHOGUN
v3.2.0
|
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) 2007-2009 Soeren Sonnenburg 00008 * Copyright (C) 2007-2009 Fraunhofer Institute FIRST and Max-Planck-Society 00009 */ 00010 00011 #include <shogun/lib/common.h> 00012 #include <shogun/io/SGIO.h> 00013 #include <shogun/distance/EuclideanDistance.h> 00014 00015 using namespace shogun; 00016 00017 CEuclideanDistance::CEuclideanDistance() : CRealDistance() 00018 { 00019 init(); 00020 } 00021 00022 CEuclideanDistance::CEuclideanDistance(CDenseFeatures<float64_t>* l, CDenseFeatures<float64_t>* r) 00023 : CRealDistance() 00024 { 00025 init(); 00026 init(l, r); 00027 } 00028 00029 CEuclideanDistance::~CEuclideanDistance() 00030 { 00031 cleanup(); 00032 } 00033 00034 bool CEuclideanDistance::init(CFeatures* l, CFeatures* r) 00035 { 00036 CRealDistance::init(l, r); 00037 00038 return true; 00039 } 00040 00041 void CEuclideanDistance::cleanup() 00042 { 00043 } 00044 00045 float64_t CEuclideanDistance::compute(int32_t idx_a, int32_t idx_b) 00046 { 00047 int32_t alen, blen; 00048 bool afree, bfree; 00049 float64_t result=0; 00050 00051 float64_t* avec=((CDenseFeatures<float64_t>*) lhs)-> 00052 get_feature_vector(idx_a, alen, afree); 00053 float64_t* bvec=((CDenseFeatures<float64_t>*) rhs)-> 00054 get_feature_vector(idx_b, blen, bfree); 00055 ASSERT(alen==blen) 00056 00057 for (int32_t i=0; i<alen; i++) 00058 result+=CMath::sq(avec[i] - bvec[i]); 00059 00060 ((CDenseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree); 00061 ((CDenseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree); 00062 00063 if (disable_sqrt) 00064 return result; 00065 00066 return CMath::sqrt(result); 00067 } 00068 00069 void CEuclideanDistance::init() 00070 { 00071 disable_sqrt=false; 00072 00073 m_parameters->add(&disable_sqrt, "disable_sqrt", "If sqrt shall not be applied."); 00074 } 00075 00076 float64_t CEuclideanDistance::distance_upper_bounded(int32_t idx_a, int32_t idx_b, float64_t upper_bound) 00077 { 00078 int32_t alen, blen; 00079 bool afree, bfree; 00080 float64_t result=0; 00081 00082 upper_bound *= upper_bound; 00083 00084 float64_t* avec=((CDenseFeatures<float64_t>*) lhs)-> 00085 get_feature_vector(idx_a, alen, afree); 00086 float64_t* bvec=((CDenseFeatures<float64_t>*) rhs)-> 00087 get_feature_vector(idx_b, blen, bfree); 00088 ASSERT(alen==blen) 00089 00090 for (int32_t i=0; i<alen; i++) 00091 { 00092 result+=CMath::sq(avec[i] - bvec[i]); 00093 00094 if (result > upper_bound) 00095 { 00096 ((CDenseFeatures<float64_t>*) lhs)-> 00097 free_feature_vector(avec, idx_a, afree); 00098 ((CDenseFeatures<float64_t>*) rhs)-> 00099 free_feature_vector(bvec, idx_b, bfree); 00100 00101 if (disable_sqrt) 00102 return result; 00103 else 00104 return CMath::sqrt(result); 00105 } 00106 } 00107 00108 ((CDenseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree); 00109 ((CDenseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree); 00110 00111 if (disable_sqrt) 00112 return result; 00113 else 00114 return CMath::sqrt(result); 00115 }