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