SHOGUN
v2.0.0
Main Page
Related Pages
Modules
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
src
shogun
distance
SparseEuclideanDistance.cpp
Go to the documentation of this file.
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) 2007-2009 Soeren Sonnenburg
8
* Copyright (C) 2007-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9
*/
10
11
#include <
shogun/lib/common.h
>
12
#include <
shogun/io/SGIO.h
>
13
#include <
shogun/distance/SparseEuclideanDistance.h
>
14
#include <
shogun/features/Features.h
>
15
#include <
shogun/features/SparseFeatures.h
>
16
17
using namespace
shogun;
18
19
CSparseEuclideanDistance::CSparseEuclideanDistance
()
20
:
CSparseDistance
<
float64_t
>()
21
{
22
init
();
23
}
24
25
CSparseEuclideanDistance::CSparseEuclideanDistance
(
26
CSparseFeatures<float64_t>
* l,
CSparseFeatures<float64_t>
* r)
27
:
CSparseDistance
<
float64_t
>()
28
{
29
init
();
30
init
(l, r);
31
}
32
33
CSparseEuclideanDistance::~CSparseEuclideanDistance
()
34
{
35
cleanup
();
36
}
37
38
bool
CSparseEuclideanDistance::init(
CFeatures
* l,
CFeatures
* r)
39
{
40
CSparseDistance<float64_t>::init
(l, r);
41
42
cleanup
();
43
44
sq_lhs
=
SG_MALLOC
(
float64_t
,
lhs
->
get_num_vectors
());
45
sq_lhs
=((
CSparseFeatures<float64_t>
*)
lhs
)->compute_squared(sq_lhs);
46
47
if
(
lhs
==
rhs
)
48
sq_rhs
=
sq_lhs
;
49
else
50
{
51
sq_rhs
=
SG_MALLOC
(
float64_t
,
rhs
->
get_num_vectors
());
52
sq_rhs
=((
CSparseFeatures<float64_t>
*)
rhs
)->compute_squared(sq_rhs);
53
}
54
55
return
true
;
56
}
57
58
void
CSparseEuclideanDistance::cleanup
()
59
{
60
if
(
sq_lhs
!=
sq_rhs
)
61
SG_FREE
(
sq_rhs
);
62
sq_rhs
= NULL;
63
64
SG_FREE
(
sq_lhs
);
65
sq_lhs
= NULL;
66
}
67
68
float64_t
CSparseEuclideanDistance::compute
(int32_t idx_a, int32_t idx_b)
69
{
70
float64_t
result=((
CSparseFeatures<float64_t>
*)
lhs
)->compute_squared_norm(
71
(
CSparseFeatures<float64_t>
*)
lhs
,
sq_lhs
, idx_a,
72
(
CSparseFeatures<float64_t>
*)
rhs
,
sq_rhs
, idx_b);
73
74
return
CMath::sqrt
(result);
75
}
76
77
void
CSparseEuclideanDistance::init()
78
{
79
sq_lhs
=NULL;
80
sq_rhs
=NULL;
81
}
SHOGUN
Machine Learning Toolbox - Documentation