-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistanceCalculator.cpp
125 lines (104 loc) · 3.22 KB
/
distanceCalculator.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
double L1_norm(vector<double> p1, vector<double> p2)
{
double result=0;
for(int i=0; i<p1.size(); i++)
{
result = result + abs(p1.at(i) - p2.at(i));
}
return result;
}
double L2_norm(vector<double> p1, vector<double> p2)
{
double result=0;
for(int i=0; i<p1.size(); i++)
{
result = result + pow(p1.at(i) - p2.at(i), 2);
}
return sqrt(result);
}
double Linf_norm(vector<double> p1, vector<double> p2)
{
vector<double> values;
for(int i=0; i<p1.size(); i++)
{
values.push_back(abs(p1.at(i) - p2.at(i)));
}
double maxValue=values.at(0);
for(int i=1; i<values.size(); i++)
{
if(maxValue < values.at(i))
maxValue = values.at(i);
}
return maxValue;
}
int main()
{
int numOfPoints;
cout << "How many data points do you want to insert: ";
cin >> numOfPoints;
int numOfDimensions;
cout << "How many dimensions does each data point have: ";
cin >> numOfDimensions;
cout <<"Please enter " << numOfPoints <<" data points, which have " << numOfDimensions << " dimensions:" << endl;
vector<vector<double>> points(numOfPoints, vector<double>(numOfDimensions,0));
for(int i=0; i<numOfPoints; i++)
{
cout << "Enter coordinates of point X" << i+1 << ":" << endl;;
for(int j=0; j<numOfDimensions; j++)
{
double coordinate;
cin >> coordinate;
points.at(i).at(j) = coordinate;
}
}
int distMeasure;
while(true)
{
cout << "Choose diatnce measure: 1 = L1-norm, 2 = L2-norm, 3 = Linf-norm:" << endl;
cin >> distMeasure;
vector<vector<double>> distances(numOfPoints, vector<double>(numOfPoints,0));
for(int k=0; k<numOfPoints; k++)
{
for(int m=k+1; m<numOfPoints; m++)
{
if(distMeasure==1)
{
cout << "L1(X"<<k+1<<", X"<<m+1<<")=";
distances.at(k).at(m) = L1_norm(points.at(k), points.at(m));
}
else if(distMeasure==2)
{
cout << "L2(X"<<k+1<<", X"<<m+1<<")=";
distances.at(k).at(m) = L2_norm(points.at(k), points.at(m));
}
else if(distMeasure==3)
{
cout << "Linf(X"<<k+1<<", X"<<m+1<<")=";
distances.at(k).at(m) = Linf_norm(points.at(k), points.at(m));
}
//as the resulting matrix is symmetric:
distances.at(m).at(k) = distances.at(k).at(m);
cout << distances.at(k).at(m) << endl;
}
}
int showAsMatrix;
cout << "Insert: 1 - to show result in matrix form: ";
cin >> showAsMatrix;
if(showAsMatrix == 1)
{
for(int i=0; i<distances.size(); i++)
{
for(int j=0; j<distances.size(); j++)
{
cout << distances.at(i).at(j)<<" ";
}
cout << endl;
}
}
}
return 0;
}