SHOGUN
3.2.1
首页
相关页面
模块
类
文件
文件列表
文件成员
全部
类
命名空间
文件
函数
变量
类型定义
枚举
枚举值
友元
宏定义
组
页
src
shogun
distance
GeodesicMetric.cpp
浏览该文件的文档.
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
#include <
shogun/distance/GeodesicMetric.h
>
15
#include <
shogun/features/Features.h
>
16
17
using namespace
shogun;
18
19
CGeodesicMetric::CGeodesicMetric
() :
CDenseDistance
<
float64_t
>()
20
{
21
}
22
23
CGeodesicMetric::CGeodesicMetric
(
CDenseFeatures<float64_t>
* l,
CDenseFeatures<float64_t>
* r)
24
:
CDenseDistance
<
float64_t
>()
25
{
26
init
(l, r);
27
}
28
29
CGeodesicMetric::~CGeodesicMetric
()
30
{
31
cleanup
();
32
}
33
34
bool
CGeodesicMetric::init(
CFeatures
* l,
CFeatures
* r)
35
{
36
bool
result=
CDenseDistance<float64_t>::init
(l,r);
37
38
return
result;
39
}
40
41
void
CGeodesicMetric::cleanup
()
42
{
43
}
44
45
float64_t
CGeodesicMetric::compute
(int32_t idx_a, int32_t idx_b)
46
{
47
int32_t alen, blen;
48
bool
afree, bfree;
49
50
float64_t
* avec=
51
((
CDenseFeatures<float64_t>
*)
lhs
)->get_feature_vector(idx_a, alen, afree);
52
float64_t
* bvec=
53
((
CDenseFeatures<float64_t>
*)
rhs
)->get_feature_vector(idx_b, blen, bfree);
54
55
ASSERT
(alen==blen)
56
57
float64_t
s=0;
58
float64_t
d=0;
59
float64_t
nx=0;
60
float64_t
ny=0;
61
{
62
for
(int32_t i=0; i<alen; i++)
63
{
64
d+=avec[i]*bvec[i];
65
nx+=avec[i]*avec[i];
66
ny+=bvec[i]*bvec[i];
67
s+=avec[i]+bvec[i];
68
}
69
}
70
71
((
CDenseFeatures<float64_t>
*)
lhs
)->free_feature_vector(avec, idx_a, afree);
72
((
CDenseFeatures<float64_t>
*)
rhs
)->free_feature_vector(bvec, idx_b, bfree);
73
74
75
// trap division by zero
76
if
(s==0 || nx==0 || ny==0)
77
return
0;
78
79
d/=
CMath::sqrt
(nx*ny);
80
81
// can only happen due to numerical problems
82
if
(
CMath::abs
(d)>1.0)
83
d=
CMath::sign
(d);
84
85
return
acos(d);
86
}
SHOGUN
机器学习工具包 - 项目文档