SHOGUN
3.2.1
首页
相关页面
模块
类
文件
文件列表
文件成员
全部
类
命名空间
文件
函数
变量
类型定义
枚举
枚举值
友元
宏定义
组
页
src
shogun
distance
Distance.h
浏览该文件的文档.
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
* Written (W) 2006-2009 Soeren Sonnenburg
9
* Copyright (C) 2006-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10
*/
11
12
#ifndef _DISTANCE_H___
13
#define _DISTANCE_H___
14
15
#include <stdio.h>
16
17
#include <
shogun/lib/common.h
>
18
#include <
shogun/io/File.h
>
19
#include <
shogun/mathematics/Math.h
>
20
#include <
shogun/base/SGObject.h
>
21
#include <
shogun/features/FeatureTypes.h
>
22
#include <
shogun/features/Features.h
>
23
24
namespace
shogun
25
{
26
class
CFile;
27
class
CMath;
28
class
CFeatures;
29
31
enum
EDistanceType
32
{
33
D_UNKNOWN
= 0,
34
D_MINKOWSKI
= 10,
35
D_MANHATTAN
= 20,
36
D_CANBERRA
= 30,
37
D_CHEBYSHEW
= 40,
38
D_GEODESIC
= 50,
39
D_JENSEN
= 60,
40
D_MANHATTANWORD
= 70,
41
D_HAMMINGWORD
= 80 ,
42
D_CANBERRAWORD
= 90,
43
D_SPARSEEUCLIDEAN
= 100,
44
D_EUCLIDEAN
= 110,
45
D_CHISQUARE
= 120,
46
D_TANIMOTO
= 130,
47
D_COSINE
= 140,
48
D_BRAYCURTIS
= 150,
49
D_CUSTOM
= 160,
50
D_ATTENUATEDEUCLIDEAN
= 170,
51
D_MAHALANOBIS
= 180,
52
D_DIRECTOR
= 190,
53
D_CUSTOMMAHALANOBIS
= 200
54
};
55
56
80
class
CDistance
:
public
CSGObject
81
{
82
public
:
84
CDistance
();
85
92
CDistance
(
CFeatures
*
lhs
,
CFeatures
*
rhs
);
93
virtual
~CDistance
();
94
102
virtual
float64_t
distance
(int32_t idx_a, int32_t idx_b);
103
117
virtual
float64_t
distance_upper_bounded
(int32_t idx_a, int32_t idx_b,
float64_t
upper_bound)
118
{
119
return
distance
(idx_a, idx_b);
120
}
121
126
SGMatrix<float64_t>
get_distance_matrix
()
127
{
128
return
get_distance_matrix<float64_t>();
129
}
130
135
template
<
class
T>
SGMatrix<T>
get_distance_matrix
();
136
143
int32_t
compute_row_start
(int64_t offs, int32_t n,
bool
symmetric)
144
{
145
int32_t i_start;
146
147
if
(symmetric)
148
i_start=(int32_t)
CMath::floor
(n-
CMath::sqrt
(
CMath::sq
((
float64_t
) n)-offs));
149
else
150
i_start=(int32_t) (offs/int64_t(n));
151
152
return
i_start;
153
}
154
159
template
<
class
T>
static
void
*
get_distance_matrix_helper
(
void
* p);
160
170
virtual
bool
init(
CFeatures
* lhs,
CFeatures
* rhs);
171
176
virtual
void
cleanup
()=0;
177
182
void
load
(
CFile
* loader);
183
188
void
save
(
CFile
* writer);
189
194
inline
CFeatures
*
get_lhs
() {
SG_REF
(lhs);
return
lhs
; };
195
200
inline
CFeatures
*
get_rhs
() {
SG_REF
(rhs);
return
rhs
; };
201
210
CFeatures
*
replace_rhs
(
CFeatures
* rhs);
211
220
CFeatures
*
replace_lhs
(
CFeatures
* lhs);
221
223
virtual
void
remove_lhs_and_rhs
();
224
226
virtual
void
remove_lhs
();
227
229
virtual
void
remove_rhs
();
230
237
virtual
EDistanceType
get_distance_type
()=0 ;
238
245
virtual
EFeatureType
get_feature_type
()=0;
246
253
virtual
EFeatureClass
get_feature_class
()=0;
254
260
inline
bool
get_precompute_matrix
() {
return
precompute_matrix
; }
261
267
virtual
void
set_precompute_matrix
(
bool
flag)
268
{
269
precompute_matrix
=flag;
270
271
if
(!
precompute_matrix
)
272
{
273
SG_FREE(
precomputed_matrix
);
274
precomputed_matrix
=NULL;
275
}
276
}
277
282
virtual
int32_t
get_num_vec_lhs
()
283
{
284
return
num_lhs
;
285
}
286
291
virtual
int32_t
get_num_vec_rhs
()
292
{
293
return
num_rhs
;
294
}
295
300
virtual
bool
has_features
()
301
{
302
return
lhs &&
rhs
;
303
}
304
309
inline
bool
lhs_equals_rhs
()
310
{
311
return
lhs==
rhs
;
312
}
313
314
protected
:
315
317
static
void
*
run_distance_thread
(
void
* p);
318
322
virtual
float64_t
compute
(int32_t idx_a, int32_t idx_b)=0;
323
325
void
do_precompute_matrix
();
326
327
private
:
328
void
init();
329
330
protected
:
334
float32_t
*
precomputed_matrix
;
335
339
bool
precompute_matrix
;
340
342
CFeatures
*
lhs
;
344
CFeatures
*
rhs
;
345
347
int32_t
num_lhs
;
349
int32_t
num_rhs
;
350
351
};
352
}
// namespace shogun
353
#endif
SHOGUN
机器学习工具包 - 项目文档