-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDistanceMetrics.cpp
More file actions
68 lines (53 loc) · 1.67 KB
/
DistanceMetrics.cpp
File metadata and controls
68 lines (53 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*! \class DistanceMetrics
\brief A class of static methods for calculating distance between DataObjects.
*/
#include <cstdlib>
#include "DistanceMetrics.h"
#include "DistanceMetricsEnum.h"
#include "ObjectMatrix.h"
#include <cmath>
#include <algorithm> // std::transform
#include <numeric>
double DistanceMetrics::getDistance(DataObject obj1, DataObject obj2, DistanceMetricsEnum dme)
{
double to_return = 0.0;
if (dme == MANHATTAN)
to_return = DistanceMetrics::getManhattan(obj1, obj2);
else if (dme == EUCLIDEAN)
to_return = DistanceMetrics::getEuclidean(obj1, obj2);
else if (dme == CHEBYSHEV)
to_return = DistanceMetrics::getChebyshev(obj1, obj2);
return to_return;
}
double DistanceMetrics::getManhattan(DataObject obj1, DataObject obj2)
{
double to_return = 0.0;
int n = obj1.getFeatureCount();
for (int i = 0; i < n; i++)
to_return += fabs(obj1.getFeatureAt(i) - obj2.getFeatureAt(i));
return to_return;
}
double DistanceMetrics::getEuclidean(DataObject obj1, DataObject obj2)
{
double s = 0.0;
int n = obj1.getFeatureCount(), i;
double diff = 0;
for (i = 0; i < n; i++)
{
diff = obj1.getFeatureAt(i) - obj2.getFeatureAt(i);
s += diff * diff;
}
double to_return = std::sqrt(s);//0.0;
return to_return;
}
double DistanceMetrics::getChebyshev(DataObject obj1, DataObject obj2)
{
double to_return = -1.0;
int n = obj1.getFeatureCount();
for (int i = 0; i < n; i++)
{
if (fabs(obj1.getFeatureAt(i) - obj2.getFeatureAt(i)) > to_return)
to_return = fabs(obj1.getFeatureAt(i) - obj2.getFeatureAt(i));
}
return to_return;
}